Oops Labstudent Record Print
Oops Labstudent Record Print
1
ExNo:1(a) SEQUENTIAL SEARCH
Date
AIM:
ALGORITHM:
PROGRAM:
2
OUTPUT:
3
ExNo: 1(b) BINARY SEARCH
Date:
AIM:
ALGORITHM:
PROGRAM:
else
high = mid - 1;
} return -1; }
OUTPUT:
5
ExNo: 1(c) SELECTION SORTING
Date:
AIM:
To Develop Java program for Selecting Sorting Using Array.
ALGORITHM:
PROGRAM:
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[])
6
{
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();
}
System.out.println( "elements in array ");printarray(a);
Sort(a);
System.out.println( "\nelements after sorting");printarray(a);
}
OUTPUT:
Enter number of elements in the array:6Enter 6 elements
9
01
23
99
5 elements in array9 0 1 23 99 5
elements after sorting0 1 5 9 23 99
7
ExNo: 1(d) INSERTION SORTING
Date:
AIM:
To develop Java program for Insertion Sorting using Array.
ALGORITHM:
PROGRAM:
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[j]; j--)
{
temp=a[j+1];a[j+1]=a[j]; a[j]=temp;
}
}
}
public static void printarray(int a[])
{
for(int i=0; i < a.length; i++)
{
System.out.print(a[i]+" ");
}}
8
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:
Enter number of
elements in the array:5
Enter 5 elements
9
5
0
1
8
elements in array9 5 0 1 8
elements after sorting0 1 5 8 9
Result:
9
ExNo: 2(a) STACK IMPLENETATION USING CLASS AND OBJECT
Date:
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.
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;
}
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(); stack.pop();
System.out.println("\nAfter
popping out");
stack.printStack();
}
}
OUTPUT:
Inserting 1
Inserting 2
Inserting 3
Stack: 1, 2, 3, After popping out1, 2,
11
ExNo: 2(b) QUEUE IMPLENETATION USING CLASS AND OBJECT
Date:
AIM:
To develop Java program for Queue Implementation using class and object.
ALGORITHM:
PROGRAM:
12
System.out.println("Queue is full");
}
else
{
if (front == -1) {front = 0;
}
rear++;
items[rear] =element; System.out.println("Insert " + 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;
}
else
{
front++;
}
System.out.println( element + " Deleted");
return (element);
}
}
void display() {
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);
}
}
public static void main(String[] args) {
Queue q = new Queue();q.deQueue();
for(int i = 1; i < 6; i ++) {q.enQueue(i);
}
q.enQueue(6);
q.display();
q.deQueue();
13
q.display();
}
}
OUTPUT:
Rear index-> 4
1 Deleted
Result:
14
ExNo: 3 GENERATING EMPLOYEE PAYROLL
Date:
AIM:
To develop a java application for generating pay roll of employees with their Gross And Net
salary.
ALGORITHM:
PROGRAM:
15
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()
{
}
}
package employee;
public class Programmer extendsEmployee{private float bPay;
private String des;
public Programmer(String name, String id, String address, String mailId, String mobileNo,
float bPay, Stringdes){
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 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;
16
this.des= des;
}
public void paySlip()
{
floatda=bPay*97/10;
floathra=bPay*10/1;
doublegrossSalary=bPay + da + hra;
floatpf=bPay*12/10;doublescf=bPay*0.0;
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);
this.bPay= bPay; this.des= des;
}
public void paySlip()
{ floatda=bPay*97/10;floathra=bPay*10/1;
doublegrossSalary=bPay + da + hra;
floatpf=bPay*12/10;doublescf=bPay*0.0;
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);
17
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------------ ");
}
}
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);
}
18
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 display Employee(){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();
}
}
OUTPUT:
D:\>javac Emp.java
19
15000
Do you want continue press 'y' y
Enter the Emp_Name:
kumar
Enter theEmp_id:
E405
Enter the Address: madurai Enter the Mail_id: kumarat@ymail.com
Enter the Mobile_no: 1237894560
Enter theDesignation:
AssistantProfessor Enter the Basic_Pay:
18000 Do you wnat continuepress '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 theDesignation:
AssociateProfessor
Enter the Basic_Pay:
20000
Do you wnat continuepress 'y' n
------------ Employees Pay Slips------------------Emp_Name : Suresh
Emp_id : E708 Address : cuddalore
Mail_id : suresh708@tgarments.org Mobile_no : 7894561230 Designation:
ProgrammerBasic_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: ProfessorBasic_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
20
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:
AssociateProfessorBasic_Pay: 20000.0
Gross Salary : 41400.0 Net Salary : 38980.0
------------ End of the Statements ----------
Result:
21
ExNo: 4 FINDING THE AREA OF CIRCLE , RECTANGLE AND TRIANGLE USING
ABSTRACT CLASS
Date:
AIM:
To write a java program to find the area of different shapes by using abstract class.
ALGORITHM:
PROGRAM:
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);
22
}
}
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)
*/ 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();
}
}
23
OUTPUT:
Result:
24
ExNo: 5 FINDING THE AREA OF CIRCLE, RECTANGLE, TRIANGLE SHAPE
USING AN
INTERFACE
Date:
AIM:
To develop Java program for circle, rectangle ,triangle Area Calculation Using Interface.
ALGORITHM:
Step 1: Import the java packages.
Step 2: Create an Interface named Area that contains two integers and an method named
Compute().
Step 3: Create a class Rectangle that implements Area. then compute the area and prints the
area of the Rectangle.
Step 4: Create a class Triangle that implements the class Area. then compute the
area and prints the area oftheTriangle.
Step 5: Create a class Circle that implenets the class Area. then compute the area and prints
the area of the Circle.
Step 6: Create object for a class in memory and assign it to the reference variable, then the
method is invoked.
PROGRAM:
OUTPUT:
The area of the Rectangle is 200.0The area of the triangle is 100.0 The area of the Circle is 706.5
Result:
26
ExNo: 6 IMPLEMNTATION OF USER DEFINED EXCEPTIONS
Date:
AIM:
ALGORITHM:
Step 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:
27
System.out.println("Caught " + e);
}
}
}
OUTPUT:
Result:
28
ExNo: 7 FINDING THE SQUARE OF EVEN NUMBERS AND CUBE OF ODD
NUMBERS USING MULTITHREADING
Date:
AIM:
To write a program that implements a multi-threaded application.
ALGORITHM:
Step 1: Import the java packages.
Step 2: Create a thread that generates random number, Obtain one random number and
check is odd or even.
Step 3: If number is even then create and start thread that computes square of a number,
Compute number * numberand display the answer.
Step 4: Notify to Random number thread and goto step 7.
Step 5: If number is odd then create and start thread that computes cube of a number,
Compute number * number *number and display the answer.
Step 6: Notify to Random number thread and goto step 7.
Step 7: Wait for 1 Second and Continue to Step 3 until user wants to exits.
PROGRAM:
import java.util.*;
30
OUTPUT:
Result:
31
ExNo: 8 READING AND WRITING THE CONTENTS FROM THE FILE
Date:
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 lengthof the file in bytes.
ALGORITHM:
PROGRAM:
32
System.out.println("\nThe given file " +fName + result);
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
{
System.out.println("The file type is unknown.");
}
}
}
}
OUTPUT:
33
34
Result:
35
ExNo: 9 FINDING THE MAXIMUM VALUE FROM THE GIVEN TYPE OF ELEMENTS
USING GENERIC CLASS
Date:
AIM:
To write a java program to find the maximum value from the given type of
elements using a generic classes..
ALGORITHM:
Step 1: Import the java packages.
Step 2: Comparable interface is used to order the objects of user-defined class.
Step 3: This interface is found in java.lang package and contains only one method named
compareTo(Object).
Step 4: The compareTo() method works by returning an int value that is either positive,
negative, or zero.
Step 5: Create a generic method max(), that can accept any type of argument.
Step 6: Then sets the first element as the max element, and then compares all other
elements with the max elementusing compareTo() method
Step 7: Finally the function returns an element which has the maximum value.
Step 8: We can call generic method by passing with different types of arguments,
the compiler handles eachmethod.
PROGRAM:
//File Name should be MyGeneric.java
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.com
pareTo(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("ByteMax: " + max(Byte.MIN_VALUE,
Byte.MAX_VALUE));
}
}
36
OUTPUT:
Result:
37
ExNo: 10 IMPLEMENTATIONS OF JAVAFX CONTROLS,
LAYOUTS AND MENUS
Date:
AIM:
To develop Java program for creating controls, layouts and menus using JavaFX.
ALGORITHM:
Step 1: Open new JavaFX New Application and save file name as JavaFXMenuSample
Step 2: Import Supporting packages into program and extends javafx application object
Application.
Step 3: Import menu package from javafx.scene.MenuBar.
Step 4: Create menu and cerate menu items add the menu items to menu bar.
Step 5: Launch the application and display the 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();
}
public static void main(String[] args) {
38
Application.launch(args);
}
}
OUTPUT:
Result:
39
ExNo: 11 MINI PROJECT - OPAC SYSTEM FOR LIBRARY
Date:
AIM:
To develop a mini project OPAC system for library using Java concepts.
ALGORITHM:
PROGRAM:
//File Name should be Data.java
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("MyApplication;Container c = getContentPane();
c.setLayout(new GridLayout(5,1));
id = new JTextField(20);
name = new JTextField(20);
next = new JButton("NextBOOK");
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);
40
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,"Thankyou","MyApplication",JOptionPane.QUESTION_MESS AGE);
}
41
}
}
OUTPUT:
Result:
42