oops program manuval
oops program manuval
PROGRAM:
import java.util.Scanner;
class LinearSearchExample2
{
public static void main(String args[])
{
int c, n, search, array[];
PROGRAM:
import java.util.Scanner;
class Main {
int binarySearch(int array[], int element, int low, int high) {
// Repeat until the pointers low and high meet each other
while (low <= high) {
return -1;
}
// element to be searched
int element = input.nextInt();
input.close();
Program:
// Selection sort in Java
import java.util.Scanner;
Output:
// Insertion sort in Java
STACK PROGRAM:
import java.util.Scanner;
interface MyStack
{
public void pop();
public void
push(); public
void display();
}
{
System.out.println(" Stack
Overflow"); return;
}
else
{
System.out.println("Enter the
element"); int ele=in.nextInt();
stack[++top]=ele;
}
}
catch(Exception e)
{
System.out.println("e");
}
}
class StackAdt
{
public static void main(String arg[])
{
Scanner in= new Scanner(System.in);
System.out.println("Implementation of Stack
using Array"); StackArray stk=new
StackArray();
int
ch=0;
do
{
System.out.println("1.Push 2.Pop 3.Display
4.Exit"); System.out.println("Enter your
choice:"); ch=in.nextInt();
switch(ch)
{
case 1:
stk.push();
break;
case 2:
stk.pop();
break;
case 3:
stk.display(
); break;
case 4:
System.exit(0);
}
}
while(ch<4);
}
}
NOTE:
To Compile,
javac StackAdt.java
To Run
java StackAdt
OUTPUT:
D:\>javac
StackAdt.java
D:\>java
StackAdt
Implementation of Stack using
Array 1.Push 2.Pop 3.Display
4.Exit
Enter your choice:
1
Enter the
element 10
1.Push 2.Pop 3.Display
4.Exit Enter your
choice:
1
Enter the
element 20
1.Push 2.Pop 3.Display
4.Exit Enter your
choice:
1
Enter the
element 30
1.Push 2.Pop 3.Display
4.Exit Enter your
choice:
1
Enter the
element 45
1.Push 2.Pop 3.Display
4.Exit Enter your
choice:
1
Enter the
element 55
1.Push 2.Pop 3.Display
4.Exit Enter your
choice:
1
Stack Overflow
1.Push 2.Pop 3.Display
4.Exit Enter your
choice:
3
Elements are: 10 --> 20 --> 30 --> 45 --> 55 -->
import java.util.*;
/* Class arrayQueue */
class arrayQueue
{
protected int Queue[] ;
protected int front, rear, size, len;
/* Constructor */
public arrayQueue(int n)
{
size = n;
len = 0;
Queue = new int[size];
front = -1;
rear = -1;
}
/* Function to check if queue is empty */
public boolean isEmpty()
{
return front == -1;
}
/* Function to check if queue is full */
public boolean isFull()
{
return front==0 && rear == size -1 ;
}
/* Function to get the size of the queue */
public int getSize()
{
return len ;
}
/* Function to check the front element of the queue */
public int peek()
{
if (isEmpty())
throw new NoSuchElementException("Underflow Exception");
return Queue[front];
}
/* Function to insert an element to the queue */
public void insert(int i)
{
if (rear == -1)
{
front = 0;
rear = 0;
Queue[rear] = i;
}
else if (rear + 1 >= size)
throw new IndexOutOfBoundsException("Overflow Exception");
else if ( rear + 1 < size)
Queue[++rear] = i;
len++ ;
}
/* Function to remove front element from the queue */
public int remove()
{
if (isEmpty())
throw new NoSuchElementException("Underflow Exception");
else
{
len-- ;
int ele = Queue[front];
if ( front == rear)
{
front = -1;
rear = -1;
}
else
front++;
return ele;
} }
/* Function to display the status of the queue */
public void display()
{
System.out.print("\nQueue = ");
if (len == 0)
{
System.out.print("Empty\n");
return ;
}
for (int i = front; i <= rear; i++)
System.out.print(Queue[i]+" ");
System.out.println();
}
}
/* Class QueueImplement */
public class QueueImplement
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
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;
}
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("Designatio
n: "+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 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("Designatio
n: "+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()
{
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("Designatio
n: "+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 ");
super.display();
System.out.println("Designatio
n: "+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("Program
mer"))
{
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();
}
}
NOTE:
To Run
java Emp
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@tgarme
nts.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:
[email protected]
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:
kumarat@yma
il.com Enter the
Mobile_no:
1237894560
Enter the
Designation:
AssistantProfess
or 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@rediffma
il.com Enter the
Mobile_no:
9873214560
Enter the
Designation:
AssociateProfes
sor Enter the
Basic_Pay:
20000
Do you wnat continue
press 'y' n
------------ Employees Pay Slips ------------
Emp_Name : Suresh Emp_id
: E708 Address : cuddalore
Mail_id : [email protected] 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 : [email protected] 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 : [email protected] 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 : [email protected] Mobile_no :
9873214560 Designation: AssociateProfessor
Basic_Pay: 20000.0
Gross Salary : 41400.0 Net Salary : 38980.0
------------ End of the Statements -----------
FINDING THE AREA OF DIFFERENT SHAPES
PROGRAM:
this.b = in.nextDouble();
this.area = a*b; /* (width*length) */
System.out.println("The area of rectangle
is:"+this.area);
}
}
OUTPUT
.
FINDING THE AREA OF DIFFERENT SHAPES BY USING
INTERFACE
Program:
interface area
{
double pi = 3.14;
double calc(double x,double y);
}
class triangle implements area
{
public double calc(double x,double y)
{
return(0.5*x*y);
}
}
class rect implements area
{
public double calc(double x,double y)
{
return(x*y);
}
}
class cir implements area
{
public double calc(double x,double y)
{
return(pi*x*x);
}
}
class test7
{
public static void main(String arg[])
{
rect r = new rect();
cir c = new cir();
triangle t= new triangle();
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(15,15));
}
Output:
CREATING OWN EXCEPTIONS
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);
System.out.println
("Normal Exit");
}
public static void main(String args[])
{
try
{
compute(
1);
compute(
20);
}
catch(MyException e)
{
System.out.println("Caught " + e);
}
}
}
NOTE:
To Compile:
javac UserException.java
To Run: java UserException
OUTPUT:
MULTI THREADED APPLICATION
PROGRAM:
import java.util.*;
import
java.io.*;
import java.
util.*; public
class FileInfo
{
public static void main(String[] args) throws IOException
{
Scanner in=new Scanner(System.in);
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:
IMPLEMENT THE FEATURES OF GENERICS CLASSES
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;
}
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");
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.addActionListen
er(this); pack();
setVisible(true);
addWindowListener(new
WIN());
}
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.
file. Then click ok. Now your database file gets added to the
System DSN.
Administrative Tools.
ODBC Data Source Administrator
Creating Microsoft Access Driver(*.mdb)
ODBC Microsoft Access Setup
OUTPUT:
To Compile:
javac Data.java
To Run:
java Data