Oop Lab
Oop Lab
Mudaiyur-606902
DEPARTMENT OF _______________________________
Name ………………………………………………………
BONAFIDE CERTIFICATE
5 INTERFACE CONCEPTS
7 MULTITHREADING IMPLEMENTATION
8(a) FILE OPERATION
8(b) COUNT THE WORDS IN A FILE
9 GENERIC CLASSES
10(a) JavaFX Control
10(b) LAYOUTS
10(c) MENUS
1(A) LINEAR SEARCH
AIM:
ALGORITHMS:
class LinearSearch
int a[]={1,5,-2,8,7,11,40,32};
int i;
for(i=0;i<a.length;i++)
if(a[i] = = key)
return i+1;
return -1;
}
Output:
RESULT:
Thus the java program for linear search program has been implemented and executed
successfully.
1(b) BINARY SEARCH
AIM:
ALGORITHMS:
class BinarySearch
int[] a= {10,20,30,40,50,60};
int key=40;
Find(a,0,5,key);
public static void Find(int[] a, int low, int high, int key)
int mid;
if(low>high)
return;
mid=(low+high)/2;
if(key= = a[mid])
else if(key<a[mid])
Find(a,low,mid-1,key);
else if(key>a[mid])
Find (a,mid+1,high,key);
}
OUTPUT:
RESULT:
Thus the java program for Binary search program has been implemented and executed
successfully.
1(c) SELECTION SORT
AIM:
To write a java program for Selection sort using quadratic sorting algorithms
ALGORITHMS:
import java.util.*;
class Selectionsort
{
void sort(int arr[])
{
int n=arr.length;
for (int i = 0; i < n-1; i++)
{
int = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
public static void main(String args[])
{
SelectionSort ob = new SelectionSort();
int arr[] = {64,25,12,22,11};
ob.sort(arr);
System.out.println("Sorted array");
ob.printArray(arr);
}
}
OUTPUT:
RESULT:
Thus the java program for selection sort program using quadratic sorting algorithms has been
implemented and executed successfully.
1(D) INSERTION SORT
AIM:
To write a java program for Selection sort using quadratic sorting algorithms.
ALGORITHMS:
class InsertionSort
{
void sort(int arr[])
{
int n = arr.length;
for (int i = 1; i < n; ++i)
{
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] =
arr[j]; j = j - 1;
}
arr[j + 1] = key;
}
}
5 6 11 12 13
RESULT:
Thus the java program for insertion sort program using quadratic sorting algorithms has been
implemented and executed successfully.
.
2(A) STACK OPERATIONS
AIM:
To develop a java program for stack data structure using class and object
ALGORITHMS:
class Stack
{
private int arr[];
private int top;
private int capacity;
Stack(int size)
{
arr = new
int[size]; capacity
= size; top = -1;
}
public void push(int x)
{
if (isFull())
{
System.out.println("Overflow\nProgram Terminated\
n"); System.exit(-1);
}
System.out.println("Inserting " + x);
arr[++top] = x;
}
public int pop()
{
if (isEmpty())
{
System.out.println("Underflow\nProgram
Terminated"); System.exit(-1);
}
System.out.println("Removing " + peek());
return arr[top--];
}
public int peek()
{
if (!isEmpty())
{
return arr[top];
}
else {
System.exit(-1);
}
return -1;
}
public int size()
{
return top + 1;
}
class Main
{
public static void main (String[] args)
{
Stack stack = new Stack(3);
stack.push(1); // inserting 1 in the stack
stack.push(2); // inserting 2 in the stack
stack.pop(); // removing the top element
(2) stack.pop(); // removing the top element (1)
stack.push(3); // inserting 3 in the stack
System.out.println("The top element is " +
stack.peek()); System.out.println("The stack size is " +
stack.size()); stack.pop(); // removing the top
element (3)
if (stack.isEmpty())
{
System.out.println("The stack is empty");
}
else
{
System.out.println("The stack is not empty");
}
}
}
OUTPUT:
Inserting 1
Inserting 2
Removing 2
Removing 1
Inserting 3
The top element is 3
The stack size is 1
Removing 3
The stack is empty
RESULT:
Thus the java program for stack data structure using class and object has been implemented and
executed successfully.
2(B) QUEUE OPERATIONS
AIM:
To develop a java program for Queue data structure using class and object
ALGORITHMS:
Enqueue Operation:
Dequeue Operation:
int deQueue()
{
int element;
if (isEmpty())
{
System.out.println("Queue is empty");
return (-1);
}
else
{
element = items[front];
if (front >= rear)
{
front = -1;
rear = -1;
}
else
{
front++;
}
System.out.println("Deleted -> " + element);
return (element);
}
}
void display()
{
int i;
if (isEmpty())
{
System.out.println("Empty Queue");
}
else
{
System.out.println("\nFront index-> " + front);
System.out.println("Items -> ");
for (i = front; i <= rear; i++)
System.out.print(items[i] + " ");
System.out.println("\nRear index-> " + rear);
}
}
public static void main(String[] args)
{
Queue q = new Queue();
q.deQueue();
q.enQueue(1);
q.enQueue(2);
q.enQueue(3);
q.enQueue(4);
q.enQueue(5);
q.enQueue(6);
q.display();
q.deQueue();
q.display();
}
}
OUTPUT:
2 3 4 5 6
RESULT:
Thus the java program for queue data structure using class and object has been implemented and
executed successfully.
3. PAYSLIP GENERATION USING INHERITANCE
AIM:
To develop a java application to generate pay slip for different category of employee using the
concepts of inheritance.
ALGORITHMS:
Salary.java
import java.util.Scanner;
class Employee
int empid;
long mobile;
void getdata()
name = get.nextLine();
mailid = get.nextLine();
address = get.nextLine();
empid = get.nextInt();
mobile = get.nextLong();
void display()
System.out.println("Employee id : "+empid);
System.out.println("Mail id : "+mailid);
System.out.println("Address: "+address);
System.out.println("Mobile Number: "+mobile);
double salary,bp,da,hra,pf,club,net,gross;
void getprogrammer()
bp = get.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("********************************************");
}
}
double salary,bp,da,hra,pf,club,net,gross;
void getasst()
bp = get.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("***********************************");
}
class Associateprofessor extends Employee
double salary,bp,da,hra,pf,club,net,gross;
void getassociate()
bp = get.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("***********************************");
System.out.println("***********************************");
}
class Professor extends Employee
double salary,bp,da,hra,pf,club,net,gross;
void getprofessor()
bp = get.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("************************");
}
class Salary
int choice,cont;
do
System.out.println("PAYROLL");
choice=c.nextInt();
switch(choice)
case 1:
p.getdata();
p.getprogrammer();
p.display();
p.calculateprog();
break;
case 2:
asst.getdata();
asst.getasst();
asst.display();
asst.claculateasst();
break;
case 3:
asso.getdata();
asso.getassociate();
asso.display();
asso.claculateassociate();
break;
case 4:
professor(); prof.getdata();
prof.getassociate();
prof.display();
prof.claculateassociate();
break;
cont=s.nextInt();
while(con= = 1);
}
OUTPUT:
RESULT:
Thus the java application to generate pay slip for different category of employee has been
implemented using inheritance and the program was executed successfully.
4. ABSTRACT CLASS IMPLEMENTATION
AIM:
ALGORITHMS:
{
public static void main(String[] args)
{
rectangle r=new rectangle();
r.printarea();
triangle t=new triangle();
t.printarea();
circle r1=new circle();
r1.printarea();
}
}
OUTPUT:
RESULT:
Thus the java program for abstract class has been implemented and executed
successfully.
5. INTERFACE CONCEPTS
AIM:
ALGORITHMS:
import java.util.*;
interface myinterface
{
public void printarea();
}
abstract class shapes
{
double a,b;
abstract void printarea();
}
class rectangle extends shapes implements myinterface
{
public void printarea()
{
System.out.println(“calculating area of rectangle”);
Scanner input=new Scanner(System.in);
System.out.println(“enter length:”);
a=input.nextDouble();
System.out.println(“enter breadth:”);
b=input.nextDouble();
double area=a*b;
System.out.println(“ area od rectangle:”+area);
}
}
class triangle extends shapes implements myinterface
{
System.out.println(“calculating area of triangle”);
Scanner input=new Scanner(System.in);
System.out.println(“enter height:”);
a=input.nextDouble();
System.out.println(“enter breadth:”);
b=input.nextDouble();
double area=0.5*a*b;
System.out.println(“ area od triangle:”+area);
}
}
class circle extends shapes implements myinterface
{
System.out.println(“calculating area of circle”);
Scanner input=new Scanner(System.in);
System.out.println(“enter radius:”);
a=input.nextDouble();
double area=3.14*a*a;
System.out.println(“ area od
circle:”+area);
}
}
class abstractclassdemo
{
public static void main(String args[])
{
shapes obj;
obj=new rectangle();
obj.printarea();
obj=new triangle();
obj.printarea();
obj=newcircle();
obj.printarea();
}
}
OUTPUT:
Enter length: 10
Enter breadth: 20
Enter height: 10
Enter breadth: 5
Enter radius: 10
RESULT:
Thus the java program for interface concept has been implemented and executed
successfully.
6(a) EXCEPTION HANDLING
AIM:
ALGORITHMS:
import java.io.*;
class GFG
{
public static void main (String[] args)
{
int a=5;
int b=0;
try
{
System.out.println(a/b);
}
catch(ArithmeticException e)
{
e.printStackTrace();
}
}
}
OUTPUT:
RESULT:
Thus the java program for exception handling has been implemented and executed
successfully.
6(b) USER DEFINED EXCEPTION
AIM:
ALGORITHMS:
String str1;
MyException(String str2)
str1=str2;
class example
try
{
System.out.println("Starting of try block");
catch(MyException exp)
System.out.println("Catch Block") ;
System.out.println(exp) ;
}
OUTPUT:
Catch Block
RESULT:
Thus the java program for user defined exception handling has been implemented and
executed successfully.
7. MULTITHREADING IMPLEMENTATION
AIM:
ALGORITHMS:
import java.util.*;
class even implements Runnable
{
public int x;
public even(int x)
{
this.x = x;
}
public void run()
{
System.out.println("New Thread "+ x +" is EVEN and Square of " + x + " is: " + x * x);
}
}
class odd implements Runnable
{
public int x;
public odd(int
x)
{
this.x = x;
}
public void run()
{
System.out.println("New Thread "+ x +" is ODD and Cube of " + x + " is: " + x * x * x);
}
}
class A extends Thread
{
public void run()
{
int num = 0;
Random r = new Random();
try
{
for (int i = 0; i < 5; i++)
{
num = r.nextInt(100);
System.out.println("Main Thread and Generated Number is " + num);
if (num % 2 == 0)
{
Thread t1 = new Thread(new even(num));
t1.start();
}
else
{
Thread t2 = new Thread(new odd(num));
t2.start();
}
Thread.sleep(1000);
System.out.println(" ");
}
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
{
public static void main(String[] args)
{
A a = new A();
a.start();
}
}
OUTPUT :
RESULT:
Thus the java program for multithreaded has been implemented and executed
successfully.
8(A) FILE OPERATION
AIM:
To develop a java program that copies the content of one file to another file by removing
unnecessary spaces between words.
ALGORITHMS:
import java.io.*;
import java.util.*;
public class CopyFromFileaToFileb
{
public static void copyContent(File a, File b) throws Exception
{
FileInputStream in = new FileInputStream(a);
FileOutputStream out = new FileOutputStream(b);
try
{
int n;
while ((n = in.read()) != -1)
{
out.write(n);
}
}
finally
{
if (in != null)
{
in.close();
}
if (out != null)
{
out.close();
}
}
System.out.println("File Copied");
}
public static void main(String[] args) throws Exception
{
Scanner sc = new Scanner(System.in);
System.out.println( "Enter the source filename from where you have to read/copy :");
String a = sc.nextLine();
File x = new File(a);
System.out.println( "Enter the destination filename where you have to write/paste :");
String b = sc.nextLine();
File y = new File(b);
copyContent(x, y);
}
}
OUTPUT:
sourcefile.txt
destinationfile.txt
File Copied
RESULT:
Thus the java program copies the content of one file to another file has been implemented and
executed successfully.
8(B) COUNT THE WORDS IN A FILE
AIM:
To develop a java program for count the words in a file using File Operations.
ALGORITHMS:
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;
}
System.out.println("Number of words present in given file: " + count);
br.close();
}
}
OUTPUT:
RESULT:
Thus the java program to count the words in a file has been implemented and executed
successfully.
9 GENERIC CLASSES
AIM:
ALGORITHMS:
mport java.util.*;
obj=new arraylist<T>(size);
obj.add(item);
public T pop()
if(obj.isEmpty())
return null;
return obj.remove(obj.size()-1);
}
PROGRAM 2:
import java.io.*;
import java.util.*
int[] ia={1,2,3,4,5};
char[] ca={‘A’,’B’,’C’,’D’,’E’};
for(int i=0;i<5;i++)
ist.push(ia[i]);
for(int i=0;i<5;i++)
ist.push(ca[i]);
for(int i=0;i<2;i++)
for(int i=0;i<5;i++)
}
OUTPUT:
Stack is empty
Null
RESULT:
Thus the java program to generic class has been implemented and executed successfully.
10.(a) JavaFX Control
AIM:
ALGORITHMS:
package myjavafxapplication;
import java.io.FileInputString;
import javafx.application.Application;
import javafx.event.ActionEvent;
importjavafx.event.EventHandler;
import javafx.scene.Scene;
import javxfx.scene.control.Button;
import javxfx.scene.control.Label;
import javxfx.scene.control..PasswordField;
import javxfx.scene.control..TextField;
import javxfx.scene.control.gridPane;
import javafx.stage.Stage;
@override
root.setVgap(10);
root.addRow(0,L1,ft1);
root.addRow(0,L2,ft2);
root.addRow(2, btn);
primaryStage.setScene(s);
primaryStage.show();
}
public staic void main(String[] args)
launch(args);
}
OUTPUT:
RESULT:
Thus the javaFX application for creating a login form has been implemented and executed
successfully.
10.(b) LAYOUTS
AIM:
ALGORITHMS:
package myjavafxapplication;
import javafx.application.Application;
import javafx.scene.Scene;
import javxfx.scene.control.Button;
import javxfx.scene.layout.Hbox;
import javafx.stage.Stage;
@override
root.getChildren().addAll(B1,B2,B3,B4,B5);
primaryStage.setScene(s);
primaryStage.setTitle(“Hbox demo”);
primaryStage.show();
}
public staic void main(String[] args)
launch(args);
}
OUTPUT:
RESULT:
Thus the javaFX application for creating a HBox layout has been implemented and executed
successfully.
10.(b) MENUS
AIM:
ALGORITHMS:
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javxfx.scene.control.Menu;
import javxfx.scene.control.MenuBar;
import javxfx.scene.control.MenuItem;
import javxfx.scene.layout.BorderPane;
import javafx.stage.Stage;
@override
fileMenu.getItems().addAll(newitem,openfileitem,saveitem,exititem);
editMenu.getItems().addAll(cutitem,copyitem,pasteitem);
menuBar.getMenus().addAll(fileMenu,editMenu,aboutMenu);
r.setTop(menuBar);
stage.setScene(s);
stage.show();
}
public staic void main(String[] args)
Application.launch(args);
}
OUTPUT:
RESULT:
Thus the javaFX application for creating a Menu has been implemented and executed
successfully.