Oops Syllab and Program
Oops Syllab and Program
CO.1: Design and develop java programs using object oriented programming concepts
CO.2: Develop simple applications using object oriented concepts such as package, exceptions
CO.3: Implement multithreading, and generics concepts
CO.4: Create GUIs and event driven programming applications for real world problems
CO.5: Implement and deploy web applications using Java
LIST OF EXPERIMENTS:
1. Solve problems by using sequential search, binary search, and quadratic sorting algorithms(selection,
insertion)
2. Develop stack and queue data structures using classes and objects.
3. Develop a java application with an Employee class with Emp_name, Emp_id, Address,Mail_id,
Mobile_no as members. Inherit the classes, Programmer, Assistant Professor,Associate Professor and
Professor from employee class. Add Basic Pay (BP) as the member of all the inherited classes with 97% of
BP as DA, 10 % of BP as HRA, 12% ofBP as PF, 0.1% of BP for staff club funds. Generate pay slips for the
employees with their gross and net salary.
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.
5. Solve the above problem using an interface.
6. Implement exception handling and creation of user defined exceptions.
7. Write a java program that implements a multi-threaded application that has three threads. First thread
generates a random integer every 1 second and if the value is even, the second thread computes the square of
the number and prints. If the value is odd, the third thread will print the value of the cube of the number.
8. Write a program to perform file operations.
9. Develop applications to demonstrate the features of generics classes.
10. Develop applications using JavaFX controls, layouts and menus.
11. Develop a mini project for any application using Java concepts
Ex.No: SEQUENTIAL SEARCH
Date:
AIM
ALGORITHM
Step 4: Traverse and match the key element with array elements
PROGRAM
import java.io.*;
import java.util.Scanner;
class sequential
{
public static void main(String args[])
{
int c, n, search, array[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements:");
n = in.nextInt();
array = new int[n];
System.out.println("Enter those " + n + " elements:");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
System.out.println("Enter value to find:");
search = in.nextInt();
for (c = 0; c < n; c++)
{
if (array[c] == search)
{
System.out.println(search + " is present at location " + (c + 1) + ".");
break;
}
}
if (c == n)
System.out.println(search + " isn't present in array.");
}
}
OUTPUT
RESULT
AIM
To write a java program to demonstrate binary search.
ALGORITHM:
Step 1: Start the program
Step 2: Begin with the mid element of the whole array as a search key.
Step 3: Check if the value of the search key is equal to the item, then return an index of
thesearch key.
Step 4: Check if the value of the search key is less than the item in the middle of the
interval,narrow the interval to the lower half. Otherwise, narrow it to the upper
half.
Step 5: Repeatedly check from the second point until the value is found or the interval is
empty.
Step 6: Stop the program
PROGRAM
import java.io.*;
class BinarySearch
{
public static void binarySearch(int arr[], int first, int last, int key)
{
int mid = (first + last)/2;
while( first <= last )
{
if ( arr[mid] < key )
{
first = mid + 1;
}
else if ( arr[mid] == key )
{
System.out.println("Element is found at index: " + mid); break;
}
else
{
last = mid - 1;
}
mid = (first + last)/2;
}
if ( first > last )
System.out.println("Element is not found!");
}
public static void main(String args[])
{
int arr[] = {10,20,30,40,50};
int key = 30;
int last=arr.length-1;
binarySearch(arr,0,last,key);
}
}
OUTPUT
RESULT
Thus, the binary search program has been executed successfully.
Ex.No: SELECTION SORT
Date:
AIM
To write a Java program for selection sort.
ALGORITHM
Step 1: Start the program.
Step 2: Initialize an array with definite values.
{
s
public static void SelectionSort(int[]arr)
{
for(int i=0;i<arr.length-1;i++)
{
int index=i;
for(int j=i+1;j<arr.length;j++)
{
if(arr[j]<arr[index])
{
index=j;
}
}
int smallernumber=arr[index];
arr[index]=arr[i];
arr[i]=smallernumber;
}
}
public static void main(String args[])
{
int[] arr1={18,4,11,16,2,7,1};
System.out.println(“Before Selection Sort”);
for(int i:arr1)
{
System.out.println(i+” “);
}
System.out.println();
SelectionSort(arr1);
System.out.println(“After Selection Sort”);
for(int i:arr1)
{
System.out.println(i+” ”);
}
}
}
OUTPUT
RESULT
Thus, the selection sort program has been executed successfully.
Ex.No: INSERTION SORT
Date:
AIM
To write a Java program for insertion sort.
ALGORITHM
Step 1: Start the program
Step 2: Iterate from array position 1 to array position n over the array.
Step 3: Compare the current element (key) to its predecessor.
Step 4: If the key element is smaller than its predecessor, compare it to the elements
before.
Step 5: Move the greater elements one position up to make space for the swappedelement.
Step 6: Stop the program
PROGRAM:
import java.io.*;
class InsertionSort
{
void sort(int arr[])
{
for (int i = 1; i < arr.length; ++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;
}
}
static void printArray(int arr[])
{
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String args[])
{
int arr[] = { 12, 11, 13, 5, 6 };
InsertionSort ob = new InsertionSort();
ob.sort(arr);
printArray(arr);
}
}
OUTPUT:
RESULT
Thus, the Java program for insertion sort has been executed successfully.
Ex.No:2 STACK AND QUEUE
Date:
AIM
To develop stack and queue data structures using classes and objects.
ALGORITHM
Step 1: Start the program.
Step 3: Define the required functions for stack and queue data structures
Step 4: Create a DataStructure class for initializing objects for stack and queue classes
Step 5: Print the Stack size, pop element, peek element, Queue size and dequeue value
Step 6: Stop the program
PROGRAM
DataStructure.java
import java.io.*;
import java.util.Scanner;
class Stack
{
Object[] items;
int top;
int maxSize;
public Stack(int maxSize)
{
this.maxSize = maxSize;
this.items = new Object[maxSize];
this.top = -1;
}
public boolean isEmpty()
{
return top == -1;
}
public boolean isFull()
{
return top == maxSize - 1;
}
public void push(Object item)
{
if (!isFull())
{
items[++top] = item;
}
else
System.out.println("Stack is full. Cannot push element.");
}
// Queue implementation
class Queue
{
Object[] items;
int front;
int rear;
int maxSize;
// Queue example
Queue queue = new Queue(5);
queue.enqueue("A");
queue.enqueue("B");
queue.enqueue("C");
System.out.println("\nQueue size: " + queue.size());
System.out.println("Dequeue from queue: " + queue.dequeue());
}
}
OUTPUT
RESULT
Thus, the stack and queue has been implemented using classes and objects successfully.
Ex.No:3 GENERATION OF PAY SLIP USING INHERITANCE
Date:
AIM
ALGORITHM
Step 1: Start.
Step 4: Create classes such as Programmer, Assistant Professor, Associate Professor, and
Professor.
Step 7: Pass the basic pay as a constructor argument in the main function.
Step 8: Use while along with switch to create or generate multiple pay slips.
Step 9: Stop.
PROGRAM
import java.io.*;
import java.util.Scanner;
class Employee
{
String empName;
int empId;
String address;
String mailId;
long mobileNo;
public Employee(String empName, int empId, String address, String mailId, long mobileNo)
{
this.empName = empName;
this.empId = empId;
this.address = address;
this.mailId = mailId;
this.mobileNo = mobileNo;
}
public void displayDetails()
{
System.out.println("Employee Details:");
System.out.println("Name: " + empName);
System.out.println("ID: " + empId);
System.out.println("Address: " + address);
System.out.println("Mail ID: " + mailId);
System.out.println("Mobile No: " + mobileNo);
}
public void generatePaySlip(double basicPay)
{
double da = 0.97 * basicPay;
double hra = 0.1 * basicPay;
double pf = 0.12 * basicPay;
double staffClubFunds = 0.001 * basicPay;
double grossSalary = basicPay + da + hra;
double netSalary = grossSalary - pf - staffClubFunds;
displayDetails();
System.out.println("Basic Pay: " + basicPay);
System.out.println("Dearness Allowance (DA): " + da);
System.out.println("House Rent Allowance (HRA): " + hra);
System.out.println("Provident Fund (PF): " + pf);
System.out.println("Staff Club Funds: " + staffClubFunds);
System.out.println("Gross Salary: " + grossSalary);
System.out.println("Net Salary: " + netSalary);
}
}
Thus, a java application for generating pay slips using classes has been executed
successfully.
AREA OF SHAPES USING ABSTRACT CLASS
Ex.No: 4
Date:
AIM
To write a java to compute area of major shapes using concepts of abstract class.
ALGORITHM
Step 1: Start the program.
Step 2: Define the abstract class Shape with two integers and abstract method printArea().
Step 3: Create the Rectangle, Triangle and Circle classes that extends the Shape class
Step4 : Implement printArea() method in all subclasses
Step 5: Create a Area class to test the shapes
Step 7: Instantiate objects of Rectangle, Triangle, and Circle classes
Step 8: Call the printArea() method for each object.
Step 9: Stop the program.
PROGRAM
abstract class Shape
{
int length;
int width;
abstract void printArea();
}
class Rectangle extends Shape
{
Rectangle(int l, int w)
{
this.length = l;
this.width = w;
}
void printArea()
{
int area = length * width;
System.out.println("Rectangle Area: " + area);
}
}
class Triangle extends Shape
{
Triangle(int l, int w)
{
this.length = l;
this.width = w;
}
void printArea()
{
double area = 0.5 * length * width;
System.out.println("Triangle Area: " + area);
}
}
class Circle extends Shape
{
int radius;
Circle(int r)
{
this.radius = r;
}
void printArea()
{
double area = Math.PI * radius * radius;
System.out.println("Circle Area: " + area);
}
}
public class Area
{
public static void main(String[] args)
{
Shape r= new Rectangle(5, 10);
r.printArea();
Shape t = new Triangle(3, 8);
t.printArea();
Shape c = new Circle(4);
c.printArea();
}
}
RESULT
Thus, the program for shape class using Abstract class has been executed successfully.
Ex.No:5 AREA OF SHAPE USING INTERFACE
Date:
AIM
To write a java to compute area of major shapes using interface concept.
ALGORITHM
Step 1: Start the program.
Step 2: Define the interface Shape with abstract method printArea().
Step 3: Create the Rectangle, Triangle and Circle classes that implements Shape interface
Step4 : Implements printArea() method in all subclasses
Step 5: Create a Area class to test the shapes
Step 7: Instantiate objects of Rectangle, Triangle, and Circle classes
Step 8: Call the printArea() method for each object.
Step 9: Stop the program.
PROGRAM
import java.io.*;
interface Shape
{
void printArea();
}
class Rectangle implements Shape
{
double length,width;
Rectangle(int l, int w)
{
this.length = l;
this.width = w;
}
public void printArea()
{
double area = length * width;
System.out.println("Rectangle Area: " + area);
}
}
class Triangle implements Shape
{
double length,width;
Triangle(int l, int w)
{
this.length = l;
this.width = w;
}
public void printArea()
{
double area = 0.5 * length * width;
System.out.println("Triangle Area: " + area);
}
}
class Circle implements Shape
{
double radius;
Circle(double r)
{
this.radius = r;
}
public void printArea()
{
double area = Math.PI * radius * radius;
System.out.println("Circle Area: " + area);
}
}
class Area
{
public static void main(String[] args)
{
Rectangle r= new Rectangle(5, 10);
r.printArea();
RESULT
Thus, the program for shape class using interfaces is executed successfully.