Cs3381 Oops Record
Cs3381 Oops Record
Ex.No: 1 a
Java Program for Sequential Search (Linear Search)
Date:
Aim:
To create a Java program for Sequential search which is also called as Linear Search.
Algorithm:
Coding:
class GFG {
if (arr[i] == x)
return i;
}
return -1;
}
int x = 10;
// Function Call
int result = search(arr, x);
if (result == -1)
System.out.print(
"Element is not present in array");
else
}
}
lOMoARcPSD|29422887
System.out.print("
Element is
present"
+
index
+
lOMoARcPSD|29422887
Output:
Result:
The java Program for Linear Search is developed and tested successfully.
lOMoARcPSD|29422887
Ex.No: 1 b
Java Program for Binary Search
Date:
Aim:
Algorithm:
Coding:
class BinarySearch {
int binarySearch(int arr[], int l,int r, int x)
{
if (r >= l) {
int mid = l + (r - l) /
2; if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l,mid - 1, x);
// Else the element can only be
// present in right subarray
return binarySearch(arr, mid + 1,r, x);
}
return -1;
}
if (result == -1)
System.out.println("Element "+ "not present");
else
}
}
lOMoARcPSD|29422887
System.out.println("Ele
ment found"+ " at index
"+ result);
lOMoARcPSD|29422887
Output:
Result:
The java Program for Binary Search is developed and tested successfully.
lOMoARcPSD|29422887
Ex.No: 1 c
Java Program for Selection Sort
Date:
Aim:
To create a Java program for Selection sort to sort the given elements.
Algorithm:
Coding:
class SelectionSort
{
void sort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
{
int min_idx = 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;
}
}
{
SelectionSort ob = new SelectionSort();
int arr[] = {64,25,12,22,11};
ob.sort(arr);
System.out.println("Sorted array");
ob.printArray(arr);
}
}
lOMoARcPSD|29422887
Output:
Sorted array:
11 12 22 25 64
Result:
The java Program for Selection Sort is developed and tested successfully.
lOMoARcPSD|29422887
Ex.No: 1 d
Java Program for Insertion Sort
Date:
Aim:
To create a Java program for Insertion sort to sort the given elements.
Algorithm:
Coding:
System.out.println();
}
// Driver method
public static void main(String args[])
{
int arr[] = { 12, 11, 13, 5, 6 };
printArray(arr);
}
}
lOMoARcPSD|29422887
Output:
5 6 11 12 13
Result:
The java Program for Insertion Sort is developed and tested successfully.
lOMoARcPSD|29422887
Ex.No: 2a
Java Application for Stack Data Structure
Date:
Aim:
To create a Java console application for stack. Stack operations must be controlled by
exception handling techniques.
Algorithm:
Coding
class Stack {
// 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");
}
}
lOMoARcPSD|29422887
Output:
Inserting 1
Inserting 2
Inserting 3
Stack: 1, 2, 3,
After popping out
1, 2,
Result:
The java console application for stack data structure is developed and tested successfully.
lOMoARcPSD|29422887
Ex.No: 2b
Java Application for Queue Data Structure
Date:
Aim:
To create a Java console application for Queue. Queue operations must be controlled by
exception handling techniques.
Algorithm:
Coding
Queue() {
front = -1;
rear = -1;
}
// if queue is full
if (isFull()) {
System.out.println("Queue is full");
}
else {
if (front == -1) {
// mark front denote first element of queue
front = 0;
}
rear++;
// insert element at the rear
items[rear] = element;
System.out.println("Insert " + element);
}
}
// if queue is empty
if (isEmpty()) {
System.out.println("Queue is empty");
return (-1);
}
else {
// remove element from the front of queue
element = items[front];
q.display();
}
}
lOMoARcPSD|29422887
Output:
Queue is empty
Insert 1
Insert 2
Insert 3
Insert 4
Insert 5
Queue is full
Front index-> 0
Items ->
12 3 4 5
Rear index-> 4
1 Deleted
Front index-> 1
Items ->
2 3 4 5
Rear index-> 4
Result:
The java console application for Queue data structure is developed and tested successfully.
lOMoARcPSD|29422887
Ex.No: 3
Employee Payroll System
Date:
Aim:
To create a Java console application for employee payroll process management. This project
includes Employee and they further classified as Programmer, Assistant Professor, Associate
Professor, Professor.
Algorithm:
Coding
Employee.java
package payscale;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;
class Employee {
String emp_name;
String emp_id;
String emp_address;
String emp_mail_id;
String emp_mobile_no;
int basic_pay;
int per_day_pay;
int current_basic_pay;
int da, hra, pf,
gross_pay; int net_pay;
int no_of_days_in_current_month;
int no_of_days_worked;
Calendar cal;
Scanner input;
Employee() {
input = new Scanner(System.in);
cal = new GregorianCalendar();
no_of_days_in_current_month =
cal.getActualMaximum(Calendar.DAY_OF_MONTH);
getUserBasicDetails();
}
public void generatePaySlip() {
this.da = (this.current_basic_pay / 100) * 97;
this.hra = (this.current_basic_pay / 100) * 12;
this.pf = (int) ((this.current_basic_pay / 100) * 0.1);
this.gross_pay = this.current_basic_pay + da + hra + pf;
lOMoARcPSD|29422887
Programmer.java
package payscale;
public Programmer() {
super();
computeProgrammerPay();
Assistant Professor.java
package payscale;
Associate Professor
package payscale;
computeCurrentBasicPay("AssociateProfessor");
generatePaySlip();
displayPaySlip();
}
}
Professor
package payscale;
Main.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import payscale.AssistantProfessor;
import payscale.AssociateProfessor;
import payscale.Programmer;
import payscale.Professor;
Output:
Choices
Basic Details
lOMoARcPSD|29422887
Programmer
Assistant Professor
lOMoARcPSD|29422887
Associate Professor
Professor
lOMoARcPSD|29422887
Result:
The java console application for employee payroll system was developed and tested
successfully.
lOMoARcPSD|29422887
Ex.No: 4
Java Application to Find the Area of different Shapes
Date:
Aim:
To create a Java console application to find the area of different shapes using abstract class
concept in java.
Algorithm:
Coding:
Shape.java
package com.raja.oopslanb.shapes;
Rectangle.java
package com.raja.oopslanb.shapes;
import java.util.Scanner;
@Override
public void printArea() {
System.out.println("\nRectangle");
System.out.println("--------\n");
Scanner input = new Scanner(System.in);
System.out.println("Enter Length of Rectangle : ");
this.length = input.nextDouble();
System.out.println("Enter Breadth of Rectangle :
"); this.hight = input.nextDouble();
this.area = this.length * this.hight;
System.out.println("Area of the Rectangle is : " + this.area);
}
Triangle.java
package com.raja.oopslanb.shapes;
import java.util.Scanner;
@Override
public void printArea() {
System.out.println("\nTriangle");
System.out.println("--------\n");
Scanner input = new Scanner(System.in);
System.out.println("Enter Length of Triangle : ");
this.length = input.nextDouble();
System.out.println("Enter Hight of Triangle : ");
this.hight = input.nextDouble();
this.area = 0.5 * this.length * this.hight;
System.out.println("Area of the Triangle is : " + this.area);
}
lOMoARcPSD|29422887
Circle.java
package com.raja.oopslanb.shapes;
import java.util.Scanner;
@Override
public void printArea() {
System.out.println("\nCircle");
System.out.println("------\n");
Scanner input = new Scanner(System.in);
System.out.println("Enter Radius of Circle : ");
this.length = input.nextDouble();
this.area = Math.PI * this.length * this.length;
System.out.println("Area of the Circle is : " + this.area);
}
Main.java
import com.raja.oopslanb.shapes.Rectangle;
import com.raja.oopslanb.shapes.Shape;
import com.raja.oopslanb.shapes.Triangle;
import java.util.Scanner;
import com.raja.oopslanb.shapes.Circle;
c intArea();
r break;
.
p System.out.println("\n\nThank You !!!");
r userInput.close();
break;
default:
System.out.println("Please enter valid input");
break;
}
} while (choice != 4);
}
}
lOMoARcPSD|29422887
Output:
Choice
Rectangle
Triangle
lOMoARcPSD|29422887
Circle
Result:
The Java console application to find the area of different shapes using abstract class concept
in java was developed and tested successfully.
lOMoARcPSD|29422887
Ex.No: 5
Java Application to Find the Area of different Shapes -
Date: Interface
Aim:
To create a Java console application to find the area of different shapes using interface
concept in java.
Algorithm:
Coding:
Shape.java
package com.raja.oopslanb.shapes;
interface Shape {
double length = 0.0;
double hight = 0.0;
public void printArea();
}
Rectangle.java
package com.raja.oopslanb.shapes;
import java.util.Scanner;
@Override
public void printArea() {
System.out.println("\nRectangle");
System.out.println("--------\n");
Scanner input = new Scanner(System.in);
System.out.println("Enter Length of Rectangle : ");
this.length = input.nextDouble();
System.out.println("Enter Breadth of Rectangle :
"); this.hight = input.nextDouble();
this.area = this.length * this.hight;
System.out.println("Area of the Rectangle is : " + this.area);
}
Triangle.java
package com.raja.oopslanb.shapes;
import java.util.Scanner;
@Override
public void printArea() {
System.out.println("\nTriangle");
System.out.println("--------\n");
Scanner input = new Scanner(System.in);
System.out.println("Enter Length of Triangle : ");
this.length = input.nextDouble();
System.out.println("Enter Hight of Triangle : ");
this.hight = input.nextDouble();
this.area = 0.5 * this.length * this.hight;
System.out.println("Area of the Triangle is : " + this.area);
}
lOMoARcPSD|29422887
Circle.java
package com.raja.oopslanb.shapes;
import java.util.Scanner;
@Override
public void printArea() {
System.out.println("\nCircle");
System.out.println("------\n");
Scanner input = new Scanner(System.in);
System.out.println("Enter Radius of Circle : ");
this.length = input.nextDouble();
this.area = Math.PI * this.length * this.length;
System.out.println("Area of the Circle is : " + this.area);
}
Main.java
import com.raja.oopslanb.shapes.Rectangle;
import com.raja.oopslanb.shapes.Shape;
import com.raja.oopslanb.shapes.Triangle;
import java.util.Scanner;
import com.raja.oopslanb.shapes.Circle;
c intArea();
r break;
.
p System.out.println("\n\nThank You !!!");
r userInput.close();
break;
default:
System.out.println("Please enter valid input");
break;
}
} while (choice != 4);
}
}
lOMoARcPSD|29422887
Output:
Choice
Rectangle
Triangle
lOMoARcPSD|29422887
Circle
Result:
The Java console application to find the area of different shapes using interface concept in
java was developed and tested successfully.
lOMoARcPSD|29422887
Ex.No: 6
Bank Transaction System with User Exceptions
Date:
Aim:
To create a Java console application for Banking transaction system that helps the users to
do their credit and debit transactions and it rises user defined exception while encountering errors
in transaction and also it should be solved using exception handling techniques.
Algorithm:
Coding:
Customer.java
package com.raja.oopslab.exception.bank;
import java.util.Scanner;
}
lOMoARcPSD|29422887
InvalidCredit.java
package com.raja.oopslab.exception.bank;
{
public InvalidCredit() {
System.out.print("Please enter valid credit amount");
}
}
InvalidDebit.java
package com.raja.oopslab.exception.bank;
Main.java
import java.util.Scanner;
import com.raja.oopslab.exception.bank.*;
public class Main {
public static void main(String[] args) {
Scanner input = new
Scanner(System.in); String name;
int acc_no;
System.out.println("Enter Customer Name");
name = input.next();
System.out.println("Enter account number");
acc_no = input.nextInt();
Customer aCustomer = new Customer(name, acc_no);
int choice = 0;
while(choice != 4){
System.out.println("\n1. Add Money\n2. Get Money\n3. Details\n4. Exit");
choice = input.nextInt();
switch(choice){
case 1:
System.out.println("Enter the amount");
aCustomer.creditTransaction(input.nextInt());
break;
case 2:
System.out.println("Enter the amount");
aCustomer.debitTransaction(input.nextInt());
break;
case 3:
aCustomer.displayDetails();
break;
case 4:
System.out.println("Thank You !!!");
break;
}
}
}
}
lOMoARcPSD|29422887
Output:
Choice:
Display Details:
lOMoARcPSD|29422887
Add Money:
Get Money:
lOMoARcPSD|29422887
Result:
The java console application that uses user defined exception handling techniques was
developed and tested successfully.
lOMoARcPSD|29422887
Ex.No: 7
Java Application for Multi threading
Date:
Aim:
To create a Java console application the uses the multi threading concepts in java. The
Application has 3 threads one creates random number, one thread computes square of that number
and another one computes the cube of that number.
Algorithm:
Coding:
RandomNumberThread.java
package com.raja.oopslab.threading;
import java.util.Random;
}
}
SquareGenThread.java
package com.raja.oopslab.threading;
}
this.squre = this.number * this.number;
System.out.println("SquareGenThread--> Square of "+number+" is
"+squre);
}
}
CubeGenThread.java
package com.raja.oopslab.threading;
int squre;
public CubeGenThread(int number) {
this.number = number;
}
@Override
public void run(){
try {
this.sleep(2000);
} catch (InterruptedException e) {
}
this.squre = this.number * this.number * this.number;
System.out.println("CubeGenThread--> Square of "+number+" is "+squre);
}
}
Main.java
import java.util.Random;
import com.raja.oopslab.threading.RandomNumberThread;
public class Main {
public static void main(String[] args) {
new RandomNumberThread().start();
}
}
lOMoARcPSD|29422887
Output:
Result:
The java console application for multithreading was developed and tested successfully.
lOMoARcPSD|29422887
Ex.No: 8
Java Application for File Handling
Date:
Aim:
To create a Java console application to handle the files and find the file properties
[Availability, Readable or Writeable or Both, Length of the File].
Algorithm:
Coding:
UserFileHandler.java
package com.raja.oopslab.files;
import java.io.File;
public class UserFileHandler{
File aFile;
boolean isReadable = false;
boolean isWriteable = false;
boolean isExists = false;
int length = 0;
public UserFileHandler(String path) {
aFile = new File(path);
this.isExists = aFile.exists();
this.isReadable = aFile.canRead();
this.isWriteable =aFile.canWrite();
this.length = (int) aFile.length();
}
public void fileDetails(){
if(isExists){
System.out.println("The File "+aFile.getName()+" is Available
at:"+aFile.getParent());
if(isReadable && isWriteable)
System.out.println("File is Readable and Writeable");
else if(isReadable)
System.out.println("File is Only Readable");
else if(isWriteable)
System.out.println("File is Only Writeable");
System.out.println("Total length of the file is :"+this.length+" bytes");
}
else System.out.println("File does not exists ");
}
}
Main.java
import java.io.File;
import java.util.Scanner;
import com.raja.oopslab.files.*;
public class Main {
Output:
Availability of File:
File Size:
Result:
The java console application for handling files was developed and tested successfully.
lOMoARcPSD|29422887
Ex.No: 9
Java Application for Generic Max Finder
Date:
Aim:
To create a Java console application that finds the maximum in a array based on the type of
the elements using generic functions in java.
Algorithm:
Coding:
class GenericMax {
public <T extends Comparable<T>> void maxFinder (T[] array){
T max = array[0];
for(T element: array){
System.out.println(element);
if(element.compareTo(max) > 0)
max = element;
}
System.out.println("Max is : "+max);
}
}
}
lOMoARcPSD|29422887
Output:
Result:
The java console application for finding generic max of given elements was developed and
tested successfully.
lOMoARcPSD|29422887
Ex.No: 10
Java applications using JavaFX controls, layouts and menus
Date:
Aim:
To create a Java application using JavaFX layouts and Menus to create a menu bar
and add a menu to it and also add menu items to menu and also add an event listener to
handle the events.
Algorithm:
Coding:
// Java program to create a menu bar and add menu to
// it and also add menuitems to menu and also add
// an event listener to handle the events
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.control.Alert.AlertType;
import java.time.LocalDate;
public class MenuBar_2 extends Application {
// create a menu
Menu m = new Menu("Menu");
// create menuitems
MenuItem m1 = new MenuItem("menu item 1");
MenuItem m2 = new MenuItem("menu item 2");
MenuItem m3 = new MenuItem("menu item 3");
// add menu items to menu
m.getItems().add(m1);
m.getItems().add(m2);
m.getItems().add(m3);
lOMoARcPSD|29422887
// add event
m1.setOnAction(event);
m2.setOnAction(event);
m3.setOnAction(event);
// create a menubar
MenuBar mb = new MenuBar();
// create a VBox
VBox vb = new VBox(mb, l);
// create a scene
Scene sc = new Scene(vb, 500, 300);
s.show();
}
Output:
Result:
The Java application using JavaFX layouts and Menus to create a menu bar and add a
menu to it and also add menu items to menu was developed and tested successfully.