Java OOP Laboratory Practices
Java OOP Laboratory Practices
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<[Link];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 [Link].*;
class Selectionsort
{
void sort(int arr[])
{
int n=[Link];
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 = [Link];
for (int i=0; i<n; ++i)
[Link](arr[i]+" ");
[Link]();
}
public static void main(String args[])
{
SelectionSort ob = new SelectionSort();
int arr[] = {64,25,12,22,11};
[Link](arr);
[Link]("Sorted array");
[Link](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 = [Link];
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())
{
[Link]("Overflow\nProgram Terminated\
n"); [Link](-1);
}
[Link]("Inserting " + x);
arr[++top] = x;
}
public int pop()
{
if (isEmpty())
{
[Link]("Underflow\nProgram
Terminated"); [Link](-1);
}
[Link]("Removing " + peek());
return arr[top--];
}
public int peek()
{
if (!isEmpty())
{
return arr[top];
}
else {
[Link](-1);
}
return -1;
}
public int size()
{
return top + 1;
}
class Main
{
public static void main (String[] args)
{
Stack stack = new Stack(3);
[Link](1); // inserting 1 in the stack
[Link](2); // inserting 2 in the stack
[Link](); // removing the top element
(2) [Link](); // removing the top element (1)
[Link](3); // inserting 3 in the stack
[Link]("The top element is " +
[Link]()); [Link]("The stack size is " +
[Link]()); [Link](); // removing the top
element (3)
if ([Link]())
{
[Link]("The stack is empty");
}
else
{
[Link]("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())
{
[Link]("Queue is empty");
return (-1);
}
else
{
element = items[front];
if (front >= rear)
{
front = -1;
rear = -1;
}
else
{
front++;
}
[Link]("Deleted -> " + element);
return (element);
}
}
void display()
{
int i;
if (isEmpty())
{
[Link]("Empty Queue");
}
else
{
[Link]("\nFront index-> " + front);
[Link]("Items -> ");
for (i = front; i <= rear; i++)
[Link](items[i] + " ");
[Link]("\nRear index-> " + rear);
}
}
public static void main(String[] args)
{
Queue q = new Queue();
[Link]();
[Link](1);
[Link](2);
[Link](3);
[Link](4);
[Link](5);
[Link](6);
[Link]();
[Link]();
[Link]();
}
}
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:
[Link]
import [Link];
class Employee
int empid;
long mobile;
void getdata()
name = [Link]();
mailid = [Link]();
address = [Link]();
empid = [Link]();
mobile = [Link]();
void display()
[Link]("Employee id : "+empid);
[Link]("Mail id : "+mailid);
[Link]("Address: "+address);
[Link]("Mobile Number: "+mobile);
double salary,bp,da,hra,pf,club,net,gross;
void getprogrammer()
bp = [Link]();
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);
[Link]("********************************************");
}
}
double salary,bp,da,hra,pf,club,net,gross;
void getasst()
bp = [Link]();
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);
[Link]("***********************************");
[Link]("***********************************");
}
class Associateprofessor extends Employee
double salary,bp,da,hra,pf,club,net,gross;
void getassociate()
bp = [Link]();
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);
[Link]("***********************************");
[Link]("***********************************");
}
class Professor extends Employee
double salary,bp,da,hra,pf,club,net,gross;
void getprofessor()
bp = [Link]();
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);
[Link]("************************");
}
class Salary
int choice,cont;
do
[Link]("PAYROLL");
choice=[Link]();
switch(choice)
case 1:
[Link]();
[Link]();
[Link]();
[Link]();
break;
case 2:
[Link]();
[Link]();
[Link]();
[Link]();
break;
case 3:
[Link]();
[Link]();
[Link]();
[Link]();
break;
case 4:
professor(); [Link]();
[Link]();
[Link]();
[Link]();
break;
cont=[Link]();
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();
[Link]();
triangle t=new triangle();
[Link]();
circle r1=new circle();
[Link]();
}
}
OUTPUT:
RESULT:
Thus the java program for abstract class has been implemented and executed
successfully.
5. INTERFACE CONCEPTS
AIM:
ALGORITHMS:
import [Link].*;
interface myinterface
{
public void printarea();
}
abstract class shapes
{
double a,b;
abstract void printarea();
}
class rectangle extends shapes implements myinterface
{
public void printarea()
{
[Link](“calculating area of rectangle”);
Scanner input=new Scanner([Link]);
[Link](“enter length:”);
a=[Link]();
[Link](“enter breadth:”);
b=[Link]();
double area=a*b;
[Link](“ area od rectangle:”+area);
}
}
class triangle extends shapes implements myinterface
{
[Link](“calculating area of triangle”);
Scanner input=new Scanner([Link]);
[Link](“enter height:”);
a=[Link]();
[Link](“enter breadth:”);
b=[Link]();
double area=0.5*a*b;
[Link](“ area od triangle:”+area);
}
}
class circle extends shapes implements myinterface
{
[Link](“calculating area of circle”);
Scanner input=new Scanner([Link]);
[Link](“enter radius:”);
a=[Link]();
double area=3.14*a*a;
[Link](“ area od
circle:”+area);
}
}
class abstractclassdemo
{
public static void main(String args[])
{
shapes obj;
obj=new rectangle();
[Link]();
obj=new triangle();
[Link]();
obj=newcircle();
[Link]();
}
}
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 [Link].*;
class GFG
{
public static void main (String[] args)
{
int a=5;
int b=0;
try
{
[Link](a/b);
}
catch(ArithmeticException e)
{
[Link]();
}
}
}
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
{
[Link]("Starting of try block");
catch(MyException exp)
[Link]("Catch Block") ;
[Link](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 [Link].*;
class even implements Runnable
{
public int x;
public even(int x)
{
this.x = x;
}
public void run()
{
[Link]("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()
{
[Link]("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 = [Link](100);
[Link]("Main Thread and Generated Number is " + num);
if (num % 2 == 0)
{
Thread t1 = new Thread(new even(num));
[Link]();
}
else
{
Thread t2 = new Thread(new odd(num));
[Link]();
}
[Link](1000);
[Link](" ");
}
}
catch (Exception ex)
{
[Link]([Link]());
}
}
}
{
public static void main(String[] args)
{
A a = new A();
[Link]();
}
}
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 [Link].*;
import [Link].*;
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 = [Link]()) != -1)
{
[Link](n);
}
}
finally
{
if (in != null)
{
[Link]();
}
if (out != null)
{
[Link]();
}
}
[Link]("File Copied");
}
public static void main(String[] args) throws Exception
{
Scanner sc = new Scanner([Link]);
[Link]( "Enter the source filename from where you have to read/copy :");
String a = [Link]();
File x = new File(a);
[Link]( "Enter the destination filename where you have to write/paste :");
String b = [Link]();
File y = new File(b);
copyContent(x, y);
}
}
OUTPUT:
[Link]
[Link]
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 [Link];
import [Link];
public class CountWordFile
{
public static void main(String[] args) throws Exception
{
String line;
int count = 0;
FileReader file = new FileReader("[Link] ");
BufferedReader br = new BufferedReader(file);
while((line = [Link]()) != null)
{
String words[] = [Link]("");
count = count + [Link];
}
[Link]("Number of words present in given file: " + count);
[Link]();
}
}
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 [Link].*;
obj=new arraylist<T>(size);
[Link](item);
public T pop()
if([Link]())
return null;
return [Link]([Link]()-1);
}
PROGRAM 2:
import [Link].*;
import [Link].*
int[] ia={1,2,3,4,5};
char[] ca={‘A’,’B’,’C’,’D’,’E’};
for(int i=0;i<5;i++)
[Link](ia[i]);
for(int i=0;i<5;i++)
[Link](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 [Link];
import [Link];
import [Link];
[Link];
import [Link];
import [Link];
import [Link];
import [Link]..PasswordField;
import [Link]..TextField;
import [Link];
import [Link];
@override
[Link](10);
[Link](0,L1,ft1);
[Link](0,L2,ft2);
[Link](2, btn);
[Link](s);
[Link]();
}
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 [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@override
[Link]().addAll(B1,B2,B3,B4,B5);
[Link](s);
[Link](“Hbox demo”);
[Link]();
}
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 [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@override
[Link]().addAll(newitem,openfileitem,saveitem,exititem);
[Link]().addAll(cutitem,copyitem,pasteitem);
[Link]().addAll(fileMenu,editMenu,aboutMenu);
[Link](menuBar);
[Link](s);
[Link]();
}
public staic void main(String[] args)
[Link](args);
}
OUTPUT:
RESULT:
Thus the javaFX application for creating a Menu has been implemented and executed
successfully.