java record
java record
Submitted in partial fulfilment of the requirement for the award of the degree of
SEMESTER II [2024-2026]
Submitted by
DONY BIJI
RegisterNo:243242210888
Mr ROJI THOMAS
CHANGANASSERY
JUNE-2025
KRISTU JYOTI COLLEGE OF MANAGEMENT AND TECHNOLOGY
CHANGANASSERY
CERTIFICATE
Certify that the practical laboratory record of OBJECT ORIENTED LAB(JAVA LAB)
is a bona-fide report of the practical works done by DONY BIJI (Reg
No.243242210888)under our guidance and supervision is submitted in partial fulfilment
of the Master of Computer Applications, awarded by Mahatma Gandhi University,
Kerala.
(Teacher-in-Charge)
(H.O.D)
(Principal)
Department Seal
Examiner(s)
1.
2.
DECLARATION
I hereby declare that the project work entitled “OBJECT ORIENTED LAB(JAVA LAB)”
submitted to Mahatma Gandhi University in partial fulfillment of requirement for the award of
post-graduation of Master of Computer Application from Kristu Jyoti College of management
and technology, Changanacherry is a record of bona-fide work done under the guidance of Mr
Roji Thomas, Department of Computer Application. This project work has not been submitted
in partial or fulfilment of any other post-graduation or similar of this University or any other
university.
DONY BIJI
Regno:243242210888
ACKNOWLEDGEMENT
I would like to express my thankfulness towards a number of people who have been instrument
in making of this project. First of all, we thank the college management who has been with us
with most helpful facilities throughout the completion of the project. I am greatly indebted to
Rev. Fr. Joshy Cheeramkuzhy CMI, Principal of Kristu Jyoti College who whole heartedly give
us the permission and whose advice was a real encouragement. I record my sincere thanks and
gratitude to Mr. Roji Thomas, HOD, Computer Application, for the valuable suggestions
provided. I am deeply indebted to Mr. Roji Thomas HOD Department of Computer Application,
for the valuable discussion and helpful counselling. Last, not the least I wish to express my
gratitude and heartfelt thanks to our friends, family and whom with their valuable advice,
encouragement and support for the successful completion of the project. Above all thanking the
GOD the almighty.
DONY BIJI
CONTENT
PART -I
1 Program to implement ‘this’ keyword 2
2 Demonstrate overloading method 3
3 Demonstrate object as parameter 4-5
4 Demonstrate member access in 5-6
inheritance
5 Program to demonstrate package 7
6 Demonstrate exception handling and 8
display the description
7 Demonstrate final keyword to prevent 9
overriding
8 String class functions 10-11
9 Demonstrating mouse events in java 12-14
10 Demonstrate Label, Button and TextField 14-15
11 Demonstrate all methods in the class 16-17
CheckBox and implement the
Itemlistener
12 Demonstrate all methods in the class 18-19
Choice and implement the Itemlistener
13 Display the content of a table on screen 19-20
14 Display the records of table based on the 21-22
partial rollno entered through keyboard
15 Create java program with the 3 22-24
textboxes,display the result in third box
based on option selected
16 Create a java applet with textfield and 25-28
button, should be displayed in the reverse
order
17 Applet to display a word on the screen. 29-30
The size of the word should be altered
based on adjustment in the scrollbar
PART-II
32
1 program to demonstrate inheritance
32-33
2 program to method overriding
program to create a package 33
3
program to implement exception handling
4 33-34
program to display roll number,marks of 2
5 34-35
subjects,sports weightage mark and total
score of a student using interfaces
PART -I
1
Kristu Jyoti College of Management and Technology
class student{
int rollno, age;
String name;
void display(){
System.out.println(rollno+" "+name+" "+age);
}
}
class Test{
public static void main(String args[]){
student s1=new student(1 ,"Rahul", 23);
student s2=new student(2 ,"Aarohi", 22);
s1.display();
s2.display();
}
}
Output
1 Rahul 23
2 Aarohi 22
2
Kristu Jyoti College of Management and Technology
Program
class Sum {
int add(int a, int b) {
return(a+b);
}
int add(int a, int b, int c) {
return (a+b+c);
}
double add(double a, double b) {
return (a+b);
}
}
class Overload {
public static void main(String args[]) {
Sum a=new Sum();
System.out.println("sum of two integers = "+a.add(10, 20));
System.out.println("sum of three integers = "+a.add(10, 20, 30));
System.out.println("sum of two double values = "+a.add(2.5, 4.5));
}
}
Output
3
Kristu Jyoti College of Management and Technology
Program
class Movie {
String title;
String actor, actress;
class MovieInfo {
void display(Movie m) {
System.out.println("Title: " + m.title);
System.out.println("Actor: " + m.actor);
System.out.println("Actress: " + m.actress);
}
}
4
Kristu Jyoti College of Management and Technology
Output
Title: Raaz
Actor: Dino Morea
Actress: Bipasha Basu
Program
class Person {
String name;
int age;
void showDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
5
Kristu Jyoti College of Management and Technology
void showStudentDetails() {
showDetails();
System.out.println("Course: " + course);
}
}
s1.setDetails("Sanjana", 22);
s1.setCourse("MCA");
s1.showStudentDetails();
}
}
Output
Name: Sanjana
Age: 22
Course: MCA
6
Kristu Jyoti College of Management and Technology
Program
myinfo/Info.java:
package myinfo;
Main.java
import myinfo.Info;
Output
7
Kristu Jyoti College of Management and Technology
Program
int result = a / b;
} catch (ArithmeticException e) {
System.out.println("Exception caught!");
System.out.println("Description : " + e.getMessage());
System.out.println("Full Exception : " + e.toString());
}
Output
Exception caught!
Description : / by zero
Full Exception : java.lang.ArithmeticException: / by zero
Program continues after exception handling.
8
Kristu Jyoti College of Management and Technology
Program
class FinalDemo {
public final void display() {
System.out.println("This is a final method.");
}
}
obj.display();
}
}
Output
9
Kristu Jyoti College of Management and Technology
8. Design a java program for demonstrate the String class functions equals(),
length(), charAt(), getChars() and trim().
Program
System.out.println("1. equals():");
boolean isEqual = str1.trim().equals(str2);
System.out.println("str1 equals str2? " + isEqual);
System.out.println("\n2. length():");
System.out.println("Length of str1: " + str1.length());
System.out.println("\n3. charAt():");
System.out.println("Character at index 2 of str2: " + str2.charAt(2));
System.out.println("\n4. getChars():");
char[] charArray = new char[5];
str2.getChars(0, 5, charArray, 0);
10
Kristu Jyoti College of Management and Technology
System.out.println("\n5. trim():");
System.out.println("Original str1: \"" + str1 + "\"");
System.out.println("Trimmed str1: \"" + str1.trim() + "\"");
}
}
Output
1. equals():
str1 equals str2? true
2. length():
Length of str1: 13
3. charAt():
Character at index 2 of str2: l
4. getChars():
First 5 characters of str2: Hello
5. trim():
Original str1: " Hello Java "
Trimmed str1: "Hello Java"
11
Kristu Jyoti College of Management and Technology
Program
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
12
Kristu Jyoti College of Management and Technology
mouseX = 0;
mouseY = 10;
msg = "Mouse exited.";
repaint();
}
public void mousePressed(MouseEvent me) {
mouseX = me.getX();
mouseY = me.getY();
msg = "Down";
repaint();
}
public void mouseReleased(MouseEvent me) {
mouseX = me.getX();
mouseY = me.getY();
msg = "Up";
repaint();
}
public void mouseDragged(MouseEvent me) {
// save coordinates
mouseX = me.getX();
mouseY = me.getY();
msg = "*";
showStatus("Dragging mouse at " + mouseX + ", " + mouseY);
repaint();
}
public void mouseMoved(MouseEvent me) {
showStatus("Moving mouse at " + me.getX() + ", " + me.getY());
}
public void paint(Graphics g) {
g.drawString(msg, mouseX, mouseY);
}
13
Kristu Jyoti College of Management and Technology
Output
10. Display a java applet form for demonstrate Label, Button and Text field.
Program
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
14
Kristu Jyoti College of Management and Technology
add(l);
add(t);
add(b);
b.addActionListener(this);
}
Output
15
Kristu Jyoti College of Management and Technology
11. Display a java applet, which contains the demonstration of all methods available
in the class “CheckBox” and implement the interface “ItemListener”.
Program
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
add(cb1);
add(cb2);
add(cb3);
cb1.addItemListener(this);
cb2.addItemListener(this);
cb3.addItemListener(this);
}
g.drawString("Python:" +cb3.getState(),10,120);
}
Output
17
Kristu Jyoti College of Management and Technology
12. Display a java applet, which contains the demonstration of all methods available
in the class “Choice” and implement the interface “ItemListener”.
Program
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
Choice language;
String msg = "";
language.add("C++");
language.add("Java");
language.add("VB");
language.add("Perl");
add(language);
language.addItemListener(this);
}
18
Kristu Jyoti College of Management and Technology
Output
Program
import java.sql.*;
class Table {
public static void main(String args[]) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c = DriverManager.getConnection("jdbc:odbc:DSN_NAME");
Statement s = c.createStatement();
ResultSet rs = s.executeQuery("SELECT * FROM " + args[0]);
19
Kristu Jyoti College of Management and Technology
while (rs.next()) {
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
}
rs.close();
s.close();
c.close();
} catch (Exception e) {
Output
Id name email
1 rahul [email protected]
2 anjali [email protected]
3 tina [email protected]
20
Kristu Jyoti College of Management and Technology
14. Write a program to display the records of a table based on the partial rollno
entered through the keyboard.
( Use proper exception and like operator with sql query).
Program
import java.sql.*;
public class student
public static void main(String args[]){
try{
String sql=”Select*from login where id=”+ Integer.parseInt(textfield.getText());
P.con=prepare statement(sql);
rs=p.execute .Query();
System.out.println(“rollno\t name\t Age”);
if(rs.next()){
int id=rs.getInt(“rollno”);
String name=rs.getString(“name”);
String age=rs.getInt(“age”);
System.out.println(“rollno+”\t\t”+ name+”\t\t “+Age”+\t);
}
}catch(SQL Exception e){
System.out.println(e);
}
21
Kristu Jyoti College of Management and Technology
Output
1 Tusshar 23
15. Create a Applet Application with three textboxes. The inputs should be provided
into the first two textboxes and based on the selection made through the Combo
box the result should be displayed in the third textbox.
• The third textbox should not be editable.
• The combo box should have options ADD, SUBSTRACT, MULTIPLY,
DIVIDE and CLOSE.
Program
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
t3.setEditable(false);
c = new Choice();
c.add("ADD");
c.add("SUBTRACT");
c.add("MULTIPLY");
c.add("DIVIDE");
c.add("CLOSE");
add(t1);
add(t2);
add(t3);
add(c);
c.addItemListener(this);
if (op.equals("ADD")) {
t3.setText(String.valueOf(num1 + num2));
} else if (op.equals("SUBTRACT")) {
t3.setText(String.valueOf(num1 - num2));
} else if (op.equals("MULTIPLY")) {
t3.setText(String.valueOf(num1 * num2));
} else if (op.equals("DIVIDE")) {
if (num2 != 0) {
t3.setText(String.valueOf(num1 / num2));
23
Kristu Jyoti College of Management and Technology
} else {
t3.setText("Cannot divide by zero");
}
} else if (op.equals("CLOSE")) {
t1.setText("");
t2.setText("");
t3.setText("");
}
}
}
//<applet code="Calc.class" width=400 height=200></applet>
Output
24
Kristu Jyoti College of Management and Technology
16. Develop an applet window having a textfield and button. The text entered in the
textfield should be displayed in the reverse order on to the applet screen when
the button in pressed.
Nb:
• The font size of the text displayed on the applet screen should be
regulated by selecting values using a combo box ( values should range
from 8 to 75 ).
• The font color should be determined by entering values into three
textboxes and the color should be set only when the right button of the
button is pressed.
• Use the null layout to place the controls to the required positions.
Program
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
Choice c;
Color fcolor;
TextField t1, t2, t3, t4;
Button b1;
String str = "";
String rev = "";
Font f;
String s;
int k = 20, r = 0, g = 0, b = 0;
25
Kristu Jyoti College of Management and Technology
t1 = new TextField(10);
t1.setBounds(50, 30, 100, 25);
add(t1);
t2 = new TextField(3);
t2.setBounds(50, 70, 40, 25);
add(t2);
t3 = new TextField(3);
t3.setBounds(100, 70, 40, 25);
add(t3);
t4 = new TextField(3);
t4.setBounds(150, 70, 40, 25);
add(t4);
b1 = new Button("Click");
c = new Choice();
for (int i = 8; i <= 75; i++) {
c.addItem(String.valueOf(i));
26
Kristu Jyoti College of Management and Technology
str = t1.getText();
rev = "";
s = c.getSelectedItem();
k = Integer.parseInt(s);
repaint();
}
Output
28
Kristu Jyoti College of Management and Technology
17. Develop an applet to display a word on the screen. The size of the word should be
altered based on adjustment in the scrollbar.
Program
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
Output
30
Kristu Jyoti College of Management and Technology
PART-II
31
Kristu Jyoti College of Management and Technology
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def display_info(self):
print(f"Name: {self.name}")
print(f"Age: {self.age}")
class Student(Person):
def __init__(self, name, age, student_id):
super().__init__(name, age)
self.student_id = student_id
super().display_info()
print(f"Student ID: {self.student_id}")
student = Student("Alice", 20, "S12345")
student.display_info()
Output
Name: Alice
Age: 20
Student ID: S12345
class Animal:
def sound(self):
print("Animals make sound")
class Dog(Animal):
def sound(self):
print("Dog barks")
class Cat(Animal):
def sound(self):
print("Cat meows")
animal = Animal()
dog = Dog()
cat = Cat()
animal.sound()
dog.sound()
cat.sound()
32
Kristu Jyoti College of Management and Technology
Output
package mypackage;
package main;
import mypackage.Calculator;
Output:
makefile
Copy code
Addition: 15
Subtraction: 5
try {
int result = numbers[1] / divisor;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Error: Division by zero is not allowed.");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error: Array index out of bounds.");
} catch (Exception e) {
System.out.println("General Exception: " + e.getMessage());
} finally {
System.out.println("This block always executes.");
}
System.out.println("Program continues...");
}
}
Output
interface Exam {
void getMarks(int m1, int m2);
}
interface Sports {
void getSportsMark(int sm);
}
34
Kristu Jyoti College of Management and Technology
Output
Roll Number: 101
Subject 1 Marks: 85
Subject 2 Marks: 90
Sports Weightage Mark: 10
Total Score: 185
35
Kristu Jyoti College of Management and Technology
package mathoperations;
int x = 20;
int y = 5;
Output
Addition: 25
Subtraction: 15
Multiplication: 100
Division: 4.0
36
Kristu Jyoti College of Management and Technology
7. Write a package program to check whether a number is prime. Import the package
to check whether a number is prime and if the number is prime, check the digits are also
prime.
package primecheck;
if (checker.isPrime(num)) {
System.out.println(num + " is a prime number.");
if (checker.areDigitsPrime(num)) {
System.out.println("All digits of " + num + " are prime digits.");
} else {
System.out.println("Not all digits of " + num + " are prime digits.");
}
} else {
37
Kristu Jyoti College of Management and Technology
sc.close();
}
}
Output
Enter a number to check: 233
233 is a prime number.
All digits of 233 are prime digits.
Output
Length of str1: 7
Concatenated string: Hello World
Character at index 2 of str1: H
38
Kristu Jyoti College of Management and Technology
import java.awt.Graphics;
import java.awt.Color;
@Override
public void paint(Graphics g) {
setBackground(Color.WHITE);
g.setColor(Color.RED);
g.fillRect(50, 50, 100, 100);
g.setColor(Color.BLUE);
g.fillOval(200, 50, 80, 80);
g.setColor(Color.GREEN);
int[] xPoints = {350, 380, 410};
int[] yPoints = {50, 100, 50};
g.fillPolygon(xPoints, yPoints, 3);
}
}
Output
39
Kristu Jyoti College of Management and Technology
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
String message;
if (message == null) {
message = "Hello, World!"; // Default message
}
}
Output
11. Create a GUI which accepts details of a student and to store them in a database.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public StudentGUI() {
super("Student Details");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
add(nameLabel);
add(nameField);
add(rollNoLabel);
add(rollNoField);
add(ageLabel);
add(ageField);
add(addressLabel);
add(addressField);
add(submitButton);
add(statusLabel);
setSize(400, 300);
setVisible(true);
setLocationRelativeTo(null);
try {
dbConnection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db", "username",
"password");
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error connecting to the database", "Database
Error", JOptionPane.ERROR_MESSAGE);
}
41
Kristu Jyoti College of Management and Technology
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == submitButton) {
try {
String name = nameField.getText();
String rollNo = rollNoField.getText();
int age = Integer.parseInt(ageField.getText());
String address = addressField.getText();
String sql = "INSERT INTO students (name, roll_no, age, address) VALUES (?, ?, ?,
?)";
PreparedStatement preparedStatement = dbConnection.prepareStatement(sql);
preparedStatement.setString(1, name);
preparedStatement.setString(2, rollNo);
preparedStatement.setInt(3, age);
preparedStatement.setString(4, address);
int rowsAffected = preparedStatement.executeUpdate();
if (rowsAffected > 0) {
statusLabel.setText("Student details saved successfully!");
nameField.setText("");
rollNoField.setText("");
ageField.setText("");
addressField.setText("");
} else {
statusLabel.setText("Failed to save student details.");
}
} catch (SQLException | NumberFormatException ex) {
ex.printStackTrace();
statusLabel.setText("Error processing student details.");
}
}
}
42
Kristu Jyoti College of Management and Technology
Output
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.*;
JTable table;
DefaultTableModel model;
Connection con;
Statement stmt;
ResultSet rs;
public StudentDisplayGUI() {
setTitle("Student Records");
setSize(500, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
model.addColumn("Roll No");
model.addColumn("Name");
model.addColumn("Marks");
connectAndLoadData();
setVisible(true);
43
Kristu Jyoti College of Management and Technology
void connectAndLoadData() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentdb", "root",
"your_password");
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM students");
while (rs.next()) {
int roll = rs.getInt("roll_no");
String name = rs.getString("name");
int marks = rs.getInt("marks");
model.addRow(new Object[]{roll, name, marks});
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Database Error: " + e.getMessage());
}
}
Output
Roll No Name Marks
101 John 85
102 Alice 90
103 Bob 78
13. Create a menu based application program which accepts details of an employee,does
some operation and to display details from the database in GUI.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
Connection con;
PreparedStatement pst;
Statement stmt;
ResultSet rs;
public EmployeeApp() {
setTitle("Employee Management");
setSize(600, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
addItem.addActionListener(this);
viewItem.addActionListener(this);
exitItem.addActionListener(this);
menu.add(addItem);
menu.add(viewItem);
menu.add(exitItem);
menuBar.add(menu);
setJMenuBar(menuBar);
connectDB();
setVisible(true);
}
void connectDB() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/companydb", "root", "your_password"
);
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "DB Connection Failed: " + e.getMessage());
}
}
void addEmployee() {
JTextField idField = new JTextField();
JTextField nameField = new JTextField();
45
Kristu Jyoti College of Management and Technology
Object[] fields = {
"Employee ID:", idField,
"Name:", nameField,
"Salary:", salaryField
};
if (option == JOptionPane.OK_OPTION) {
try {
int id = Integer.parseInt(idField.getText());
String name = nameField.getText();
double salary = Double.parseDouble(salaryField.getText());
void viewEmployees() {
try {
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM employees");
while (rs.next()) {
Object[] row = {
rs.getInt("emp_id"),
rs.getString("name"),
rs.getDouble("salary")
};
model.addRow(row);
}
46
Kristu Jyoti College of Management and Technology
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Error: " + e.getMessage());
}
}
Output
To Compile & Run -
import java.applet.Applet;
import java.awt.*;
47
Kristu Jyoti College of Management and Technology
g.setColor(Color.BLUE);
g.drawOval(50, 50, 100, 100);
g.setColor(Color.BLACK);
g.drawOval(170, 50, 100, 100);
g.setColor(Color.RED);
g.drawOval(290, 50, 100, 100);
g.setColor(Color.YELLOW);
g.drawOval(110, 110, 100, 100);
g.setColor(Color.GREEN);
g.drawOval(230, 110, 100, 100);
}
}
Output
48
Kristu Jyoti College of Management and Technology
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
JLabel label;
public MouseEventExample() {
setTitle("Mouse Event Demo");
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
addMouseListener(this);
setVisible(true);
}
}
}
Output
1.Save the code as MouseEventExample.java.
2.Complie:
javac MouseEventExample.java
3.Run
java MouseEventExample
50