Oops Lab Manual 2vazur
Oops Lab Manual 2vazur
EX. PG.
DATE EXPERIMENT SIGNATURE
NO NO
1. 1
2. 6
3. 11
4. 20
5. 23
6. 25
7. 27
8. 30
9. 33
10. 35
11. 39
Ex.No:1
DATE:
AIM:
ALGORITHM:
PROGRAM:
package
exlcode;
public class
SequentialSearchExample
{
public static void main(String[]
{ args)
int[] exampleVariableOne = {2, 9, 6, 7, 4, 5, 3, 0,
1};
int target =
4;
sequentialSearch(exampleVariableOne,
} target);
1
z
Output
:Enter value to
search:
68
Searched item 68 found at index
6
Enter value to
search:
8
2
BINARY
SEARCH
import
java.util.*;
class
Main{
public static void main(String
args[]){
int numArray[] =
{5,10,15,20,25,30,35};
System.out.println("The array " +
input
//key to be : Arrays.toString(numArray));
searched
int key =
20;
System.out.println("\nKey to be searched=" +
key);
//set first to first
index
int first =
0;
//set last to last elements in
array
int last=numArray.length-
1;
//calculate mid of the
array
int mid = (first +
last)/2;
//while first and last do not
overlap
while( first <=
last//if
){the mid < key, then key to be searched is in the first half of
array
if ( numArray[mid] <
keyfirst
){ = mid +
1; if ( numArray[mid] ==
}else
key//if
){key = element at mid, then print the
location
System.out.println("Element is found at index: " +
mid);
break
;
}else{
//the key is to be searched in the second half of the
array
last = mid -
} 1;
mid = (first +
} last)/2;
//if first and last overlap, then key is not present in the
array
if ( first >
last ){
System.out.println("Element is not
} found!");
}
}
Output
:Enter value to
search:
68
Searched item 68 found at index
6
Enter value to
search:
8
Searched item 8 not found in the
array
Enter value to
search:
10
Searched item 10 found at index
3
2
SELECTION
SORT
public class SelectionSortExample
{ 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;//searching for lowest
} index
}
int smallerNumber =
arr[index];
arr[index] =
arr[i];
arr[i] =
} smallerNumber;
}
Output
:Before Selection
Sort
9 14 3 2 43 11 58
22
After Selection
Sort
2 3 9 11 14 22 43
58
4
INSERTION
SORT
public class InsertionSortExample
{ 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;
}
Output
:Before Insertion
Sort
9 14 3 2 43 11 58
22
After Insertion
Sort
2 3 9 11 14 22 43
58
RESULT:
.
5
EX.NO:2
DATE:
AIM:
ALGORITHM:
PROGRAM:
STACK IMPLEMENTATION
class
Stack
{ private
int
arr[];private
int
top;
private int
capacity;
// Creating a
stack
Stack(int size)
{ arr = new
int[size];
capacity =
size;
top = -
} 1;
System.out.println("Inserting " +
x);
arr[++top] =
} x;
OUTPUT
:
Inserting
1
Inserting
2
Inserting
3
Stack: 1, 2,
3,
After popping
out
1, 2,
7
QUEUE IMPLEMENTATION
public class
Queue
{int SIZE =
5;
int items[] = new
int[SIZE];
int front,
rear;
Queue(
) { front = -
1;
rear = -
} 1;
boolean isFull()
{ if (front == 0 && rear == SIZE -
1){return
}true;
return
} false;
boolean
isEmpty()
{if (front == -
1)
return
true;
else
return
} false;
void enQueue(int
element)
{if (isFull())
{System.out.println("Queue is
}full");
else
{if (front == -
1)
front =
0;
rear++
;items[rear] =
element;
System.out.println("Inserted " +
}element);
}
int
deQueue()
{int
element;
if
(isEmpty())
{ System.out.println("Queue
is
empty");return (-
}1);
else
{element =
items[front];
if (front >=
rear)
{ front = -
1;rear = -
} 1;
/* Q has only one element, so we reset the queue after deleting it.
else {
*/
front++
};
System.out.println("Deleted -> " +
element);
return
}(element);
} 8
void display()
{
/* Function to display elements of Queue
*/
int i;
if
(isEmpty())
{ System.out.println("Empty
Queue");
} else
{System.out.println("\nFront index-> " +
front);
System.out.println("Items ->
");
for (i = front; i <= rear;
i++)
System.out.print(items[i] + "
");
System.out.println("\nRear index-> " +
}rear);
}
9
OUTPUT
:
Queue is empty
Insert 1
Insert 2
Insert 3
Insert 4
Insert 5
Queue is full
Front index-> 0
Items ->
12345
Rear index-> 4
1 Deleted
Front index-> 1
Items ->
2345
Rear index-> 4
RESULT:
10
EX.NO:3
DATE:
AIM:
ALGORITHM:
PROGRAM:
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;
}
11
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()
{
}
}
package employee;
public class Programmer extends Employee
{
private float bPay;
private String des;
public Programmer(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------------ ");
}
}
12
//For Packages, Folder Name should be employee
// File Name should be AssistantProfessor.java
package employee;
public class AssistantProfessor extends Employee
{
private float bPay;
private String des;
public AssistantProfessor(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------------ ");
}
}
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);
13
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------------ ");
}
}
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------------- ");
14
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);
15
}
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();
}
}
16
OUTPUT
:
D:\>javac Emp.java
D:\>java Emp
Enter the Emp_Name:
Suresh
Enter the Emp_id:
E708
Enter the Address:
cuddalore
Enter the Mail_id:
suresh708@tgarments.org
Enter the Mobile_no:
7894561230
Enter the Designation:
Programmer
Enter the Basic_Pay:
7500
Do you wnat continue press 'y'
y
Enter the Emp_Name:
Rakesh
Enter the Emp_id:
E705
Enter the Address:
pondy
Enter the Mail_id:
rakesh@gmail.com
Enter the Mobile_no:
4567891230
Enter the Designation:
Professor
Enter the Basic_Pay:
15000
Do you wnat continue press 'y'
y
Enter the Emp_Name:
kumar
Enter the Emp_id:
E405
Enter the Address:
madurai
Enter the Mail_id:
17
kumarat@ymail.com
Enter the Mobile_no:
1237894560
Enter the Designation:
AssistantProfessor
Enter the Basic_Pay:
18000
Do you wnat continue press 'y'
y
Enter the Emp_Name:
Naresh
Enter the Emp_id:
E102
Enter the Address:
villupuram
Enter the Mail_id:
nar12@rediffmail.com
Enter the Mobile_no:
9873214560
Enter the Designation:
AssociateProfessor
Enter the Basic_Pay:
20000
Do you wnat continue press 'y'
n
18
------------ Employees Pay Slips ------------
Emp_Name : Suresh Emp_id : E708
Address : cuddalore
Mail_id : suresh708@tgarments.org Mobile_no : 7894561230
Designation: Programmer
Basic_Pay: 7500.0
Gross Salary : 15525.0 Net Salary : 14617.5
------------ End of the Statements -----------
------------ Employees Pay Slips ------------
Emp_Name : Rakesh Emp_id : E705
Address : pondy
Mail_id : rakesh@gmail.com Mobile_no : 4567891230
Designation: Professor
Basic_Pay: 15000.0
Gross Salary : 31050.0 Net Salary : 29235.0
------------ End of the Statements -----------
------------ Employees Pay Slips ------------
Emp_Name : kumar Emp_id : E405
Address : madurai
Mail_id : kumarat@ymail.com Mobile_no : 1237894560
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 : nar12@rediffmail.com Mobile_no : 9873214560
Designation: AssociateProfessor
Basic_Pay: 20000.0
Gross Salary : 41400.0 Net Salary : 38980.0
------------ End of the Statements -----------
D:\>
RESULT:
19
EX.NO:4
DATE:
AIM:
ALGORITHM:
PROGRAM:
import java.io.*;
import java.util.*;
20
this.b = in.nextDouble();
this.area = a*b; /* (width*length) */
System.out.println("The area of rectangle is:"+this.area);
}
}
NOTE:
To Compile:
javac Area.java
To Run:
java Area
OUTPUT:
RESULT:
22
EX.NO:05
DATE:
AIM:
ALGORITHM:
is invoked.
PROGRAM:
import java.io.*;
import java.util.*;
interface
area {
double pi =
3.14;
double calc(double x,double
} y);
}
}
class
test7 {
public static void main(String
arg[])
{
rect r = new
rect();
cir c = new
cir();
tri t =new
tri():
area
a;
a = r;
System.out.println("\nArea of Rectangle is : "
+a.calc(10,20));
a = c;
System.out.println("\nArea of Circle is : "
+a.calc(15,15));
a = t;
System.out.println("\nArea of triangle is : "
+a.calc(10,15));
}
OUTPUT:
RESULT:
24
EX.NO:06
DATE:
AIM:
ALGORITHM:
PROGRAM:
import java.io.*;
import java.util.*;
class UserException
{
static void compute(int a) throws MyException
{
System.out.println ("Called Compute(" + a + ")");
if(a>10)
throw new MyException(a);
25
System.out.println ("Normal Exit");
}
OUTPUT:
RESULT:
26
EX.NO:07
DATE:
AIM:
ALGORITHM:
PROGRAM:
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) 27
{
this.x = x;
}
public void run()
{
System.out.println("New Thread "+ x +" is ODD and Cube of " + x + " is: " + x * x * x);
}
}
28
public class Multithread
{
public static void main(String[] args)
{
Generate g = new Generate();
g.start();
}
}
NOTE:
To Compile:
javac Multithread.java
To Run:
java Multithread
OUTPUT:
RESULT:
29
EX.NO:08
DATE:
AIM:
ALGORITHM:
PROGRAM:
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(); 30
File f = new File(fName);
String result = f.exists() ? " exists." : " does not exist.";
System.out.println("\nThe given file " +fName + result);
if(f.exists())
{
result = f.canRead() ? "readable." : "not readable.";
System.out.println("\nThe file is " + result);
31
RESULT:
32
EX.NO:09
DATE:
AIM:
ALGORITHM:
PROGRAM:
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;
}
NOTE:
To Compile:
javac MyGeneric.java
To Run:
java MyGeneric
OUTPUT:
RESULT:
34
EX.NO:10
DATE:
AIM:
PROGRAM:
importjavafx.application.Application;
importjavafx.collections.FXCollections;
importjavafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
importjavafx.scene.Scene;
importjavafx.scene.control.Button;
importjavafx.scene.control.CheckBox;
importjavafx.scene.control.ChoiceBox;
importjavafx.scene.control.DatePicker;
importjavafx.scene.control.ListView;
importjavafx.scene.control.RadioButton;
importjavafx.scene.layout.GridPane;
importjavafx.scene.text.Text;
importjavafx.scene.control.TextField;
importjavafx.scene.control.ToggleGroup;
importjavafx.scene.control.ToggleButton;
importjavafx.stage.Stage;
//Label for
gender
Text genderLabel = newText("gender");
//Creating a Grid
Pane
GridPane gridPane = newGridPane();
36
\
gridPane.add(genderLabel 0, 2);
,
gridPane.add(maleRadio, 1 2);
gridPane.add(femaleRadio 2, 2);
,
gridPane.add(reservationLabel 0, 3);
,
gridPane.add(yes, 1,3);
gridPane.add(no, 2,3);
gridPane.add(technologiesLabel 0, 4);
,
gridPane.add(javaCheckBox 1, 4);
,
gridPane.add(dotnetCheckBox 2, 4);
,
gridPane.add(educationLabel 0, 5);
,
gridPane.add(educationListView, 1, 5);
gridPane.add(locationLabel 0, 6);
,
gridPane.add(locationchoiceBox 1, 6);
,
gridPane.add(buttonRegister, 2, 8);
//Styling
nodes
buttonRegister.setStyle(
"-fx-background-color: darkslateblue -fx-textfill: white;")
; ;
nameLabel.setStyle("-fx-font: normal bold 15px 'serif'");
dobLabel.setStyle("-fx-font: normal bold 15px 'serif ");
genderLabel.setStyle("-fx-font: normal bold 15px 'serif ");
reservationLabel.setStyle("-fx-font: normal bold 15px 'serif'");
technologiesLabel.setStyle("-fxfont: norma bold 15px'serif' ");
l
educationLabel.setStyle("-fx-font: normal bold 15px 'serif'");
locationLabel.setStyle("-fx-font: normal bold 15px 'serif'");
37
//Setting the back ground
color
gridPane.setStyle("-fx-backgroundcolor: BEIGE;")
;
//Creating a scene
object
Scene scene = newScene(gridPane);
OUTPUT:
RESULT:
38
EX.NO:11
DATE:
AIM:
ALGORITHM:
PROGRAM:
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");
39
p = new JPanel();
p.add(next);
next.addActionListener(this);
pack();
setVisible(true);
addWindowListener(new WIN());
}
40
{
id.setText(res.getString(2));
name.setText(res.getString(3));
}
catch(Exception e)
{
}
}//end of the main
NOTE:
Create a new Database
1. Create a new Database file in MS ACCESS (our backend) named “books.mdb”.
2. Then create a table named “stu” in it.
3. The table stu contains the following fields and data types
i. ISBN - Text
ii. BookName - Text
4. Enter various records as you wish.
5. Save the database file.
Next step is to add our “books.mdb” to the System DSN. To do that follows the procedure
given below,
i. Go to Start-> Control Panel -> Administrative tools.
ii. In that double click “Data Sources (ODBC)”.
iii. ODBC Data Source Administrator dialog appears.
iv. In that select “System DSN” tab and click the Add Button.
v. Select “Microsoft Access Driver(*.mdb)” and click Finish.
vi. ODBC Microsoft Access Setup appears. In the “Data Source name” type “stu”.
vii. Click on the “Select” button and choose your database file. Then click ok.
41
Table: Design View
42
Administrative Tools.
43
Creating Microsoft Access Driver(*.mdb)
44
OUTPUT:
RESULT:
45