0% found this document useful (0 votes)
24 views27 pages

Oops

OOPS MANUAL

Uploaded by

hearthacker2k123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views27 pages

Oops

OOPS MANUAL

Uploaded by

hearthacker2k123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

EX.

NO: 1 SEQENTIAL SEARCH, BINARY SEARCH, QUADRADIC


SORTING ALGORITHMS

AIM:

To solve the problem by using sequential search, binary search, and


quadratic sorting algorithms.

ALGORITHM:
1. Start the program.
2. To create sequential search algorithm.
3. Searching the values and solve the problems.
4. To create binary values.
5. To create quadratic sorting and searching algorithm.
6. To included solve the problems (Selection and Insertion).
7. Stop the program.
PROGRAM:

SEQUENTIAL SEARCH

public class SequentialSearch{


public static void main(String[] args){
int[] One = {2, 9, 6, 7, 4, 5, 3, 0, 1};
int target = 4;
sequentialSearch(One, target);
}
public static void sequentiallSearch(int[] a, intb){
int index = -1;
for (int i = 0; i <a.length; i++) {
if (a[i]== b) {
index= i;
break;
}
}
if (index== -1) {
System.out.println("Your target integer does not exist in the array");
} else
{
System.out.println("Your target integer is in index " + "index+of the array");
}
}
}
BINARY SEARCH
class BinarySearchExample1{
public static int binarySearch(int arr[], int first, int last, int key){
if (last>=first){
int mid = first + (last - first)/2;
if (arr[mid] == key){
return mid;
}
if (arr[mid] > key){
return binarySearch(arr, first, mid-1, key);//search in left subarray
}else{
return binarySearch(arr, mid+1, last, key);//search in right subarray
}
}
return -1;
}
public static void main(String args[]){
int arr[] = {10,20,30,40,50};
int key = 30;
int last=arr.length-1;
int result = binarySearch(arr,0,last,key);
if (result == -1)
System.out.println("Element is not found!");
else
System.out.println("Element is found at index: "+result);
}
}
SELECTION SORT
class SelectionSort {
void sort(int arr[])
{
int n = arr.length;
// One by one move boundary of unsorted subarray
for (int i = 0; i < n - 1; i++) {
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min_idx])
min_idx = j;
}
// Swap the found minimum element with the first
// element
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
// Prints the array
void printArray(int arr[])
{
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
// main function
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);
}
INSERTION SORT
public class InsertionSort {
public static void insertionSort(int array[]) {
int n = array.length;
for (int j = 1; j < n; j++) {
int key = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > key ) ) {
array [i+1] = array [i];
i--;
}
array[i+1] = key;
}}
public static void main(String a[]){
int[] arr1 = {9,14,3,2,43,11,58,22};
System.out.println("Before Insertion Sort");
for(int i:arr1){
System.out.print(i+" ");
}
System.out.println();
insertionSort(arr1);//sorting array using insertion sort
System.out.println("After Insertion Sort");
for(int i:arr1){
System.out.print(i+" ");
}
}
}
OUTPUT:

SELECTION SORT:

SEQUENTIAL SEARCH:

BINARY SEARCH:

OUTPUT:

RESULT:
Thus the program using sequential search, binary search, and quadratic
sorting algorithms was executed successfully.
EX.NO: 2 DEVELOP STACK AND QUEUE DATA STRUCTURES
USING CLASSES AND OBJECTS

AIM:

To develop stack and queue data structures using classes and objects.

ALGORITHM:
1. Start the program.
2. An array is a random access data.
3. Stack using classes and objects.
4. Queue using also classes and objects.
5. End the program.

PROGRAM:

STACK:
public 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();}}
QUEUE
public class Queue {
int SIZE = 5;
int items[] = new int[SIZE];
int front, rear;

Queue() {
front = -1;
rear = -1;
}

// check if the queue is full


boolean isFull() {
if (front == 0 && rear == SIZE - 1) {
return true;
}
return false;
}
// check if the queue is empty
boolean isEmpty() {
if (front == -1)
return true;
else
return false;
}
// insert elements to the queue
void enQueue(int element) {

// 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);
}
}
// delete element from the queue
int deQueue() {
int 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];

// if the queue has only one element


if (front >= rear) {
front = -1;
rear = -1;
}
else {
// mark next element as the front
front++;
}
System.out.println( element + " Deleted");
return (element);
}
}
// display element of the queue
void display() {
int i;
if (isEmpty()) {
System.out.println("Empty Queue");
}
else {
// display the front of the queue
System.out.println("\nFront index-> " + front);
// display element of the queue
System.out.println("Items -> ");
for (i = front; i <= rear; i++)
System.out.print(items[i] + " ");
// display the rear of the queue
System.out.println("\nRear index-> " + rear);
}
}
public static void main(String[] args) {
// create an object of Queue class
Queue q = new Queue();
// try to delete element from the queue
// currently queue is empty
// so deletion is not possible
q.deQueue();
// insert elements to the queue
for(int i = 1; i < 6; i ++) {
q.enQueue(i);
}
// 6th element can't be added to queue because queue is full
q.enQueue(6);
q.display();
// deQueue removes element entered first i.e. 1
q.deQueue();
// Now we have just 4 elements
q.display();
}
}

OUTPUT:
RESULT:

Thus the stack and queue program was executed successfully.


EX.NO:3 PAYSLIP GENERATION USING INHERITANCE

AIM:
To develop a Java application with employee class and generate pay slips for
the employees with their gross and net salary.
ALGORITHM:

1. Start the program.


2. Create Employee class with Emp_name, Emp_id, Address, Mail_id, Mobile_no
as members. 3. Inherit the classes, Programmer, Assistant Professor, Associate
Professor and Professor from employee class.
4. Add Basic Pay (BP) as the member of all the inherited classes with 97% of BP as
DA, 10 % of BP as HRA, 12% of BP as PF, 0.1% of BP for staff club fund.
5. Generate pay slips for the employees with their gross and net salary.
6. Stop the program

PROGRAM:
class Person {
String name;
int age;

Person(int age, String name) {


this.name = name;
this.age = age;
}
}

class Employee extends Person {


int emp_id;
int emp_salary;

Employee(int id, String name, int age, int salary) {


super(age, name);
emp_id = id;
emp_salary = salary;
}
void printEmployeeDetails() {
System.out.println("Employee ID : " + emp_id);
System.out.println("Employee Name : " + name);
System.out.println("Employee Age : " + age);
System.out.println("Employee Salary : " + emp_salary);
}
}
public class main {
public static void main(String[] args) {
Employee emp = new Employee(101, "AMU", 23, 22340);
Employee emp1 = new Employee(102, "Bala", 25, 22341);
emp.printEmployeeDetails();
}
}

OUTPUT:

RESULT:
Thus the Java application has been created with employee class and pay
slips are generated for the employees with their gross and net salary.
EX.NO:4 CALCULATE AREA OF RECTANGLE, TRIANGLE
AND CIRCLE

AIM:
To Write a java program to create an abstract class named Shape that
contains two integers and an empty method named print Area().Each one of the
classes contain only the method print Area( ) that prints the area of the given shape.

ALGORITHMS:
1. Start the program
2. To create an abstract class name.
3. That contain two integers and an empty method named print Area().
4. To provide three classes named Rectangle, Triangle and Circle.
5. Stop the program.

PROGRAM:
class Rectangle extends Shape {
void printArea() {
System.out.println("*** Finding the Area of Rectangle ***");
System.out.print("Enter length and breadth: ");
length = input.nextInt();
breadth = input.nextInt();
System.out.println("The area of Rectangle is: " + length * breadth);
}
}

class Triangle extends Shape {


void printArea() {
System.out.println("\n*** Finding the Area of Triangle ***");
System.out.print("Enter Base And Height: ");
length = input.nextInt();
breadth = input.nextInt();
System.out.println("The area of Triangle is: " + (length * breadth) / 2);
}
}
class Cricle extends Shape {
void printArea() {
System.out.println("\n*** Finding the Area of Cricle ***");
System.out.print("Enter Radius: ");
radius = input.nextInt();
System.out.println("The area of Cricle is: " + 3.14f * radius * radius);
}
}
public class Abstract {
public static void main(String[] args)
{
Rectangle rec = new Rectangle();
rec.printArea();

Triangle tri = new Triangle();


tri.printArea();

Cricle cri = new Cricle();


cri.printArea();
}
}

OUTPUT:

RESULT:
Thus the java program to create an abstract class named Shape that contains two
Integer sand an empty method named print Area() executed successfully.
EX.NO:5 INTERFACE

AIM:
To solve the above problem using an interface.

ALGORITHM:
1. Start the program.
2. Interface are used to implement abstraction.
3. It is used to achieve total abstraction.
4. Interface are declared by specifying a keyword “interface”.
5. Stop the program.

PROGRAM:
interface Animal {
public void animalSound(); // interface method (does not have a body)
public void sleep(); // interface method (does not have a body)
}

class Cat implements Animal {


public void animalSound() {
System.out.println("The Cat says: meyow meyow");
}
public void sleep() {
System.out.println("meyow");
}
}

public class CATY {


public static void main(String[] args) {
Cat myCat = new Cat();
myCat.animalSound();
myCat.sleep();
}
}
OUTPUT:

RESULT:

Thus the above problem using an interface done successfully.


EX.NO:6 EXCEPTION HANDLING

AIM:

To write a Java program to implement user defined exception handling.

ALGORITHM:

1. Start the program.


2. Create a class NegativeAmtException which extends Exception class.
3. Create a constructor which receives the string as argument.
Get the Amount as input from the user.
4. If the amount is negative, the exception will be generated.
5. Using the exception handling mechanism, the thrown exception is handled by the
catch construct.
7. After the exception is handled, the string “invalid amount “ will be displayed.
8. If the amount is greater than 0, the message “Amount Deposited “will be displayed
9. Stop.

PROGRAM:
class JavaException{
public static void main(String args[])
{
Try
{
throw new MyException(2); // throw is used to create a new exception and throw it.
}
catch(MyException e)
{
System.out.println(e);
}
}
}
class MyException extends Exception{
int a;
MyException(int b)
{
a=b;
}
public String toString()
{
return("Exception Number = "+a);
}
}
OUTPUT:

RESULT:

Thus the Implement exception handling and creation of user defined


exceptions was executed successfully.
EXNO: 7 MULTI-THREADED

AIM:
To write a java program that implements a multi-thread application that has three
threads.

ALGORITHMS:
1. Start the program
2. Design the first thread that generates a random integer for every 1 second.
3. If the first thread value is even, design the second thread as the square of the number
and then print it.
4. If the first thread value is odd, then third thread will print the value of cube of the
number.
5. Stop the program.

PROGRAM:
import java.util.Random;
class Square extends Thread {
int x;
Square(int n) {
x = n;
}
public void run() {
int sqr = x * x;
System.out.println("Square of " + x + " = " + sqr);
}
}
class Cube extends Thread {
int x;
Cube(int n) {
x = n;
}
public void run() {
int cub = x * x * x;
System.out.println("Cube of " + x + " = " + cub);
}
}
class Rnumber extends Thread {
public void run() {
Random random = new Random();
for (int i = 0; i < 5; i++) {
int randomInteger = random.nextInt(10);
System.out.println("Random Integer generated : " + randomInteger);
Square s = new Square(randomInteger);
s.start();
Cube c = new Cube(randomInteger);
c.start();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
System.out.println(ex);
}
}
}
}
public class ThreadP {
public static void main(String[] args) {
Rnumber n = new Rnumber();
n.start();
}
}

RESULT:
Thus the java program that implements a multi-thread application that has
three threads was successfully.
EX.NO:8 READ AND WRITE OPERATIONS

AIM:

To write a program to perform file operation.

ALGORITHM:
1. Start the program.
2. To write data into the file.
3. To read the data from the file.
4. To write a character into the file.
5. Open new file or existing file
6. Stop the program.

PROGRAM:
import java.io.File;
// Importing the IOException class for handling errors
import java.io.IOException;
class CreateFile {
public static void main(String args[]) {
try {
// Creating an object of a file
File f0 = new File("D:FileOperationExample.txt");
if (f0.createNewFile()) {
System.out.println("File " + f0.getName() + " is created successfully.");
} else {
System.out.println("File is already exist in the directory.");
}} catch (IOException exception) {
System.out.println("An unexpected error is occurred.");
exception.printStackTrace();
} }}
OUTPUT:

RESULT:

Thus the program to perform file operation was executed successfully.


EX.NO:9 GENERICS CLASSES

AIM:

To develop applications to demonstrate the features of generics classes.

ALGORITHM:
1. Start the program.
2. To write and develop the program.
3. Open the application files.
4. Run the program.
5. Stop the program.

PROGRAM:
class Main {
public static void main(String[] args) {
// initialize generic class
// with Integer data
GenericsClass<Integer> intObj = new GenericsClass<>(5);
System.out.println("Generic Class returns: " + intObj.getData());
// initialize generic class
// with String data
GenericsClass<String> stringObj = new GenericsClass<>("Java Programming");
System.out.println("Generic Class returns: " + stringObj.getData());
}
}
// create a generics class
class GenericsClass<T> {
// variable of T type
private T data;
public GenericsClass(T data) {
this.data = data;
}
// method that return T type variable
public T getData() {
return this.data;
}
}
OUTPUT

RESULT:

Thus the develop applications to demonstrate the features of generics


classes was executed successfully.
EX.NO:10 JAVAFX

AIM:
To develop applications using JavaFX controls, layouts and menus.

ALGORITHM:
1. Start the program.
2. To write the program.
3. Using the java FX controls.
4. To run the program.
5. Stop the program.

PROGRAM:
import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.util.Arrays;
import static javax.swing.LayoutStyle.ComponentPlacement.UNRELATED;
public class PasswordEx extends JFrame {
private JTextField loginField;
private JPasswordField passField;
public PasswordEx() {
initUI();
}
private void initUI() {
var lbl1 = new JLabel("Login");
var lbl2 = new JLabel("Password");
loginField = new JTextField(15);
passField = new JPasswordField(15);
var submitButton = new JButton("Submit");
submitButton.addActionListener(new SubmitAction());
createLayout(lbl1, loginField, lbl2, passField, submitButton);
setTitle("Login");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class SubmitAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
doSubmitAction();
}
private void doSubmitAction() {
var login = loginField.getText();
var passwd = passField.getPassword();
if (!login.isEmpty() && passwd.length != 0) {
System.out.format("User %s entered %s password%n", login, String.valueOf(passwd));
}
Arrays.fill(passwd, '0');
}
}
private void createLayout(Component... arg) {
var pane = getContentPane();
var gl = new GroupLayout(pane);
pane.setLayout(gl);
gl.setAutoCreateGaps(true);
gl.setAutoCreateContainerGaps(true);
gl.setHorizontalGroup(gl.createSequentialGroup()
.addGap(50)
.addGroup(gl.createParallelGroup()
.addComponent(arg[0])
.addComponent(arg[1])
.addComponent(arg[2])
.addComponent(arg[3])
.addComponent(arg[4]))
.addGap(50)
);
gl.setVerticalGroup(gl.createSequentialGroup()
.addGap(50)
.addGroup(gl.createSequentialGroup()
.addComponent(arg[0])
.addComponent(arg[1], GroupLayout.DEFAULT_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(arg[2])
.addComponent(arg[3], GroupLayout.DEFAULT_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(UNRELATED)
.addComponent(arg[4]))
.addGap(50));
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
var ex = new PasswordEx();
ex.setVisible(true);
});
}
}

OUTPUT:

RESULT:
Thus the develop application using JAVAFX controls, layouts and menus was
executed successfully.

You might also like