Final Oops Lab Manual
Final Oops Lab Manual
To save: Prime.java
To compile: javac Prime.java
To run: java Prime
***********OUTPUT***********
Enter the value of n: 10
Prime Numbers from 1 to n Are:
2 3 5 7
2. Roots of Quadratic Equation
Program
import static java.lang.Math.*;
public class EQR
{
static void calculateRoots(int a, int b, int c)
{
if (a == 0)
{
System.out.println("The value of a cannot be 0.");
return;
}
int d = b * b - 4 * a * c;
double sqrtval = sqrt(abs(d));
if (d > 0)
{
System.out.println("The roots of the equation are real and different. \n");
System.out.println((double)(-b + sqrtval) / (2 * a) + "\n"+ (double)(-b - sqrtval) / (2 * a));
}
else if (d == 0)
{
System.out.println("The roots of the equation are real and same. \n");
System.out.println(-(double)b / (2 * a) + "\n"+ -(double)b / (2 * a));
}
else
{
System.out.println("The roots of the equation are complex and different. \n");
System.out.println(-(double)b / (2 * a) + " + i"+ sqrtval + "\n"+ -(double)b / (2 * a)+ " - i" +
sqrtval);
}
}
public static void main(String args[])
{
int a = 1, b = -2, c = 1;
calculateRoots(a, b, c);
}
}
COMPILATION & EXECUTION
To save: EQR.java
To compile: javac EQR.java
To run: java EQR
***********OUTPUT***********
import java.util.*;
class EBill
{
public static void main(String args[])
{
int units=280;
double billpay=0;
if(units<100)
{
billpay=units*1.20;
}
else if(units<300)
{
billpay=100*1.20+(units-100)*2;
}
else if(units>300)
{
billpay=100*1.20+200*2+(units-300)*3;
}
To save: EBill.java
To compile: javac EBill.java
To run: java EBill
***********OUTPUT***********
To save: Matrix.java
To compile: javac Matrix.java
To run: java Matrix
***********OUTPUT***********
6 12 18
6 12 18
6 12 18
5. Dynamic Binding
Program
public class DynamicBinding
void print() {
//@Override
void print(){
sup.print();
sub.print();
}}
COMPILATION & EXECUTION
To save : DynamicBinding.java
***********OUTPUT***********
Class Calculation
int z;
z=x+y;
z=x-y;
z=x*y;
int a=20,b=10;
demo.addition(a,b);
demo.subtraction(a,b);
demo.multiplication(a,b);
To save: My_Calculation.java
***********OUTPUT***********
class Emp
String name;
int id;
String address;
this.name=name;
this.id=id;
this.address=address;
void putdata()
System.out.println("name:"+name);
System.out.println("id:"+id);
System.out.println("address:"+address);
class Empdemo{
{
Emp e=new Emp();
e.getdata("smith",2084,"anathapur");
e.putdata();
To save : Empdemo.java
***********OUTPUT***********
Employee Details are
Name : smith
Id : 2048
Address : ananthapur
8. Define a class Define instance method for getting
and instance of object
Program
class Sum
System.out.println("sum of two:"+(a+b));
System.out.println("sum of three:"+(a+b+c));
class polymorphism
s.add(10,15);
s.add(10,20,30);
}
COMPILATION & EXECUTION
To save : Polymorphism.java
***********OUTPUT***********
Sum of two : 25
Sum of three: 30
9. Preventing Inheritance using Final
Program
void Display()
System.out.println("main class");
void Display()
System.out.println("sub class");
obj.Display();
}
COMPILATION & EXECUTION
To save : Javaapp.java
***********OUTPUT***********
void callmetoo()
void callme()
class AbstractDemo
b.callme();
b.callmetoo();
}
COMPILATION & EXECUTION
To save : AbstractDemo.java
import java.lang.*;
interface Area{
return(pi*x*y);
return(pi*x*x);
class InterfaceDemo{
Area A;
A=rect;
System.out.println("Area of rectangle="+A.compute(10,20));
A=cir;
System.out.println("Area of circle="+A.compute(30,0));
To save : InterfaceDemo.java
import java.lang.*;
class A
void cal(double x)
System.out.println("square value="+(x*x));
class B extends A
void cal(double x)
System.out.println("square root="+Math.sqrt(x));
class Polymorphism
A a=new A();
B b=new B();
b.cal(15);
a.cal(15);
}
To save : Polymorphism.java
import java.lang.*;
interface Area
return(pi*x*y);
class InterfaceDemo{
double rect.compute(10,2,12,3);
rect.display_result(result);
}
COMPILATION & EXECUTION
To save : InterfaceDemo.java
******************** OUTPUT*************
Area of Rectangle : 628.0
import java.util.*;
import java.io.*;
class Rfile
IOException{
int j=1;
char ch;
String str=scr.next();
int n=f.available();
System.out.print(j+":");
for(int i=0;i<n;i++)
ch=(char)f.read();
System.out.print(ch);
if(ch=='\n')
System.out.println(++j+":");
}}}}
COMPILATION & EXECUTION
To save : Rfile.java
1:Import java.io.*;
2:Class Demo
3:{
5:{
6:System.out.println(“This is demo”);
7:}
8:}
*/
15.Program for implementing Traffic Signals
Program
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*<applet code="Signals" width=400 height=250></applet>*/
public class Signals extends Applet implements ItemListener
{
String msg="";
Checkbox stop,ready,go;
CheckboxGroup cbg;
public void init()
{
cbg = new CheckboxGroup();
stop = new Checkbox("Stop", cbg, false);
ready = new Checkbox("Ready", cbg, false);
go= new Checkbox("Go", cbg, false);
add(stop);
add(ready);
add(go);
stop.addItemListener(this);
ready.addItemListener(this);
go.addItemListener(this);
msg=cbg.getSelectedCheckbox().getLabel();
g.drawOval(165,40,50,50);
g.drawOval(165,100,50,50);
g.drawOval(165,160,50,50);
if(msg.equals("Stop"))
{
g.setColor(Color.red);
g.fillOval(165,40,50,50);
}
else if(msg.equals("Ready"))
{
g.setColor(Color.yellow);
g.fillOval(165,100,50,50);
}
else
{
g.setColor(Color.green);
g.fillOval(165,160,50,50);
}
}
}
To save : Signals.java
To run : appletviewer.java
***************** OUTPUT **************
16. Java program to implement solution of producer
consumer problem.
Program
import java.util.LinkedList;
public class ThreadExample {
public static void main(String[] args) throws InterruptedException
{
t1.start();
t2.start();
t1.join();
t2.join();
}
list.add(value++);
notify();
Thread.sleep(1000);
}
}
}
while (list.size() == 0)
wait();
System.out.println("Consumeconsumed-+ val);
notify();
Thread.sleep(1000);
}
}
}
}
}
*************/OUTPUT***********
Producer produced-0
Producer produced-1
Consumer consumed-0
Consumer consumed-1
Producer produced-2
17. Program for using “this” keyword
Program
import java.io.*;
class Si{
float p,t,r;
this.p=p;
this.t=t;
this.r=r;
void rate()
class This
Si s=new Si();
s.getdata(10.0f,11.2f,29.8f);
s.rate();
}
Compilation & Execution
To Save: Si.java
Execution: java Si
*************OUTPUT***********
Rate is 3337.5999
18. program to print student details
Program
import java.io.*;
class Student
String SName;
String Htn;
float per;
SName=s;
Htn=h;
per=p;
void disp()
System.out.println("Name="+SName);
System.out.println("Percentage="+per);
class StudentDetails
ob.disp();
To Save: StudentDetails.java
*************OUTPUT***********
Name=saranyadevi
Percentage=99.9
19.Establishing JDBC Connection in Java
Program
import java.sql.*;
import java.util.*;
class Main
{
public static void main(String a[])
{
//Creating the connection
System.out.println("enter class");
String cls = k.next();
try
{
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
//Reference to connection interface
con = DriverManager.getConnection(url,user,pass);
Statement st = con.createStatement();
int m = st.executeUpdate(sql);
if (m == 1)
System.out.println("inserted successfully : "+sql);
else
System.out.println("insertion failed");
con.close();
}
catch(Exception ex)
{
System.err.println(ex);
}
}
}
To Save: Main.java
int seconds;
int minutes;
int hours;
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
diff;
start.minutes, start.seconds);
stop.seconds);
diff.seconds);
{
Time diff = new Time(0, 0, 0);
--start.minutes;
start.seconds += 60;
--start.hours;
start.minutes += 60;
return(diff);
To compile:javac Time.java
*************OUTPUT***********
T max = x;
if (y.compareTo(max) > 0)
max = y;
if (z.compareTo(max) > 0)
max = z;
return max;
System.out.printf("Maximum of %.1f, %.1f and %.1f is %.1f\n\n", 6.6, 8.8, 7.7, maximum(6.6,
8.8, 7.7));
}
Compilation & Execution
To save: MainClass.java
*************OUTPUT***********
Maximum of 3, 4 and 5 is 5
import java.awt.event.*;
import javax.swing.*;
public SwingControlDemo(){
prepareGUI();
swingControlDemo.showTextFieldDemo();
mainFrame.setSize(400,400);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
});
statusLabel.setSize(350,100);
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
loginButton.addActionListener(new ActionListener() {
statusLabel.setText(data);
});
controlPanel.add(namelabel);
controlPanel.add(userText);
controlPanel.add(passwordLabel);
controlPanel.add(passwordText);
controlPanel.add(loginButton);
mainFrame.setVisible(true);
To Save: SwingControlDemo.java
import javax.swing.JOptionPane;
length = Integer.parseInt(input);
width = Integer.parseInt(input);
To Save: RectangleTest.java
buttons[0].addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MES
SAGE);
} catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Please Input a
Integer","Error",JOptionPane.ERROR_MESSAGE);
);
buttons[1].addActionListener(
new ActionListener() {
catch(NumberFormatException e)
}
}
);
buttons[2].addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
String input1, input2;
int num1, num2, result;
input1 = JOptionPane.showInputDialog("Please Input First Number: ");
input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
num1 = Integer.parseInt(input1);
num2 = Integer.parseInt(input2);
result = num1 * num2;
JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MES
SAGE);
} catch (NumberFormatException e){
JOptionPane.showMessageDialog(null, "Please Input a
Integer","Error",JOptionPane.ERROR_MESSAGE);
}
}
}
);
buttons[3].addActionListener(
new ActionListener() {
try {
String input1, input2;
int num1, num2, result;
input1 = JOptionPane.showInputDialog("Please Input First Number: ");
input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
num1 = Integer.parseInt(input1);
num2 = Integer.parseInt(input2);
);
buttons[0].setMnemonic('A');
buttons[1].setMnemonic('S');
buttons[2].setMnemonic('M');
buttons[3].setMnemonic('D');
setVisible(true);setResizable(false);
}
public static void main (String[] args) {
*************OUTPUT***********
26.Program to find the number of words in the given
text file
Program
import java.io.BufferedReader;
import java.io.FileReader;
public class CountWordFile
{
public static void main(String[] args) throws Exception
{
String line;
int count = 0;
FileReader file = new FileReader("data.txt");
BufferedReader br = new BufferedReader(file);
while((line = br.readLine()) != null) {
String words[] = line.split(" ");
count = count + words.length;
}
To Save: CountWordFile.java
*************OUTPUT***********
import java.lang.Exception;
import java.lang.*;
import java.lang.Exception;
import java.io.DataInputStream;
MyException(String Message)
super(Message);
class User
int age;
try
age=Integer.parseInt(ds.readLine());
if(age<15||age>25)
{
throw new MyException("number not in range");
catch(MyException e)
System.out.println("caught MyException");
System.out.println(e.getMessage());
catch(Exception e)
System.out.println(e);
To save: User.java
*************OUTPUT***********
16