Nitesh Mor 1
Nitesh Mor 1
Of
Advance Java
INDEX
S.NO AIM DATE SIGN
10
11
12
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 Calculator implements ActionListener
{
//Declaring Objects
Frame f=new Frame();
Label l1=new Label("First Number");
Label l2=new Label("Second Number");
Label l3=new Label("Result");
TextField t1=new TextField();
TextField t2=new TextField();
TextField t3=new TextField();
Button b1=new Button("Add");
Button b2=new Button("Sub");
Button b3=new Button("Mul");
Button b4=new Button("Div");
Button b5=new Button("Cancel");
Calculator()
{
//Giving Coordinates
l1.setBounds(50,100,100,20);
l2.setBounds(50,140,100,20);
l3.setBounds(50,180,100,20);
t1.setBounds(200,100,100,20);
t2.setBounds(200,140,100,20);
t3.setBounds(200,180,100,20);
b1.setBounds(50,250,50,20);
b2.setBounds(110,250,50,20);
b3.setBounds(170,250,50,20);
b4.setBounds(230,250,50,20);
b5.setBounds(290,250,50,20);
//Adding components to the frame
f.add(l1);
f.add(l2);
f.add(l3);
f.add(t1);
f.add(t2);
f.add(t3);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
f.setLayout(null);
f.setVisible(true);
f.setSize(400,350);
}
public void actionPerformed(ActionEvent e)
{
int n1=Integer.parseInt(t1.getText());
int n2=Integer.parseInt(t2.getText());
if(e.getSource()==b1)
{
t3.setText(String.valueOf(n1+n2));
}
if(e.getSource()==b2)
{
t3.setText(String.valueOf(n1-n2));
}
if(e.getSource()==b3)
{
t3.setText(String.valueOf(n1*n2));
}
if(e.getSource()==b4)
{
t3.setText(String.valueOf(n1/n2));
}
if(e.getSource()==b5)
{
System.exit(0);
}
}
public static void main(String...s)
{
new Calculator();
}
}
Output: -
Experiment No.4(A)
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("fortuner legender");
list.add("porsche 718 boxster");
list.add("lamborghini aventador convertible");
System.out.println("The list is: " + list);
list.add("rolls royce ghost ");
list.add("Toyota GR Supra 2.0");
System.out.println("The new List is: " + list);
}
}
Output: -
Experiment No.4(B)
Aim: -Write a java program to create set and remove
interface.
import java.util.HashSet;
import java.util.Set;
public class setremove {
public static void main(String[] args) {
// Create a HashSet (a class that implements the Set interface)
Set<String> set = new HashSet<>();
// Add elements to the set
set.add("Apple");
set.add("Banana");
set.add("Orange");
// Print the elements in the set
System.out.println("Elements in the set:");
for (String element : set) {
System.out.println(element);
}
// Remove an element from the set
set.remove("Banana");
// Print the elements in the updated set
System.out.println("Elements in the updated set:");
for (String element : set) {
System.out.println(element);
}
}
}
Output: -
Experiment No.4(C)
Aim: -Write a java program to create arraylist interface.
import java.util.ArrayList;
public class SetExample {
public static void main(String[] args)
{ArrayList<Integer> arr = new ArrayList<Integer>(4);
arr.add(41);
arr.add(62);
arr.add(35);
arr.add(50);
System.out.println("The list initially: " + arr);
arr.clear();
System.out.println( "The list after using clear() method: " + arr);
}
}
Output: -
Experiment No.4(D)
Aim: -Write a java program to create linkedlist
interface.
import java.io.*;
// Java program to implement
// a Singly Linked List
public class LinkedList {
Node head; // head of list
// Linked list Node.
// Node is a static nested class
// so main() can access it
static class Node {
int data;
Node next;
// Constructor
Node(int d)
{
data = d;
next = null;
}
}
// Method to insert a new node
public static LinkedList insert(LinkedList list,int data)
{
// Create a new node with given data
Node new_node = new Node(data);
new_node.next = null;
// If the Linked List is empty,
// then make the new node as head
if (list.head == null) {
list.head = new_node;
}
else {
// Else traverse till the last node
// and insert the new_node there
Node last = list.head;
while (last.next != null) {
last = last.next;
}
// Insert the new_node at last node
last.next = new_node;
}
// Return the list by head
return list;
}
// Method to print the LinkedList.
public static void printList(LinkedList list)
{
Node currNode = list.head;
System.out.print("LinkedList: ");
// Traverse through the LinkedList
while (currNode != null) {
// Print the data at current node
System.out.print(currNode.data + " ");
// Go to next node
currNode = currNode.next;
}
System.out.println();
}
// **************DELETION BY KEY**************
// Method to delete a node in the LinkedList by KEY
public static LinkedList deleteByKey(LinkedList list, int key)
{
// Store head node
Node currNode = list.head, prev = null;
// CASE 1:
// If head node itself holds the key to be deleted
if (currNode != null && currNode.data == key) {
list.head = currNode.next; // Changed head
// Display the message
System.out.println(key + " found and deleted");
// Return the updated List
return list;
}
// CASE 2:
// If the key is somewhere other than at head
// Search for the key to be deleted,
// keep track of the previous node
// as it is needed to change currNode.next
while (currNode != null && currNode.data != key) {
// If currNode does not hold key
// continue to next node
prev = currNode;
currNode = currNode.next;
}
// If the key was present, it should be at currNode
// Therefore the currNode shall not be null
if (currNode != null) {
// Since the key is at currNode
// Unlink currNode from linked list
prev.next = currNode.next;
// Display the message
System.out.println(key + " found and deleted");
}
// CASE 3: The key is not present
// If key was not present in linked list
// currNode should be null
if (currNode == null) {
// Display the message
System.out.println(key + " not found");
}
// return the List
return list;
}
// **************MAIN METHOD**************
// method to create a Singly linked list with n nodes
public static void main(String[] args)
{
/* Start with the empty list. */
LinkedList list = new LinkedList();
//
// ******INSERTION******
//
// Insert the values
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);
// Print the LinkedList
printList(list);
//
// ******DELETION BY KEY******
//
// Delete node with value 1
// In this case the key is ***at head***
deleteByKey(list, 1);
// Print the LinkedList
printList(list);
// Delete node with value 4
// In this case the key is present ***in the
// middle***
deleteByKey(list, 4);
// Print the LinkedList
printList(list);
// Delete node with value 10
// In this case the key is ***not present***
deleteByKey(list, 10);
// Print the LinkedList
printList(list);
}
}
Output: -
Experiment No.5
Aim: - Write a program to implements arraylist
interface using (Traversing and Sorting Operation).
import java.util.*;
public class GEG {
public static void main(String[] args) {
// Create an ArrayList
ArrayList<Integer>arr=new ArrayList<>();
// Add elements to the ArrayList
arr.add(5);
arr.add(2);
arr.add(8);
arr.add(1);
arr.add(10);
// Traversing the ArrayList
System.out.println("Elements in the ArrayList:");
for (int number : arr) {
System.out.println(number);
}
// Sorting the ArrayList in ascending order
Collections.sort(arr);
// Traversing the sorted ArrayList
System.out.println("Elements in the sorted ArrayList:");
for (int number : arr) {
System.out.println(number);
}}}
Output: -
Experiment No.6
Aim: - Write a program to implements Bouncing Ball
using awt and swing.
import java.awt.*;
import javax.swing.*;
public class BouncingBall extends JPanel {
// Box height and width
int width;
int height;
// Ball Size
float radius = 40;
float diameter = radius * 2;
// Center of Call
float X = radius + 50;
float Y = radius + 20;
// Direction
float dx = 3;
float dy = 3;
public BouncingBall() {
Thread thread = new Thread() {
public void run() {
while (true) {
width = getWidth();
height = getHeight();
X = X + dx ;
Y = Y + dy;
if (X - radius < 0) {
dx = -dx;
X = radius;
} else if (X + radius > width) {
dx = -dx;
X = width - radius;
}
if (Y - radius < 0) {
dy = -dy;
Y = radius;
} else if (Y + radius > height) {
dy = -dy;
Y = height - radius;
}
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
}
}
}
};
thread.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillOval((int)(X-radius), (int)(Y-radius), (int)diameter,
(int)diameter);
}
public static void main(String[] args) {
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.7
Aim: - Write a program to implements student
management app using swing.
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.Arrays;
public class StudentMgmtApp {
private String[] colName = new String[]{"Delete", "ID", "Name", "Age",
"Standard"};
private static Object[][] studentList = new Object[][]{{false, 1, "Shubham", 23,
12}};
private DefaultTableModel studentModel;
private JTable studentTable;
private StudentMgmtApp() {
JFrame jFrame = new JFrame("Student Management Application");
jFrame.setBounds(100, 100, 500, 300);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JTabbedPane tabbedPane = new JTabbedPane();
CreateUpdateStudentPanel createUpdateStudentPanel = new
CreateUpdateStudentPanel();
ReadDeleteStudentPanel readDeleteStudentPanel = new
ReadDeleteStudentPanel();
tabbedPane.addTab("Create Student", createUpdateStudentPanel);
tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
tabbedPane.addTab("Read/Delete Student", readDeleteStudentPanel);
tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
jFrame.setContentPane(tabbedPane);
jFrame.setVisible(true);
}
public static void main(String[] args) {
new StudentMgmtApp();
}
private static JTextField addLabelAndTextField(String labelText, int yPos,
Container containingpanel) {
JLabel label = new JLabel(labelText);
GridBagConstraints gridBagConstraintForLabel = new GridBagConstraints();
gridBagConstraintForLabel.fill = GridBagConstraints.BOTH;
gridBagConstraintForLabel.insets = new Insets(0, 0, 5, 5);
gridBagConstraintForLabel.gridx = 0;
gridBagConstraintForLabel.gridy = yPos;
containingpanel.add(label, gridBagConstraintForLabel);
JTextField textField = new JTextField();
GridBagConstraints gridBagConstraintForTextField = new
GridBagConstraints();
gridBagConstraintForTextField.fill = GridBagConstraints.BOTH;
gridBagConstraintForTextField.insets = new Insets(0, 0, 5, 0);
gridBagConstraintForTextField.gridx = 1;
gridBagConstraintForTextField.gridy = yPos;
containingpanel.add(textField, gridBagConstraintForTextField);
textField.setColumns(10);
return textField;
}
private static JButton addButton(String btnText, int yPos, Container
containingPanel) {
JButton button = new JButton(btnText);
GridBagConstraints gridBagConstraintForButton = new GridBagConstraints();
gridBagConstraintForButton.fill = GridBagConstraints.BOTH;
gridBagConstraintForButton.insets = new Insets(0, 0, 5, 5);
gridBagConstraintForButton.gridx = 0;
gridBagConstraintForButton.gridy = yPos;
containingPanel.add(button, gridBagConstraintForButton);
return button;
}
private static void append(Object[][] array, Object[] value) {
Object[][] result = Arrays.copyOf(array, array.length + 1);
result[result.length - 1] = value;
StudentMgmtApp.studentList = result;
}
public class CreateUpdateStudentPanel extends JPanel {
private JTextField idTextField;
private JTextField nameTextField;
private JTextField ageTextField;
private JTextField stdTextField;
CreateUpdateStudentPanel() {
Border border = getBorder();
Border margin = new EmptyBorder(10, 10, 10, 10);
setBorder(new CompoundBorder(border, margin));
GridBagLayout panelGridBagLayout = new GridBagLayout();
panelGridBagLayout.columnWidths = new int[]{86, 86, 0};
panelGridBagLayout.rowHeights = new int[]{20, 20, 20, 20, 20, 0};
panelGridBagLayout.columnWeights = new double[]{0.0, 1, 0,
Double.MIN_VALUE};
panelGridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0,
Double.MIN_VALUE};
setLayout(panelGridBagLayout);
idTextField = StudentMgmtApp.addLabelAndTextField("ID:", 0, this);
nameTextField = StudentMgmtApp.addLabelAndTextField("Name :", 1,
this);
ageTextField = StudentMgmtApp.addLabelAndTextField("Age :", 2, this);
stdTextField = StudentMgmtApp.addLabelAndTextField("Standard :", 3,
this);
setPreferredSize(new Dimension(400, 200));
JButton createStudentbtn= StudentMgmtApp.addButton("Create", 4, this);
createStudentbtn.addActionListener(e -> createStudent());
}
private void createStudent() {
String studentId = idTextField.getText();
String studentName = nameTextField.getText();
String studentAge = ageTextField.getText();
String studentStd = stdTextField.getText();
Object[] studentData = new Object[]{false, studentId, studentName,
studentAge, studentStd};
StudentMgmtApp.append(StudentMgmtApp.studentList, studentData);
studentModel.addRow(studentData);
idTextField.setText("");
nameTextField.setText("");
ageTextField.setText("");
stdTextField.setText("");
}
}
public class ReadDeleteStudentPanel extends JPanel {
ReadDeleteStudentPanel() {
setPreferredSize(new Dimension(400, 200));
JButton deleteButton = StudentMgmtApp.addButton("Delete", 0, this);
deleteButton.addActionListener(e -> deleteStudent());
studentModel = new DefaultTableModel(StudentMgmtApp.studentList,
colName);
studentTable = new JTable(studentModel) {
@Override
public Class<?> getColumnClass(int column) {
switch (column) {
case 0:
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);
JScrollPane pane = new JScrollPane(studentTable);
add(pane, BorderLayout.CENTER);
}
private void deleteStudent() {
DefaultTableModel model = (DefaultTableModel) studentTable.getModel();
for (int i = 0; i < model.getRowCount(); i++) {
Boolean checked = (Boolean) model.getValueAt(i, 0);
if (checked != null && checked) {
model.removeRow(i);
i--;
}
}
}
}
}
Output: -
Before
After
Exp
eriment No.8
Aim: - How to download Apache Tomcat Server.
Step 1: -
Step 12: - After complete the services (green line) then click on
close button.
Experiment No.9
Aim: - Write a program to find the factorial of a
number using JSP.
<%@ page language="java" contentType="text/html; charset=ISO-
8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</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.10
Aim: - Write a program to print Fibonacci Series using
JSP.
<%@ page language="java" contentType="text/html; charset=ISO-
8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<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: -
Experiment No.11
Aim: - Write a program to implement JDBC
CONNECTION STEPS BY STEPS
// This code is for establishing connection with MySQL
// database and retrieving data
// from db Java Database connectivity
/*
*1. import --->java.sql
*2. load and register the driver ---> com.jdbc.
*3. create connection
*4. create a statement
*5. execute the query
*6. process the results
*7. close
*/
import java.io.*;
import
java.sql.*; class
GFG {
System.out.println(
"Connection Established successfully");
Statement st = con.createStatement(); ResultSet
rs
String name
= rs.getString("name"); // Retrieve name from db
}
}