0% found this document useful (0 votes)
3 views

oops program manuval

The document contains Java programs that implement various algorithms and data structures, including sequential search, binary search, selection sort, insertion sort, stack, and queue. Each section provides code examples and user interaction for inputting data and displaying results. Additionally, it includes a basic structure for generating employee payroll details using classes and inheritance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

oops program manuval

The document contains Java programs that implement various algorithms and data structures, including sequential search, binary search, selection sort, insertion sort, stack, and queue. Each section provides code examples and user interaction for inputting data and displaying results. Additionally, it includes a basic structure for generating employee payroll details using classes and inheritance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

IMPLEMENTATION OF SEQUENTIAL SEARCH

PROGRAM:
import java.util.Scanner;

class LinearSearchExample2
{
public static void main(String args[])
{
int c, n, search, array[];

Scanner in = new Scanner(System.in);


System.out.println("Enter number of elements");
n = in.nextInt();
array = new int[n];

System.out.println("Enter those " + n + " elements");

for (c = 0; c < n; c++)


array[c] = in.nextInt();

System.out.println("Enter value to find");


search = in.nextInt();

for (c = 0; c < n; c++)


{
if (array[c] == search) /* Searching element is present */
{
System.out.println(search + " is present at location " + (c + 1) + ".");
break;
}
}
if (c == n) /* Element to search isn't present */
System.out.println(search + " isn't present in array.");
}
}
Output:
IMPLEMENTATION OF BINARY SEARCH

PROGRAM:

import java.util.Scanner;

// Binary Search in Java

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) {

// get index of mid element


int mid = low + (high - low) / 2;

// if element to be searched is the mid element


if (array[mid] == element)
return mid;

// if element is less than mid element


// search only the left side of mid
if (array[mid] < element)
low = mid + 1;

// if element is greater than mid element


// search only the right side of mid
else
high = mid - 1;
}

return -1;
}

public static void main(String args[]) {

// create an object of Main class


Main obj = new Main();
// 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:
IMPLEMENTATION OF QUADRATIC SORTING ALGORITHMS
(SELECTION, INSERTION)

Program:
// Selection sort in Java

import java.util.Scanner;

public class SelectionSortExample2


{
public static void main(String args[])
{
int size, i, j, temp;
int arr[] = new int[50];
Scanner scan = new Scanner(System.in);

System.out.print("Enter Array Size : ");


size = scan.nextInt();

System.out.print("Enter Array Elements : ");


for(i=0; i<size; i++)
{
arr[i] = scan.nextInt();
}

System.out.print("Sorting Array using Selection Sort Technique..\n");


for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
if(arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.print("Now the Array after Sorting is :\n");
for(i=0; i<size; i++)
{
System.out.print(arr[i]+ " ");
}
}
}

Output:
// Insertion sort in Java

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;
}
}

public static void main(String a[])


{
int[] arr1 = {9,14,3,2,43,11,58,22};
System.out.println("Before Insertion Sort");
for(int i:arr1){
System.out.print(i+" ");
}
System.out.println();

insertionSort(arr1);//sorting array using insertion sort

System.out.println("After Insertion Sort");


for(int i:arr1){
System.out.print(i+" ");
}
}
}
Output:
IMPLEMENT STACK AND QUEUE DATA STRUCTURES USING
CLASSES AND OBJECTS.

STACK PROGRAM:
import java.util.Scanner;
interface MyStack
{
public void pop();
public void
push(); public
void display();
}

class StackArray implements MyStack


{
final static int n=5;
int stack[]=new
int[n]; int top=-1;

public void push()


{
Scanner
in; try
{
in=new
Scanner(System.in);
if(top==(n-1))

{
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");
}
}

public void pop()


{
if(top<0)
{
System.out.println("Stack
underflow"); return;
}
else
{
int
popper=stack[to
p]; top--;
System.out.println("Popped element:" +popper);
}
}

public void display()


{
if(top<0)
{
System.out.println("Stack is
empty"); return;
}
else
{
String str=" ";
for(int i=0; i<=top; i++)
str=str+" "+stack[i]+"
-->";
System.out.println("Elements are:"+str);
}
}
}

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 -->

1.Push 2.Pop 3.Display


4.Exit Enter your
choice:
2
Popped element:55
1.Push 2.Pop 3.Display
4.Exit Enter your
choice:
3
Elements are: 10 --> 20 --> 30 --> 45 -->

1.Push 2.Pop 3.Display


4.Exit Enter your
choice:
2
Popped element:45
1.Push 2.Pop 3.Display
4.Exit Enter your
choice:
3
Elements are: 10 --> 20 --> 30 -->

1.Push 2.Pop 3.Display


4.Exit Enter your
choice:
2
Popped element:30
1.Push 2.Pop 3.Display
4.Exit Enter your
choice:
3
Elements are: 10 --> 20 -->

1.Push 2.Pop 3.Display


4.Exit Enter your
choice:
2
Popped element:20
1.Push 2.Pop 3.Display
4.Exit Enter your
choice:
3
Elements are: 10 -->
1.Push 2.Pop 3.Display
4.Exit Enter your
choice:
2
Popped element:10
1.Push 2.Pop 3.Display
4.Exit Enter your
choice:
3
Stack is empty

1.Push 2.Pop 3.Display


4.Exit Enter your
choice:
3
Stack is empty

1. Push 2.Pop 3.Display


4.Exit Enter your
choice:
4
D:\>
QUEUE PROGRAM:

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);

System.out.println("Array Queue Test\n");


System.out.println("Enter Size of Integer Queue ");
int n = scan.nextInt();
/* creating object of class arrayQueue */
arrayQueue q = new arrayQueue(n);
/* Perform Queue Operations */
char ch;
do{
System.out.println("\nQueue Operations");
System.out.println("1. insert");
System.out.println("2. remove");
System.out.println("3. peek");
System.out.println("4. check empty");
System.out.println("5. check full");
System.out.println("6. size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
try
{
q.insert( scan.nextInt() );
}
catch(Exception e)
{
System.out.println("Error : " +e.getMessage());
}
break;
case 2 :
try
{
System.out.println("Removed Element = "+q.remove());
}
catch(Exception e)
{
System.out.println("Error : " +e.getMessage());
}
break;
case 3 :
try
{
System.out.println("Peek Element = "+q.peek());
}
catch(Exception e)
{
System.out.println("Error : "+e.getMessage());
}
break;
case 4 :
System.out.println("Empty status = "+q.isEmpty());
break;
case 5 :
System.out.println("Full status = "+q.isFull());
break;
case 6 :
System.out.println("Size = "+ q.getSize());
break;
default : System.out.println("Wrong Entry \n ");
break;
}
/* display Queue */
q.display();
System.out.println("\nDo you want to continue (Type y or n) \n");
ch = scan.next().charAt(0);

} while (ch == 'Y'|| ch == 'y');


}
}
Output:
C:\sheela>javac QueueImplement.java
C:\sheela>java QueueImplement
Array Queue Test
Enter Size of Integer Queue
5
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. check full
6. size
1
Enter integer element to insert
5
Queue = 5
Do you want to continue (Type y or n)
y
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. check full
6. size
1
Enter integer element to insert
6
Queue = 5 6
Do you want to continue (Type y or n)
y
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. check full
6. size
1
Enter integer element to insert
8
Queue = 5 6 8
Do you want to continue (Type y or n)
y
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. check full
6. size
5
Full status = false
Queue = 5 6 8
Do you want to continue (Type y or n)
y
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. check full
6. size
6
Size = 3
Queue = 5 6 8
Do you want to continue (Type y or n)
y
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. check full
6. size
2
Removed Element = 5
Queue = 6 8
Do you want to continue (Type y or n)
GENERATING EMPLOYEE PAYROLL DETAILS

PROGRAM:

//For Packages, Folder Name should be employee


//File Name should be Employee.java

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
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 ------------------------------------------------ ");
}
}

//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;
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 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("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 Compile, go to employee folder


javac
Employee.java
javac
Programmer.java
javac
AssistantProfessor.java
javac
AssociateProfessor.java
javac Professor.java

To Compile, it should be outside the package


javac Emp.java

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:

//File Name should be Area.java

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) */
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; /* 3.14*r*r */
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(
);
}
}

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:

//File Name should be UserException.java

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);
}
}
}
NOTE:

To Compile:
javac UserException.java
To Run: java UserException

OUTPUT:
MULTI THREADED APPLICATION

PROGRAM:

//File Name should be Multithread.java

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
{
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());
}
}
}
OUTPUT:
IMPLEMENT THE FILE OPERATIONS
PROGRAM:

//File Name should be FileInfo.java

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
{
System.out.println("The file type is unknown.");}}}}

OUTPUT:
IMPLEMENT THE FEATURES OF GENERICS CLASSES

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.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:
MINI PROJECT - OPAC SYSTEM

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("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());
}

public static void main(String args[])


{
Data d = new
Data(); try
{
Class.forName("sun.jdbc.odbc.JdbcOdbc
Driver"); 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.getSt
ring(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,"Than
k you","My
Application",JOptionPane.QUESTION_MESSAGE);
}
}
}

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. Now your database file gets added to the

System DSN.

Table: Design View


Table Name: stu

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

You might also like