Orig Oops Rec
Orig Oops Rec
Date:
Aim:
Algorithm:
Step 1: start
Step 3:Start from the leftmost element of arr[] and one by one compare x with each
element of arr[].
Step 5:If x doesn’t match with any of elements then return -1.
Step6: stop
Program:
import java.io.
import java.util.*
int Index,size,i,key;
Size=sc.nextInt();
For(i=0;i<size;i++)
arr[i]-se.nextInt();
key=sc.nextInt();
Index=search(arr,key);
If(Index>-1)
else
for(int j=0;j<x.length;j++)
if(x[]== element)
return j;
return -1;
}
Output:
10
50
70
25
47
47
Result:
Thus the application for sequencial search has been successfully executed
Ex.no:1)b) BinarySearch
Date:
Aim:
Algorithm:
Step1:start
Step 3: If x matches with the middle element, then you have to return the mid
index.
Step 4: Else, If key is greater than the mid element, then x can only lie in the right
side half array after the mid element. Hence you recur the right half.
Step 5: Else, if (key is smaller) then recur for the left half.
Step 6:stop
Program;
import java.io.*;
import java.util.";
class BinarySearch
public static void binarySearch(int arr[], int first, int last, int key)(
first = mid + 1;
break;
}else{
last = mid-1;
}
public static void main(String args[])
/*int arr[]={10,20,30,40,50};
int Index,size,l,key:
size=sc.nextInt();
for(i=0;i<size;i++)
arr[i]=sc.nextInt();
key-sc.nextInt();
binarySearch(arr,0,last,key);
}
OUTPUT:
40
50
Result:
Thus the application for binary search has been successfully executed
Ex.no:1)c) Selection Sort
Date:
Aim:
Algorithm:
Step 1: Set i to 0
Program:
import java.io.*;
import java.util.*;
arr[i]-scan.nextInt();
if(arr[i]> arr[j])
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
System.out.print(arr[i]+" ");
}
Output
: C:\Users\HP\Desktop>Javac Selection.java
C:\Users\HP\Desktop>Java Selection
40
39
54
51
22
14
Result:
Thus the application for selection sort has been executed successfully
Ex.no:1)d Insertion Sort
Date:
Aim:
Algorithm:
Step 1: start
Step 4:If the key element is smaller than its predecessor, compare it to the elements
before. Move the greater elements one position up to make space for the swapped
element.
Program:
import java.io.
import java.util.*;
int n = array.length;
{
int key = array[J];
int i=j-1;
i--;
int[] arr1(9.14,3,2,43,11,58,22);
for(int i=0;i<arrl.length;i++)
System.out.print(arr1[i]+" ");
System.out.println():
insertionSort(arr1);
System.out.print(arr1[i]+" ");
}
Output:
C:\Users\HP\Danktop>
Result:
Thus the application for insertion sort has been successfully executed
Ex.no:2)a)
Aim:
Algorithm:
1.start
2.push inserts an item at the top of the stack (i.e., above its current top element).
3.pop removes the object at the top of the stack and returns that object from the
function. The stack size will be decremented by one.
4.peek returns the object at the top of the stack without removing it from the stack
or modifying the stack in any way.
5.Search function used to find the particular element in the stack.
Program:
import java.io.*;
import java.util.*;
class Stackcode
stack.push(i);
System.out.println("Pop Operation:");
System.out.println(y);
}
// Searching element in the stack
if(pos == -1)
else
stack_push(stack);
stack_pop(stack);
stack_push(stack);
stack_peek(stack);
stack_search(stack, 2);
stack_search(stack, 6);
}
Output
C:\Users\HP\Desktop>javac Stackcode.java
C:\Users\HP\Desktop>java Stackcode
Pop Operation:
C:\Users\HP\Desktop>
Result:
Thus the application for stack implantation has been successfully executed
Ex.no: 2)b QUEUE IMPLEMENTATION
Date:
Aim:
Algorithm:
1.start
//import java.util.LinkedList;
import java.util.*;//Queue;
class Que
if (queue.isEmpty()) {
else {
System.out.println("The queue is not empty");
}
Output:
C:\Users\HP Desktop Javac que Java Que.java:8: error: cannot find symbol
Queue<String> queue = new LinkedList<String><>;
1 error
C:\Users\HP\Desktop>javac Que.java
The front element is C The queue size is 2 The queue is not empty
C:\Users\HP\Desktop>javac Que.java
The front element after remove C The queue size is 2 The queue is not empty
Result:
AIM:
ALGORITHM:
6. Each sub classes has its own instance variable like bPay and des.
7. Override the paySlip method in each sub class to calculate the cross and net
salary
8.by using super() method sub class initialize the super class conductor
9.import employee package and create the object for employee class
11.display employee method is used to display all employee play slip details
Program:
Package employee;
this.name=name;
this.id=id;
this.address=address;
this.mailId=mailId;
this.moblieNo= moblieNo;
package employee;
public Programmer(String name, String id, String address, String mailld, String
mobileNo,
this.bPay =bPay;
this.des=des;
}
Public void payslip()
double netSalary=grossSalary-pf-scf;
Super.display();
System.out.println(“Designation:”+des);
System.out.println(“Basic_pay:”+bPay);
package employee;
public Assistant Professor(String name, String id, String address, String mailld,
String
this.bPay Pay;
this.des-des;
super.display();
System.out.println("Designation: "+des);
System.out.println("Basic_Pay: "+bPay);
Package employee;
Public class Associate Professor extends Employee
Public Associate Professor(String name, String id, String address, String mailld,
String mobileNo, float bpay, string des)
Super(name,id,address,mailld, mobileNo);
this.bPay= bPay;
this.des =des;
super.display();
System.out.println("Designation: "+des);
System.out.println("Basic_Pay: "+bPay);
System.out.println("Gross Salary: "+ grossSalary + "u" + "Net Salary:
"+netSalary);
package employee;
public Professor(String name, String id, String address, String mailld, String
mobileNo,float bpay,String des)
Super(name,id,address,mailld, mobileNo);
this.bpay=bpay;
this.des=des;
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;
super.display();
System.out.println("Designation: "+des);
System.out.println("Basic_Pay: "+bPay);
import employee.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
Employee e;
String mailld=get.next();
String mobileNo=get.next();
Float bpay=get.nextFloat();
If(des.equalsIgnoreCase(“Programmer”))
obj.add(e);
obj.add(e);
for(Employee e:obj)
e.paySlip();
String check;
do
{
x.addEmployee();
check-x.get.next();
while(check.equalsIgnoreCase("y"));
x.displayEmployee();
}
}
OUTPUT:
D:\>java Emp
Suresh
E708
7894561230
7500
Rakesh
E705
pondy
4567891230
Professor
15000
Do you wnat continue press 'y'
kumar
E405
madurai
1237894560
Assistant Professor
18000
Naresh
[email protected]
Enter the Mobile no
E102 villupuram
9873214560
20000
10
E708
Address cuddalore
Mail_id:
Mobile_no: 7894561230
Designation:
Programmer
Basic Pay:
7500.0
Gross Salary:
15525.0
Net Salary:
14617.5
Emp Name:
Address:
pondy
Mobile_no:
4567891230
Gross Salary:
31050.0
Net Salary:
29235.0
Mail id:
Designation:
15000.0
Emp kumar
Emp_id: E405
Address
madurai
Mail id
Basic Pay:
18000.0
Mobile_no:
1237894560
Gross Salary:
37260.0
Net Salary:
35082.0
E102
Emp Name:
Naresh
Address:
villupuram
Mail id:
Mobile_no:
9873214560
Designation:
Associate Professor
Gross Salary:
41400.0
Net Salary:
38980.0
DS
RESULT:
Thus the application for generating pay slips of employees with their gross and net
salary has been successfully executed
Ex.no: 4 Area of Rectangle, Triangle, and Circle using abstract class
Date:
AIM:
To develop a java application for area of rectangle , triangle and
circle
Algorithm:
Step1: start
Step2: initialize the abstract class shapes
Step3: initialize the length and breadth
Step4: extent the class rectangle and print Area of rectangle is
length*breadth
Step5: extent the class triangle and print Area of triangle is
(breadth*length)/2
Step6: extent the class circle and print Area of circle is
(Math.PI*breadth*breadth))
Step7: create a new class for all shapes and print the values
Step8: stop
program:
import java.lang.*;
import java.io.*;
abstract class Shapes{
int length=3;
int breadth=4;
void printArea();
}
class Rectangle extends Shapes{
void printArea() {
System.out.println("Area of rectangle : "+length*breadth);
}
}
class Triangle extends Shapes{
void printArea(){
System.out.println("Area of triangle : "+(breadth*length)/2);
}
}
class Circle extends Shapes{
void printArea() {
System.out.println("Area of circle : "+
(Math.PI*breadth*breadth));
}
}
public class Main {
public static void main(String[] args){
Shapes rectangle = new Rectangle();
Shapes triangle = new Triangle();
Shapes circle = new Circle();
rectangle.printArea();
triangle.printArea();
circle.printArea();
}
}
Output:
Area of rectangle : 12
Area of triangle : 6
Area of circle : 50.26548245743669
RESULT:
Thus the application for area of rectangle , triangle and circle
Ex.no:5 Area of Rectangle, Triangle, and Circle using
interface
Date:
AIM:
To develop a java application for area of rectangle , triangle and
circle using interface
Algorithm:
Step1: start
Step2: initialize the interface shapes and declare the value of length and
breadth
Step3: implement the class rectangle in shapes print Area of rectangle is
length*breadth
step4: implement the class triangle in shapes print Area of triangle is
breadth*length /2
step5: implement the class circle in shapes print Area of circle is
Math.PI*breadth*breadth
Step6: create the new class for all the shapes and print the class
Step7: stop
program:
import java.lang.*;
import java.io.*;
interface Shapes{
int length=3;
int breadth=4;
static void printArea();
}
class Rectangle implements Shapes{
void printArea() {
System.out.println("Area of rectangle : "+length*breadth);
}
}
class Triangle implements Shapes{
void printArea(){
System.out.println("Area of triangle : "+(breadth*length)/2);
}
}
class Circle implements Shapes{
void printArea() {
System.out.println("Area of circle : "+
(Math.PI*breadth*breadth));
}
}
public class Main {
public static void main(String[] args){
Rectangle rectangle = new Rectangle();
Triangle triangle = new Triangle();
Circle circle = new Circle();
rectangle.printArea();
triangle.printArea();
circle.printArea();
}
}
Output:
Area of rectangle : 12
Area of triangle : 6
Area of circle : 50.26548245743669
RESULT:
Thus the application for area of rectangle , triangle and circle using
interface
Ex.no :
Date :
AIM:
ALGORITHM:
4. The main ( ) method sets up an exception handler for MyException, then calls
compute ( ) with a legal value (less than 10) and an illegal one to show both
paths through the code.
PROGRAM:
import java.io.*;
import java.util.*;
private int d;
MyException(int a)
d = a;
}
public String toString()
class UserException
if(a>10)
try
compute(1);
compute(20);
catch(MyException e)
}
}
OUTPUT:
RESULT:
Thus the Implementation for user defined exception handling has been
successfully executed.
Ex.no:
Date:
AIM:
ALGORITHM:
2.Create a thread that generates random number, Obtain one random number
and check is odd or even.
3.If number is even then create and start thread that computes square of a
number, Compute number * number and display the answer.
5.If number is odd then create and start thread that computes cube of a number,
Compute number * number * number and display the answer.
7.Wait for 1 Second and Continue to Step 3 until user wants to exits.
PROGRAM:
import java.util.*;
public int x;
public Even(int x)
{
this.x = x;
System.out.println("New Thread "+ x +" is EVEN and Square of " + x + " is: "
+ x * x);
public int x;
this.x = x;
System.out.println("New Thread "+ x +" is ODD and Cube of " + x + " is: " + x
* x * x);
{
int num = 0;
try
num = r.nextInt(100);
if (num % 2 == 0)
t1.start();
else
t2.start();
Thread.sleep(1000);
System.out.println("--------------------------------------");
{
System.out.println(ex.getMessage());
g.start();
OUTPUT:
RESULT:
Date:
AIM:
ALGORITHM:
3. By using File class method create a File object associated with the file or
directory specified by pathname. The pathname can contain path information as
well as a file or directory name.
4. The exists() checks whether the file denoted by the pathname exists. Returns
true if and only if the file denoted by the pathname exists; false otherwise
6. The canRead() checks whether the application can read the file denoted by
the pathname. Returns true if and only if the file specified by the pathname
exists and can be read by the application; false otherwise.
7. The canWrite() checks whether the application can modify to the file denoted
by the pathname. Returns true if and only if the file system actually contains a
file denoted by the pathname and the application is allowed to write to the file;
false otherwise.
8. The length() returns the length of the file denoted by the pathname. The
return value is unspecified if the pathname denotes a directory.
9. The endsWith() returns true if the given string ends with the string given as
argument for the method else it returns false.
10. The program uses conditional operator to check different functionalities of
the given file.
PROGRAM:
import java.io.*;
if(f.exists())
if (fName.endsWith(".jpg") || fName.endsWith(".gif") ||
fName.endsWith(".png"))
{
else if (fName.endsWith(".pdf"))
else if (fName.endsWith(".txt"))
else
}
OUTPUT :
RESULT:
Thus the Implementation for getting file information has been successfully
executed.
Ex.no:
Date :
GENERIC PROGRAMMING
AIM:
To write a java program to find the maximum value from the given type of
elements using a generic function.
ALGORITHM:
3.This interface is found in java.lang package and contains only one method
named compareTo(Object).
5.Create a generic method max(), that can accept any type of argument.
6.Then sets the first element as the max element, and then compares all other
elements with the max element using compareTo() method
7.Finally the function returns an element which has the maximum value.
8.We can call generic method by passing with different types of arguments, the
compiler handles each method.
PROGRAM:
import java.util.*;
class MyGeneric {
T max = elements[0];
for (T element : elements) {
if (element.compareTo(max) > 0)
max = element;
return max;
}
OUTPUT:
RESULT:
Thus the Implementation for finding the maximum value from the given type of
elements using a generic function has been successfully executed.
Ex.no:
Date:
AIM:
a) Decimal manipulations
b) Scientific manipulations
ALGORITHM:
2. Create the class calculator by implementing the class Jframe and interface
actionListener.
Panel(),Jtextfield(),setfont() methods.
6. Enable the scientific or standard calculator using the method method add().
PROGRAM:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
JTextField tfield;
int k = 1, x = 0, y = 0, z = 0;
char ch;
JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, zero, clr, pow2, exp, plus, min, div,
log, rec,
Container cont;
cont = getContentPane();
cont.setLayout(new BorderLayout());
tfield.setHorizontalAlignment(SwingConstants.RIGHT);
tfield.addKeyListener(new KeyAdapter() {
char c = keyevent.getKeyChar();
keyevent.consume();
});
textpanel.add(tfield);
boolean t = true;
b1 = new JButton("1");
buttonpanel.add(b1);
b1.addActionListener(this);
b2 = new JButton("2");
buttonpanel.add(b2);
b2.addActionListener(this);
b3 = new JButton("3");
buttonpanel.add(b3);
b3.addActionListener(this);
b4 = new JButton("4");
buttonpanel.add(b4);
b4.addActionListener(this);
b5 = new JButton("5");
buttonpanel.add(b5);
b5.addActionListener(this);
b6 = new JButton("6");
buttonpanel.add(b6);
buttonpanel.add(b7);
b7.addActionListener(this);
b8 = new JButton("8");
buttonpanel.add(b8);
b8.addActionListener(this);
b9 = new JButton("9");
buttonpanel.add(b9);
b9.addActionListener(this);
buttonpanel.add(zero);
zero.addActionListener(this);
buttonpanel.add(plus);
plus.addActionListener(this);
buttonpanel.add(min);
min.addActionListener(this);
buttonpanel.add(mul);
mul.addActionListener(this);
buttonpanel.add(div);
buttonpanel.add(dot);
dot.addActionListener(this);
eq = new JButton("=");
buttonpanel.add(eq);
eq.addActionListener(this);
buttonpanel.add(rec);
buttonpanel.add(sqrt);
sqrt.addActionListener(this);
buttonpanel.add(log);
log.addActionListener(this);
buttonpanel.add(sin);
sin.addActionListener(this);
buttonpanel.add(cos);
cos.addActionListener(this);
buttonpanel.add(tan);
tan.addActionListener(this);
buttonpanel.add(pow2);
pow2.addActionListener(this);
exp.addActionListener(this);
buttonpanel.add(exp);
buttonpanel.add(clr);
clr.addActionListener(this);
cont.add("Center", buttonpanel);
cont.add("North", textpanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String s = e.getActionCommand();
if (s.equals("1")) {
if (z == 0) {
tfield.setText(tfield.getText() + "1");
} else {
tfield.setText("");tfield.setText(tfield.getText() + "1");
z = 0;
}
if (s.equals("2")) {
if (z == 0) {
tfield.setText(tfield.getText() + "2");
} else {
tfield.setText("");
tfield.setText(tfield.getText() + "2");
z = 0;
if (s.equals("3")) {
if (z == 0) {
tfield.setText(tfield.getText() + "3");
} else {
tfield.setText("");
tfield.setText(tfield.getText() + "3");
z = 0;
if (s.equals("4")) {
if (z == 0) {
tfield.setText(tfield.getText() + "4");
} else {
tfield.setText("");
tfield.setText(tfield.getText() + "4");
z = 0;
if (s.equals("5")) {
if (z == 0) {
tfield.setText(tfield.getText() + "5");
} else {
tfield.setText("");
tfield.setText(tfield.getText() + "5");
z = 0;
}if (s.equals("6")) {
if (z == 0) {
tfield.setText(tfield.getText() + "6");
} else {
tfield.setText("");
tfield.setText(tfield.getText() + "6");
z = 0;
if (s.equals("7")) {
if (z == 0) {
tfield.setText(tfield.getText() + "7");
} else {
tfield.setText("");
tfield.setText(tfield.getText() + "7");
z = 0;
if (s.equals("8")) {
if (z == 0) {
tfield.setText(tfield.getText() + "8");
} else {
tfield.setText("");
tfield.setText(tfield.getText() + "8");
z = 0;
if (s.equals("9")) {
if (z == 0) {
tfield.setText(tfield.getText() + "9");
} else {
tfield.setText("");
tfield.setText(tfield.getText() + "9");
z = 0;
if (s.equals("0")) {
if (z == 0) {
tfield.setText(tfield.getText() + "0");
} else {tfield.setText("");
tfield.setText(tfield.getText() + "0");
z = 0;
if (s.equals("AC")) {
tfield.setText("");
x = 0;
y = 0;
z = 0;
if (s.equals("log")) {
if (tfield.getText().equals("")) {
tfield.setText("");
} else {
a = Math.log(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
if (s.equals("1/x")) {
if (tfield.getText().equals("")) {
tfield.setText("");
} else {
a = 1 / Double.parseDouble(tfield.getText());
tfield.setText("");
tfield.setText(tfield.getText() + a);
if (s.equals("Exp")) {
if (tfield.getText().equals("")) {
tfield.setText("");
} else {
a = Math.exp(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
}if (s.equals("x^2")) {
if (tfield.getText().equals("")) {
tfield.setText("");
} else {
a = Math.pow(Double.parseDouble(tfield.getText()), 2);
tfield.setText("");
tfield.setText(tfield.getText() + a);
}
if (s.equals(".")) {
if (y == 0) {
tfield.setText(tfield.getText() + ".");
y = 1;
} else {
tfield.setText(tfield.getText());
if (s.equals("+")) {
if (tfield.getText().equals("")) {
tfield.setText("");
temp = 0;
ch = '+';
} else {
temp = Double.parseDouble(tfield.getText());
tfield.setText("");
ch = '+';
y = 0;
x = 0;
tfield.requestFocus();
if (s.equals("-")) {
if (tfield.getText().equals("")) {
tfield.setText("");
temp = 0;
ch = '-';
} else {
x = 0;
y = 0;
temp = Double.parseDouble(tfield.getText());
tfield.setText("");ch = '-';
tfield.requestFocus();
if (s.equals("/")) {
if (tfield.getText().equals("")) {
tfield.setText("");
temp = 1;
ch = '/';
} else {
x = 0;
y = 0;
temp = Double.parseDouble(tfield.getText());
ch = '/';
tfield.setText("");
tfield.requestFocus();
}
if (s.equals("*")) {
if (tfield.getText().equals("")) {
tfield.setText("");
temp = 1;
ch = '*';
} else {
x = 0;
y = 0;
temp = Double.parseDouble(tfield.getText());
ch = '*';
tfield.setText("");
tfield.requestFocus();
if (s.equals("Sqrt")) {
if (tfield.getText().equals("")) {
tfield.setText("");
} else {
a = Math.sqrt(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
}if (s.equals("SIN")) {
if (tfield.getText().equals("")) {
tfield.setText("");
} else {
a = Math.sin(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
if (s.equals("COS")) {
if (tfield.getText().equals("")) {
tfield.setText("");
} else {
a = Math.cos(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
if (s.equals("TAN")) {
if (tfield.getText().equals("")) {
tfield.setText("");
} else {
a = Math.tan(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
}
if (s.equals("=")) {
if (tfield.getText().equals("")) {
tfield.setText("");
} else {
temp1 = Double.parseDouble(tfield.getText());
switch (ch) {
case '+':
break;
case '-':
break;
case '/':
case '*':
break;
tfield.setText("");
tfield.setText(tfield.getText() + result);
z = 1;
}
}
tfield.requestFocus();
try
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLook
AndFeel");
catch (Exception e)
f.setTitle("ScientificCalculator");
f.pack();
f.setVisible(true);
}
OUTPUT:
RESULT:
Thus the Implementation for designing the scientific calculator has been
successfully executed.
Ex.no:
Date:
AIM:
To develop a mini project OPAC system for library using Java concepts.
ALGORITHM:
3. Create the textfield for id, name and button for next, address and the panel.
5. Assign the length and breadth value for the layout using gridlayout.
8. Create the bookname under the driver jdbc odbc driver in the try block.
9. Create the object for exception as e and use it for catching the error.
PROGRAM:
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
JTextField id;
JTextField name;
JButton next;
JButton addnew;
JPanel p;
public Data()
super("My Application");
Container c = getContentPane();
c.setLayout(new GridLayout(5,1));
id = new JTextField(20);
p = new JPanel();
c.add(id);
c.add(name);
c.add(p);
p.add(next);
next.addActionListener(this);
pack();
setVisible(true);
addWindowListener(new WIN());
try
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:stu");
stat = conn.createStatement();
res.next();
catch(Exception e)
System.out.println("Error" +e);
d.showRecord(res);
if(e.getSource() == next)
try
res.next();
catch(Exception e)
showRecord(res);
try
id.setText(res.getString(2));
name.setText(res.getString(3));
catch(Exception e)
jop.showMessageDialog(null,"Thank you","My
Application",JOptionPane.QUESTION_MESSAGE);
}
OUTPUT:
RESULT:
Thus the program to develop the simple OPAC for the libraries is executed
successfully.