REG NO:622423104050
THAMIZHTHENDRAL K
PROGRAM:
public class linear
{
public static int linear(int[] arr,int key)
{
for(int i=0;i<arr.length;i++)
{
if(arr[i]==key)
{
return i;
}}
return -1;
}j
public static void main(String a[])
{
int []a1={10,20,30,40,50,70,90};
int key=50;
System.out.println(key +"is found at index:" +linear(a1,key));
}}
REG NO:622423104050
THAMIZHTHENDRAL K
OUTPUT:
50is found at index:4
REG NO:622423104050
THAMIZHTHENDRAL K
PROGRAM:
class binarys
{
public static void binarys(int arr[], int first, int last, int key)
{
int mid=(first+last)/2;
while(first<=last)
{
if(arr [mid]<key)
{
first=mid+1;
}
else if(arr [mid]==key)
{
System.out.println("Element is found at index:"+mid);
break;
}
else{
last=mid-1;
}
mid=(first+last)/2;
}
if (first>last){
System.out.println("Element is not found !");
}}
public static void main(String args[])
{
int arr[]={10,20,30,40,50};
int key=30;
int last=arr.length-1;
binarys(arr,0,last,key);
}}
REG NO:622423104050
THAMIZHTHENDRAL K
OUTPUT:
Element is found at index:2
REG NO:622423104050
THAMIZHTHENDRAL K
PROGRAM:
public class select
{
public static void main(String args[])
{
int array[]={10,20,25,63,96,57};
int size=array.length;
for(int i=0;i<size-1;i++)
{
int min=i;
for(int j=i+1;j<size;j++)
{
if (array [j]<array[min])
{
min=j;
}}
int temp=array[min];
array[min]=array[i];
array[i]=temp;
}
for(int i=0;i<size;i++)
{
System.out.println(" "+array[i]);
}}}
REG NO:622423104050
THAMIZHTHENDRAL K
OUTPUT:
10
20
25
57
63
96
REG NO:622423104050
THAMIZHTHENDRAL K
PROGRAM:
import java.util.*;
public class insert
{
public static void main(String args[])
{
int []numArray={10,6,15,4,1,45};
System.out.println("original array:"+Arrays.toString(numArray));
for(int k=1;k<numArray.length-1;k++)
{
int temp=numArray[k];
int j=k-1;
while(j>=0 && temp <=numArray[j])
{
numArray[j=1]=numArray[j];
j=j-1;
}
numArray[j+1]=temp;
}
System.out.println("Sorted array:"+Arrays.toString(numArray));
}}
REG NO:622423104050
THAMIZHTHENDRAL K
OUTPUT:
Original Array:[10, 6, 15, 4, 1, 45]
Sorted Array:[1, 4, 6, 10, 15, 45]
REG NO:622423104050
THAMIZHTHENDRAL K
PROGRAM:
import java.util.Stack;
public class Loki {
public static void main(String[] args) {
Stack stack=new Stack();
stack.push("A");
stack.push("B");
stack.push("C");
stack.push("D");
System.out.println("Top element is:"+stack.peek());
stack.pop();
stack.pop();
System.out.println("The stack size is:"+stack.size());
if(stack.empty())
{
System.out.println("The stack is empty");
}
else
{
System.out.println("The stack in not empty");
}
}
}
REG NO:622423104050
THAMIZHTHENDRAL K
OUTPUT:
Top element is:D
The stack size is:2
The stack in not empty
REG NO:622423104050
THAMIZHTHENDRAL K
PROGRAM:
import java.util.LinkedList;
import java.util.Queue;
class queue
{
public static void main(String args[])
{
Queue<String> queue=new LinkedList<String>();
queue.add("A");
queue.add("B");
queue.add("C");
queue.add("D");
System.out.println("the front element is:"+queue.peek());
queue.remove();
queue.remove();
System.out.println("the front element is:"+queue.peek());
System.out.println("the queue size is:"+queue.size());
if(queue.isEmpty())
{
System.out.println("the queue is empty");
}
else{
System.out.println("the queue is not empty");
}}}
REG NO:622423104050
THAMIZHTHENDRAL K
OUTPUT:
the front element is:A
the front element is:C
the queue size is:2
the queue is not empty
REG NO:622423104050
THAMIZHTHENDRAL K
PROGRAM:
import java.util.Scanner;
class Employee {
public String empName, address, mailId, mobile, empId;
public double da, hra, pf, sc, gross, net;
Scanner scan = new Scanner(System.in);
void getEmpDetails() {
System.out.print("Enter Name: ");
empName = scan.nextLine();
System.out.print("Enter ID: ");
empId = scan.nextLine();
System.out.print("Enter Address: ");
address = scan.nextLine();
System.out.print("Enter Mail ID: ");
mailId = scan.nextLine();
System.out.print("Enter Mobile: ");
mobile = scan.nextLine();
}
void eval(float salary) {
da = 0.97 * salary;
hra = 0.1 * salary;
pf = 0.12 * salary;
sc = 0.001 * salary;
gross = da + hra + pf;
net = gross - pf - sc;
}
void disp() {
System.out.println("==========================================");
System.out.println(">>>>>>>>>>>>>>>>> Pay Slip <<<<<<<<<<<<<<<");
System.out.println("Name: " + empName);
System.out.println("ID: " + empId);
System.out.println("Address: " + address);
System.out.println("Mail ID: " + mailId);
System.out.println("Mobile: " + mobile);
System.out.println("DA: " + da);
System.out.println("HRA: " + hra);
System.out.println("PF: " + pf);
System.out.println("Staff Club: " + sc);
System.out.println("Gross Salary: " + gross);
System.out.println("Net Salary: " + net);
System.out.println("==========================================");
}}
REG NO:622423104050
THAMIZHTHENDRAL K
class EmployeeRole extends Employee {
EmployeeRole() {
super.getEmpDetails();
System.out.print("Enter Your Salary: ");
int salary = scan.nextInt();
super.eval(salary);
super.disp();
}}
class Programmer extends EmployeeRole {
}
class AsstProf extends EmployeeRole {
}
class AssoProf extends EmployeeRole {
}
class Prof extends EmployeeRole {
}
public class EmployeePaySlip {
public static void main(String args[]) {
int con;
Scanner scan = new Scanner(System.in);
do {
System.out.println("\tMain Menu");
System.out.println("==========================================");
System.out.println("Your Designation:");
System.out.println("\t1. Programmer\n\t2. Asst. Professor\n\t3. Asso.
Professor\n\t4. Professor");
System.out.println("==========================================");
System.out.print("Enter Your Choice: ");
int ch = scan.nextInt();
switch(ch)
{
case 1:
{Programmer p=new Programmer(); break; }
case 2:
{AsstProf asp=new AsstProf(); break; }
case 3:
{AssoProf aop=new AssoProf(); break; }
case 4:
{Prof pr=new Prof(); break; }
}
System.out.print("Do you want to continue..?1:0 :");
con=scan.nextInt();
}
while(con!=0);
}}
REG NO:622423104050
THAMIZHTHENDRAL K
OUTPUT:
Main Menu
==========================================
Your Designation:
1. Programmer
2. Asst. Professor
3. Asso. Professor
4. Professor
==========================================
Enter Your Choice: 1
Enter Name: xyz
Enter ID: 2024
Enter Address: 12\2 small street
Enter Mail ID: [email protected]
Enter Mobile: 1234567890
Enter Your Salary: 12000
==========================================
>>>>>>>>>>>>>>>>> Pay Slip <<<<<<<<<<<<<<<
Name: xyz
ID: 2024
Address: 12\2 small street
Mail ID: [email protected]
Mobile: 1234567890
DA: 11640.0
HRA: 1200.0
PF: 1440.0
Staff Club: 12.0
Gross Salary: 14280.0
Net Salary: 12828.0
==========================================
Do you want to continue..?1:0 :0
REG NO:622423104050
THAMIZHTHENDRAL K
PROGRAM:
import java.util.*;
abstract class shape
{
Scanner scan=new Scanner(System.in);
abstract void printArea();
}
class rectangle extends shape
{
double area,l,b;
void printArea()
{
System.out.print("Enter Length: ");
l=scan.nextDouble();
System.out.print("Enter Breadth: ");
b=scan.nextDouble();
area=l*b;
System.out.println("Area of Rectangle = "+area);
}
}
class triangle extends shape
{
double area,l,h;
void printArea()
{
System.out.print("Enter Length: ");
l=scan.nextDouble();
System.out.print("Enter Height: ");
h=scan.nextDouble();
area=0.5*l*h;
System.out.print("Area of Triangle = "+area);
}
}
class circle extends shape
{
double area,rad;
void printArea()
{
System.out.print("Enter Radius: ");
rad=scan.nextDouble();
area=3.14*rad*rad;
System.out.println("Area of Circle = "+area);
}}
REG NO:622423104050
THAMIZHTHENDRAL K
class AbsShape
{
public static void main(String agrs[])
{
int con;
Scanner scan=new Scanner(System.in);
do
{
System.out.println("\t\tMain Menu");
System.out.println("******************************************");
System.out.println("1.Rectangle\n2.Triangle\n3.Circle\n");
System.out.println("******************************************");
System.out.print("Enter your Choice: ");
int ch=scan.nextInt();
switch(ch)
{
case 1:
System.out.println("\tRECTANGLE");
shape r=new rectangle();
r.printArea();
break;
case 2:
System.out.println("\tTRIANGLE");
shape t=new triangle();
t.printArea();
break;
case 3:
System.out.println("\tCIRCLE");
shape c=new circle();
c.printArea();
break;
default:
System.out.println("Invalid choice");
}
System.out.println("\n******************************************");
System.out.print("Do you want continue again..?1:0 :");
con=scan.nextInt();
}
while(con!=0);
}
}
REG NO:622423104050
THAMIZHTHENDRAL K
OUTPUT:
Main Menu
******************************************
1.Rectangle
2.Triangle
3.Circle
******************************************
Enter your Choice: 3
CIRCLE
Enter Radius: 10
Area of Circle = 314.0
******************************************
Do you want continue again..?1:0 :0
REG NO:622423104050
THAMIZHTHENDRAL K
PROGRAM:
Import java.util.*;
interface Shape
{
Scanner scan=new Scanner(System.in);
void disp();
}
class Triangle implements Shape
{
double b,h,area;
public void disp()
{
System.out.print("Enter base : ");
b=scan.nextDouble();
System.out.print("Enter height : ");
h=scan.nextDouble();
area=0.5*b*h;
System.out.print("Area of Triangle : "+area);
}
}
class Rectangle implements Shape
{
double l,b,area;
public void disp()
{
System.out.print("Enter length : ");
l=scan.nextDouble();
System.out.print("Enter breadth : ");
b=scan.nextDouble();
area=l*b;
System.out.print("Area of Rectangle : "+area);
}
}
class Circle implements Shape
{
double r,area;
public void disp()
{
System.out.print("Enter radius : ");
r=scan.nextDouble();
area=3.14*r*r;
System.out.print("Area of Circle : "+area);
}}
REG NO:622423104050
THAMIZHTHENDRAL K
class InterfaceShape
{
public static void main(String args[])
{
int ch;
do
{
Scanner scan=new Scanner(System.in);
System.out.println("\n**************************************");
System.out.println("\t\tMain Menu");
System.out.println("**************************************");
System.out.println("\t1.Triangle\n\t2.Rectangle\n\t3.Circle");
System.out.println("**************************************");
System.out.print("\nEnter your Choice : ");
int op=scan.nextInt();
switch(op)
{
case 1:
{ Triangle ob=new Triangle();
ob.disp();
break; }
case 2:
{ Rectangle ob=new Rectangle();
ob.disp();
break; }
case 3:
{ Circle ob=new Circle();
ob.disp();
break; }
default:
System.out.println("Invalid...!");
}
System.out.print("\n\nDo you want to continue again? 1 : 0 : ");
ch=scan.nextInt();
}
while(ch!=0);
}
}
REG NO:622423104050
THAMIZHTHENDRAL K
OUTPUT:
**************************************
Main Menu
**************************************
1.Triangle
2.Rectangle
3.Circle
**************************************
Enter your Choice : 3
Enter radius : 10
Area of Circle : 314.0
Do you want to continue again? 1 : 0 : 0
REG NO:622423104050
THAMIZHTHENDRAL K
PROGRAM:
import java.util.*;
import java.lang.*;
class MarkException extends Exception
{ MarkException(String a)
{
super(a);
}}
class students
{ public static void main(String args[])
{
int ch;
do
{ Scanner scan=new Scanner(System.in);
System.out.println("\n ");
System.out.println("\t Student Grade System");
System.out.println("\n ");
System.out.println("Enter student name:");
String s_name=scan.next();
System.out.println("Enter your mark:");
int m=scan.nextInt();
try
{
if(m>0 && m<=100)
{
if(m>90 && m<=100)
{ System.out.println("\'A\' Grade");
System.out.println("Excellent...!!!");
}
else if(m>80 && m<=90)
{
System.out.println("\'B\' Grade");
System.out.println("Very Good..!!");
}
else if(m>70 && m<=80)
{
System.out.println("\'C\' Grade");
System.out.println(" Good.!");
}
else if(m>60 && m<=70)
{
System.out.println("\'D\' Grade");
System.out.println("Satisfactory..!");
}
REG NO:622423104050
THAMIZHTHENDRAL K
else
{
System.out.println("\'U\' Grade");
System.out.println("You are failed...");
}
}else
throw new MarkException("Invalid Mark..!");
}
catch(MarkException e)
{
System.out.println("Exception Occurred..!");
}
finally
{
System.out.println("Thank you..!!!");
}
System.out.println("\n ");
System.out.println("\n Do you want to continue for next student..? 1:0 ::");
ch=scan.nextInt();
}
while(ch==1);
}}
REG NO:622423104050
THAMIZHTHENDRAL K
OUTPUT:
Student Grade System
Enter student name:
xyz
Enter your mark:
75
'C' Grade
Good.!
Thank you..!!!
Do you want to continue for next student..? 1:0 :: 0
REG NO:622423104050
THAMIZHTHENDRAL K
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)
{
this.x=x;
}
public void run()
{
System.out.println("New Thread "+x+"is ODD and cube of "+x+"is:"+x*x*x);
}}
class A 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 and generated number is:"+num);
if(num % 2==0)
{
Thread t1=new Thread(new even(num));
t1.start();
}
REG NO:622423104050
THAMIZHTHENDRAL K
else
{
Thread t2=new Thread(new odd(num));
t2.start();
}
Thread.sleep(1000);
System.out.println("
");
}}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}}}
public class ThreeThreads
{public static void main(String args[])
{
A a=new A();
a.start();
}}
REG NO:622423104050
THAMIZHTHENDRAL K
OUTPUT:
Main Thread and generated number is:35
New Thread 35is ODD and cube of 35is:42875
Main Thread and generated number is:32
New Thread 32is EVEN and square of 32is:1024
Main Thread and generated number is:56
New Thread 56is EVEN and square of 56is:3136
Main Thread and generated number is:54
New Thread 54is EVEN and square of 54is:2916
Main Thread and generated number is:17
New Thread 17is ODD and cube of 17is:4913
REG NO:622423104050
THAMIZHTHENDRAL K
PROGRAM:
class MyClass <T extends Comparable<T>>
{
T[] vals;
MyClass(T[]obj)
{
vals=obj;
}
public T min()
{
T v=vals[0];
for(int i=1;i<vals.length;i++)
if(vals[i].compareTo(v)<0)
v=vals[i];
return v;
}
public T max()
{
T v=vals[0];
for(int i=1;i<vals.length;i++)
if(vals[i].compareTo(v)>0)
v=vals[i];
return v;
}}
class GenericsDemo
{
public static void main(String args[])
{
Integer num[]={10,2,5,4,6,1};
Character ch[]={'v','p','s','a','n','h'};
Double d[]={20.2,45.4,71.6,88.3,54.6,10.4};
String str[]={"hai","how","are","you"};
MyClass<Integer>iob=new MyClass<Integer>(num);
MyClass<Character>cob=new MyClass<Character>(ch);
MyClass<Double>dob=new MyClass<Double>(d);
MyClass<String>sob=new MyClass<String>(str);
System.out.println("Max value in num:"+iob.max());
System.out.println("Min vakue of num:"+iob.min());
System.out.println("Max value in ch:"+cob.max());
System.out.println("Min vakue of ch:"+cob.min());
System.out.println("Max value in d:"+dob.max());
System.out.println("Min vakue of d:"+dob.min());
System.out.println("Max value in str:"+sob.max());
System.out.println("Min vakue of str:"+sob.min());
}}
REG NO:622423104050
THAMIZHTHENDRAL K
OUTPUT:
Max value in num:10
Min vakue of num:1
Max value in ch:v
Min vakue of ch:a
Max value in d:88.3
Min vakue of d:10.4
Max value in str:you
Min vakue of str:are
REG NO:622423104050
THAMIZHTHENDRAL K
PROGRAM:
import java.io.*;
class copyfilez {
public static void main(String args[]) throws IOException {
int i;
FileInputStream fin = null;
FileOutputStream fout = null;
if (args.length != 2) {
System.out.println("Usage: CopyFile <source>
<destination>");
return;
}
System.out.println("Displaying contents of " + args[0] + "\n");
try {
fin = new FileInputStream(args[0]);
do {
i = fin.read();
if (i != -1)
System.out.print((char) i);
} while (i != -1);
} catch (IOException e) {
System.out.println("Error Reading File: " + e.getMessage());
} finally {
try {
if (fin != null) fin.close();
} catch (IOException e) {
System.out.println("Error Closing File: " + e.getMessage());
}
}
System.out.println("\nCopying contents of " + args[0] + " to "
+ args[1] + "\n");
try {
fin = new FileInputStream(args[0]);
fout = new FileOutputStream(args[1]);
do {
i = fin.read();
if (i != -1) fout.write(i);
} while (i != -1);
} catch (IOException e) {
System.out.println("I/O Error: " + e.getMessage());
}
REG NO:622423104050
THAMIZHTHENDRAL K
finally {
try {
if (fin != null) fin.close();
} catch (IOException e2) {
System.out.println("Error Closing Input File: " +
e2.getMessage());
}
try {
if (fout != null) fout.close();
} catch (IOException e2) {
System.out.println("Error Closing Output File: " +
e2.getMessage());
}
}
System.out.println("\nFile Copied\n");
System.out.println("\nDisplaying contents of " + args[1] +
"\n");
try {
fin = new FileInputStream(args[1]);
do {
i = fin.read();
if (i != -1)
System.out.print((char) i);
} while (i != -1);
} catch (IOException e) {
System.out.println("Error Reading File: " + e.getMessage());
} finally {
try {
if (fin != null) fin.close();
} catch (IOException e) {
System.out.println("Error Closing File: " + e.getMessage());
}
}
}
}
REG NO:622423104050
THAMIZHTHENDRAL K
OUTPUT:
Displaying contents of first1.txt
hello world
Copying contents of first1.txt to second2.txt
File Copied
Displaying contents of second2.txt
hello world