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

OOps Record

This document is a laboratory record for the CS3381 Object Oriented Programming course at Ponjesly College of Engineering, detailing various experiments conducted during the academic year 2022-2023. It includes a list of experiments such as searching algorithms, sorting algorithms, and data structure implementations using Java. Each experiment outlines the aim, algorithm, program code, and results of the execution.

Uploaded by

sherin vijaya
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)
14 views

OOps Record

This document is a laboratory record for the CS3381 Object Oriented Programming course at Ponjesly College of Engineering, detailing various experiments conducted during the academic year 2022-2023. It includes a list of experiments such as searching algorithms, sorting algorithms, and data structure implementations using Java. Each experiment outlines the aim, algorithm, program code, and results of the execution.

Uploaded by

sherin vijaya
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/ 58

PONJESLY COLLEGE OF ENGINEERING

(Affiliated to Anna University, Chennai.)

CS3381 OBJECT ORIENTED PROGRMMING


LABORATORY RECORD

Certified that this is the bonafide record of the work done by


…………………………………………… Register No ……………………............... in the
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING, PONJESLY
COLLEGE OF ENGINEERING, Nagercoil during the academic year 2022-2023.

Head of the Department Staff In-Charge

Submitted for the Anna University Practical Examination, held at PONJESLY


COLLEGE OF ENGINEERING, Nagercoil on …………………………………………………

Internal Examiner External Examiner


LIST OF EXPERIMENTS

SL. DATE NAME OF THE EXPERIMENT PAGE MARKS SIGNATURE


NO NO

1a SEQUENTIAL SEARCH

1b BINARY SEARCH

1c SELECTION SORT

1d INSERTION SORT

DEVELOP STACK DATA STRUCTURES USING


2a CLASSES AND OBJECTS

DEVELOP QUEUE DATA STRUCTURES USING


2b CLASSES AND OBJECTS

3 GENERATE PAYSLIP USING INHERITANCE

4 CALCULATE AREA USING ABSTRACT CLASS

5 CALCULATE AREA USING INTERFACE

6 CREATION OF USER DEFINED EXCEPTION

7 CREATION OF USER DEFINED EXCEPTION

8 PROGRAM FOR DISPLAYING FILE INFORMATION

PROGRAM TO FIND THE MAXIMUM VALUE


9 FROM THE GIVEN TYPE OF ELEMENTS
USING GENERICS

10a JAVAFX CONTROLS AND LAYOUTS

10b JAVAFX MENUS

PROGRAM TO IMPLEMENT CURRENCY


11 CONVERTER, DISTANCECONVERTER AND TIME
CONVERTER USING PACKAGES
Ex No : 1 a. LINEAR SEARCH
Date :

AIM
To write a java program to implement linear searching algorithm.

ALGORITHM

Step1: Start with the first item in the list

Step2: Compare the current item to the key


Step 3: If the current value matches the key then update with the location
Step 4: Else repeat from 2.

PROGRAM

import java.util.*;
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)
{
System.out.println(search + " is present at location " + (c + 1) + ".");
break;
}
}
if (c == n)
System.out.println(search + " isn't present in array.");
}
}

RESULT
Thus the java program to perform linear searching algorithms was executed and verified successfully.
EX NO : 1 b BINARY SEARCH
Date :
AIM

To write a java program to implement binary searching algorithm.

ALGORITHM

Step1: Choose the middle element in the list


Step2: If it matches the middle element, its position in the list is returned.
Step 3: If the target value is less than or greater than the middle element, the search
continues in the lower or upper half of the array, respectively, eliminating the other half
from consideration
PROGRAM
import java.util.Scanner;
class BinarySearchExample
{
public static void main(String args[])
{
int counter, num, item, array[], first, last, middle;
Scanner input = new Scanner(System.in);
System.out.println("Enter number of elements:");
num = input.nextInt();
array = new int[num];
System.out.println("Enter " + num + " integers");
for (counter = 0; counter < num; counter++)
array[counter] = input.nextInt();
System.out.println("Enter the search value:");
item = input.nextInt();
first = 0;
last = num - 1;
middle = (first + last)/2;
while( first <= last )
{
if ( array[middle] < item )
first = middle + 1;
else if ( array[middle] == item )
{
System.out.println(item + " found at location " + (middle + 1) + ".");
break;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if ( first > last )
System.out.println(item + " is not found.\n");
}
}

RESULT:

Thus the java program to perform binary searching algorithms was executed and verified successfully.
Ex No : 1 c SELECTION SORT
Date :

AIM
To write a java program to implement selection sorting algorithm.

ALGORITHM

Step 1 − Set Min_Index to 0


Step 2 − Search for the smallest element in the array
Step 3 − Swap with value with the element at the Min_Index

Step 4 − Increment Min_Index to point to next element


Step 5 − Repeat until the complete array is sorted
PROGRAM
import java.util.Scanner;
public class SelectionSortExample
{

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

RESULT:

Thus the java program to perform selection sorting algorithms was executed and verified
successfully.
Ex No:1 d INSERTION SORT
Date :

AIM
To write a java program to implement insertion sorting algorithm.

ALGORITHM

Step1: Choose the first two elements in the list

Step2: If it is sorted leave as it is


Step 3: If not swap the element with the previous
Step 4: Check whether the elements in the sub list is sorted

Step 5: Repeat until list is sorted

PROGRAM
import java.util.Scanner;
public class CodesCracker

{
public static void main(String[] args)

{
int n, i, j, element;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the Size of Array: ");
n = scan.nextInt();
int[] arr = new int[n]; System.out.print("Enter " +n+ " Elements: ");
for(i=0; i<n; i++)

arr[i] = scan.nextInt();
for(i=1; i<n; i++)
{
element = arr[i];
for(j=(i-1); j>=0 && arr[j]>element; j--)
arr[j+1] = arr[j];
arr[j+1] = element;
}
System.out.println("\nThe new sorted array is: ");
for(i=0; i<n; i++)

System.out.print(arr[i]+ " ");


}
}

RESULT:

Thus the java program to perform selection sorting algorithms was executed and verified
successfully.
Ex No: 2a DEVELOP STACK DATA STRUCTURES USING CLASSES AND OBJECTS
Date :

AIM
To develop a stack program in java using classes and objects.

ALGORITHM

1. Create class stack and declare methods for push and pop.
2. Check whether the stack is full. If the stack is full, displays stack is full
3. If the stack is not full, increment the top pointer.
4. Add data element to the stack location where top is pointing.
5. If the stack is not empty, accesses the data element at which top is pointing.
6. Decrement the value of top by 1.
7. Return success.

PROGRAM
import java.io.*;
import java.util.*;
public class Stack1
{
static final int MAX=5;
int top=-1;

int[] stack=new int[MAX];


public static void main(String args[])

{
int option;
Scanner sc = new Scanner(System.in);
Stack1 s = new Stack1();
while(true)
{
System.out.println("Enter the choice You want to perform on the stack: ");
System.out.println(" 1. push \n 2. Pop \n 3. Display \n 4. peep \n 5. Exit");
System.out.println("Enter your option: ");

option = sc.nextInt();
switch(option)
{
case 1:
System.out.println("Enter the element you want to push into the stack: ");
s.push(sc.nextInt());

break;
case 2:
s.pop();
break;
case 3:

System.out.println("Displaying the stack contents: ");


s.display();
break;
case 4:

System.out.println("The top element in the stack is: ");


s.peep();
break;
case 5:

System.out.println("You selected Exit!!");


break;
default:
System.out.println("Wrong choice!! Please enter a valid option!!");
return;
}
}
}
public void push(int val)
{

if(top==MAX-1)
{
System.out.println("Stack is FULL!");
}
else
{

top++;
stack[top]=val;
System.out.println("Element added to the stack is: "+val);
display();

}
}

public void pop()


{
int x;
if(top==-1)

{
System.out.println("Stack is EMPTY!");
}

else
{
x=stack[top];
System.out.println("The element deleted from the stack is: "+x);
top--;
display();
}
}
public void peep()

{
int n;
n=stack[top];
System.out.println("The value at the top of the stack is: "+n);
}
public void display()

{
int i;
if(top==-1)
System.out.println("STACK IS EMPTY!");
else
{
for(i=0; i<=top; i++)
System.out.println("The elements in the stack are: "+stack[i]);
}
}
}

RESULT
Thus the java program to perform Stack data structures using classes and objects is
executed and verified successfully.
Ex No: 2b DEVELOP QUEUE DATA STRUCTURES USING CLASSES AND OBJECTS
Date :

AIM
To write a java program to implement queue data structures using classes and objects.

ALGORITHM

1. Initialize the array variable for storing elements and declare the required variable.
2. Define a function to insert, delete, and display the data items.
3. For insert function, get the new elements, if the rear is greater than array size, display queue is
“overflow” otherwise, insert the new element and increment the top pointer.
4. For delete function, if the queue is empty, then display “queue is overflow” otherwise decrement
the top pointer.
5. For display function, apply the loop to display queue elements.

PROGRAM
import java.util.Random;
import java.util.Scanner;
class Que

{
private int size;
private int front = -1;
private int rear = -1;
private Integer[] queArr;
public Que(int size) {
this.size = size;
queArr = new Integer[size];
}
public void insert(int item)
{
if(rear == size-1)
{

System.out.println("queue is overflowing");
}
else if(front==-1) {
rear++;
queArr[rear] = item;
front = rear;
}
else
{

rear++;
queArr[rear] = item;
}
}
public void delete()
{
if(front == -1) {
System.out.println("queue is underflow");
}
else if(front==rear) {
System.out.println("removing "+queArr[front]);
queArr[front] = null;

front--;
rear--;
}
else
{
System.out.println("removing "+queArr[front]);
queArr[front] = null;
for(int i=front+1;i<=rear;i++) {
queArr[i-1]=queArr[i];
}
rear--;
}
}

public void display() {


if(front==-1)
System.out.println("queue is empty");
else

{
System.out.println("queue is:");
for(int i=front;i<=rear;i++) {
System.out.print(queArr[i]+"\t");

}
}
}
}
public class TestQueue {
public static void main(String[] args)
{
System.out.println("Hi user!");

Scanner scan = new Scanner(System.in);


System.out.println("please enter size of queue array");
int size = scan.nextInt();
Que que = new Que(size);
char ch;
do{
System.out.println("\nQueue operations \n");
System.out.println("1. insert");
System.out.println("2. delete");
System.out.println("3. random");

int choice = scan.nextInt();


switch(choice)
{
case 1:
System.out.println("enter integer element to insert");
que.insert(scan.nextInt());

break;
case 2:
que.delete();
break;
case 3:
Random rand = new Random();
rand.nextInt(size);
break;
}
que.display();
System.out.println("\nDo you want to continue (Type y or n) \n");
ch = scan.next().charAt(0);
}while(!(ch=='N' || ch=='n'));
}
}

RESULT
Thus the java program to perform Queue data structures using classes and objects is
executed and verified successfully.
Ex No: 3 GENERATE PAYSLIP USING INHERITANCE
Date :

AIM

To develop a java application to generate pay slip for different category of employees using
the concept of inheritance.

ALGORITHM

1. Create the class employee with name, Empid, address, mailid, mobileno as members.
2. Inherit the classes programmer, asstprofessor,associateprofessor and professor from
employee class.
3. Add Basic Pay (BP) as the member of all the inherited classes.
4. Calculate DA as 97% of BP, HRA as 10% of BP, PF as 12% of BP, Staff club fund as 0.1% of
BP.
5. Calculate gross salary and net salary.
6. Generate payslip for all categories of employees.
7. Create the objects for the inherited classes and invoke the necessary methods to display the
Payslip.
PROGRAM
import java.util.*;
class employee
{
int empid;
long mobile;
String name, address, mailid;
Scanner get = new Scanner(System.in);
void getdata()
{
System.out.println("Enter Name of the Employee");
name = get.nextLine();
System.out.println("Enter Mail id");
mailid = get.nextLine();
System.out.println("Enter Address of the Employee:");
address = get.nextLine();
System.out.println("Enter employee id ");
empid = get.nextInt();
System.out.println("Enter Mobile Number");
mobile = get.nextLong();
}
void display()
{
System.out.println("Employee Name: "+name);
System.out.println("Employee id : "+empid);
System.out.println("Mail id : "+mailid);
System.out.println("Address: "+address);
System.out.println("Mobile Number: "+mobile);
}
}
class programmer extends employee
{
double salary,bp,da,hra,pf,club,net,gross;
void getprogrammer()
{
System.out.println("Enter basic pay");
bp = get.nextDouble();
}
void calculateprog()
{
da=(0.97*bp);
hra=(0.10*bp);
pf=(0.12*bp);
club=(0.1*bp);
gross=(bp+da+hra);
net=(gross-pf-club);
System.out.println("************************************************");
System.out.println("PAY SLIP FOR PROGRAMMER");
System.out.println("************************************************");
System.out.println("Basic Pay:Rs"+bp);
System.out.println("DA:Rs"+da);
System.out.println("PF:Rs"+pf);
System.out.println("HRA:Rs"+hra);
System.out.println("CLUB:Rs"+club);
System.out.println("GROSS PAY:Rs"+gross);
System.out.println("NET PAY:Rs"+net);
}
}
class asstprofessor extends employee
{
double salary,bp,da,hra,pf,club,net,gross;
void getasst()
{
System.out.println("Enter basic pay");
bp = get.nextDouble();
}
void calculateasst()
{
da=(0.97*bp);
hra=(0.10*bp);
pf=(0.12*bp);
club=(0.1*bp);
gross=(bp+da+hra);
net=(gross-pf-club);
System.out.println("************************************************");
System.out.println("PAY SLIP FOR ASSISTANT PROFESSOR");
System.out.println("************************************************");
System.out.println("Basic Pay:Rs"+bp);
System.out.println("DA:Rs"+da);
System.out.println("HRA:Rs"+hra);
System.out.println("PF:Rs"+pf);
System.out.println("CLUB:Rs"+club);
System.out.println("GROSS PAY:Rs"+gross);
System.out.println("NET PAY:Rs"+net);
}
}
class associateprofessor extends employee
{
double salary,bp,da,hra,pf,club,net,gross;
void getassociate()
{
System.out.println("Enter basic pay");
bp = get.nextDouble();
}
void calculateassociate()
{
da=(0.97*bp);
hra=(0.10*bp);
pf=(0.12*bp);
club=(0.1*bp);
gross=(bp+da+hra);
net=(gross-pf-club);
System.out.println("************************************************");
System.out.println("PAY SLIP FOR ASSOCIATE PROFESSOR");
System.out.println("************************************************");
System.out.println("Basic Pay:Rs"+bp);
System.out.println("DA:Rs"+da);
System.out.println("HRA:Rs"+hra);
System.out.println("PF:Rs"+pf);
System.out.println("CLUB:Rs"+club);
System.out.println("GROSS PAY:Rs"+gross);
System.out.println("NET PAY:Rs"+net);
}
}
class professor extends employee
{
double salary,bp,da,hra,pf,club,net,gross;
void getprofessor()
{
System.out.println("Enter basic pay");
bp = get.nextDouble();
}
void calculateprofessor()
{
da=(0.97*bp);
hra=(0.10*bp);
pf=(0.12*bp);
club=(0.1*bp);
gross=(bp+da+hra);
net=(gross-pf-club);
System.out.println("************************************************");
System.out.println("PAY SLIP FOR PROFESSOR");
System.out.println("************************************************");
System.out.println("Basic Pay:Rs"+bp);
System.out.println("DA:Rs"+da);
System.out.println("HRA:Rs"+hra);
System.out.println("PF:Rs"+pf);
System.out.println("CLUB:Rs"+club);
System.out.println("GROSS PAY:Rs"+gross);
System.out.println("NET PAY:Rs"+net);
}
}
class salary
{
public static void main(String args[])
{
int choice,cont;
do
{
System.out.println("PAYROLL");

System.out.println("1.PROGRAMMER\t 2.ASSISTANT PROFESSOR\t 3.ASSOCIATE


PROFESSOR\t 4.PROFESSOR");

Scanner c = new Scanner(System.in);

choice=c.nextInt();
switch(choice)
{
case 1:
{
programmer p=new programmer();
p.getdata();
p.getprogrammer();
p.display();
p.calculateprog();
break;
}
case 2:
{
asstprofessor asst=new asstprofessor();
asst.getdata();
asst.getasst();
asst.display();
asst.calculateasst();
break;
}
case 3:
{
associateprofessor asso=new associateprofessor();
asso.getdata();
asso.getassociate();
asso.display();
asso.calculateassociate();
break;
}
case 4:
{
professor prof=new professor();
prof.getdata();
prof.getprofessor();
prof.display();
prof.calculateprofessor();
break;
}
}
System.out.println("Do u want to continue 0 to quit and 1 to continue ");
cont=c.nextInt();
}while(cont==1);
}
}

RESULT
Thus the java application to generate pay slip for different category of employees was
implemented using inheritance and the program was executed successfully.
Ex No : 4 CALCULATE AREA USING ABSTRACT CLASS
Date :

AIM

To write a java program to calculate the area of rectangle, circle and triangle using the
concept of abstract class.

ALGORITHM
1. Create an abstract class named shape that contains two integers and an empty method named
printarea().
2. Provide three classes named rectangle, triangle and circle such that each one of the classes
extends the class Shape.
3.Each of the inherited class from shape class should provide the implementation for the method
printarea().
4.Get the input and calculate the area of rectangle,circle and triangle .
5. In the shapeclass , create the objects for the three inherited classes and invoke the methods and
display the area values of the different shapes.
PROGRAM
import java.util.*;
abstract class shape
{
int a,b;
abstract public void printarea();

}
class rectangle extends shape
{
public int area_rect;
public void printarea()
{
Scanner s=new Scanner(System.in);
System.out.println("enter the length and breadth of rectangle");
a=s.nextInt();
b=s.nextInt();
area_rect=a*b;

System.out.println("Length of rectangle "+a +"breadth of rectangle "+b);


System.out.println("The area ofrectangle is:"+area_rect);
}
}
class triangle extends shape

{
double area_tri;
public void printarea()
{
Scanner s=new Scanner(System.in);
System.out.println("enter the base and height of triangle");
a=s.nextInt();

b=s.nextInt();
System.out.println("Base of triangle "+a +"height of triangle "+b);
area_tri=(0.5*a*b);

System.out.println("The area of triangle is:"+area_tri);


}
}

class circle extends shape


{
double area_circle;
public void printarea()

{
Scanner s=new Scanner(System.in);
System.out.println("enter the radius of circle");
a=s.nextInt();
area_circle=(3.14*a*a);
System.out.println("Radius of circle"+a);
System.out.println("The area of circle is:"+area_circle);
}

}
public class shapeclass
{

public static void main(String[] args)


{
rectangle r=new rectangle();
r.printarea();

triangle t=new triangle();


t.printarea();
circle r1=new circle();
r1.printarea();
}
}

RESULT

Thus a java program to calculate the area of rectangle, circle and triangle was implemented
and executed successfully.
Ex No : 5 CALCULATE AREA USING INTERFACE
Date :

AIM
To write a java program to calculate the area of rectangle, circle and triangle using the
concept of interface.

ALGORITHM

1. Create an interface named shape that contains empty method named printarea().
2. Provide three classes named rectangle, triangle and circle which implements the interface and
provides implementation for the method printarea.
3.Get the input and calculate the area of rectangle,circle and triangle .
5. In the shapeclass , create the objects and invoke the methods and display the area values of the
different shapes
PROGRAM
import java.util.*;
interface shape

{
public void printarea();

}
class rectangle implements shape
{
int a,b;

public int area_rect;


public void printarea()
{
Scanner s=new Scanner(System.in);
System.out.println("enter the length and breadth of rectangle");
a=s.nextInt();
b=s.nextInt();
area_rect=a*b;
System.out.println("Length of rectangle "+a +"breadth of rectangle "+b);
System.out.println("The area ofrectangle is:"+area_rect);
}

}
class triangle implements shape
{
int a,b;
double area_tri;
public void printarea()
{
Scanner s=new Scanner(System.in);
System.out.println("enter the base and height of triangle");
a=s.nextInt();

b=s.nextInt();
System.out.println("Base of triangle "+a +"height of triangle "+b);
area_tri=(0.5*a*b);

System.out.println("The area of triangle is:"+area_tri);


}
}
class circle implements shape
{
int a,b;

double area_circle;
public void printarea()
{
Scanner s=new Scanner(System.in);
System.out.println("enter the radius of circle");
a=s.nextInt();
area_circle=(3.14*a*a);
System.out.println("Radius of circle"+a);
System.out.println("The area of circle is:"+area_circle);
}
}
public class shapeclass

{
public static void main(String[] args)
{

rectangle r=new rectangle();


r.printarea();
triangle t=new triangle();
t.printarea();

circle r1=new circle();


r1.printarea();
}
}

RESULT:

Thus a java program to calculate the area of rectangle, circle and triangle was implemented
and executed successfully.
Ex No :6 CREATION OF USER DEFINED EXCEPTION
Date :

AIM
To write a java program to implement user defined exception handling

ALGORITHM
1. Create a class which extends Exception class.

2. Create a constructor which receives the string as argument.

3.Get the Amount as input from the user.


4. If the amount is negative , the exception will be generated.

5. Using the exception handling mechanism , the thrown exception is handled by the catch
construct.
6. After the exception is handled , the string “invalid amount “ will be displayed.

7. If the amount is greater than 0 , the message “Amount Deposited “ will be displayed
PROGRAM
import java.util.Scanner;
class NegativeAmtException extends Exception

{
String msg;
NegativeAmtException(String msg)
{
this.msg=msg;
}

public String toString()


{
return msg;
}
}
public class userdefined

{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.print("Enter Amount:");
int a=s.nextInt();

try
{
if(a<0)
{

throw new NegativeAmtException("Invalid Amount");


}
System.out.println("Amount Deposited");

}
catch(NegativeAmtException e)
{

System.out.println(e);
}
}
}

RESULT

Thus a java program to implement user defined exception handling has been
implemented and executed successfully.
Ex No :7 MULTITHREADED APPLICATION
Date :

AIM
To write a java program that implements a multi-threaded application .

ALGORITHM

1. Create a class even which implements first thread that computes the square of the number
2. run() method implements the code to be executed when thread gets executed.
3. Create a class odd which implements second thread that computes the cube of the number.
4.Create a third thread that generates random number.If the random number is even , it displays
the square of the number.If the random number generated is odd , it displays the cube of the
given number .
5.The Multithreading is performed and the task switched between multiple threads.
6.The sleep () method makes the thread to suspend for the specified time.
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();

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

{
public static void main(String[] args)
{
A a = new A();
a.start();
}
}

RESULT

Thus a java program for multi-threaded application has been implemented and executed
successfully.
Ex No :8 PROGRAM FOR DISPLAYING FILE INFORMATION
Date :

AIM

To write a java program that 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 length of
the file in bytes.

ALGORITHM

1. Create a class filedemo. Get the file name from the user .
2. Use the file functions and display the information about the file.
3.getName() displays the name of the file.
4. getPath() diplays the path name of the file.
5. getParent () -This method returns the pathname string of this abstract pathname’s parent, or
null if this pathname does not name a parent directory.
6. exists() – Checks whether the file exists or not.
7. canRead()-This method is basically a check if the file can be read.
8. canWrite()-verifies whether the application can write to the file.
9. isDirectory() – displays whether it is a directory or not.
10. isFile() – displays whether it is a file or not.
11. lastmodified() – displays the last modified information.
12.length()- displays the size of the file.
13. delete() – deletes the file
14.Invoke the predefined functions abd display the iformation about the file.
PROGRAM
import java.io.*;
import java.util.*;
class filedemo

{
public static void main(String args[])
{
String filename;
Scanner s=new Scanner(System.in);
System.out.println("Enter the file name ");
filename=s.nextLine();

File f1=new File(filename);


System.out.println("*****************");
System.out.println("FILE INFORMATION");
System.out.println("*****************");
System.out.println("NAME OF THE FILE "+f1.getName());
System.out.println("PATH OF THE FILE "+f1.getPath());
System.out.println("PARENT"+f1.getParent());
if(f1.exists())

System.out.println("THE FILE EXISTS ");


else
System.out.println("THE FILE DOES NOT ExISTS ");
if(f1.canRead())
System.out.println("THE FILE CAN BE READ ");
else
System.out.println("THE FILE CANNOT BE READ ");
if(f1.canWrite())
System.out.println("WRITE OPERATION IS PERMITTED");
else
System.out.println("WRITE OPERATION IS NOT PERMITTED");
if(f1.isDirectory())
System.out.println("IT IS A DIRECTORY ");
else

System.out.println("NOT A DIRECTORY");
if(f1.isFile())
System.out.println("IT IS A FILE ");
else
System.out.println("NOT A FILE");
System.out.println("File last modified "+ f1.lastModified());

System.out.println("LENGTH OF THE FILE "+f1.length());


System.out.println("FILE DELETED "+f1.delete());
}
}

RESULT

Thus a java program to display file information has been implemented and
executed successfully.
Ex No: 9 PROGRAM TO FIND THE MAXIMUM VALUE FROM THE GIVEN
Date: TYPE OF ELEMENTS USING GENERICS

AIM

To write a java program to find the maximum value from the given type of elements using
a generic function.

ALGORITHM

1.Create a class Myclass to implement generic class and generic methods.


2.Get the set of the values belonging to specific data type.
3. Create the objects of the class to hold integer,character and double values.
4. Create the method to compare the values and find the maximum value stored in the array.
5.Invoke the method with integer, character or double values . The output will be displayed
based on the data type passed to the method.

PROGRAM
class MyClass<T extends Comparable<T>>
{

T[] vals;
MyClass(T[] o)
{
vals = o;
}
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 gendemo
{
public static void main(String args[])
{
int i;

Integer inums[]={10,2,5,4,6,1};
Character chs[]={'v','p','s','a','n','h'};
Double d[]={20.2,45.4,71.6,88.3,54.6,10.4};

MyClass<Integer> iob = new MyClass<Integer>(inums);


MyClass<Character> cob = new MyClass<Character>(chs);
MyClass<Double>dob = new MyClass<Double>(d);
System.out.println("Max value in inums: " + iob.max());
System.out.println("Min value in inums: " + iob.min());
System.out.println("Max value in chs: " + cob.max());
System.out.println("Min value in chs: " + cob.min());
System.out.println("Max value in chs: " + dob.max());
System.out.println("Min value in chs: " + dob.min());

} }

RESULT

Thus a java program to find the maximum value from the given type of elements has been
implemented using generics and executed successfully.
Ex No: 10a JAVAFX CONTROLS AND LAYOUTS
Date:
AIM
To write a java program for JavaFX controls and layouts.

ALGORITHM
1. import the javafx packages.
2. Create the class Registration that extends the application class.
3. Create the controls for Text, Radiobutton, Checkbox, Togglebutton
4. The different layouts can be used to lay the controls.
5. When the user presses the control , the event is generated and handled .
6. Launch method is used to launch the javafx application.
PROGRAM
package registration;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.ListView;
import javafx.scene.control.RadioButton;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;

import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.ToggleButton;
import javafx.stage.Stage;
public class Registration extends Application
{
public void start(Stage stage) {
Text nameLabel = new Text("Name");
TextField nameText = new TextField();
Text dobLabel = new Text("Date of birth");
DatePicker datePicker = new DatePicker();
Text genderLabel = new Text("gender");
ToggleGroup groupGender = new ToggleGroup();
RadioButton maleRadio = new RadioButton("male");
maleRadio.setToggleGroup(groupGender);
RadioButton femaleRadio = new RadioButton("female");
femaleRadio.setToggleGroup(groupGender);
Text reservationLabel = new Text("Reservation");
ToggleButton Reservation = new ToggleButton();
ToggleButton yes = new ToggleButton("Yes");
ToggleButton no = new ToggleButton("No");
ToggleGroup groupReservation = new ToggleGroup();
yes.setToggleGroup(groupReservation);
no.setToggleGroup(groupReservation);
Text technologiesLabel = new Text("Technologies Known");
CheckBox javaCheckBox = new CheckBox("Java");
javaCheckBox.setIndeterminate(false);
CheckBox dotnetCheckBox = new CheckBox("DotNet");
javaCheckBox.setIndeterminate(false);
Text educationLabel = new Text("Educational qualification");
ObservableList<String> names = FXCollections.observableArrayList( "Engineering", "MCA",
"MBA", "Graduation", "MTECH", "Mphil", "Phd");
ListView<String> educationListView = new ListView<String>(names);
Text locationLabel = new Text("location");
ChoiceBox locationchoiceBox = new ChoiceBox();
locationchoiceBox.getItems().addAll("Hyderabad", "Chennai", "Delhi", "Mumbai",
"Vishakhapatnam");
Button buttonRegister = new Button("Register");
buttonRegister.setOnAction(new EventHandler<ActionEvent>()
{ public void handle(ActionEvent t) {
System.out.println("Register successfully");
} };
GridPane gridPane = new GridPane();
gridPane.setMinSize(500, 500);
gridPane.setPadding(new Insets(10, 10, 10, 10));
gridPane.setVgap(5);
gridPane.setHgap(5);
gridPane.setAlignment(Pos.CENTER);
gridPane.add(nameLabel, 0, 0);
gridPane.add(nameText, 1, 0);
gridPane.add(dobLabel, 0, 1);
gridPane.add(datePicker, 1, 1);
gridPane.add(genderLabel, 0, 2);
gridPane.add(maleRadio, 1, 2);
gridPane.add(femaleRadio, 2, 2);
gridPane.add(reservationLabel, 0, 3);
gridPane.add(yes, 1, 3);
gridPane.add(no, 2, 3);
gridPane.add(technologiesLabel, 0, 4);
gridPane.add(javaCheckBox, 1, 4);
gridPane.add(dotnetCheckBox, 2, 4);
gridPane.add(educationLabel, 0, 5);
gridPane.add(educationListView, 1, 5);
gridPane.add(locationLabel, 0, 6);
gridPane.add(locationchoiceBox, 1, 6);
gridPane.add(buttonRegister, 2, 8);
buttonRegister.setStyle("-fx-background-color: darkslateblue; -fx-textfill: white;");
nameLabel.setStyle("-fx-font: normal bold 15px 'serif' ");
dobLabel.setStyle("-fx-font: normal bold 15px 'serif' ");
genderLabel.setStyle("-fx-font: normal bold 15px 'serif' ");
reservationLabel.setStyle("-fx-font: normal bold 15px 'serif' ");
technologiesLabel.setStyle("-fx-font: normal bold 15px 'serif' ");
educationLabel.setStyle("-fx-font: normal bold 15px 'serif' ");
locationLabel.setStyle("-fx-font: normal bold 15px 'serif' ");
gridPane.setStyle("-fx-background-color: BEIGE;");
Scene scene = new Scene(gridPane);
stage.setTitle("Registration Form");
stage.setScene(scene);
stage.show();
}
public static void main(String args[])
{
launch(args);
}
}

RESULT

Thus the program for Javafx controls and layout was verified and executed
successfully
Ex No: 10b JAVAFX MENUS
Date:
AIM

To write a java program for JavaFX Menus

ALGORITHM
1. import the javafx packages.
2. Create the class MenuBarExample that extends the application class.
3. Create the menu for File and Edit
4.Create the submenus for File and Edit
5. The different layouts can be used to lay the menus
6.. When the user click the File or Edit menu the event is generated and handled
7. Launch method is used to launch the javafx application.
PROGRAM
package MenuBarExample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.control.Alert.AlertType;
import java.time.LocalDate;
public class MenuBarExample extends Application
{
public void start(Stage s)
{
s.setTitle("creating MenuBar");
Menu m = new Menu("File");
Menu e = new Menu("Edit");
MenuItem m1 = new MenuItem("New");
MenuItem m2 = new MenuItem("Open");
MenuItem m3 = new MenuItem("Save");
MenuItem m4 = new MenuItem("Cut");
MenuItem m5 = new MenuItem("Copy");
MenuItem m6 = new MenuItem("Paste");
m.getItems().add(m1);
m.getItems().add(m2);
m.getItems().add(m3);
e.getItems().add(m4);
e.getItems().add(m5);
e.getItems().add(m6);
Label l = new Label("\t\t\t\t"+ "no menu item selected");
EventHandler<ActionEvent> event = new EventHandler<ActionEvent>()
{
public void handle(ActionEvent e)
{
l.setText("\t\t\t\t" + ((MenuItem)e.getSource()).getText() + " selected");
}};
m1.setOnAction(event);
m2.setOnAction(event);
m3.setOnAction(event);
m4.setOnAction(event);
m5.setOnAction(event);
m6.setOnAction(event);
MenuBar mb = new MenuBar();
mb.getMenus().add(m);
mb.getMenus().add(e);
VBox vb = new VBox(mb, l);
Scene sc = new Scene(vb, 500, 300);
s.setScene(sc);
s.show();
}
public static void main(String args[])
{
launch(args);
}
}

RESULT
Thus the program for Javafx Menus was verified and executed successfully
Ex No: 11 PROGRAM TO IMPLEMENT CURRENCY CONVERTER, DISTANCE
CONVERTER AND TIME CONVERTER USING PACKAGES
Date :

AIM
To develop a java application to implement currency converter ,distance converter and time
converter using the concept of packages .

ALGORITHM

1. Create a Package currencyconversion and place the class currency under the package
2. Create the methods to perform currency conversion from dollar to rupee ,rupee to dollar,euro to
rupee , rupee to euro , yen to rupee and rupee to yen.
3. Create the package distanceconverion and create the class distance within the package
4.Create the methods to convert from meter to km, km to meter, miles to km,km to miles
5.Create the package timeconversion and create the class timer .Create the methods to convert
from hours to minutes ,hours to seconds , minutes to hours and seconds to hours
6. Create a class and import the packages currencyconversion,distanceconversion and time
conversion.Create the objects for the class currency,distance and timer.
7. Get the choice from the user and invoke the methods to perform the corresponding conversion
and display the value.

PROGRAM
currencyconversion.java
package currencyconversion;
import java.util.*;
public class currency
{

double inr,usd;
double euro,yen;
Scanner in=new Scanner(System.in);
public void dollartorupee()
{
System.out.println("Enter dollars to convert into Rupees:");
usd=in.nextInt();
inr=usd*67;
System.out.println("Dollar ="+usd+"equal to INR="+inr);
}
public void rupeetodollar()

{
System.out.println("Enter Rupee to convert into Dollars:");
inr=in.nextInt();
usd=inr/67;
System.out.println("Rupee ="+inr+"equal to Dollars="+usd);
}
public void eurotorupee()
{
System.out.println("Enter euro to convert into Rupees:");
euro=in.nextInt();

inr=euro*79.50;
System.out.println("Euro ="+euro +"equal to INR="+inr);

}
public void rupeetoeuro()
{
System.out.println("Enter Rupees to convert into Euro:");
inr=in.nextInt();

euro=(inr/79.50);
System.out.println("Rupee ="+inr +"equal to Euro="+euro);

}
public void yentorupee()
{
System.out.println("Enter yen to convert into Rupees:");
yen=in.nextInt();
inr=yen*0.61;
System.out.println("YEN="+yen +"equal to INR="+inr);
}
public void rupeetoyen()

{
System.out.println("Enter Rupees to convert into Yen:");
inr=in.nextInt();
yen=(inr/0.61);
System.out.println("INR="+inr +"equal to YEN"+yen);
}
}

distance.java
package distanceconversion;
import java.util.*;

public class distance


{

double km,m,miles;
Scanner sc = new Scanner(System.in);
public void kmtom()
{

System.out.print("Enter in km ");
km=sc.nextDouble();
m=(km*1000);
System.out.println(km+"km" +"equal to"+m+"metres");
}
public void mtokm()
{
System.out.print("Enter in meter ");
m=sc.nextDouble();
km=(m/1000);
System.out.println(m+"m" +"equal to"+km+"kilometres");
}

public void milestokm()


{
System.out.print("Enter in miles");
miles=sc.nextDouble();
km=(miles*1.60934);
System.out.println(miles+"miles" +"equal to"+km+"kilometres");
}
public void kmtomiles()
{

System.out.print("Enter in km");
km=sc.nextDouble();
miles=(km*0.621371);
System.out.println(km+"km" +"equal to"+miles+"miles");
}
}

timer.java
package timeconversion;
import java.util.*;
public class timer
{
int hours,seconds,minutes;
int input;
Scanner sc = new Scanner(System.in);
public void secondstohours()
{

System.out.print("Enter the number of seconds: ");


input = sc.nextInt();
hours = input / 3600;

minutes = (input % 3600) / 60;


seconds = (input % 3600) % 60;
System.out.println("Hours: " + hours);
System.out.println("Minutes: " + minutes);
System.out.println("Seconds: " + seconds);
}
public void minutestohours()
{
System.out.print("Enter the number of minutes: ");
minutes=sc.nextInt();
hours=minutes/60;
minutes=minutes%60;
System.out.println("Hours: " + hours);

System.out.println("Minutes: " + minutes);


}
public void hourstominutes()

{
System.out.println("enter the no of hours");
hours=sc.nextInt();

minutes=(hours*60);
System.out.println("Minutes: " + minutes);
}
public void hourstoseconds()
{
System.out.println("enter the no of hours");
hours=sc.nextInt();

seconds=(hours*3600);
System.out.println("Minutes: " + seconds);
}
}

converter.java
import java.util.*;
import java.io.*;
import currencyconversion.*;
import distanceconversion.*;
import timeconversion.*;
class converter
{
public static void main(String args[])
{

Scanner s=new Scanner(System.in);


int choice,ch;
currency c=new currency();
distance d=new distance();
timer t=new timer();

do
{

System.out.println("1.dollar to rupee ");


System.out.println("2.rupee to dollar ");
System.out.println("3.Euro to rupee ");
System.out.println("4..rupee to Euro ");
System.out.println("5.Yen to rupee ");
System.out.println("6.Rupee to Yen ");
System.out.println("7.Meter to kilometer ");
System.out.println("8.kilometer to meter ");
System.out.println("9.Miles to kilometer ");
System.out.println("10.kilometer to miles");
System.out.println("11.Hours to Minutes");
System.out.println("12.Hours to Seconds");
System.out.println("13.Seconds to Hours");
System.out.println("14.Minutes to Hours");
System.out.println("Enter ur choice");
choice=s.nextInt();
switch(choice)
{

case 1:
{
c.dollartorupee();
break;

}
case 2:
{

c.rupeetodollar();
break;
}
case 3:
{
c.eurotorupee();
break;
}
case 4:

{
c.rupeetoeuro();
break;
}
case 5:
{

c.yentorupee();
break;
}
case 6 :
{
c.rupeetoyen();
break;

}
case 7 :

{
d.mtokm();
break;
}

case 8 :
{
d.kmtom();
break;

}
case 9 :
{
d.milestokm();
break;
}
case 10 :
{

d.kmtomiles();
break;
}
case 11 :
{
t.hourstominutes();
break;

}
case 12 :

{
t.hourstoseconds();
break;
}
case 13 :
{
t.secondstohours();
break;

}
case 14 :

{
t.minutestohours();
break;
}
}
System.out.println("Enter 0 to quit and 1 to continue ");
ch=s.nextInt();
}while(ch==1);
}
}

RESULT
Thus the java application to implement currency converter ,distance converter and time
converter was implemented and executed successfully.

You might also like