OOP Lab Manual - A7603 - II Year - 2 Semester - CSM
OOP Lab Manual - A7603 - II Year - 2 Semester - CSM
import Measure.*;
public class NeedConverter
{
public static void main(String args[])
{
Converter c=new Converter();
System.out.println(" mm to m is "+c.mmtom(100));
System.out.println(" cm to m is "+c.cmtom(1000));
System.out.println(" m to km is "+c.mtokm(3000));
}
}
Output:
interface Student
{
void displayGrade();
void attendence();
}
}
class UGStudent implements Student
{
String name;
int rollno;
String grade;
double att;
}
}
Output:
WEEK-7
Objective: After the completion of the practice session, the
student will be able toimplement Exception Handling .
a) Creates a user interface to perform integer divisions. The user
enters two numbers in the text fields, Num1 and Num2. The
division of Num1 andNum2 is displayed in the Result field when the
Div- id button is clicked. If Num1 or Num2 were not an integer, the
program would throw a NumberFormatException. If Num2 is Zero,
the program would throw an ArithmeticException. Display the
exception in a message dialog box.
import java.util.*;
public class ExcepDemo1
{
public static void main (String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number num1");
String s1 = sc.next();
System.out.println("Enter the number2");
String s2=sc.next();
try
{
int num1 = Integer.parseInt(s1);
int num2 = Integer.parseInt(s2);
System.out.println("num1 is " +num1);
System.out.println("num2 is " +num2);
if(num2 ==0)
throw new ArithmeticException ("Division Error");
catch(ArithmeticException e)
{
System.out.println("num2 must not be zero");
System.out.println("Exception" +e);
}
finally
{
System.out.println("Finally block is executed");
}
System.out.println("Remaining statements");
}
}
Output:
b) In the CustomExceptionTest class, the age is expected to be
a positive number. It would throw the user defined
exception NegativeAgeException if the age is assigned a
negative number.
Output:
WEEK-8
Objective: After the completion of the practice session, the
student will be able todevelop applications on Multithreaded
Programming and thread synchronization.
a) Create a multithreaded java program by creating a subclass of
Thread and then creating, initializing, and staring two Thread
objects from your class. The threads will execute concurrently
and display “Java is object oriented” in console window.
class NewThread extends Thread
{
NewThread(String name)
{
super(name);
//start();
}
t1.start();
t2.start();
System.out.println("Main
Program");
}
}
b) Implement the concept of producer consumer problem using
thread synchronization.
class Buffer
{
int item;
boolean produced = false;
synchronized void produce(int x)
{
if(produced)
{
try{
wait();
}
catch(InterruptedException ie)
{
System.out.println("Exception Caught");
}
}
item =x;
System.out.println("Producer - Produced-->" +item);
produced =true;
notify();
}
Buffer b;
Producer( Buffer b)
{
this.b = b;
start();
}
}
}
Consumer(Buffer b)
{
this.b = b;
start();
}
public void run()
{
b.consume();
b.consume();
b.consume();
b.consume();
}
}
import java.util.*;
class Employee
{
int eid;
String ename;
double sal;
import java.util.*;
class HashDemo
{
public static void main(String args[])
{
//Creating HashSet
HashSet<String> set=new HashSet<String>();
Iterator<String> i=set.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}
Output:
WEEK-10
Objective: After the completion of the practice session, the student
will be able to implement Collection Frameworks to retrieve data.
a) Implement MouseListener and MouseMotionListener.
import java.awt.*;
import java.awt.event.*;
MouseDemo(String title)
{
super(title);
addMouseListener(this);
addMouseMotionListener(this);
setSize(500,500);
setVisible(true);
//window close
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}
} );
msg= "MouseClicked";
x = e.getX();
y = e.getY();
repaint();
}
Output:
import java.awt.*;
import java.awt.event.*;
public class KeyDemo extends Frame implements KeyListener
{
Label l;
TextArea area;
KeyDemo()
{
l=new Label();
l.setBounds(20,50,100,20);
area=new TextArea();
area.setBounds(20,80,300, 300);
area.addKeyListener(this);
add(l);add(area);
setSize(400,400);
setLayout(null);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}
});
}
Output:
WEEK-11
Objective: After the completion of the practice session, the student
will be able to Develop GUI applications using AWT.
l3 = new Label("->");
l3.setBounds(80,200,200,20);
add(l1);add(t1);
add(l2);add(t2);
add(b1);add(l3);
b1.addActionListener(this);
setSize(400,400);
setLayout(null);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}});
Output:
b) Using Grid Layout design a Simple calculator with
appropriate event handling.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
add(b7);
add(b8);
add(b9);
add(t1);
add(b4);
add(b5);
add(b6);
add(mul);
add(b1);
add(b2);
add(b3);
add(sub);
add(b0);
add(eql);
add(add);
add(div);
//t1.setBounds(30,30,200,40);
add(clr);
add(rem);
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
div.addActionListener(this);
mul.addActionListener(this);
add.addActionListener(this);
sub.addActionListener(this);
eql.addActionListener(this);
clr.addActionListener(this);
rem.addActionListener(this);
}
Output:
WEEK-12
Objective: After the completion of the practice session, the student will be
able to Develop GUI applications using Swing Controls.
EmpDemo( )
{
jf = new JFrame( ); //Top or High Level Window
jf.setSize(300,300);
jf.setTitle("Demo");
jp = new JPanel(); // lower or second level window
l1 = new JLabel("EMP-ID");
l2 = new JLabel("EMP-NAME");
l3 = new JLabel("Designation");
l4 = new JLabel("Gender");
l5 = new JLabel("CITY");
bg = new ButtonGroup();
bg.add(r1);
bg.add(r2);
jc = new JComboBox(cities);
b1 = new JButton("Submit");
jf.setLayout(new GridLayout(5,4));
jp.add(l1); jp.add(t1);
jp.add(l2); jp.add(t2);
jp.add(l3); jp.add(t3);
jp.add(l4); jp.add(r1);jp.add(r2);
jp.add(l5); jp.add(jc);
jf.add(jp);
jf.setSize(1000,600);
jf.setVisible(true);
b1.addActionListener (this);
}
if(r1.isSelected())
data= data+ "Male" +";";
else
data= data+ "FeMale" +";";
if(c1.isSelected())
data= data + c1.getText();
else if(c2.isSelected())
data=data +c2.getText();
else
data=data+c3.getText();
ta1.setText(data);
}
public static void main(String args[])
{
new EmpDemo();
}
}
Output:
b) Create a JTable to display various fields of Student data like
RollNo, Name, Branch ,Year, Percentage etc.
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
/* import javax.swing.*/
JFrame f;
JTable j;
JStudent()
{
f = new JFrame();
f.setTitle("JTable Example");
String[][] data = {
{ "101", "Rajesh", "CSE","II","78.5"},
{ "102", "Harsha", "CSE","II","87.5"},
{ "103", "Varsha", "CSE","II","65.5"},
{ "104", "Kiran", "IT","II","75.5"},
{ "105", "Karan", "IT","II","87.5"},
};
// Column Names
String[] head = { "RollNo", "Name", "Department","Branch" , "Percentage"};
// adding it to JScrollPane
JScrollPane sp = new JScrollPane(j);
f.add(sp);
f.setSize(500, 200);
f.setVisible(true);
}
Output: