Oops Lab Manual-1
Oops Lab Manual-1
PANRUTI-607106
Student Name :
Register Number :
Year/Semester :
Department :
List of Experiment
AIM:
ALGORITHM:
import java.util.Scanner;
class Linear
{
public static void main(String args[])
{
int i,len, key, array[];
Scanner input = new Scanner(System.in);
System.out.println("Enter Array length:");
len = input.nextInt();
array = new int[len];
System.out.println("Enter " + len + " elements");
for (i = 0; i < len; i++)
{
array[i] = input.nextInt();
}
System.out.println("Enter the search key value:");
key = input.nextInt();
for (i = 0; i < len; i++)
{
if (array[i]== key)
{
System.out.println(key+" is present at location "+(i+1));
break;
}
}
if (i == len)
System.out.println(key + " doesn't exist in array.");
}
}
Output:
RESULT:
AIM:
import java.util.Scanner;
public class Binary {
int binarySearch(int array[], int element, int low, int high) {
while (low <= high) {
int mid = low + (high - low) / 2;
if (array[mid] == element)
return mid;
if (array[mid] < element)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
public static void main(String args[]) {
binary obj = new binary();
// create a sorted array
int[] array = { 3, 4, 5, 6, 7, 8, 9 };
int n = array.length;
// get input from user for element to be searched
Scanner input = new Scanner(System.in);
System.out.println("Enter element to be searched:");
// element to be searched
int element = input.nextInt();
input.close();
// call the binary search method
// pass arguments: array, element, index of first and last element
int result = obj.binarySearch(array, element, 0, n - 1);
if (result == -1)
System.out.println("Not found");
else
System.out.println("Element found at index " + result);
}
}
Output:
RESULT:
AIM:
ALGORITHM:
import java.util.Scanner;
public class SSort
{
public static void Sort(int a[])
{
int n=a.length,i,j,p,temp;
for (i = 0; i < n - 1; i++)
{
p = i;
for (j=i+1; j < n; j++)
{
if (a[p]>a[j])
p=j;
}
temp=a[p];
a[p]=a[i];
a[i]=temp;
}
}
public static void printarray(int a[])
{
for(int i=0; i < a.length; i++)
{
System.out.print(a[i]+" ");
}
}
public static void main(String[] args)
{
int n, res,i;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of elements in the array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter "+n+" elements ");
for( i=0; i < n; i++)
{
a[i] = s.nextInt();
}
Output:
System.out.println( "elements in array ");
printarray(a);
Sort(a);
System.out.println( "\nelements after sorting");
printarray(a);
}
}
RESULT:
AIM:
ALGORITHM:
import java.util.Scanner;
public class ISort { public static void Sort(int a[])
{
int n=a.length,i,j,p,temp;
for (i = 1;i < n; i++)
{
for (j=i-1; j >=0 && a[j+1]< a.length; i++)
{
System.out.print(a[i]+" ");
}
}
public static void main(String[] args)
{ int n, res,i;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of elements in the array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter "+n+" elements ");
for( i=0; i < n; i++)
{
a[i] = s.nextInt();
}
System.out.println( "elements in array ");
printarray(a);
Sort(a);
System.out.println( "\nelements after sorting");
printarray(a);
}
}
Output:
RESULT:
AIM:
To develop Java program for Stack Implementation using Class and Object.
ALGORITHM:
1. Create stack and Store elements in stack for push pop operation
2. Push the elements to the top of stack, before push element in stack should stack
is not full
3. Pop the elements from the stack should not be empty.
4. After the push and pop operation print the stack elements
5. Stop the process.
Sample Output:
Inserting 1
Inserting 2
Inserting 3
Stack: 1, 2, 3,
After popping out
1, 2,
PROGRAM:
class stack {
private int arr[];
private int top;
private int capacity;
stack(int size) {
arr = new int[size];
capacity = size;
top = -1;
}
public void push(int x) {
if (isFull()) {
System.out.println("Stack OverFlow");
System.exit(1);
}
System.out.println("Inserting " + x);
arr[++top] = x;
}
public int pop() {
if (isEmpty()) {
System.out.println("STACK EMPTY");
System.exit(1);
}
return arr[top--];
}
public int getSize() {
return top + 1;
}
public Boolean isEmpty() {
return top == -1;
}
public Boolean isFull() {
return top == capacity - 1;
}
public void printStack() {
for (int i = 0; i <= top; i++) {
System.out.print(arr[i] + ", ");
Output:
}
}
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();
stack.pop();
System.out.println("\nAfter popping out");
stack.printStack();
}
}
RESULT:
AIM:
To develop Java program for Queue Implementation using class and object.
ALGORITHM:
Rear index-> 4
1 Deleted
Front index-> 1
Items ->
2345
Rear index-> 4
Output :
PROGRAM:
AIM:
To develop a java application for generating pay slips of employees with their
Gross and Net salary.
ALGORITHM:
package employee;
public class Employee
{
private String name;
private String id;
private String address;
private String mailId;
private String mobileNo;
public Employee(String name, String id, String address, String mailId,
String mobileNo)
{
this.name= name;
this.id= id;
this.address= address;
this.mailId= mailId;
this.mobileNo= mobileNo;
}
public void display()
{
System.out.println("Emp_Name : "+ name + "\t" + "Emp_id : "+ id);
System.out.println("Address : " + address);
System.out.println("Mail_id : "+ mailId + "\t" + "Mobile_no : " +
mobileNo);
}
public void paySlip()
{
}
}
//For Packages, Folder Name should be employee
//File Name should be Programmer.java
package employee;
public class Programmer extends Employee
{
private float bPay;
private String des;
public Programmer(String name, String id, String address, String
Enter the Mail_id:
[email protected]
Designation: AssistantProfessor
Basic_Pay: 18000.0
Gross Salary : 37260.0 Net Salary : 35082.0
------------ End of the Statements -----------
------------ Employees Pay Slips ------------
Emp_Name : Naresh Emp_id : E102
Address : villupuram
Mail_id : [email protected] Mobile_no : 9873214560
Designation: AssociateProfessor
Basic_Pay: 20000.0
Gross Salary : 41400.0 Net Salary : 38980.0
------------ End of the Statements -----------
public void paySlip()
{
float da=bPay*97/100;
float hra=bPay*10/100;
double grossSalary=bPay + da + hra;
float pf=bPay*12/100;
double scf=bPay*0.1/100;
double netSalary=grossSalary - pf - scf;
System.out.println("------------ Employees Pay Slips------------- ");
super.display();
System.out.println("Designation: "+des);
System.out.println("Basic_Pay: "+bPay);
System.out.println("Gross Salary : "+ grossSalary + "\t" + "Net Salary :
" + netSalary);
System.out.println("------------ End of the Statements----------- ");
}
}
//For Packages, Folder Name should be employee
//File Name should be AssociateProfessor.java
package employee;
public class AssociateProfessor extends Employee
{
private float bPay;
private String des;
public AssociateProfessor(String name, String id, String address,
String mailId, String
mobileNo, float bPay, String des)
{
super(name, id, address, mailId, mobileNo);
this.bPay= bPay;
this.des= des;
}
public void paySlip()
{
float da=bPay*97/100;
float hra=bPay*10/100;
double grossSalary=bPay + da + hra;
float pf=bPay*12/100;
double scf=bPay*0.1/100;
double netSalary=grossSalary - pf - scf;
Output
System.out.println("------------ Employees Pay Slips------------- ");
super.display();
System.out.println("Designation: "+des);
System.out.println("Basic_Pay: "+bPay);
System.out.println("Gross Salary : "+ grossSalary + "\t" + "Net Salary :
" + netSalary);
System.out.println("------------ End of the Statements----------- ");
}
}
//For Packages, Folder Name should be employee
//File Name should be Professor.java
package employee;
public class Professor extends Employee
{
private float bPay;
private String des;
public Professor(String name, String id, String address, String mailId,
String mobileNo, float
bPay, String des)
{
super(name, id, address, mailId, mobileNo);
this.bPay= bPay;
this.des= des;
}
public void paySlip()
{
float da=bPay*97/100;
float hra=bPay*10/100;
double grossSalary=bPay + da + hra;
float pf=bPay*12/100;
double scf=bPay*0.1/100;
double netSalary=grossSalary - pf - scf;
System.out.println("------------ Employees Pay Slips------------- ");
super.display();
System.out.println("Designation: "+des);
System.out.println("Basic_Pay: "+bPay);
System.out.println("Gross Salary : "+ grossSalary + "\t" + "Net Salary :
" + netSalary);
System.out.println("------------ End of the Statements----------- ");
}}
//File Name should be Emp.java separate this file from above folder
import employee.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Emp
{
Employee e;
ArrayList<Employee> obj= new ArrayList<>();
Scanner get= new Scanner(System.in);
public void addEmployee()
{
System.out.println("Enter the Emp_Name:");
String name = get.next();
System.out.println("Enter the Emp_id:");
String id = get.next();
System.out.println("Enter the Address:");
String address = get.next();
System.out.println("Enter the Mail_id:");
String mailId = get.next();
System.out.println("Enter the Mobile_no:");
String mobileNo = get.next();
System.out.println("Enter the Designation:");
String des = get.next();
System.out.println("Enter the Basic_Pay:");
float bPay = get.nextFloat();
if(des.equalsIgnoreCase("Programmer"))
{
e= new Programmer(name, id, address, mailId, mobileNo, bPay, des);
obj.add(e);
}
else if(des.equalsIgnoreCase("AssistantProfessor"))
{
e= new AssistantProfessor(name, id, address, mailId, mobileNo, bPay,
des);
obj.add(e);
}
else if(des.equalsIgnoreCase("AssociateProfessor"))
{e= new AssociateProfessor(name, id, address, mailId, mobileNo,
bPay, des);
obj.add(e);
}
else if(des.equalsIgnoreCase("Professor"))
{
e= new Professor(name, id, address, mailId, mobileNo, bPay, des);
obj.add(e);
}
}
public void displayEmployee()
{
for(Employee e:obj)
{
e.paySlip();
}
}
public static void main(String args[]) throws IOException
{
Emp x= new Emp();
String check;
do
{
x.addEmployee();
System.out.println("Do you wnat continue press 'y'");
check=x.get.next();
}
while(check.equalsIgnoreCase("y"));
x.displayEmployee();
}
}
RESULT:
Thus the application for generating pay slips of employees with their gross
and net salary has been successfully executed.
ExNo : 4 FINDING THE AREA OF DIFFERENT SHAPE USING ABSTRACT
Date: CLASS
AIM:
To write a java program to find the area of different shapes by using abstract
class.
ALGORITHM:
import java.io.*;
import java.util.*;
abstract class Shape
{
double a = 0.0, b = 0.0;
abstract public void printArea();
}
class Rectangle extends Shape
{
double area = 0.0;
public void printArea()
{
System.out.println("Area of Rectangle");
System.out.println(" --------- ");
Scanner in = new Scanner(System.in);
System.out.println("Enter the Width:");
this.a = in.nextDouble();
System.out.println("Enter the Length:");
this.b = in.nextDouble();
this.area = a*b; /* (width*length) */
System.out.println("The area of rectangle is:"+this.area);
}
}
class Triangle extends Shape
{
double area = 0.0;
public void printArea()
{
System.out.println("-----Area of Triangle ---- ");
System.out.println(" --------- ");
Scanner in = new Scanner(System.in);
System.out.println("Enter the Base:");
this.a = in.nextDouble();
System.out.println("Enter the Height:");
this.b = in.nextDouble();
this.area = 0.5*a*b; /* 1/2 (base*height) */
Output :
System.out.println("The area of triangle is:"+this.area);
}
}
class Circle extends Shape
{
double area = 0.0;
public void printArea()
{
System.out.println("-----Area of Circle-----");
System.out.println(" --------- ");
Scanner in = new Scanner(System.in);
System.out.println("Enter the Radius:");
this.a = in.nextDouble();
this.area = 3.14*a*a;
System.out.println("The area of circle is:"+this.area);
}
}
public class Area
{
public static void main(String[] args)
{
System.out.println("-----Finding the Area of Shapes ----- ");
Shape s;
s=new Rectangle();
s.printArea();
s=new Triangle();
s.printArea();
s=new Circle();
s.printArea();
}
}
RESULT:
Thus the Implementation for finding the area of different shapes using abstract
class has been successfully executed
ExNo : 5 CIRCLE, RECTANGLE, TRIANGLE AREA CALCULATION
Date: USING INTERFACE
AIM:
ALGORITHM:
Output:
PROGRAM:
RESULT:
AIM:
ALGORITHM:
import java.io.*;
import java.util.*;
class MyException extends Exception
{
private int d;
MyException(int a)
{
d = a;
}
public String toString()
{
return "MyException [" + d + "]";
}
}
class UserException
{
static void compute(int a) throws MyException
{
System.out.println ("Called Compute(" + a + ")");
if(a>10)
throw new MyException(a);
System.out.println ("Normal Exit");
}
public static void main(String args[])
{
try
{
compute(1);
compute(20);
}
catch(MyException e)
{
System.out.println("Caught " + e);
}
}
}
Output:
RESULT:
Thus the Implementation for user defined exception handling has been
successfully executed.
ExNo : 7 MULTI THREADED APPLICATION
Date:
AIM:
ALGORITHM:
import java.util.*;
class Even implements Runnable
{
public int x;
public Even(int x)
{
this.x = x;
}
public void run()
{
System.out.println("New Thread "+ x +" is EVEN and Square of " + x +
" is: " + x * x);
}
}
class Odd implements Runnable
{
public int x;
public Odd(int x)
{
this.x = x;
}
public void run()
{
System.out.println("New Thread "+ x +" is ODD and Cube of " + x + "
is: " + x * x * x);
}
}
class Generate extends Thread
{
public void run()
{
int num = 0;
Random r = new Random();
try
{
Output:
for (int i = 0; i < 5; i++)
{
num = r.nextInt(100);
System.out.println("Main Thread Generates Random Integer: " +
num);
if (num % 2 == 0)
{
Thread t1 = new Thread(new Even(num));
t1.start();
}
else
{
Thread t2 = new Thread(new Odd(num));
t2.start();
}
Thread.sleep(1000);
System.out.println(" ");
}
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
public class Multithread
{
public static void main(String[] args)
{
Generate g = new Generate();
g.start();
}
}
RESULT:
AIM:
To write a java program to implement file information such as reads a file name
from the user, displays information about whether the file exists, whether the file is
readable, or writable, the type of file and the length of the file in bytes.
ALGORITHM:
import java.io.*;
import java. util.*;
public class FileInfo
{
public static void main(String[] args) throws IOException
{
Scanner in=new Scanner(System.in);
System.out.print("\nEnter the FileName: ");
String fName = in.next();
File f = new File(fName);
String result = f.exists() ? " exists." : " does not exist.";
System.out.println("\nThe given file " +fName + result);
System.out.println("\nFile Location: "+f.getAbsolutePath());
if(f.exists())
{
result = f.canRead() ? "readable." : "not readable.";
System.out.println("\nThe file is " + result);
result = f.canWrite() ? "writable." : "not writable.";
System.out.println("\nThe file is " + result);
System.out.println("\nFile length is " + f.length() + " in bytes.");
if (fName.endsWith(".jpg") || fName.endsWith(".gif") ||
fName.endsWith(".png"))
{
System.out.println("\nThe given file is an image file.");
}
else if (fName.endsWith(".pdf"))
{
System.out.println("\nThe given file is an portable document
format.");
}
else if (fName.endsWith(".txt"))
{
System.out.println("\nThe given file is a text file.");
}
else
Output:
{
System.out.println("The file type is unknown.");
}
}
}
}
RESULT:
Thus the Implementation for getting file information has been successfully
executed.
ExNo : 9 GENERIC CLASSES
Date:
AIM:
To write a java program to find the maximum value from the given type of elements
using a generic function.
ALGORITHM:
import java.util.*;
class MyGeneric {
public static <T extends Comparable<T>> T max(T... elements)
{
T max = elements[0];
for (T element : elements) {
if (element.compareTo(max) > 0)
{
max = element;
}
}
return max;
}
public static void main(String[] args)
{
System.out.println("Integer Max: " + max(Integer.valueOf(32),
Integer.valueOf(89)));
System.out.println("String Max: " + max("GaneshBabu", "Ganesh"));
System.out.println("Double Max: " + max(Double.valueOf(5.6),
Double.valueOf(2.9)));
System.out.println("Boolean Max: " + max(Boolean.TRUE,
Boolean.FALSE));
System.out.println("Byte Max: " + max(Byte.MIN_VALUE,
Byte.MAX_VALUE));
}
}
Output:
RESULT:
Thus the Implementation for finding the maximum value from the
given type of elements using a generic function has been successfully executed.
ExNo : 10 JAVAFX CONTROLS, LAYOUTS AND MENUS
Date:
AIM:
To develop Java program for creating controls, layouts and menus using JavaFX.
ALGORITHM:
1. Open new JavaFX New Application and save file name as JavaFXMenuSample
2. Import Supporting packages into program and extends javafx application object
Application.
3.Import menu package from javafx.scene.MenuBar.
4.Create menu and cerate menu items add the menu items to menu bar.
5. Launch the application and display the output.
Sample Output:
PROGRAM:
package javafxapplicationmenu;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class JavaFXApplicationMenu extends Application {
@Override
public void start(Stage stage) {
// Create MenuBar
MenuBar menuBar = new MenuBar();
// Create menus
Menu fileMenu = new Menu("File");
Menu editMenu = new Menu("Edit");
Menu helpMenu = new Menu("Help");
// Create MenuItems
MenuItem newItem = new MenuItem("New");
MenuItem openFileItem = new MenuItem("Open File");
MenuItem exitItem = new MenuItem("Exit");
MenuItem copyItem = new MenuItem("Copy");
MenuItem pasteItem = new MenuItem("Paste");
// Add menuItems to the Menus
fileMenu.getItems().addAll(newItem, openFileItem, exitItem);
editMenu.getItems().addAll(copyItem, pasteItem);
// Add Menus to the MenuBar
menuBar.getMenus().addAll(fileMenu, editMenu, helpMenu);
BorderPane root = new BorderPane();
root.setTop(menuBar);
Scene scene = new Scene(root, 350, 200);
stage.setTitle("JavaFX Menu (o7planning.org)");
stage.setScene(scene);
stage.show();
}
Output:
public static void main(String[] args) {
Application.launch(args);
}
}
RESULT:
AIM:
To develop a mini project OPAC system for library using Java concepts.
ALGORITHM:
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Data extends JFrame implements ActionListener
{
JTextField id;
JTextField name;
JButton next;
JButton addnew;
JPanel p;
static ResultSet res;
static Connection conn;
static Statement stat;
public Data()
{
super("My Application");
Container c = getContentPane();
c.setLayout(new GridLayout(5,1));
id = new JTextField(20);
name = new JTextField(20);
next = new JButton("Next BOOK");
p = new JPanel();
c.add(new JLabel("ISBN Number",JLabel.CENTER));
c.add(id);
c.add(new JLabel("Book Name",JLabel.CENTER));
c.add(name);
c.add(p);
p.add(next);
next.addActionListener(this);
pack();
setVisible(true);
addWindowListener(new WIN());
}
public static void main(String args[])
{
Data d = new Data();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:stu");
// cust is the DSN Name
stat = conn.createStatement();
res = stat.executeQuery("Select * from stu"); // stu is the table name
res.next();
}
catch(Exception e)
{
System.out.println("Error" +e);
}
d.showRecord(res);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == next)
{
try
{
res.next();
}
catch(Exception e)
{
}
showRecord(res);
}
}
public void showRecord(ResultSet res)
{
try
{
id.setText(res.getString(2));
name.setText(res.getString(3));
}
catch(Exception e)
{
}
}//end of the main
//Inner class WIN implemented
class WIN extends WindowAdapter
{
public void windowClosing(WindowEvent w)
{
JOptionPane jop = new JOptionPane();
jop.showMessageDialog(null,"Thank you","My
Application",JOptionPane.QUESTION_MESSAGE);
}
}
}
RESULT:
Thus the program to develop the simple OPAC for the libraries is
executed successfully.