OOPS FULL LAB MANUAL-01
OOPS FULL LAB MANUAL-01
Output:
Tests of sequentialSearch
0
2
4
-1
B.Binary search
public class SearchTest
{
public static int binarySearch(int[] elements, int target)
{
int left = 0;
int right = elements.length - 1;
while (left <= right)
{
int middle = (left + right) / 2;
if (target < elements[middle])
{
right = middle - 1;
}
else if (target > elements[middle])
{
left = middle + 1;
}
else {
return middle;
}
}
return -1;
}
Output:
2
0
4
-1
Program
A.Stack
\// Stack implementation in Java
class Stack {
// store elements of stack
private int arr[];
// represent top of stack
private int top;
// total capacity of the stack
private int capacity;
// Creating a stack
Stack(int size) {
// initialize the array
// initialize the stack variables
arr = new int[size];
capacity = size;
top = -1;
}
// push elements to the top of stack
public void push(int x) {
if (isFull()) {
System.out.println("Stack OverFlow");
// terminates the program
System.exit(1);
}
// insert element on top of stack
System.out.println("Inserting " + x);
arr[++top] = x;
}
// pop elements from top of stack
public int pop() {
// if stack is empty
// no element to pop
if (isEmpty()) {
System.out.println("STACK EMPTY");
// terminates the program
System.exit(1);
}
// pop element from top of stack
return arr[top--];
}
// return size of the stack
public int getSize() {
return top + 1;
}
// check if the stack is empty
public Boolean isEmpty() {
return top == -1;
}
// check if the stack is full
public Boolean isFull() {
return top == capacity - 1;
}
// display elements of stack
public void printStack() {
for (int i = 0; i <= top; i++) {
System.out.print(arr[i] + ", ");
}
}
public static void main(String[] args) {
Stack stack = new Stack(5);
stack.push(1);
stack.push(2);
stack.push(3);
System.out.print("Stack: ");
stack.printStack();
// remove element from stack
stack.pop();
System.out.println("\nAfter popping out");
stack.printStack();
}
}
Output
Inserting 1
Inserting 2
Inserting 3
Stack: 1, 2, 3,
After popping out
1, 2,
B.QUEUE
import java.util.*;
public class Main {
public static void main(String[] args) {
Queue<Integer> q1 = new
LinkedList<Integer>();
//Add elements to the Queue
q1.add(10);
q1.add(20);
q1.add(30);
q1.add(40);
q1.add(50);
System.out.println("Elements in
Queue:"+q1);
//remove () method =>removes first
element from the queue
System.out.println("Element
removed from the queue: "+q1.remove());
//element() => returns head of the
queue
System.out.println("Head of the
queue: "+q1.element());
//poll () => removes and returns the
head
System.out.println("Poll():Returned
Head of the queue: "+q1.poll());
//returns head of the queue
System.out.println("peek():Head of
the queue: "+q1.peek());
//print the contents of the Queue
System.out.println("Final
Queue:"+q1);
}
}
Output:
Elements in Queue:[10, 20, 30, 40, 50]
Element removed from the queue: 10
Head of the queue: 20
Poll():Returned Head of the queue: 20
peek():Head of the queue: 30
Final Queue:[30, 40, 50]
Program:
import java.util.*;
class employee
{
int empid;
long mobile;
String name, address, mailid;
Scanner get = new Scanner(System.in);
void getdata()
{
System.out.println("Enter Name of the Employee");
name = get.nextLine();
System.out.println("Enter Mail id");
mailid = get.nextLine();
System.out.println("Enter Address of the
Employee:");
address = get.nextLine();
System.out.println("Enter employee id ");
empid = get.nextInt();
System.out.println("Enter Mobile Number");
mobile = get.nextLong();
}
void display()
{
System.out.println("Employee Name: "+name);
System.out.println("Employee id : "+empid);
System.out.println("Mail id : "+mailid);
System.out.println("Address: "+address);
System.out.println("Mobile Number: "+mobile);
19
}
}
class programmer extends employee
{
double salary,bp,da,hra,pf,club,net,gross;
void getprogrammer()
{
System.out.println("Enter basic pay");
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("**************************
**********************");
System.out.println("PAY SLIP FOR
PROGRAMMER");
System.out.println("**************************
**********************");
System.out.println("Basic Pay:Rs"+bp);
System.out.println("DA:Rs"+da);
System.out.println("PF:Rs"+pf);
System.out.println("HRA:Rs"+hra);
System.out.println("CLUB:Rs"+club);
System.out.println("GROSS PAY:Rs"+gross);
20
System.out.println("NET PAY:Rs"+net);
}
}
class asstprofessor extends employee
{
double salary,bp,da,hra,pf,club,net,gross;
void getasst()
{
System.out.println("Enter basic pay");
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("PAY SLIP FOR ASSISTANT
PROFESSOR");
System.out.println("**************************
**********************");
System.out.println("Basic Pay:Rs"+bp);
System.out.println("DA:Rs"+da);
System.out.println("HRA:Rs"+hra);
Syste m.out.println("PF:Rs"+pf);
Syste m.out.println("CLUB:Rs"+club);
21
System.out.println("GROSS PAY:Rs"+gross);
System.out.println("NET PAY:Rs"+net);
}
}
class associateprofessor extends employee
{
double salary,bp,da,hra,pf,club,net,gross;
void getassociate()
{
System.out.println("Enter basic pay");
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("PAY SLIP FOR ASSOCIATE
PROFESSOR");
System.out.println("**************************
**********************");
System.out.println("Basic Pay:Rs"+bp);
System.out.println("DA:Rs"+da);
System.out.println("HRA:Rs"+hra);
System.out.println("PF:Rs"+pf);
22
System.out.println("CLUB:Rs"+club);
System.out.println("GROSS PAY:Rs"+gross);
System.out.println("NET PAY:Rs"+net);
}
}
class professor extends employee
{
double salary,bp,da,hra,pf,club,net,gross;
void getprofessor()
{
Syste m.out.println("Enter basic pay");
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("**************************
**********************");
System.out.println("PAY SLIP FOR PROFESSOR");
System.out.println("**************************
**********************");
System.out.println("Basic Pay:Rs"+bp);
System.out.println("DA:Rs"+da);
System.out.println("HRA:Rs"+hra);
System.out.println("PF:Rs"+pf);
System.out.println("CLUB:Rs"+club);
System.out.println("GROSS PAY:Rs"+gross);
System.out.println("NET PAY:Rs"+net);
}
}
class salary
{
public static void main(String args[])
{
int choice,cont;
do
{
System.out.println("PAYROLL");
System.out.println(" 1.PROGRAMMER \t
2.ASSISTANT PROFESSOR \t 3.ASSOCIATE
PROFESSOR \t 4.PROFESSOR ");
Scann er c = new Scanner(System.in);
choice=c.nextInt();
switch(choice)
{
case 1:
{
programmer p=new programmer();
p.getdata();
p.getprogrammer();
p.display();
p.calculateprog();
break;
}
case 2:
{
asstprofessor asst=new asstprofessor();
asst.getdata();
asst.getasst();
asst.display();
asst.calculateasst();
break;
}
case 3:
{
associateprofessor asso=new associateprofessor();
asso.g etdata();
asso.getassociate();
asso.display();
asso.calculateassociate();
break;
}
case 4:
{
professor prof=new professor();
profg etdata();
profgetprofessor();
prof.display();
prof.calculateprofessor();
break;
}
}
System.out.println("Do u want to continue 0 to quit
and 1 to continue ");
cont=c.nextInt();
}while(cont==1);
}
}
Output:
4.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.
Program:
import java.util.*;
abstract class Shapes
{
double a,b;
abstract void printArea();
}
class Rectangle extends Shapes
{
void printArea()
{
System. out.println("\t\tCalculating Area of
Rectangle");
Scanner input=new Scanner(System.in);
System.out.println("Enter length:");
a=input.nextDouble();
System.out.println("\nEnter breadth:");
b=input.nextDouble();
double area=a*b;
System.out.println("Area of Rectangle:"+area);
}
}
class Triangle extends Shapes
{
void printArea()
{
System.out.println("\t\tCalculating Area of
Triangle");
Scanner input=new Scanner(System.in);
System.out.println("Enter height:");
a=input.nextDouble();
System.out.println("\nEnter breadth:");
b=input.nextDouble();
double area=0.5*a*b;
System.out.println("Area of Triangle:" +area);
}
}
class Circle extends Shapes
{
void printArea()
{
System.out.println("\t\tCalculating 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 of Circle:" +area);
}
}
class AbstractClassDemo
{
public static void main(String[] args)
{
Shapes obj;
obj=new Rectangle();
obj.printArea();
obj=ne w Triangle();
obj.printArea();
obj=new Circle();
obj.printArea();
}
}
Output:
5. Using Interface
import java.util.Scanner;
interface S{
public void rectangle();
public void triangle();
public void circle();
}
class a implements S{
@Override
public void rectangle()
{
System.out.println("\t\tCalculating Area of
Rectangle");
Scanner input=new Scanner(System.in);
System.out.println("Enter length:");
double a = input.nextDouble();
System.out.println("\nEnter breadth:");
double b=input.nextDouble();
double area=a*b;
System.out.println("Area of Rectangle:"+area);
}
@Override
public void triangle()
{
System.out.println("\t\tCalculating Area of
Triangle");
Scanner input=new Scanner(System.in);
System.out.println("Enter height:");
double a = input.nextDouble();
System.out.println("\nEnter breadth:");
double b=input.nextDouble();
double area=0.5*a*b;
System.out.println("Area of Triangle:" +area);
}
public void circle()
{
System.out.println("\t\tCalculating Area of
Circle");
Scanner input=new Scanner(System.in);
System.out.println("Enter radius:");
double a = input.nextDouble();
double area=3.14*a*a;
System.out.println("Area of Circle:" +area);
}
}
public class inter {
public static void main(String[] args) {
a p=new a() ;
p.rectangle();
p.triangle();
p.circle();
}
}
Program:
import java.util.Scanner;
45
class NegativeAmtException extends Exception
{
String msg;
NegativeAmtException(String msg)
{
this.msg=msg;
}
public String toString()
{
return msg;
}
}
public class userdefined
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.print("Enter Amount:");
int a=s.nextInt();
try
{
if(a<0)
{
throw new NegativeAmtException("Invalid Amount");
}
System.out.println("Amount Deposited");
}
46
catch(NegativeAmtException e)
{
System.out.println(e);
}
}
}
OUTPUT
(b) PROGRAM
class MyException extends Exception{
47
String str1;
MyException(String str2)
{
str1=str2;
}
public String toString()
{
return ("MyException Occurred: "+str1) ;
}
}
class example
{
public static void main(String args[])
{
try
{
System.out.println("Starting of try block");
throw new MyException("This is My error Message");
}
catch(MyException exp)
{
System.out.println("Catch Block") ;
System.out.println(exp) ;
}
}}
OUTPUT:
8.File operations
import java.io.FileWriter;
import java.io.IOException;
class GFG {
public static void main(String[] args)
{
Output:
10. Develop application using javaFX control, layouts
and menus
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class MenuExample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws
Exception {
// TODO Auto-generated method stub
BorderPane root = new BorderPane();
Scene scene = new Scene(root,200,300);
MenuBar menubar = new MenuBar();
Menu FileMenu = new Menu("File");
MenuItem filemenu1=new MenuItem("new");
MenuItem filemenu2=new MenuItem("Save");
MenuItem filemenu3=new MenuItem("Exit");
Menu EditMenu=new Menu("Edit");
MenuItem EditMenu1=new MenuItem("Cut");
MenuItem EditMenu2=new MenuItem("Copy");
MenuItem EditMenu3=new MenuItem("Paste");
EditMenu.getItems().addAll(EditMenu1,EditMenu2,E
ditMenu3);
root.setTop(menubar);
FileMenu.getItems().addAll(filemenu1,filemenu2,filem
enu3);
menubar.getMenus().addAll(FileMenu,EditMenu);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Output: