II CSE OOP LAB Manual
II CSE OOP LAB Manual
a) LINEAR SEARCH
DATE :
AIM:
To write a java Program to implement the linear search algorithm.
ALGORITHM:
1.Import the needed packages.
2.Declare the variables.
3.Get the number of elements in the array and store it in a variable n.
4.Get the elements of the array.
5.Get the value to be searched and store in the variable data.
6.Check the value in data with the elements in the array.
7. Once the data is found ,print the location of the data.
8. When the data is not found in the array, print the data is not in the array.
PROGRAM:
import java.io.*;
import java.util.Scanner;
public class LinearSearch
{
public static void main(String args[])
{
int i, n, data, array[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements:");
n = in.nextInt();
array = new int[n];
for (i = 0;i< n; i++)
{
System.out.println("Enter the element:"+(i+1));
array[i] = in.nextInt();
}
System.out.println("Enter value to find");
data = in.nextInt();
for (i = 0; i< n; i++)
{
if (array[i] == data)
{
System.out.println(data + " is present at location " + (i+ 1) );
break;
}
}
if (i== n)
System.out.println(data +" is not present in the array");
}
}
OUTPUT:
RESULT:
Thus the java program for linear search has been implemented successfully.
EX.NO:1.b) BINARY SEARCH
DATE:
AIM:
ALGORITHM:
7. If key>middle element, then the key lies in the right half of the array. Repeat steps 4,5 and 6
8. If key<middle element, then the key lies in the left half of the array. Repeat steps 4,5 and 6
Program:
import java.lang.*;
import java.util.*;
import java.io.*;
class Binary
public static void binarysearch(int arr[], int first, int last, int key)
int pos;
if ( arr[mid] <key )
first = mid + 1;
pos=mid+1;
break;
else
last = mid - 1;
int n;
n=sc.nextInt();
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
binarysearch(arr,0,last,key);
}}
OUTPUT:
RESULT :
DATE :
AIM:
ALGORITHM:
PROGRAM:
import java.util.*;
class SelectionSort
{
public static void main(String args[])
{
int size;
Scanner in=new Scanner(System.in);
System.out.println("Enter the no.of elements in the array");
size=in.nextInt();
int array[]=new int[size];
System.out.println("Enter the elements");
for(int j=0;j<size;j++)
{
array[j]=in.nextInt();
}
OUTPUT:
RESULT:
Thus the java program to implement selection sort was executed successfully.
EX.NO: 1.d) INSERTION SORT
DATE :
AIM:
To write a java program to implement the insertion sort in java.
ALGORITHM:
1 . If the element is the first element, assume that it is already sorted. Return 1.
3. Now, compare the key with all elements in the sorted array.
4. If the element in the sorted array is smaller than the current element, then move to the next
element. Else, shift greater elements in the array towards the right.
PROGRAM:
import java.util.*;
class InsertionSort
{
public static void sortInsertion(int[] arr)
{
for(int i=0;i<arr.length;++i)
{
int j = i;
}
}
}
for(int i=0;i<arr.length;++i)
{
System.out.print(arr[i] + " ");
}
}
}
OUTPUT:
RESULT:
Thus the java program for insertion sort has been implemented successfully.
EX.NO: 2.a) STACK
DATE :
AIM:
ALGORITHM:
PROGRAM:
import java.util.*;
class Operation
{
void push(Stack<Integer>stack,int a)
{
stack.push(a);
}
void pop(Stack<Integer> stack)
{
int ele=(Integer)stack.pop();
System.out.println("The popped element:"+ele);
}
void peek(Stack<Integer> stack)
{
int ele=(Integer)stack.peek();
System.out.println("The top element of the stack:"+ele);
}
void search(Stack<Integer>stack,int a)
{
int pos=(Integer)stack.search(a);
if(pos==0)
System.out.println("Element not found");
else
System.out.println("Element is found in the position"+pos);
}
class StackExample
{
public static void main(String[] args)
{
Stack<Integer> stack= new Stack<Integer>();
Scanner in=new Scanner(System.in);
Operation ob=new Operation();
int ch,data;
do
{
System.out.println("\n1.Push\n2.Pop\n3.Peek\n4.Search\n5.Exit\nEnter the choice:");
ch=in.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter the data");
data=in.nextInt();
ob.push(stack,data);
System.out.println("Elements in the stack:"+stack);
break;
case 2:
ob.pop(stack);
System.out.println("Elements in the stack:"+stack);
break;
case 3:
ob.peek(stack);
break;
case 4:
System.out.println("Enter the data to be searched");
data=in.nextInt();
ob.search(stack,data);
break;
case 5:
ch=0;
break;
default:
System.out.println("Enter the proper choice");
}
}while(ch>0);
}
}
OUTPUT:
RESULT:
Thus the java application to implement Stack using classes and objects was executed successfully.
EX.NO: 2.b) QUEUE
DATE :
AIM:
ALGORITHM:
PROGRAM:
import java.util.*;
class Operation
queue.add(a);
int ele=(Integer)queue.poll();
int ele=(Integer)queue.peek();
class QueueExample
{
int ch,data,value;
do
ch=in.nextInt();
switch(ch)
case 1:
data=in.nextInt();
ob.enqueue(queue,data);
break;
case 2:
ob.dequeue(queue);
break;
case 3:
ob.peek(queue);
break;
case 4:
ch=0;
break;
default:
}while(ch>0);
OUTPUT:
RESULT:
Thus the java application to implement Queue using classes and objects was executed successfully.
EX NO: 3 PAYSLIP GENERATION USING INHERITANCE
DATE:
AIM:
To develop a java application with Employee class with Emp_name, Emp_id, Address, Mail_id,
Mobile_no as members. Inherit the classes, Programmer, Assistant Professor, Associate Professor and
Professor from employee class. Add Basic Pay (BP) as the member of all the inherited classes with 97%
of BP as DA, 10 % of BP as HRA, 12% of BP as PF, 0.1% of BP for staff club fund. Generate pay slips
for the employees with their gross and net salary.
.
ALGORITHM:
PROGRAM:
import java.util.*;
class Employee
int empid;
long mobile;
void getdata()
name = sc.nextLine();
mailid = sc.nextLine();
empid = sc.nextInt();
mobile = sc.nextLong();
void display()
System.out.println("Employee id : "+empid);
System.out.println("Mail id : "+mailid);
System.out.println("Address: "+address);
double salary,bp,da,hra,pf,club,net,gross;
void getprogrammer()
bp = sc.nextDouble();
void calculateprog()
da=(0.97*bp);
hra=(0.10*bp);
pf=(0.12*bp);
club=(0.1*bp);
gross=(bp+da+hra);
net=(gross-pf-club);
System.out.println("********************************************");
System.out.println("********************************************");
double salary,bp,da,hra,pf,club,net,gross;
void getasst()
bp = sc.nextDouble();
void calculateasst()
da=(0.97*bp);
hra=(0.10*bp);
pf=(0.12*bp);
club=(0.1*bp);
gross=(bp+da+hra);
net=(gross-pf-club);
System.out.println("***********************************");
System.out.println("***********************************");
double salary,bp,da,hra,pf,club,net,gross;
void getassociate()
bp = sc.nextDouble();
void calculateassociate()
da=(0.97*bp);
hra=(0.10*bp);
pf=(0.12*bp);
club=(0.1*bp);
gross=(bp+da+hra);
net=(gross-pf-club);
System.out.println("***********************************");
double salary,bp,da,hra,pf,club,net,gross;
void getprofessor()
bp =sc.nextDouble();
void calculateprofessor()
da=(0.97*bp);
hra=(0.10*bp);
pf=(0.12*bp);
club=(0.1*bp);
gross=(bp+da+hra);
net=(gross-pf-club);
System.out.println("************************");
System.out.println("************************");
class Salary
int choice,cont;
do
System.out.println("PAYROLL");
choice=sc.nextInt();
switch(choice)
case 1:
p.getdata();
p.getprogrammer();
p.display();
p.calculateprog();
break;
case 2:
asst.getdata();
asst.getasst();
asst.display();
asst.calculateasst();
break;
case 3:
asso.getdata();
asso.getassociate();
asso.display();
asso.calculateassociate();
break;
case 4:
prof.getdata();
prof.getprofessor();
prof.display();
prof.calculateprofessor();
break;
default:
cont=sc.nextInt();
}while(cont==1);
OUTPUT:
RESULT:
Thus the Java application to generate pay slip for different category of employees was
implemented using inheritance and the program was executed successfully.
EX.NO:4 ABSTRACT CLASS
DATE :
AIM:
To write a Java Program to create an abstract class named Shape that contains two
integers and an empty method named printArea(). Provide three classes named Rectangle,
Triangle and Circle such that each one of the classes extends the class Shape. Each one of the
classes contains only the method printArea() that prints the area of the given shape.
ALGORITHM:
1. Start the program.
2. Create an abstract class as Shape it contains two integers and one abstact method as
printArea().
3. Create the classes Rectangle,Triangle and Circle which extends from the class Shape.
4. Calculate the area of the given shape in the method printArea().
5. Stop the program.
PROGRAM:
import java.util.*;
public int x, y;
{
System.out.println("Area of Triangle is " + (x * y) / 2);
class AbstractExample
System.out.println("Rectangle");
r.x = sc.nextInt();
r.y = sc.nextInt();
r.printArea();
System.out.println("Triangle");
t.x = sc.nextInt();
t.y = sc.nextInt();
t.printArea();
System.out.println("Circle");
c.x = sc.nextInt();
c.printArea();
OUTPUT:
RESULT:
Thus the program for calculating the area of the given shape using abstract class was
executed successfully.
EX.NO : 5 INTERFACES
DATE :
AIM:
To write a Java Program to create interfaces named Data and Method that contains two
integers and an empty method named print Area(). Provide three classes named Rectangle,
Triangle and Circle such that each one of the classes implements the interfaces Data and
Method . Each one of the classes contains only the method print Area () that prints the area of
the given shape.
ALGORITHM:
1. Start the program.
2. Create an interface as Data with 2 integers and another interface as Method that contains
themethod printArea().
3. Create a classes as Rectangle,Triangle and Circle which implements the interfaces Data
and Method.
4. Calculate the area of the given shapes in the method printArea().
5. Stop the program.
PROGRAM:
interface Data
interface Method
{
void printArea()
class MainShape
r.printArea();
t.printArea();
c.printArea();
}
OUTPUT:
RESULT:
Thus the java program to implement the interface for calculating the area of the given shape
was executed successfully.
EX. NO:6 IMPLEMENT USER DEFINED EXCEPTION HANDLING
DATE :
AIM:
ALGORITHM:
4.Else find the square root of the given number and display it.
PROGRAM:
import java.util.*;
import java.io.*;
MyException(String str)
System.out.println(str);
class ExceptionExample
int no;
try
no=sc.nextInt();
if(no<0)
else
catch(Exception e)
}
OUTPUT:
RESULT:
Thus the program for user defined exception handling was executed successfully.
EX. NO:7 MULTI THREADING
DATE :
AIM:
To write a java program that implements a multi-threaded application that has three
threads. First thread generates a random integer every 1 second and if the value is even,
second thread computes the square of the number and prints. If the value is odd, the third
thread will print the value of cube of the number.
ALGORITHM:
PROGRAM:
import java.util.*;
import java.lang.*;
public int x;
public EvenThread(int x)
this.x = x;
System.out.println("New Thread "+ x +" is EVEN \n Square of " + x + " is: " + x *x);
public int x;
public OddThread(int x)
this.x = x;
System.out.println("New Thread "+ x +" is ODD\n Cube of " + x + " is: " + x * x *x);
int num = 0;
try
num = r.nextInt(100);
if (num % 2 == 0)
t1.start();
else
{
Thread t2 = new Thread(new OddThread(num));
t2.start();
Thread.sleep(1000);
System.out.println(" ");
catch (Exception e)
System.out.println(e.getMessage());
rt.start();
}
OUTPUT:
RESULT:
AIM :
ALGORITHM:
PROGRAM:
import java.io.*;
import java.util.*;
class FileOperation
String str=sc.nextLine();
String filename=f.getPath();
String path=f.getAbsolutePath();
dis.read(data);
for(int i=0;i<count;i++)
char k=(char)data[i];
out.write(data[i]);
System.out.print(k+””);
OUTPUT:
RESULT:
Thus the java program to implement the different file operations was executed
successfully.
EX. NO:9 GENERIC METHOD
DATE:
AIM:
To write a java program for finding the maximum value from the given integer and
float array using a generic method.
ALGORITHM:
PROGRAM:
import java.lang.*;
import java.util.*;
class Maximum
E max = list[0];
if (list[i].compareTo(max)>0)
max = list[i];
return max;
class GenericExample
{
public static void main (String args[])
String str=arr1.toString();
for(int i=0;i<10;i++)
System.out.print(arr[i]+" ");
System.out.println("\nMaximum: "+Maximum.max(arr1));
for(int j=0;j<10;j++)
System.out.print(arr2[j]+" ");
System.out.println("\n\nMaximum: "+Maximum.max(arr2));
OUTPUT:
RESULT:
Thus the java program for finding the maximum value from the given integer and float
array using a generic method was implemented successfully.
EX.NO 10.a) JAVA FX CONTROLS
DATE :
AIM:
To write a Java program for javafx controls.
ALGORITHM:
1. Extend javafx.application.Application and override start ().
2.Create a button.
3. Create a layout and add button to it.
4. Create a scene.
5. Prepare the stage.
6. Create an event for the button.
7. Create the main method.
PROGRAM:
package application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import javafx.scene.layout.StackPane;
public class Hello_World extends Application
{
public void start(Stage primaryStage) throws Exception
{
// TODO Auto-generated method stub
Button btn1=new Button("Say, Hello World");
btn1.setOnAction(new EventHandler<ActionEvent>()
{
public void handle(ActionEvent arg0) {
// TODO Auto-generated method stub
System.out.println("hello world");
}
});
StackPane root=new StackPane();
root.getChildren().add(btn1);
Scene scene=new Scene(root,600,400);
primaryStage.setTitle("First JavaFX Application");
primaryStage.setScene(scene);
primaryStage.show();
}
publicstaticvoid main (String[] args)
{
launch(args);
}
}
OUTPUT:
RESULT:
DATE :
AIM:
To write java program to create a layout.
ALGORITHM:
1.Start the program.
2. Call the constructors.
3. To arrange the components in Five Regions: North, South, East, West,
Center.
4.Each region may contain one Component only.
5. Display the layout.
6. Stop the program.
PROGRAM:
import java.awt.*;
import javax.swing.*;
public class Border
{
JFrame f;
Border()
{
f = new JFrame();
JButton b1 = new JButton("NORTH");;
JButton b2 = new JButton("SOUTH");;
JButton b3 = new JButton("EAST");;
JButton b4 = new JButton("WEST");;
JButton b5 = new JButton("CENTER");;
f.add(b1, BorderLayout.NORTH);
f.add(b2, BorderLayout.SOUTH);
f.add(b3, BorderLayout.EAST);
f.add(b4, BorderLayout.WEST);
f.add(b5, BorderLayout.CENTER);
f.setSize(300, 300);
f.setVisible(true);
}
public static void main(String[] args)
{
new Border();
}
}
OUTPUT:
RESULT:
Thus the program for border layout was executed successfully.
EX.NO: 10.c) MENUS
DATE :
AIM:
To write a java program to create a menu and display.
ALGORITHM:
1. Create a menu using JMenu class.
2. To create menu items use the JMenuItem class.
3. Create a menu bar using JMenuBar and add the menu to it.
4. Set the menu to the frame using setJMenuBar function.
5. Display the label of the menu item selected.
PROGRAM:
import javax.swing.*;
class MenuExample
{
JMenu menu, submenu;
JMenuItem i1, i2, i3, i4, i5;
MenuExample()
{
JFrame f= new JFrame("Menu and MenuIte m Example");
JMenuBar mb=new JMenuBar();
menu=new JMenu("Menu");
submenu=new JMenu("Sub Menu");
i1=new JMenuItem("Item 1");
i2=new JMenuItem("Item 2");
i3=new JMenuItem("Item 3");
i4=new JMenuItem("Item 4");
i5=new JMenuItem("Item 5");
menu.add(i1); menu.add(i2); menu.add(i3);
submenu.add(i4); submenu.add(i5);
menu.add(submenu);
mb.add(menu);
f.setJMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new MenuExample();
}}
OUTPUT:
RESULT:
Thus the program for menus was executed successfully.