Advanced Java
Advanced Java
Of
Advance Java
10
11
12
13
14
15
Experiment No.1
Aim: - Write a program to create a frame using AWT.
Implement mouseClicked(), mouseEntered() and
mouseExited() events. Frame should become visible when
mouse enters it.
import java.awt.*;
import java.awt.event.*;
public class Main extends Frame implements MouseListener {
Label l;
Main() {
super("AWT Frame");
l = new Label();
l.setBounds(25, 60, 250, 30);
l.setAlignment(Label.CENTER);
this.add(l);
this.setSize(300, 300);
this.setLayout(null);
this.setVisible(true);
this.addMouseListener(this);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
}
public static void main(String[] args) {
new Main();
}
@Override
public void mouseClicked(MouseEvent e) {
l.setText("Mouse Clicked");
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
l.setText("Mouse Entered");
}
@Override
public void mouseExited(MouseEvent e) {
l.setText("Mouse Exited");
}
}
Output: -
Experiment No.2
Aim: - Write a program to create a login form using swing.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LoginDemo extends JFrame implements ActionListener {
JPanel panel;
JLabel user_label, password_label, message;
JTextField userName_text;
JPasswordField password_text;
JButton submit, cancel;
LoginDemo() {
// Username Label
Output: -
Experiment No.3
Aim: -Java Program to Design Calculator using AWT
Controls with ActionLisener
import java.awt.*;
import java.awt.event.*;
class MyCalc extends WindowAdapter implements ActionListener{
Frame f;
Label l1;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0;
Button badd,bsub,bmult,bdiv,bmod,bcalc,bclr,bpts,bneg,bback;
double xd;
double num1,num2,check;
MyCalc(){
f= new Frame("MY CALCULATOR");
// INSTANTIATING COMPONENETS
l1=new Label();
l1.setBackground(Color.LIGHT_GRAY);
l1.setBounds(50,50,260,60);
b1=new Button("1");
b1.setBounds(50,340,50,50);
b2=new Button("2");
b2.setBounds(120,340,50,50);
b3=new Button("3");
b3.setBounds(190,340,50,50);
b4=new Button("4");
b4.setBounds(50,270,50,50);
b5=new Button("5");
b5.setBounds(120,270,50,50);
b6=new Button("6");
b6.setBounds(190,270,50,50);
b7=new Button("7");
b7.setBounds(50,200,50,50);
b8=new Button("8");
b8.setBounds(120,200,50,50);
b9=new Button("9");
b9.setBounds(190,200,50,50);
b0=new Button("0");
b0.setBounds(120,410,50,50);
bneg=new Button("+/-");
bneg.setBounds(50,410,50,50);
bpts=new Button(".");
bpts.setBounds(190,410,50,50);
bback=new Button("back");
bback.setBounds(120,130,50,50);
badd=new Button("+");
badd.setBounds(260,340,50,50);
bsub=new Button("-");
bsub.setBounds(260,270,50,50);
bmult=new Button("*");
bmult.setBounds(260,200,50,50);
bdiv=new Button("/");
bdiv.setBounds(260,130,50,50);
bmod=new Button("%");
bmod.setBounds(190,130,50,50);
bcalc=new Button("=");
bcalc.setBounds(245,410,65,50);
bclr=new Button("CE");
bclr.setBounds(50,130,65,50);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
bpts.addActionListener(this);
bneg.addActionListener(this);
bback.addActionListener(this);
badd.addActionListener(this);
bsub.addActionListener(this);
bmult.addActionListener(this);
bdiv.addActionListener(this);
bmod.addActionListener(this);
bcalc.addActionListener(this);
bclr.addActionListener(this);
f.addWindowListener(this);
//ADDING TO FRAME
f.add(l1);
f.add(b1); f.add(b2); f.add(b3); f.add(b4); f.add(b5);f.add(b6); f.add(b7);
f.add(b8);f.add(b9);f.add(b0);
f.setSize(360,500);
f.setLayout(null);
f.setVisible(true);
}
//FOR CLOSING THE WINDOW
public void windowClosing(WindowEvent e) {
f.dispose();
}
}
//MAIN METHOD where objects of MyCalc is instantaiated
public static void main(String args[]){
new MyCalc();
}
}
Output: -
+
=
Experiment No.4
Aim: -Write a java program to create collection interface
import java.io.*;
import java.util.*;
public class CollectionDemo {
public static void main(String args[])
{Collection<String> list = new LinkedList<String>();
list.add("Geeks");
list.add("for");
list.add("Geeks");
System.out.println("The list is: " + list);
list.add("Last");
list.add("Element");
System.out.println("The new List is: " + list);
}
}
Output: -
Aim: -Write a java program to create set interface
import java.util.*;
public class SetDemo {
public static void main(String args[])
{Set<String> set = new HashSet<String>();
set.add("hello");
set.add("To");
set.add("everyone");
set.add("i am ");
set.add("akash");
System.out.println("Set: " + set);
set.remove("to");
set.remove("i am");
set.remove("everyone");
System.out.println("Set after removing elements: "
+ set);
}
}
Output: -
Aim: -Write a java program to create arraylist interface
import java.util.ArrayList;
public class GFG {
public static void main(String[] args)
{ArrayList<Integer> arr = new ArrayList<Integer>(4);
arr.add(1);
arr.add(2);
arr.add(3);
arr.add(4);
System.out.println("The list initially: " + arr);
arr.clear();
System.out.println( "The list after using clear() method: " + arr);
}
}
Output: -
Aim: -Write a java program to create linkedlist interface
import java.io.*;
public class LinkedList {
Node head;
static class Node {
int data;
Node next;
Node(int d)
{data = d;
next = null;
}
}
public static LinkedList insert(LinkedList list, int data)
{Node new_node = new Node(data);
if (list.head == null) {
list.head = new_node;
}
else {Node last = list.head;
while (last.next != null) {
last = last.next;
}
last.next = new_node;
}
return list;
}
public static void printList(LinkedList list)
{Node currNode = list.head;
System.out.print("LinkedList: ");
while (currNode != null) {
System.out.print(currNode.data + " ");
currNode = currNode.next;
}
}
public static void main(String[] args)
{
/* Start with the empty list. */
LinkedList list = new LinkedList();
list = insert(list, 1);
list = insert(list, 2);
list = insert(list, 3);
list = insert(list, 4);
list = insert(list, 5);
list = insert(list, 6);
list = insert(list, 7);
list = insert(list, 8);
printList(list);
}
}
Output: -
Experiment No. 7
Aim:-write a program to install Tomcat Server step by step.
Step 1. Open the Google Chrome or any of your web browser and type "download Tomcat for
windows" in the search box. You can also go directly on Tomcat's website by clicking on this
https://tomcat.apache.org/download-90.cgi#9.O.38
Now download
32-bit/64-bit Windows Service Installer (pgp, sha512)
C [email protected]/downloed-90.cgi+9.0.38
•omcat Native Other mirrors: https://dlcdn apache org/ Change
-aglibs
9.0.80
xumentation please see the README file for packaging information. It explains what every distribution
contains.
•omcat 11.0 (alpha)
-omcet 10.1 Binary Distributions
•omcat 0.0
Tomcat 8.5
• Core:
•omcat Connectors
0 sha512)
-omcet Native 2 o lu.gz(ugu, sha512)
•omcat Native 1.2
0 32 bit Windows ziu (QgQ, sha512)
Viki
0 64-bit Windows shi12)
Aigretion Guide
'resentations 0 32-biV64-blt Windows Service Installer shd512)
ipecifications • Full documentation: 0
tarp7(nøn qhA51)l
a-oose
Choose"hichf*tures Apache Tomcat you vvant to install
Check the componen ts you want to install and uncheck the components Vou don't want
to install, ClickNext to continue.
inst—Il
Sys&rrvv3.Og
I. Defintjons,
•License • shall mean the terms and condibons for use. reproduction,
and distribubon as defined by Sections I through g of this document.
ff you accept •he terms of the agreement, dick I Agree to continue. you must accept the
agreement to install Apache Tomcat.
Nultsoft: System
Shovv details
Chose Loc*ion
Choose the folder In A'hich to install Apache Tomcat,
Setup will install Apache Tomcatin the following folder, To install in a dfferent folder, dick
3rovvseand select another folder. Click Install to start the installation.
Desmabon Folder
Spacerequired: 14.2 ME
Space available:
23,4
Nullsot Install
Sysr_ern vs. 04
Wait for some time until the Tomcat gets installed.
9. Now "Finish" button, here the installation of Tomcat is
completed. It may ask you to restart your system, so restart your system.
Experiment No.8
Aim:- Write a program to implements Bouncing
Ball using awt and swing.
import java.awt.*; import javax.swing.*;
int width;
int height;
/
/
B
a
l
l
S
i
z
e
f
l
o
a
t
r
a
d
i
u
s
=
4
0
;
//
Cent
er of
Call
float
X=
radi
us +
50;
float
Y=
radi
us +
20;
/
/
D
i
r
e
c
t
i
o
n
f
l
o
a
t
d
x
3
;
f
l
o
a
t
d
y
3
;
public BouncingBall() {
while (true) {
width = getWidth();
height = getHeight();
X = X + dx ;
Y = Y + dy;
if (X - radius < 0) {
dx = -
dx;
X=
radius;
dx = -dx;
X = width - radius;
}
Y = radius;
} else if (Y + radius > height) {
dy = -dy;
Y = height - radius;
repaint();
try {
Thread.sleep(50);
}
;
thread.start();
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Bouncing Ball");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_
CLOSE); frame.setSize(300, 200);
frame.setContentPane(new BouncingBall());
frame.setVisible(true);
Output:-
Experiment No.9
Aim:- Write a program to implements student
management app using swing.
javax.swing.border.CompoundBorder; import
javax.swing.border.EmptyBorder; import
javax.swing.table.DefaultTableModel; import java.awt.*; import
private StudentMgmtApp() {
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
createUpdateStudentPanel); tabbedPane.setMnemonicAt(0,
readDeleteStudentPanel); tabbedPane.setMnemonicAt(1,
KeyEvent.VK_2); jFrame.setContentPane(tabbedPane);
jFrame.setVisible(true);
}
public static void
main(String[] args)
{ new
StudentMgmtApp();
GridBagConstraints(); gridBagConstraintForLabel.fill =
GridBagConstraints.BOTH; gridBagConstraintForLabel.insets =
gridBagConstraintForLabel.gridy = yPos;
containingpanel.add(label, gridBagConstraintForLabel);
GridBagConstraints(); gridBagConstraintForTextField.fill =
textField;
GridBagConstraints(); gridBagConstraintForButton.fill =
GridBagConstraints.BOTH; gridBagConstraintForButton.insets =
gridBagConstraintForButton.gridy = yPos;
containingPanel.add(button, gridBagConstraintForButton);
return button;
StudentMgmtApp.studentList = result;
}
public class CreateUpdateStudentPanel
stdTextField;
CreateUpdateStudentPanel() {
Double.MIN_VALUE};
setLayout(panelGridBagLayout); idTextField =
StudentMgmtApp.addLabelAndTextField("ID:", 0, this);
this); ageTextField =
StudentMgmtApp.append(StudentMgmtApp.studentList,
studentData); studentModel.addRow(studentData);
idTextField.setText(""); nameTextField.setText("");
ageTextField.setText(""); stdTextField.setText("");
extends JPanel
{ ReadDeleteStudentPanel() {
setPreferredSize(new Dimension(400,
200));
@Override public
return
Boolean.class;
case 1:
return
String.class;
case 2:
return
String.class;
case 3:
return
String.class;
case 4:
return
String.class;
default:
return Boolean.class;
};
studentTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
add(pane, BorderLayout.CENTER);
i++) {
{ model.removeRow(i);
i--;
Output:-
Before
After
Experiment No.10
Aim: - Write a program to find the factorial of a number
using JSP.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Factorial of a Number</title>
</head>
<body>
<form name="f1" method="post" action="Factorial.jsp">
Enter a Number: <input type="text" name="n"/>
<br/>
<input type ="submit" value="Find Factorial"/>
</form>
<%!
int find_factorial(int n)
{
if(n==0)
return 1;
return n*find_factorial(n-1);
}
%>
<%
String inp=request.getParameter("n");
if(inp!=null)
{
int px=Integer.parseInt(inp);
int fact=find_factorial(px);
out.println("<br/>Factorial = " + fact);
}
%>
</body>
</html>
Output: -
Experiment No.11
Aim: - Write a program to print Fibonacci Series using JSP.
<html>
<head><title>FIBONACCI SERIES IN JSP</title></head>
<body>
<form method="get">
<h3> Enter the number of terms you want:
<input type="text" name="limit">
</h3>
</form>
<h3>
<%
String s = request.getParameter("limit");
if (s != null) {
%>
<%@ page import = "java.io.*" %>
<%@ page import = "java.lang.*" %>
<%
int n=0;
n=Integer.parseInt(s);
out.println("No of terms to be printed is "+n);
%>
<br>
<br>
<br>
The series generated are listed below :<br><br>
<%
int a=1;
int b=1;
out.println(""+a+",\t"+b+",\t");
for(int i=3;i<= n;i++)
{
int c=a+b;
out.print(""+c+",\t");
a=b;
b=c;
}
}
%>
</h3>
</body>
</html>
Output: -