CS3381 OBJECT ORIENTED PROGRAMMING LABORATORY
CS3381 OBJECT ORIENTED PROGRAMMING LABORATORY
NO : 1A LINEAR SEARCH
DATE:
AIM:
To develop a Java program to search a key element from multiple elements using Linear Search
ALGORITHM:
Step 1: Start
Step 2: Traverse the array
Step 3: Match the key element with array element
Step 4: If the key element is found return the index position of the element
Step 5: If the key is not found return -1
Step 6: End
PROGRAM:
1
OUTPUT:
RESULT:
Thus the java program to linear search was implemented, executed successfully and
the output was verified.
2
EX NO : 1B BINARY SEARCH
DATE:
AIM:
To develop a Java program to search a key element from multiple elements using Binary search
ALGORITHM:
Step 1: Start
Step 2: Traverse the array
Step 3: Calculate the MID value of the array and match the mid elements with key
Step 4: If it is found return mid value
Step 5: Else check the value with the key if the value of the elements is less than key first =
MID+1
Step 6: Else last=MID-1
Step 7: Respect the process until arr (MID)==Key
Step 8: If it is not found return -1
Step 9: End
PROGRAM:
class BinarySearchExample{
public static void binarySearch(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;
}
3
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;
binarySearch(arr,0,last,key);
}
}
OUTPUT:
RESULT
Thus the java program to binary search was implemented, executed successfully and
the output was verified.
4
EX NO: 1C SELECTION SORT
DATE:
AIM:
To develop a Java program to sort the array elements using selection sort.
ALGORITHM:
Step 1: Start
Step 2: Traverse the array
Step 3: Find the minimium number in the array from unsorted part put in the beginning
Step 4: Print the sorted array
Step 5: End
PROGRAM:
5
for(int i:arr1){
System.out.print(i+" ");
}
System.out.println();
selectionSort(arr1);//sorting array using selection sort
System.out.println("After Selection Sort");
for(int i:arr1){
System.out.print(i+" ");
}
}
}
OUTPUT:
RESULT:
Thus the java program to selection search was implemented, executed successfully
and the output was verified.
6
EX NO: 1D INSERTION SORT
DATE:
AIM:
ALGORITHM:
Step 1: Start
Step 2: Using the loop function
Step 3: Compare element to another element
Step 4: enter some elements in array
Step 5: sorting array using insertion sort
Step 6: Print before number and after numbers
Step 7: End
PROGRAM:
OUTPUT:
RESULT:
Thus the java program to insertion search was implemented, executed successfully
and the output was verified.
8
EX NO: 2A STACK ADT IMPLEMENTATION USING CLASSES AND OBJECTS
DATE:
AIM:
ALGORITHM:
PROGRAM:
class Stack {
int top;
boolean isEmpty()
}
Stack()
{
top = -1;
9
boolean push(int x)
System.out.println("Stack Overflow");
return false;
else {
a[++top] = x;
return true;
int pop()
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
else {
int x = a[top--];
return x;
void print(){
10
for(int i = top;i>-1;i--){
// Driver code
class Main {
s.push(10);
s.push(20);
s.push(30);
s.print();
11
OUTPUT:
\
RESULT:
Thus the java application for array implementation of stack ADT was implemented, executed
successfully and the output was verified.
12
Ex.No.2B QUEUE ADT USING CLASSES AND OBJECTS
DATE :
Aim
To develop a Java program to implement array implementation of queue using classes and objects
Algorithm:
PROGRAM
class Queue {
int front, rear, size; int capacity;
int array[];
public Queue(int capacity)
{
this.capacity = capacity; front = this.size = 0; rear = capacity - 1;
array = new int[this.capacity];
}
// Queue is full when size becomes
// equal to the capacity boolean isFull(Queue queue)
{
return (queue.size == queue.capacity);
}
// Queue is empty when size is 0
boolean isEmpty(Queue queue)
{
return (queue.size == 0);
if (isFull(this)) return;
this.rear = (this.rear + 1)% this.capacity; this.array[this.rear] = item;
13
this.size = this.size + 1;
}
// Method to remove an item from queue.
if (isEmpty(this))
}
// Method to get front of queue int front()
14
{
if (isEmpty(this))
if (isEmpty(this))
return Integer.MIN_VALUE; return this.array[this.rear];
}
}
// Driver class public class Test {
public static void main(String[] args)
Queue queue = new Queue(1000);
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.enqueue(40);
System.out.println(queue.dequeue()+ " dequeued from queue\n");
System.out.println("Front item is " + queue.front());
System.out.println("Rear item is " + queue.rear());
}
}
Output
15
RESULT:
Thus the java application for queue ADT was implemented, executed successfully and the output
was verified
16
EX NO: 3 PAYSLIP GENERATION 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. Start
2. Create the class Employee with name, Empid, address, mailid, mobileno as data members.
3. Inherit the classes Programmer, Asst professor, Associate professor and Professor from
employee class.
4. Add Basic Pay (BP) as the member of all the inherited classes.
5. Calculate DA as 97% of BP, HRA as 10% of BP, PF as 12% of BP, Staff club fund as 0.1% of
BP.
6. Calculate gross salary and net salary.
7. Generate payslip for all categories of employees.
8. Create the objects for the inherited classes and invoke the necessary methods to display the
payslip
9. Stop
PROGRAM:
Salary.java
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();
17
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);
}
}
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);
18
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);
}
}
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()
{
20
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.ASSOCIATEPROFESSOR \t 4.PROFESSOR ");
Scanner c = new Scanner(System.in);
System.out.print(“Enter Your Choice:”);
choice=c.nextInt();
switch(choice)
{
case 1:
Programmer p=new Programmer(); p.getdata();
21
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;
}
22
OUTPUT
RESULT
Thus the Java application to generate pay slip for different category of employees was
implementedusing inheritance and the program was executed successfully.
23
EX NO : 4 ABSTRACT CLASS IMPLEMENTATION
DATE :
AIM
To write a Java program to calculate the area of rectangle, circle and triangle using the
concept of abstract class.
ALGORITHM:
1. Start
2. Create an abstract class named shape that contains two integers and an empty method named
printarea().
3. Provide three classes named rectangle, triangle and circle such that each one of the
classesextends the class Shape.
4. Each of the inherited class from shape class should provide the implementation for the method
printarea().
5. Get the input and calculate the area of rectangle, circle and triangle.
6. In the shapeclass, create the objects for the three inherited classes and invoke the
methods anddisplay the area values of the different shapes.
7. Stop.
PROGRAM
Shapeclass.java
import java.util.*;
24
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 of rectangle 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);
}}
RESULT
Thus the Java program for calculate the area of rectangle, circle and triangle was
implementedand executed successfully.
26
EX. NO : 5 AREA OF SHAPES USING INTERFACE
DATE:
AIM:
To write a Java program to calculate the area of rectangle, circle and triangle using interface.
ALGORITHM:
1. Create an abstract class named shape that contains two integers and an empty method named
printarea().
2. Provide two classes named rectangle 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 and circle.
5. In the shapeclass , create the objects for the two inherited classes and invoke the methods and
display the area values of the different shapes.
PROGRAM:
package pkginterface.java;
import java.util.*;
interface myinterface
{
public void printArea();
}
abstract class Shapes
{
double a,b;
abstract void printArea();
}
class Rectangle extends Shapes implements myinterface
{
public void printArea()
{
27
System.out.println("\t\tcalculating Area of Rectangle");
Scanner input=new Scanner(System.in);
System.out.print("Enter length:");
a=input.nextDouble();
System.out.print("\nEnter breath:");
b=input.nextDouble();
double area=a*b;
System.out.println("Area of Rectangle:"+area);
}
}
class Triangle extends Shapes implements myinterface
{
public void printArea()
{
System.out.println("\t\tCalculating Arae of Triangle");
Scanner input=new Scanner(System.in);
System.out.print("Enter height:");
a=input.nextDouble();
System.out.print("\nEnter breadth:");
b=input.nextDouble();
double area=0.5*a*b;
System.out.println("Area of triangle:"+area);
}
}
class Circle extends Shapes implements myinterface
{
public void printArea()
{
System.out.println("\t\tCalculating Area of circle");
Scanner input=new Scanner(System.in);
System.out.print("Enter radius:");
a=input.nextDouble();
double area=3.14*a*a;
System.out.println("Area of Circle:"+area);
}
}
28
class InterfaceJava
{
public static void main(String[]args)
{
Shapes obj;
obj=new Rectangle();
obj.printArea();
obj=new Triangle();
obj.printArea();
obj=new Circle();
obj.printArea();
}
}
OUTPUT:
29
RESULT:
Thus the java program to calculate the area of shapes was implemented, executed
successfully and the output was verified.
30
EX NO : 6 USER DEFINED EXCEPTION HANDLING IMPLEMENTATION
DATE :
AIM:
To write a Java program to implement user defined exception handling.
ALGORITHM:
1. Start
2. Create a class which extends Exception class.
3. Create a constructor which receives the string as argument.
4. Get the Amount as input from the user.
5. If the amount is negative, the exception will be generated.
6. Using the exception handling mechanism , the thrown exception is handled by
the catchconstruct.
7. After the exception is handled , the string “invalid amount “ will be displayed.
8. If the amount is greater than 0, the message “Amount Deposited “ will be displayed
9. Stop.
PROGRAM :
userdefined.java
import java.util.*;
class NegativeAmtException extends Exception
{
String msg;
NegativeAmtException(String msg)
{
this.msg=msg;
}
public String toString()
{
return msg;
}
}
public class userdefined
31
{
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); }}}
OUTPUT
RESULT:
Thus the Java program to implement user defined exception handling has been implementedand
executed successfully.
32
EX NO : 7 MULTITHREADING IMPLEMENTATION
DATE :
AIM:
To write a java program that implements a multi-threaded application.
ALGORITHM:
1. Start
2. Create a class even which implements first thread that computes the square of the number .
3. run() method implements the code to be executed when thread gets executed.
4. Create a class odd which implements second thread that computes the cube of the number.
5. Create a third thread that generates random number. If the random number is even, it
displaysthe square of the number. If the random number generated is odd, it displays
the cube of the given number.
6. The Multithreading is performed and the task switched between multiple threads.
7. The sleep () method makes the thread to suspend for the specified time.
8. Stop.
PROGRAM
multithreadprog.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;
33
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);
if (num % 2 == 0)
{
Thread t1 = new Thread(new
even(num));t1.start();
}
elsee
{
Thread t2 = new Thread(new
}
odd(num));t2.start();
Thread.sleep(1000);
System.out.println(" ");
}
}
catch (Exception ex)
{
34
System.out.println(ex.getMessage());
}
}
}
OUTPUT
RESULT
Thus the Java program for multi-threaded application has been implemented and
executed successfully.
35
EX NO : 8 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 thefile in bytes.
ALGORITHM:
1. Start
2. Create a class filedemo. Get the file name from the user.
3. Use the file functions and display the information about the file.
4. getName() displays the name of the file.
5. getPath() diplays the path name of the file.
6. getParent () -This method returns the pathname string of this abstract pathname’s parent, or
7. null if this pathname does not name a parent directory.
8. exists() – Checks whether the file exists or not.
9. canRead()-This method is basically a check if the file can be read.
10. canWrite()-verifies whether the application can write to the file.
11. isDirectory() – displays whether it is a directory or not.
12. isFile() – displays whether it is a file or not.
13. lastmodified() – displays the last modified information.
14. length()- displays the size of the file.
15. delete() – deletes the file
16. Invoke the predefined functions to display the information about the file.
17. Stop.
PROGRAM:
filedemo.java
class filedemo
{
public static void main(String args[])
{
36
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());
}
}
37
OUTPUT:
RESULT
Thus the java program to display file information has been implemented and
executedsuccessfully.
38
EX NO : 9 GENERIC FUNCTION IMPLEMENTATION
DATE :
AIM
To write a java program to find the maximum value from the given type of elements
using ageneric function.
ALGORITHM:
1. Start
2. Create a class Myclass to implement generic class and generic methods.
3. Get the set of the values belonging to specific data type.
4. Create the objects of the class to hold integer, character and double values.
5. Create the method to compare the values and find the maximum value stored in the array.
6. Invoke the method with integer, character or double values. The output will be
displayedbased on the data type passed to the method.
7. Stop.
PROGRAM:
genericdemo.java
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];
39
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 genericdemo
{
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());
}
}
40
OUTPUT:
RESULT:
Thus the Java program to find the maximum value from the given type of elements
has been implemented using generics and executed successfully
41
EX. NO : 10A APPLICATION USING JAVAFX CONTROLS
DATE:
AIM:
ALGORITHM:
PROGRAM:
package javafxapplication2;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
42
//creating label email
Text text1 = new Text("Email");
//creating label password
Text text2 = new Text("Password");
//Creating Text Filed for email
TextField textField1 = new TextField();
//Creating Text Filed for password
TextField textField2 = new TextField();
//Creating Buttons
Button button1 = new Button("Submit");
Button button2 = new Button("Clear");
//Creating a Grid Pane
GridPane gridPane = new GridPane();
//Setting size for the pane
gridPane.setMinSize(400, 200);
//Setting the padding
gridPane.setPadding(new Insets(10, 10, 10, 10));
//Setting the vertical and horizontal gaps between the columns
gridPane.setVgap(5);
gridPane.setHgap(5);
//Setting the Grid alignment
gridPane.setAlignment(Pos.CENTER);
//Arranging all the nodes in the grid
gridPane.add(text1, 0, 0);
gridPane.add(textField1, 1, 0);
gridPane.add(text2, 0, 1);
gridPane.add(textField2, 1, 1);
gridPane.add(button1, 0, 2);
gridPane.add(button2, 1, 2);
//Creating a scene object
Scene scene = new Scene(gridPane);
43
//Displaying the contents of the stage
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
OUTPUT:
RESULT:
Thus the program using JavaFX controls was implemented, executed successfully and
the output was verified.
44
EX. NO : 10B MENUBAR APPLICATION USING JAVAFX
DATE:
AIM:
To create javafx program to create a menu bar and to add a menu items.
ALGORITHM:
Step 1:Start
Step 2:Instantiate the Menu class.
Step 3:Create a required number of menu items by instantiating the MenuItem class.
Step 4:Add the created menu items to the observable list of the menu.
Step 5:End
PROGRAM:
//package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class MenuExample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
BorderPane root = new BorderPane();
Scene scene = new Scene(root,200,300);
MenuBar menubar = new MenuBar();
Menu FileMenu = new Menu("File");
45
MenuItem filemenu1=new MenuItem("new");
MenuItem filemenu2=new MenuItem("Save");
MenuItem filemenu3=new MenuItem("Exit");
Menu EditMenu=new Menu("Edit");
MenuItem EditMenu1=new MenuItem("Cut");
MenuItem EditMenu2=new MenuItem("Copy");
MenuItem EditMenu3=new MenuItem("Paste");
EditMenu.getItems().addAll(EditMenu1,EditMenu2,EditMenu3);
root.setTop(menubar);
FileMenu.getItems().addAll(filemenu1,filemenu2,filemenu3);
menubar.getMenus().addAll(FileMenu,EditMenu);
primaryStage.setScene(scene);
primaryStage.show();
}
}
OUTPUT:
RESULT:
Thus the JavaFX program to create a menu bar and to add menu item was implemented,
executed successfully and the output was verified.
46
EX. NO : 10C APPLICATION USING JAVAFX LAYOUTS
DATE:
AIM:
ALGORITHM:
STEP 1: start
STEP 2: create a new horizontal flowpane layout.
STEP 3: create with specified horizontal and vertical gap.
STEP 4: create a flowpane with specified orientation.
STEP5: And vertical gap and specified childrens.
STEP6: creates a flowpane with specified orientation and specified nodes
STEP7: End of the program
PROGRAM:
package javafxapplication2;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.canvas.*;
import javafx.scene.web.*;
import javafx.scene.layout.*;
import javafx.scene.shape.*;
public class JavaFXApplication2 extends Application {
public void start(Stage stage)
47
{
try {
// set title for the stage
stage.setTitle("FlowPane");
// create a labels
Label label = new Label("this is FlowPane example");
Label label1 = new Label("label no 1");
Label label2 = new Label("label no 2");
Label label3 = new Label("label no 3");
Label label4 = new Label("label no 4");
// create a FlowPane
FlowPane flow_pane = new FlowPane(20.0, 20.0, label, label1, label2, label3, label4);
// create a scene
Scene scene = new Scene(flow_pane, 400, 300);
// set the scene
stage.setScene(scene);
stage.show();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static void main(String args[])
{
launch(args);
}
}
48
OUTPUT:
RESULT:
Thus the JavaFX program for creating flowpane using layouts was implemented,
executed successfully and the output was verified.
49
EX. NO : 11 MINI PROJECT FOR ELECTRICITY BILL GENERATION
DATE:
AIM:
To develop a Java application to generate Electricity bill. Create a class with the following
members: Consumer no., consumer name, previous month reading, current month reading, type
of EB connection (i.e domesticor commercial). Compute the bill amount using the following
tariff.
If the type of the EB connection is domestic, calculate the amount to be paid as follows:
First 100 units - Rs. 1 per unit
101-200 units - Rs. 2.50 per unit
201 -500 units - Rs. 4 per unit
> 501 units - Rs. 6 per unit
If the type of the EB connection is commercial, calculate the amount to be paid as follows:
First 100 units - Rs. 2 per unit
101-200 units - Rs. 4.50 per unit
201 -500 units - Rs. 6 per unit
> 501 units - Rs. 7 per unit
ALGORITHM:
1. Start
2. Create a class Ebill with three member functions: getdata(), calc(), display() and
4. Using getdata() function get the consumed units for current and previous month .
7. Stop.
50
PROGRAM:
import java.util.*;class Ebill
{
public static void main (String args[])
{
Customerdata ob = new
Customerdata();ob.getdata();
ob.calc();
ob.display();
}
}
class Customerdata
{
Scanner in = new
Scanner(System.in); Scanner ins
= new Scanner(System.in);
String cname,type;
int bn;
double
current,previous,tbill,units;
void getdata()
{
System.out.print ("\n\t Enter consumer number ");bn = in.nextInt();
System.out.print ("\n\t Enter Type of connection (D for Domestic or C for Commercial) ");
type = ins.nextLine();
System.out.print ("\n\t Enter consumer name ");
cname = ins.nextLine();
System.out.print ("\n\t Enter previous month reading ");
previous= in.nextDouble();
System.out.print ("\n\t Enter current month reading ");
current= in.nextDouble();
}
51
void calc()
{
units=current-previous;
if(type.equals("D"))
{
if (units<=100)
tbill=1 * units;
else if (units>100 && units<=200)
tbill=2.50*units;
else if(units>200 && units<=500)
tbill= 4*units;
else
tbill= 6*units;
else
tbill= 7*units;
}}
void display()
{
System.out.println("\n\t Consumer number = "+bn);
System.out.println ("\n\t Consumer name = "+cname);
if(type.equals("D"))
System.out.println ("\n\t type of connection = DOMESTIC ");
else
System.out.println ("\n\t type of connection = COMMERCIAL ");
52
OUTPUT:
RESULT:
Thus the java program to generate electricity bill was implemented, executed
successfully and theoutput was verified.
53
EX. NO : 12
STRING OPERATIONS USING ARRAYLIST IN JAVA
DATE:
(CONTENT BEYOND THE SYLLABUS)
AIM:
ALGORITHM:
Step 1: Create the class arraylistexample. Create the object for the arraylist class.
Step 2: Display the options to the user for performing string handling .
Step 3:Use the function add() to append the string at the end and to insert the string at the
particular index.
Step 4: The function sort () is used to sort the elements in the array list.
Step 5: The function indexof() is used to search whether the element is in the array list or not.
Step 6:The function startsWith () is used to find whether the element starts with the specified
character.
Step 7:The function remove() is used to remove the element from the arraylist.
Step 8:The function size() is used to determine the number of elements in the array list.
54
PROGRAM:
import java.util.*;
import java.io.*;
public class arraylistexample
{
public static void main(String args[])throws IOException
{
ArrayList<String> obj = new ArrayList<String>();
DataInputStream in=new DataInputStream(System.in);
int c,ch;
int i,j;
String str,str1;
do
{
System.out.println("STRING MANIPULATION");
System.out.println("******************************");
System.out.println(" 1. Append at end \t 2.Insert at particular index \t 3.Search \t");
System.out.println( "4.List string that starting with letter \t");
System.out.println("5.Size \t 6.Remove \t 7.Sort\t 8.Display\t" );
System.out.println("Enter the choice ");
c=Integer.parseInt(in.readLine());
switch(c)
{
55
case 1:
{
System.out.println("Enter the string ");str=in.readLine();
obj.add(str);break;
}
case 2:
{
System.out.println("Enter the string ");str=in.readLine();
System.out.println("Specify the index/position to insert");
i=Integer.parseInt(in.readLine());
obj.add(i-1,str);
System.out.println("The array list has following elements:"+obj);break;
}
case 3:
{
System.out.println("Enter the string to search ");str=in.readLine();
j=obj.indexOf(str);if(j==-1)
System.out.println("Element not found");else
System.out.println("Index of:"+str+"is"+j);break;
}
case 4:
{
System.out.println("Enter the character to List string that starts with specified character");
str=in.readLine();
for(i=0;i<(obj.size()-1);i++)
{
str1=obj.get(i);
if(str1.startsWith(str))
{
System.out.println(str1);
}
}
break;
}
case 5:
56
{
System.out.println("Size of the list "+obj.size());break;
}
case 6:
{
System.out.println("Enter the element to remove");
str=in.readLine();
if(obj.remove(str))
{
System.out.println("Element Removed"+str);
}
else
{
System.out.println("Element not present");
}
break;
}
case 7:
{
Collections.sort(obj);
System.out.println("The array list has following elements:"+obj);break;
}
case 8:
{
System.out.println("The array list has following elements:"+obj);break;
}
}
System.out.println("enter 0 to break and 1 to continue");
ch=Integer.parseInt(in.readLine());
}while(ch==1);
}
}
57
OUTPUT:
RESULT:
Thus the java program to perform string operations using ArrayList was completed and executed
successfully.
58