java manual r22
java manual r22
0021
Course Objectives:
Course Outcomes:
● Able to write the programs for solving real world problems using Java OOP principles.
List of Experiments:
1. Use Eclipse or Net bean platform and acquaint yourself with the various menus. Create a test
project, add a test class, and run it. See how you can use auto suggestions, auto fill. Try code
formatter and code refactoring like renaming variables, methods, and classes. Try debug step
by step with a small program of about 10 to 15 lines which contains at least one if else condition
2. Write a Java program to demonstrate the OOP principles. [i.e., Encapsulation, Inheritance,
3. Write a Java program to handle checked and unchecked exceptions. Also, demonstrate the
4. Write a Java program on Random Access File class to perform different read and write
operations.
5. Write a Java program to demonstrate the working of different collection classes. [Use package
6. Write a program to synchronize the threads acting on the same object. [Consider the example
7. Write a program to perform CRUD operations on the student table in a database using JDBC.
8. Write a Java program that works as a simple calculator. Use a grid layout to arrange buttons
for the digits and for the +, -,*, % operations. Add a text field to display the result. Handle any
9. Write a Java program that handles all mouse events and shows the event name at the center
REFERENCE BOOKS:
1. Java for Programmers, P. J. Deitel and H. M. Deitel, 10th Edition Pearson education.
4. Core Java, Volume 1, 9th edition, Cay S. Horstmann and G Cornell, Pearson.
EXPERIMENT-01
Aim:
Use eclipse or Netbean platform and acquaint with the various menus, create a test project, add a test
class and run it see how you can use auto suggestions, auto fill. Try code formatter and code refactoring
like renaming variables, methods and classes. Try debug step by step with a small program of about 10 to
15 lines which contains at least one if else condition and a for loop.
Source Code:
Sample_Program.java
//Importing packages
import java.lang.System;
import java.util.Scanner;
// Creating Class
class Sample_Program {
// main method
int i,count=0,n;
n=sc.nextInt();
for(i=1;i<=n;i++) {
if(n%i==0) {
count++;
}
if(count==2)
System.out.println(n+" is prime");
else
Output:
EXPERIMENT-2
Aim:
Write a Java program to demonstrate the OOP principles. [i.e., Encapsulation, Inheritance, Polymorphism
and Abstraction]
Source Code:
OopPrinciplesDemo.java
/* Encapsulation:
The fields of the class are private and accessed through getter and setter methods.*/
class Person {
// private fields
// constructor
this.name = name;
this.age = age;
return name;
this.name = name;
this.age = age;
/* Abstraction:
The displayInfo() method provides a simple interface to interact with the object.*/
/* Inheritance:
// private field
// constructor
super(name, age);
this.salary = salary;
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
/* Polymorphism:
@Override
super.displayInfo();
System.out.println("Person Info:");
person.displayInfo();
System.out.println("====================");
System.out.println("Employee Info:");
employee.displayInfo();
Output:
EXPERIMENT-3
Aim:
Write a Java program to handle checked and unchecked exceptions. Also, demonstrate the usage of
custom exceptions in real time scenario.
Source Code:ExceptionsDemo.java
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
// Custom Exception
super(message);
}
} else {
try {
} catch (FileNotFoundException e) {
try {
System.out.println(arr[6]);
} catch (ArrayIndexOutOfBoundsException e) {
finally {
try {
register("Madhu", 17);
} catch (InvalidAgeException e) {
Output:
EXPERIMENT-4
Aim:
Write a Java program on Random Access File class to perform different read and write operations.
Source Code:
RandomAccessFileExample.java
import java.io.*;
try {
// Create a RandomAccessFile object with read-write mode
file.writeUTF(data1);
file.writeUTF(data2);
file.seek(0);
System.out.println(readData1);
System.out.println(readData2);
file.seek(file.length());
file.writeUTF(newData);
file.seek(0);
readData1 = file.readUTF();
readData2 = file.readUTF();
System.out.println(readData1);
System.out.println(readData2);
System.out.println(readData3);
file.close();
} catch (IOException e) {
e.printStackTrace();
Output:
EXPERIMENT-5
Aim:
Write a Java program to demonstrate the working of different collection classes. [Use package structure to
store multiple classes].
Source Code:
ListExample.java
package collections;
import java.util.ArrayList;
list.add("Apple");
list.add("Banana");
list.add("Orange");
// to display
System.out.println("List Example:");
System.out.println(fruit);
SetExample.java
package collections;
import java.util.HashSet;
set.add("Apple");
set.add("Banana");
set.add("Orange");
// To display
System.out.println("Set Example:");
System.out.println(fruit);
MapExample.java
package collections;
import java.util.HashMap;
map.put(1, "Apple");
map.put(2, "Banana");
map.put(3, "Orange");
// To display
System.out.println("Map Example:");
}
}
CollectionsDemo.java
package collections;
ListExample.main(args);
SetExample.main(args);
MapExample.main(args);
Output:
EXPERIMENT-6
6. Write a program to synchronize the threads acting on the same object. [Consider the example
}
public class Safe {
public static void main(String[] args) {
// Tell that one berth is needed
Reservation obj = new Reservation(1);
}
EXPERIMENT-7
Aim:
Write a program to perform CRUD operations on the student table in a database using JDBC.
Source Code:
InsertData.java
import java.sql.*;
import java.util.Scanner;
try {
Class.forName("com.mysql.jdbc.Driver");
Statement s = con.createStatement();
System.out.println("________________________________________");
s.close();
con.close();
UpdateData.java
import java.sql.*;
import java.util.Scanner;
try {
Class.forName("com.mysql.jdbc.Driver");
Statement s = con.createStatement();
System.out.println("________________________________________");
s.close();
con.close();
DeleteData.java
import java.sql.*;
import java.util.Scanner;
Class.forName("com.mysql.jdbc.Driver");
Statement s = con.createStatement();
System.out.println("________________________________________");
s.close();
con.close();
DisplayData.java
import java.sql.*;
import java.util.Scanner;
public class DisplayData {
try {
Class.forName("com.mysql.jdbc.Driver");
Statement s = con.createStatement();
if (rs != null) {
System.out.println("________________________________________");
while (rs.next())
System.out.println("________________________________________");
s.close();
con.close();
}
}
Output:
EXPERIMENT-8
Aim:
Write a Java program that works as a simple calculator. Use a grid layout to arrange buttons for the digits
and for the , -,*, % operations. Add a text field to display the result. Handle any possible exceptions like
divided by zero.
Source Code:
MyCalculator.java
import java.awt.*;
import java.awt.event.*;
public class MyCalculator extends Frame implements ActionListener {
double num1,num2,result;
Label lbl1,lbl2,lbl3;
TextField tf1,tf2,tf3;
Button btn1,btn2,btn3,btn4;
char op;
MyCalculator() {
lbl1.setBounds(50,100,100,30);
tf1=new TextField();
tf1.setBounds(160,100,100,30);
lbl2.setBounds(50,170,100,30);
tf2=new TextField();
tf2.setBounds(160,170,100,30);
btn1=new Button("+");
btn1.setBounds(50,250,40,40);
btn2=new Button("-");
btn2.setBounds(120,250,40,40);
btn3=new Button("*");
btn3.setBounds(190,250,40,40);
btn4=new Button("/");
btn4.setBounds(260,250,40,40);
lbl3.setBounds(50,320,100,30);
tf3=new TextField();
tf3.setBounds(160,320,100,30);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
setSize(400,500);
setLayout(null);
setTitle("Calculator");
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
num1 = Double.parseDouble(tf1.getText());
num2 = Double.parseDouble(tf2.getText());
if(ae.getSource() == btn1)
tf3.setText(String.valueOf(result));
if(ae.getSource() == btn2)
tf3.setText(String.valueOf(result));
if(ae.getSource() == btn3)
tf3.setText(String.valueOf(result));
if(ae.getSource() == btn4)
tf3.setText(String.valueOf(result));
}
}
Output:
EXPERIMENT-9
Aim:
Write a Java program that handles all mouse events and shows the event name at the center of the window
when a mouse event is fired. [Use Adapter classes]
Source Code:
MouseEventPerformer.java
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
JLabel l1;
public MouseEventPerformer()
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,300);
setLayout(new FlowLayout(FlowLayout.CENTER));
l1 = new JLabel();
l1.setFont(f);
l1.setForeground(Color.BLUE);
add(l1);
addMouseListener(this);
setVisible(true);
l1.setText("Mouse Exited");
l1.setText("Mouse Entered");
l1.setText("Mouse Released");
l1.setText("Mouse Pressed");
l1.setText("Mouse Clicked");
Output: