Java Lap Manual
Java Lap Manual
PROGRAM:-
import java.util.Arrays;
class InsertionSort {
void insertionSort(int array[]) {
int size = array.length;
for (int step = 1; step < size; step++) {
int key = array[step];
int j = step - 1;
while (j >= 0 && key < array[j]) {
array[j + 1] = array[j];
--j;
}
array[j + 1] = key;
}
}
public static void main(String args[]) {
int[] data = { 9, 5, 1, 4, 3 };
InsertionSort is = new InsertionSort();
is.insertionSort(data);
System.out.println("Sorted Array in
Ascending Order: ");
System.out.println(Arrays.toString(data));
}
}
OUT PUT:-
Sorted Array in Ascending Order:
[1, 3, 4, 5, 9]
RESULT:-
The above java is executed successfully and
output is verified
//selection short
AIM:- To write the java program to performe
the selection short.
PROGRAM:-
import java.util.Arrays;
class selectioshort {
void selectionSort(int array[]) {
int size = array.length;
System.out.println(Arrays.toString(data));
}
}
OUTPUT:-
Sorted Array in Ascending Order:
[2, 10, 12, 15, 20]
RESULT:-
The above java is executed successfully and
output is verified
RESULT:-
The above java programe is executed
successfully and output is verified
AIM:-TO develop the java program for implementation
of qeue
PROGRAME:-
// Queue implementation in Java
public class Queue {
int SIZE = 5;
int items[] = new int[SIZE];
int front, rear;
Queue() {
front = -1;
rear = -1;
}
Boolean isFull() {
if (front == 0 && rear == SIZE - 1) {
return true;
}
return false;
}
Boolean isEmpty() {
if (front == -1)
return true;
else
return false;
}
void enQueue(int element) {
if (isFull()) {
System.out.println("Queue is full");
} else {
if (front == -1)
front = 0;
rear++;
items[rear] = element;
System.out.println("Inserted " + 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("Deleted -> " + element);
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] + " ");
RESULT:-
The above java program is executed
successfully and output is verified
EX.NO:3 Develop a java application with an Employee class with
Emp_name, Emp_id, Address, Mail_id, Mobile_no as members.
Inherit the classes, Programmer, Assistant Professor, Associate
Professor and Professor from employee class. Add Basic Pay (BP) as
the member of all the inherited classes with 97% of BP as DA, 10 % of
BP as HRA, 12% of BP as PF, 0.1% of BP for staff club funds. Generate
pay slips for the employees with their gross and net salary.
Import java.io.*;
import java.util.Scanner;
class Employee {
String Emp_name;
int Emp_id;
String Address;
String Mail_id;
long Mobile_no;
}
class programmer extends Employee{
int BP;
double net_amount;
void display() {
System.out.println("NAME="+Emp_name);
System.out.println("ID="+Emp_id);
System.out.println("ADDRESS="+Address);
System.out.println("G-MAIL="+Mail_id);
System.out.println("PHONE NO="+Mobile_no);
System.out.println("BASIC_PAY="+BP);
System.out.println("DA="+0.97*BP);
System.out.println("HRA="+0.10*BP);
System.out.println("PF="+0.12*BP);
System.out.println("staff club
funds="+0.001*BP);
net_amount=(0.97*BP)+(0.10*BP)+(0.12*BP)
+(0.001*BP);
System.out.println("Net_salary="+net_amount);
}
}
class Assistant_Professor extends Employee{
int BP;
double net_amount;
void display() {
System.out.println("NAME="+Emp_name);
System.out.println("ID="+Emp_id);
System.out.println("ADDRESS="+Address);
System.out.println("G-MAIL="+Mail_id);
System.out.println("PHONE NO="+Mobile_no);
System.out.println("BASIC_PAY="+BP);
System.out.println("DA"+0.97*BP);
System.out.println("HRA"+0.10*BP);
System.out.println("PF"+0.12*BP);
System.out.println("staff club
funds"+0.001*BP);
net_amount=(0.97*BP)+(0.10*BP)+(0.12*BP)
+(0.001*BP);
System.out.println("Net_salary="+net_amount);
}
}
class Associate_Professor extends Employee{
int BP;
double net_amount;
void display() {
System.out.println("NAME="+Emp_name);
System.out.println("ID="+Emp_id);
System.out.println("ADDRESS="+Address);
System.out.println("G-MAIL="+Mail_id);
System.out.println("PHONE NO="+Mobile_no);
System.out.println("BASIC_PAY="+BP);
System.out.println("DA"+0.97*BP);
System.out.println("HRA"+0.10*BP);
System.out.println("PF"+0.12*BP);
System.out.println("staff club
funds"+0.001*BP);
net_amount=(0.97*BP)+(0.10*BP)+(0.12*BP)
+(0.001*BP);
System.out.println("Net_salary="+net_amount);
}
}
class professor extends Employee{
int BP;
double net_amount;
void display() {
System.out.println("NAME="+Emp_name);
System.out.println("ID="+Emp_id);
System.out.println("ADDRESS="+Address);
System.out.println("G-MAIL="+Mail_id);
System.out.println("PHONE NO="+Mobile_no);
System.out.println("BASIC_PAY="+BP);
System.out.println("DA"+0.97*BP);
System.out.println("HRA"+0.10*BP);
System.out.println("PF"+0.12*BP);
System.out.println("staff club
funds"+0.001*BP);
net_amount=(0.97*BP)+(0.10*BP)+(0.12*BP)
+(0.001*BP);
System.out.println("Net_salary="+net_amount);
}
}
class keerthi{
public static void main(String[] args) {
System.out.println("1.programmer\
n2.Assistant_Professor\n3.Associative_Professor\
n4.professor");
Scanner o=new Scanner(System.in);
int no;
System.out.println("\nenter the person no to print
the details");
no=o.nextInt();
switch(no) {
case 1:
programmer e1=new programmer();
e1.Emp_name="kumar";
e1.Address="chennai";
e1.Mail_id="kumar123@";
e1.Emp_id=123;
e1.Mobile_no=1;
e1.BP=10000;
e1.display();
break;
case 2:
Assistant_Professor e2=new Assistant_Professor();
e2.Emp_name="raja";
e2.Address="madurai";
e2.Mail_id="raja124@";
e2.Emp_id=124;
e2.Mobile_no=2;
e2.BP=20000;
e2.display();
break;
case 3:
Associate_Professor e3=new Associate_Professor();
e3.Emp_name="santhosh";
e3.Address="trichy";
e3.Mail_id="santhosh125@";
e3.Emp_id=125;
e3.Mobile_no=3;
e3.BP=30000;
e3.display();
break;
case 4:
professor e4=new professor();
e4.Emp_name="siva";
e4.Address="dindigul";
e4.Mail_id="siva125@";
e4.Emp_id=125;
e4.Mobile_no=4;
e4.BP=40000;
e4.display();
break;
default:System.out.println("enter the correct no");
}
}}
OUTPUT:-
1.programmer
2.Assistant_Professor
3.Associative_Professor
4.professor
enter the person no to print the details
1
NAME=kumar
ID=123
ADDRESS=chennai
G-MAIL=kumar123@
PHONE NO=1
BASIC_PAY=10000
DA=9700.0
HRA=1000.0
PF=1200.0
staff club funds=10.0
Net_salary=11910.0
RESULT:-
The above java programe is executed
successfully and output is verified
PROGRAM:-
import java.util.*;
abstract class shape
{
int x,y;
abstract void area(double x,double y);
}
class Rectangle extends shape
{
void area(double x,double y)
{
System.out.println("Area of rectangle is :"+(x*y));
}
}
class Circle extends shape
{
void area(double x,double y)
{
System.out.println("Area of circle is :"+(3.14*x*x));
}
}
class Triangle extends shape
{
void area(double x,double y)
{
System.out.println("Area of triangle is :"+(0.5*x*y));
}
}
public class program
{
public static void main(String[] args)
{
Rectangle r=new Rectangle();
r.area(2,5);
Circle c=new Circle();
c.area(5,5);
Triangle t=new Triangle();
t.area(2,5);
}
}
Output:-
Area of rectangle is :10.0
Area of circle is :78.5
Area of triangle is :5.0
RESULT:-
The above java programe is executed
successfully and output is verified
PROGRAM:-
interface shape
{
int x=0,y=0;
void area(double x,double y);
}
class Rectangle implements shape
{
public void area(double x,double y)
{
System.out.println("Area of rectangle is :"+(x*y));
}
}
class Circle implements shape
{
public void area(double x,double y)
{
System.out.println("Area of circle is :"+(3.14*x*x));
}
}
class Triangle implements shape
{
PROGRAM:-
class king
{
public static void main(String args[])
{
int a=10,b=0;
int c=0;
try
{
c=a/b;
}
catch(Exception e)
{
System.out.println("we can not able to
run sorry!!!");
}
System.out.println("c="+c);
System.out.println("the program is
executed");
}
}
OUTPUT:-
we can not able to run sorry!!!
c=0
the program is executed
RESULT:-
The above java programe is executed
successfully and output is verified
EXNO 7:- write a java program that implements a
multi-threaded application that has three threads. First
thread generates a random integer every 1 second and
if the value is even, second thread computes the
square of the number and prints. If the value is odd,
the third thread will print the value of cube of the
number
RESULT:-
The above java programe is executed
successfully and output is verified
AIM:-
TO Write a program to performe file operations.
PROGRAME:-
import java.io.*;
import java.io.IOException;
import java.util.Scanner;
class king {
public static void main(String args[]) throws
IOException
{
File f0 = new File("D:FileOperationExample2.txt");
if (f0.createNewFile()) {
System.out.println("File " + f0.getName() + " is created
successfully.");
} else {
System.out.println("File is already exist in the
directory.");
}
FileWriter fw=new
FileWriter("D:FileOperationExample2.txt");
fw.write("hi welcome to java programe");
fw.close();
System.out.print("name="+f0.getName());
System.out.println("path="+f0.getAbsolutePath());
System.out.println("size of file is:="+f0.length());
String filedata;
Scanner dataReader=new Scanner(f0);
filedata=dataReader.nextLine();
System.out.println(filedata);
}
}
OUTPUT:-
File FileOperationExample2.txt is created successfully.
name=FileOperationExample2.txt
path=D:\\FileOperationExample2.txt
size of file is:=27
hi welcome to java programe
RESULT:-
The above java programe is executed
successfully and output is verified
EXNO 9:-