1 Oops Lab Manual Soundarya Senthil Murugan 2024
1 Oops Lab Manual Soundarya Senthil Murugan 2024
AIM
To develop a Java application to generate Electricity bill with the following members:
Consumer no., consumer name, previous month reading, current month reading, type of EB
connection (i.e domestic or commercial). Compute the bill amount using the following tariff.
If the type of the EB connection is domestic, calculate the amount to be paid as follows:
If the type of the EB connection is commercial, calculate the amount to be paid as follows:
PROCEDURE
1. Start
2. Create a class Ebill with three member functions: getdata(), calc() and
display() and object ob is created for another class Ebconsumer.
3. Create another class Ebconsumer which defines the above methods.
4. Using getdata() function get the consumed units for current and previous month .
5. Using calc()function calculate the amount depending on the type of EB
connection and no. of units consumed.
6. Using display() function display the bill.
7. Stop.
1
PROGRAM
import java.util.*;
class Ebill
{
public static void main (String args[])
{
Ebconsumer ob = new Ebconsumer();
ob.getdata();
ob.calc();
ob.display();
}
}
class Ebconsumer
{
String consumer_name,consumer_type;
long ebill_no;
double current_reading,previous_reading,total_bill,units;
Scanner in = new Scanner(System.in);
void getdata()
{
System.out.println("Enter Type of connection (D for Domestic or C for Commercial): ");
consumer_type=in.next();
System.out.println("Enter consumer number:");
ebill_no=in.nextLong();
System.out.println("Enter consumer name:");
consumer_name=in.next();
System.out.println("Enter previous month reading:");
previous_reading= in.nextDouble();
System.out.println("Enter current month reading:");
current_reading= in.nextDouble();
}
2
void calc()
{
if(previous_reading<current_reading)
{
units=current_reading - previous_reading;
if(consumer_type.equals("D"))
{
if (units<=100)
total_bill=units*1;
else if (units<=200)
total_bill=(100*1+(units-100)*2.50);
else if(units<=500)
total_bill=(100*1+100*2.50)+(units-200)*4;
else
total_bill= (100*1+100*2.50+300*4+(units-500)*6);
}
else
{
if (units<=100)
total_bill=units*2;
else if (units<=200)
total_bill=(100*2+(units-100)*4.50);
else if(units<=500)
total_bill=(100*2+100*4.50)+(units-200)*6;
else
total_bill= (100*2+100*4.50+300*6+(units-500)*7);
}
}}
3
void display()
{
System.out.println("************ELECTRICITY BILL************");
System.out.println("Consumer number = "+ebill_no);
System.out.println("Consumer name = "+consumer_name);
if(consumer_type.equals("D"))
System.out.println("Type of connection = DOMESTIC ");
else
System.out.println("Type of connection = COMMERCIAL ");
4
OUTPUT
RESULT
Thus the java program to generate electricity bill was implemented and executed successfully.
5
EX. NO:2 Develop a Program to implement Constructor Overloading.
AIM
PROCEDURE
1. Start
7. Stop.
6
PROGRAM
package constructoroverloading;
7
OUTPUT
Student Id : 0
Student Name : null
Student Id : 10
Student Name : David
RESULT
Thus the java program to perform Constructor overloading was implemented and executed successfully.
8
EX. NO:3 Develop a Java Program to implement String Handling Methods
AIM
PROCEDURE
1. Start
a) String Concatenation
b) Finding char in a string using index position
c) Comparing 2 string methods
d) Comparing 2 strings ignoring case.
e) Finding the hash code of a string
f) Finding index position for a given character
g) Finding last index position for a given character
h) Finding the length of a string
i) Replacing one character with another character in a given string
j) Finding the substring in a given string
k) Converting the given string to lower case
l) Converting the given string to upper string
m) Trimming the spaces in a given string
4. Stop.
9
PROGRAM
String s1="VTHTCSE";
10
OUTPUT
index of E in string 2 is 5
length of string 2 is 14
RESULT
Thus the java program to perform string operations using string handling methods has been implemented
and executed successfully.
11
EX. NO:4a Implementation of Inheritance using Java.
AIM
PROCEDURE
1. Start
2. Create a Class Named As C1 With Two Variable l & b.
3. Extends the Class C1 in New Class C2 And Create a Constructor c2(int,int).
4. In c2(int,int) Constructor Multiply the Values.
5. In Main Method Read Two Values And Pass The Values to c2(l,b).
6. Display the Result.
7. Stop.
12
PROGRAM
import java.io.*;
class c1 {
int l,b; }
class c2 extends c1 {
c2(int x, int y)
l=x;
b=y;
}}
class inhr
int len,brth;
len=Integer.parseInt(br.readLine());
brth=Integer.parseInt(br.readLine());
c2 c=new c2(len,brth); }}
13
OUTPUT
D: \Java>javac inhr.java
D:\Java>java inhr
RESULT
Thus the Above Simple Inheritance Program has been implemented and executed successfully.
14
EX. NO:4b Implementation of Inheritance using Java.
AIM
PROCEDURE
1. Start
2. Create a Class Named As ‘a’ With ipo() Method And Get the Value.
3. Extends the Class ‘a” in New Class ‘b’ And Create a Method ftoc( ) And Call
the Method ipo( ).
4. Create Another Class ‘c’ Extends Class ‘b’ And Create Method disp( ).
5. In disp( ) Call the Method ftoc( ).
6. In Main Method Create An Object For c As c1 And Call the Method disp( ).
7. Display the Result.
8. Stop.
15
PROGRAM
import java.io.*;
class a
int po;
po=Integer.parseInt(br.readLine());
class b extends a
ipo();
double kil=po*0.4536;
16
class c extends b
ftoc();
}}
class mulinhr
c c1=new c();
c1.disp();
17
OUTPUT
Enter Pound : 5
Kilogram : 2.268
RESULT
Thus the above Multilevel Inheritance Program has been implemented and executed Successfully.
18
EX. NO:5a
Implementation of Interfaces using Java.
AIM
PROCEDURE
1. Create a Class Named As stud With Two Methods getno(int) & putno ( ).
2. Extends the Class stud in New Class test And Create a Methods
getmrk(float,float) & putmrk( ).
3. Create An Interface sports Declare putwt( ) Method
4. Create Another Class result Extends test And Implements sports.
5. In result Call the Methods And Define the Method putwt( ).
6. In Main Method Read Create Object For result And Call the Methods
getno(1234),getmrk(67.6,87.5) & disp().
7. Display the Result.
8. Stop the Program.
19
PROGRAM
class stud
int rno;
void getno(int n)
rno = n;
void putno()
float p1,p2;
p1 = m1;
p2 = m2;
20
void putmrk()
interface sports
void putwt();
float tot;
void disp()
tot = p1 + p2 + spwt;
putno();
21
putmrk();
putwt();
}}
class multiple
Weight=6.0 ");
std.getno(1234);
std.getmrk(67.6f,87.5f);
std.disp();
22
OUTPUT
Roll No : 1234
Marks obtained
Part1 = 67.6
Part2 = 87.5
Sports wt = 6.0
RESULT
Thus the Above Multiple Inheritance Program has been executed successfully.
23
EX. NO:5b
Implementation of Packages using Java.
AIM
To Write a Java Program Working With Package And Imports the Package Classes to Outside of the
Classes.
PROCEDURE
4. .Create Another Class Named as ‘stud’ And Imports the Package student Store the Class
5. In stud Class Create An Object For vh As v and Call the Method in( ).
24
PROGRAM
package student;
import java.io.*;
public class vh
name = br.readLine();
no = br.readLine();
25
import student.vh;
import java.io.*;
vh v = new vh();
v.in();
26
OUTPUT
RESULT
Thus the above Package Program has been implemented and executed Successfully.
27
EX. NO:6
Implementation of Abstract Class in Java.
AIM
PROCEDURE
1. Start
2. Create an abstract class named shape that contains two integers and an empty method named
printarea().
3. Provide three classes named rectangle, triangle and circle such that each one of the classes extends the
class Shape.
4. Each of the inherited class from shape class should provide the implementation for the method
printarea().
5. Get the input and calculate the area of rectangle, circle and triangle.
6. In the shapeclass, create the objects for the three inherited classes and invoke the methods and display
7. Stop.
28
PROGRAM
import java.util.*;
int a,b;
a=s.nextInt();
b=s.nextInt();
area_rect=a*b;
29
class triangle extends shape
double area_tri;
a=s.nextInt();
b=s.nextInt();
double area_circle;
a=s.nextInt();
area_circle=(3.14*a*a);
System.out.println("Radius of circle:"+a);
}}
30
public class Shapeclass
r.printarea();
t.printarea();
r1.printarea();
31
OUTPUT
32
EX. NO:7
Write a Java Program using Exception Handling
AIM
PROCEDURE
1. Start
6. Using the exception handling mechanism , the thrown exception is handled by the catch construct.
7. After the exception is handled , the string “invalid amount “ will be displayed.
8. If the amount is greater than 0, the message “Amount Deposited “ will be displayed
9. Stop.
33
PROGRAM
import java.util.*;
this.msg=msg;
return msg;
}}
try
if(a<0)
System.out.println("Amount Deposited");
catch(NegativeAmtException e){
System.out.println(e);
}}}
34
OUTPUT
RESULT
Thus the Java program to implement user defined exception handling has been implemented and
executed successfully.
35
EX. NO:8
Implementation of I/O Streams in Java Interface
AIM
To write a java program that reads a file name from the user, displays information about
whether the file exists, whether the file is readable, or writable, the type of file and the length of the file in
bytes.
PROCEDURE
1.Start
2.Create a class filedemo. Get the file name from the user.
3.Use the file functions and display the information about the file.
6.getParent () -This method returns the pathname string of this abstract pathname’s parent, or
16. Invoke the predefined functions to display the information about the file.
17. Stop.
36
PROGRAM
import java.io.*;
import java.util.*;
class filedemo
String filename;
filename=s.nextLine();
System.out.println("******************");
System.out.println("FILE INFORMATION");
System.out.println("******************");
if(f1.exists())
else
if(f1.canRead())
else
37
System.out.println("THE FILE CANNOT BE READ ");
if(f1.canWrite())
else
if(f1.isDirectory())
else
System.out.println("NOT A DIRECTORY");
if(f1.isFile())
else
System.out.println("NOT A FILE");
38
OUTPUT
RESULT
Thus the java program to display file information has been implemented and executed successfully.
39
AIM
PROCEDURE
1.Start
2.Create a class even which implements first thread that computes the square of the number .
3.run() method implements the code to be executed when thread gets executed.
4.Create a class odd which implements second thread that computes the cube of the number.
5.Create a third thread that generates random number. If the random number is even, it displays the
square of the number. If the random number generated is odd, it displays the cube of the given number.
6.The Multithreading is performed and the task switched between multiple threads.
7.The sleep () method makes the thread to suspend for the specified time.
8.Stop.
PROGRAM
import java.util.*;
40
class even implements Runnable
this.x = x;
System.out.println("New Thread "+ x +" is EVEN and Square of " + x + " is: " + x * x);
this.x = x;
System.out.println("New Thread "+ x +" is ODD and Cube of " + x + " is: " + x * x * x);
int num = 0;
41
Random r = new Random(); try
num = r.nextInt(100);
if (num % 2 == 0){
t1.start();
else{
Thread.sleep(1000);
System.out.println("-----------------------------------");
}}
System.out.println(ex.getMessage());}}}
OUTPUT
42
RESULT
Thus the Java program for multi-threaded application has been implemented and executed successfully.
43
EX. NO:10 Implementation of Generic Function in Java Interface
AIM
To write a java program to print different type of array using a single Generic function.
PROCEDURE
1. start
2. Create one dimensional arrays of specific data types and initialize them with values.
4. Create a generic function which supports all the different data typed arrays.
6. Stop.
44
PROGRAM
Integer[] intArray={1,2,3,4,5};
Double[] doubleArray={5.5,4.4,3.3,2.2,1.1};
Character[] charArray={'H','E','L','L','O'};
String[] stringArray={"B","Y","E"};
displayArray(intArray);
displayArray(doubleArray);
displayArray(charArray);
displayArray(stringArray);
System.out.print(temp+" ");
System.out.println();
45
OUTPUT
RESULT
Thus the Java program to print different type of array elements using a generic function has been
implemented and executed successfully.
46
EX. NO:11a Implementation of AWT controls and Event classes using Java Program.
AIM
To develop an AWT Applet program using Buttons and Controls.
PROCEDURE
4. Set the Layout and add necessary Text field, Labels and Buttons.
47
PROGRAM
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
Button b1,b2,b3,b4;
Label l1,l2,l3;
TextField t1,t2,t3;
String s1,s2;
int a,b,c;
setLayout(new GridLayout(5,2));
l3=new Label("Result",l3.LEFT);
add(l1);
t1=new TextField(20);
add(t1);
48
add(l2);
t2=new TextField(20);
add(t2);
add(l3);
t3=new TextField(20);
add(t3);
b1=new Button("ADDITION");
add(b1);
b2=new Button("SUBTRACTION");
add(b2);
b3=new Button("MULTIPLICATION");
add(b3);
b4=new Button("DIVISION");
add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
49
public void actionPerformed(ActionEvent ae)
s1=t1.getText();
s2=t2.getText();
a=Integer.parseInt(s1);
b=Integer.parseInt(s2);
if(ae.getSource()==b1)
c=a+b;
else if(ae.getSource()==b2)
c=a-b;
else if(ae.getSource()==b3)
c=a*b;
else if(ae.getSource()==b4)
c=a/b;
t3.setText(""+c);
}}
50
OUTPUT
RESULT
Thus the above program has been implemented and executed Successfully.
51
EX. NO:11b Implementation of AWT controls and Event classes using Java Program.
AIM
To develop an AWT Applet program using Event classes.
PROCEDURE
4. Create Panel,set the Layout and add necessary Text field, Labels and Buttons.
52
//Java program to handle MouseListener events
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// default constructor
mouseex(){ }
// main class
// create a frame
f.setSize(600, 100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p.setLayout(new FlowLayout());
f.addMouseListener(m);
p.add(label1);
p.add(label2);
p.add(label3);
f.add(p);
f.show();
// mouse position
54
public void mousePressed(MouseEvent e)
// show the point where the user released the mouse click
// show the point through which the mouse exited the frame
{
55
// show the point through which the mouse entered the frame
label3.setText("mouse clicked at point:"+ e.getX() + " "+ e.getY() + "mouse clicked :" +
e.getClickCount());
56
OUTPUT
RESULT
Thus the above program has been implemented and executed Successfully.
57
EX. NO:12 Develop a miniproject for any application using JDBC
AIM:
To develop a Query application with database connectivity
PROCEDURE:
4. Create Panel,set the Layout and add necessary Text field, Labels and Buttons.
58
PROGRAM
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
TextField pid;
TextField pname;
Button query;
Panel p;
public PreparedQueryApp()
setLayout(new GridLayout(5,1));
pid=new TextField(20);
pname=new TextField(50);
query=new Button("Query");
59
p=new Panel();
add(pid);
add(pname);
add(p);
p.add(query);
query.addActionListener(this);
pack();
setVisible(true);
try
Class.forName("com.mysql.jdbc.Driver");
System.out.println("driver loaded");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/dsn","root","ad
min");
System.out.println("connected");
60
catch(Exception e)
System.out.println("Error"+e);
obj.showRecord(result);
if(event.getSource()==query)
try
stat.setString(1,pid.getText());
result=stat.executeQuery();
result.next();
catch(Exception e) {
showRecord(result);
}
61
}
try
pid.setText(result.getString(1));
pname.setText(result.getString(2));
catch(Exception e)
62
OUTPUT
63
64
RESULT
Thus the above Query application miniproject has been Executed Successfully.
AIM
PROCEDURE
65
PROGRAM
//democlass.java
import java.sql.*;
import java.util.Scanner;
try
{ int choice=0;
do
choice=choicein.nextInt();
switch(choice)
case 1:
s.getStudentDetails();
s.insertStudent();
break;
case 2:
66
s.updateStudentPassword();
break;
case 3:
s.deleteStudentRecord();
break;
case 4:
s.searchStudent();
break;
case 5:
break;
default:
}while(choice!=5);
catch(Exception e)
System.out.println(e.getMessage());
67
class student
name = input.nextLine();
password = input.nextLine();
country = input.nextLine();
mark = input.nextInt();
68
dbmsconnection dbmsconnect = new
dbmsconnection("jdbc:oracle:thin:@localhost:1521:XE","sakthi","sakthi");
System.out.println("oracle started");
stmt.setString(1, name);
stmt.setString(2, password);
stmt.setString(3, country);
stmt.setInt(4, mark);
int i = stmt.executeUpdate();
dbmsconnect.closeConnection(con, stmt);
String inputname=input.nextLine();
String inputpass=input.nextLine();
69
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1, inputpass);
stmt.setString(2, inputname);
int i = stmt.executeUpdate();
if(i>0)
}else
dbmsconnect.closeConnection(con, stmt);
String inputname=input.nextLine();
stmt.setString(1, inputname);
int i = stmt.executeUpdate();
70
if(i>0)
else
dbmsconnect.closeConnection(con, stmt);
String inputname=input.nextLine();
stmt.setString(1, inputname);
ResultSet rs = stmt.executeQuery();
if(rs.next()==false)
71
}
else
dbmsconnect.closeConnection(con, stmt);
}}
class dbmsconnection
String url;
String username;
String password;
this.url = url;
this.username = username;
this.password = password;
Connection con=null;
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
con = DriverManager.getConnection(url,username,password);
return con;
72
{
stmt.close();
con.close();
OUTPUT
Table creation:
number(10));
Table created.
NAME VARCHAR2(10)
PASSWORD VARCHAR2(10)
COUNTRY VARCHAR2(10)
MARK NUMBER(10)
73
Microsoft Windows [Version 6.3.9600]
C:\Users\Students>d:
D:\>cd javapro
D:\javapro>path=C:\Program Files\Java\jdk1.8.0\bin;
D:\javapro>set classpath=%classpath%;C:\oraclexe\app\oracle\product\11.2.0\server\jdbc\lib\ojdbc6.jar;
D:\javapro>javac democlass.java
D:\javapro>java democlass
Select an operation
1- Registration
2- Password Update
3- Delete a Record
5- Exit
74
Enter your choice:
sakthi
sa
india
85
oracle started
Select an operation
1- Registration
2- Password Update
3- Delete a Record
5- Exit
75
Connection Established Successfully
sakthi
sakthi123
Select an operation
1- Registration
2- Password Update
3- Delete a Record
5- Exit
sakthi
76
RESULT
Thus the student information system has been Executed Successfully.
AIM
PROCEDURE
1. Start
7. Stop.
77
PROGRAM:
p1.start();
c1.start(); }}
class CubbyHole
try {
wait();
} catch (InterruptedException e) {}
available = false;
notifyAll();
return contents;
78
public synchronized void put(int value) {
try {
wait();
} catch (InterruptedException e) { }
contents = value;
}}
cubbyhole = c;
this.number = number;
int value = 0;
value = cubbyhole.get();
79
System.out.println("Consumer #" + this.number + " got: " + value);
} }}
cubbyhole = c;
this.number = number;
try {
sleep((int)(Math.random() * 100));
} catch (InterruptedException e) { }
80
OUTPUT:
Producer #1 put: 0
Consumer #1 got: 0
Producer #1 put: 1
Consumer #1 got: 1
Producer #1 put: 2
Consumer #1 got: 2
Producer #1 put: 3
Consumer #1 got: 3
Producer #1 put: 4
Consumer #1 got: 4
Producer #1 put: 5
Consumer #1 got: 5
Producer #1 put: 6
Consumer #1 got: 6
Producer #1 put: 7
Consumer #1 got: 7
Producer #1 put: 8
Consumer #1 got: 8
Producer #1 put: 9
Consumer #1 got: 9
81
RESULT:
Thus the producer consumer problem using threads had been executed successfully.
82