OOP Lab Record 1-10
OOP Lab Record 1-10
AIM:
ALGORITHM:
Step 1: Start from the leftmost element of array[] and one by one compare element with each
element of array[]
Step 3: If element doesn’t match with any of the elements, return -1.
PROGRAM:
import java.util.Scanner;
class Array
intn,element,pos=-1;
n = input.nextInt();
for(int i=0;i<n;i++)
{
marks[i] = input.nextInt();
element = input.nextInt();
//Linear Search
if(marks[i] == element)
pos = i;
break;
if(pos==-1)
else
{
System.out.println("Element is at " + pos);
RESULT:
The above program to implement sequential search is executed and verified successfully.
SAMPLE INPUT
Exp. No: 1b BINARY SEARCH USING JAVA
AIM:
ALGORITHM:
Step 1: Begin with the mid element of the whole array as a search element.
Step 2: If the value of the search element is equal to the item, then return an index of the search
element.
Step 3: Or if the value of the search element is less than the item in the middle of the interval,
narrow the interval to the lower half.
Step 5: Repeatedly check from the second point until the value is found or the interval is empty.
PROGRAM:
import java.util.Scanner;
class Array
intn,element,pos=-1;
n = input.nextInt();
for(int i=0;i<n;i++)
{
marks[i] = input.nextInt();
element = input.nextInt();
//Binary Search
int first = 0;
while(first<=last)
if(marks[mid] == element)
pos = mid;
break;
else if(marks[mid]>element)
last = mid-1;
else
{
first = mid + 1;
if(pos==-1)
else
RESULT:
The above program to implement binary search is executed and verified successfully.
SAMPLE INPUT
SAMPLE OUTPUT
Element is at 2
Exp. No: 1c SELECTION SORT USING JAVA
AIM:
ALGORITHM:
(minIndex will hold the index of the smallest number in the unsorted subarray)
Step 2: Search for the smallest element in the unsorted subarray and update minIndex
Step 3: Swap the element at the position minIndex with the first element of the unsorted subarray.
Step 4: Again set minIndex to the first position of the unsorted subarray
PROGRAM:
a[i] = a[j];
a[j] = temp;
// Iterate from 0 to n - 1
intminIndex = i;
// Iterating from i + 1 to n
minIndex = j;
swap(a, i, minIndex);
System.out.println();
selectionSort(a, a.length);
System.out.println();
}
RESULT:
SAMPLE OUTPUT
3,54,2,6,4,23,43
2,3,4,6,23,43,54
Exp. No: 1d INSERTION SORT USING JAVA
AIM:
ALGORITHM:
Step2:Takethesecondelementandstoreit separately
Step 5:If the first element is greater than key , then key is placed in front of the first element
PROGRAM:
import java.util.Scanner;
class Array
intn,element,pos=-1;
n = input.nextInt();
for(int i=0;i<n;i++)
marks[i] = input.nextInt();
}
for(int i = 1; i<n;i++)
intind = i - 1;
marks[ind+1] = marks[ind];
marks[ind+1] = key;
for(int i = 0; i<n;i++)
System.out.println(marks[i]);
}
RESULT:
SAMPLE OUTPUT
3,54,2,6,4,23,43
2,3,4,6,23,43,54
Exp. No: 2a IMPLEMENTATION OF STACK USING CLASS AND OBJECTS
AIM:
Write a program to implement Stack data structure using class and Objects in Java
ALGORITHM:
Step 2: Implement the methods push(), pop(), isEmpty(), isFull(), printStack() for stack ADT
operations.
Step 3: write main() method and create object for stack class
class Stack {
private intarr[];
Stack(int size) {
capacity = size;
top = -1;
if (isFull()) {
System.out.println("Stack OverFlow");
System.exit(1);
arr[++top] = x;
if (isEmpty()) {
System.out.println("STACK EMPTY");
System.exit(1);
return arr[top--];
public intgetSize() {
return top + 1;
stack.push(1);
stack.push(2);
stack.push(3);
System.out.print("Stack: ");
stack.printStack();
stack.pop();
stack.printStack();
}
RESULT:
SAMPLE OUTPUT
Inserting 1
Inserting 2
Inserting 3
Stack: 1, 2, 3,
1, 2,
Exp. No: 2b IMPLEMENTATION OF QUEUE USING CLASS AND OBJECTS
AIM:
Write a program to implement Queue data structure using class and Objects in Java
ALGORITHM:
Step 2: Implement the methods enQueue(), deQueue(), isEmpty(), isFull(), display() for stack
ADT operations.
Step 3: write main() method and create object for stack class
PROGRAM:
int SIZE = 5;
Queue() {
front = -1;
rear = -1;
}
// check if the queue is full
booleanisFull() {
return true;
return false;
booleanisEmpty() {
if (front == -1)
return true;
else
return false;
if (isFull()) {
System.out.println("Queue is full");
else {
if (front == -1) {
front = 0;
rear++;
items[rear] = element;
intdeQueue() {
int element;
if (isEmpty()) {
System.out.println("Queue is empty");
return (-1);
else {
element = items[front];
front = -1;
rear = -1;
else {
front++;
return (element);
void display() {
int i;
if (isEmpty()) {
System.out.println("Empty Queue");
else {
q.deQueue();
q.enQueue(i);
q.enQueue(6);
q.display();
// deQueue removes element entered first i.e. 1
q.deQueue();
q.display();
}
RESULT:
SAMPLE OUTPUT
Queue is empty
Insert 1
Insert 2
Insert 3
Insert 4
Insert 5
Queue is full
Front index->0
Items->
12345
Rear index->4
1 Deleted
Front index->1
Items ->
2345
Rear index->4
Ex. No: 3 INHERITANCE
AIM:
ALGORITHM:
Step 1: Start
Step 2: Create the class Employee with eId, eDId, eName, eAddr, eEmail, eMob as
data members.
Step 4: Add Basic Pay (BP) as the member of all the inherited classes.
Step 5: Calculate DA as 97% of BP, HRA as 10% of BP, PF as 12% of BP, Staff club
Step 8: Create the objects for the inherited classes and invoke the necessary methods
Step 9: Stop
PROGRAM:
import java.util.Scanner;
class Employee
inteId, eDId;
double bp;
Employee(inteId, String eName, String eAddr, String eEmail, String eMob, inteDId)
this.eId=eId; this.eName=eName;
this.eAddr=eAddr; this.eEmail=eEmail;
this.eMob=eMob; this.eDId=eDId;
void display()
{
double da = Math.round((bp*0.97*100.0)/100.0);
double pf = Math.round((bp*0.12*100.0)/100.0);
System.out.println("******************************\n"
+ "******************************");
System.out.println("ID : "+eId);
System.out.println("Name : "+eName);
System.out.println("Address : "+eAddr);
System.out.println("Email : "+eEmail);
System.out.println("Mobile : "+eAddr);
System.out.println("******************************");
System.out.println("Earnings ");
System.out.printf("HRA : %8.2f \2
System.out.println("******************************");
System.out.println("******************************");
System.out.println("Deduction ");
System.out.println("******************************");
System.out.println("******************************");
Programmer(inteId, String eName, String eAddr, String eEmail, String eMob, inteDId)
bp=10000;
Professor(inteId, String eName, String eAddr, String eEmail, String eMob, inteDId)
bp=52000;
inteId,eDId;
System.out.println("ID : ");
eId = s.nextInt();
System.out.println("Name : ");
eName = s.next();
System.out.println("Address : ");
eAddr=s.next();
System.out.println("Email : ");
eEmail=s.next();
System.out.println("Mobile : ");
eMob=s.next();
System.out.println("Designation ID : ");
eDId=s.nextInt();
int c = eDId;
switch(c)
case 1:
p.display();
break;
case 2:
eDId);
ap.display();
break;
case 3:
eDId);
asp.display();
break;
case 4:
pr.display();
break;
default:
System.out.println("Invalid Designation");
break;
}
RESULT:
ID :
001
Name :
Shankar
Address :
123,GandhiNagar,Covai
Email :
abc12@gmail.com
Mobile :
8661237980
Designation ID :
******************************
******************************
ID : 1
Name : Shankar
Designation : Professor - 4
Address : 123,GandhiNagar,Covai
Email : abc12@gmail.com
Mobile : 8661237980
******************************
Earnings
DA : 50440.00
HRA : 5200.00
******************************
******************************
Deduction
DA : 6240.00
HRA : 520.00
******************************
******************************
******************************
AIM:
To write a java program to create an abstract class name shape.
ALGORITHM:
Step 1: Start
Step 2: Create an abstract class named Shape that contains abstract method named
Step 3: Create a class Rectangle which should inherit abstract class Shape and
Step 4: Create a class Triangle which should inherit abstract class Shape and
Step 5: Create a class Circle which should inherit abstract class Shape and implement
Step 6: Create a class Demo with main method to access the printArea
method.
Step 7: Stop.
Program:
intl,b;
public Rectangle(intl,int b) {
this.l = l;
this.b = b;
}
public void printArea() {
public Triangle(inth,int b) {
this.l = h;
this.b = b;
public Circle(int r) {
this.l = r;
t.printArea();
c.printArea();
}
RESULT:
SAMPLE OUTPUT
Area of Rectangle=200
Area of Triangle=100.0
.
Ex. No: 5 IMPLEMENTATION OF INTERFACE
Aim:
Write a java program to create and implement interface for the given requirement.
Algorithm:
Step 1: Start
Step 2: Create an Interface named Shape that contains abstract method named
printArea().
Step 3: Create a class Rectangle which should implement Interface Shape and
Step 4: Create a class Triangle which should implement Interface Shape and
Step 5: Create a class Circle which should which should implement Interface Shape and
method.
Step 7: Stop.
Program:
interface Shape {
intl,b;
public Rectangle(intl,int b) {
this.l = l;
this.b = b;
intl,b;
public Triangle(inth,int b) {
this.l = h;
this.b = b;
}
public void printArea() {
intl,b;
public Circle(int r) {
this.l = r;
class Demo {
r.printArea();
t.printArea();
c.printArea();
}
RESULT:
SAMPLE OUTPUT
Area of Rectangle=200
Area of Triangle=100.0
Aim:
Algorithm:
Step 1: Start
Step 6: Using the exception handling mechanism , the thrown exception is handled by
Step 7: After the exception is handled, the string “invalid amount “ will be displayed.
Step 8: If the amount is greater than 0, the message “Amount Deposited “will be
displayed
Step 9: Stop.
Program:
import java.util.Scanner;
String msg;
NegativeAmtException(String msg)
this.msg = msg;
}
return msg;
System.out.print("Enter Amount:");
int a = s.nextInt();
try
if (a < 0)
System.out.println("Amount Deposited");
catch (NegativeAmtException e)
e.printStackTrace();
}
}
RESULT:
SAMPLE OUTPUT
Enter Amount:-500
Invalid Amount
contains methods.
ALGORITHM:
Step 1: Create a class even which implements first thread that computes the square ofthe number
Step 2: run() method implements the code to be executed when thread gets executed.
Step 3: Create a class odd which implements second thread that computes the cube ofthe number.
Step 4: The Multithreading is performed and the task switched between multiplethreads.
Step 5: The sleep () method makes the thread to suspend for the specified time.
PROGRAM:
importjava.util.*;
public int x;
public even(int x)
this.x = x;
}
}
public int x;
public odd(int x)
this.x = x;
intnum = 0;
try
num = r.nextInt(100);
t1.start();
else {
t2.start();
Thread.sleep(1000);
System.out.println("--------------------------------------");
System.out.println(ex.getMessage());
A a = new A();
a.start();
}
}
RESULT:
Thus the java program to implement a multi-threaded application with three methods is executed
successfully and the output is verified.
SAMPLE OUTPUT
--------------------------------------
--------------------------------------
--------------------------------------
--------------------------------------
ALGORITHM:
Step 1: Create a class and get the file name from the user .
Step 2: Use the file functions and display the information about the file.
Step 5: Open a notepad- type any text and save the file in c:\
Step 6: Give the location path of the file as input to this program
PROGRAM:
import java.io.File;
import java.util.Scanner;
if (!file.exists() || !file.isFile())
else
RESULT:
Thus the java program to implement file operations is executed successfullyand the output is verified.
SAMPLE OUTPUT
Enter the Filename:
test.txt
test.txt exists
To write a java program to find the maximum value using a generic function.
ALGORITHM:
Step 2: Get the set of the values belonging to specific data type.
Step 3: Create the objects of the class to hold integer, character and double values.
Step 4: Create the method to compare the values and find the maximum value stored
in the array.
Step 5: Invoke the method with integer, character or double values . The output will
PROGRAM:
T max = elements[0];
if (element.compareTo(max) > 0)
max = element;
}
return max;
Double.valueOf(10.25), Double.valueOf(18.6001)));
"Date"));
Boolean.FALSE));
Byte.MAX_VALUE));
}
RESULT:
Thus the java program to find the maximum value using a generic function isexecuted successfully and
the output is verified.
SAMPLE OUTPUT
ALGORITHM:
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;
@Override
@Override
System.out.println("hello world");
});
root.getChildren().add(btn1);
primaryStage.setScene(scene);
primaryStage.show();
launch(args);
}
RESULT:
SAMPLE OUTPUT
Ex. No: 10b JAVAFX MENUS
AIM:
ALGORITHM:
PROGRAM:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
importjavafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
importjavafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.control.Alert.AlertType;
import java.time.LocalDate;
s.setTitle("creating MenuBar");
// create a menu
// create menuitems
m.getItems().add(m1);
m.getItems().add(m2);
m.getItems().add(m3);
// create a menubar
mb.getMenus().add(m);
// create a VBox
// create a scene
s.setScene(sc);
s.show();
launch(args);
}
RESULT:
SAMPLE OUTPUT