0% found this document useful (0 votes)
15 views51 pages

II CSE OOP LAB Manual

The document outlines Java programs for implementing various algorithms including linear search, binary search, selection sort, insertion sort, stack, queue, and a payslip generation system using inheritance. Each section includes the aim, algorithm, and program code for the respective algorithm or data structure. The results indicate successful implementation for all programs.

Uploaded by

vinoth25012006
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)
15 views51 pages

II CSE OOP LAB Manual

The document outlines Java programs for implementing various algorithms including linear search, binary search, selection sort, insertion sort, stack, queue, and a payslip generation system using inheritance. Each section includes the aim, algorithm, and program code for the respective algorithm or data structure. The results indicate successful implementation for all programs.

Uploaded by

vinoth25012006
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/ 51

EX.NO: 1.

a) LINEAR SEARCH
DATE :
AIM:
To write a java Program to implement the linear search algorithm.

ALGORITHM:
1.Import the needed packages.
2.Declare the variables.
3.Get the number of elements in the array and store it in a variable n.
4.Get the elements of the array.
5.Get the value to be searched and store in the variable data.
6.Check the value in data with the elements in the array.
7. Once the data is found ,print the location of the data.
8. When the data is not found in the array, print the data is not in the array.

PROGRAM:
import java.io.*;
import java.util.Scanner;
public class LinearSearch
{
public static void main(String args[])
{
int i, n, data, array[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements:");
n = in.nextInt();
array = new int[n];
for (i = 0;i< n; i++)
{
System.out.println("Enter the element:"+(i+1));
array[i] = in.nextInt();
}
System.out.println("Enter value to find");
data = in.nextInt();
for (i = 0; i< n; i++)
{
if (array[i] == data)
{
System.out.println(data + " is present at location " + (i+ 1) );
break;

}
}
if (i== n)
System.out.println(data +" is not present in the array");
}
}
OUTPUT:

RESULT:
Thus the java program for linear search has been implemented successfully.
EX.NO:1.b) BINARY SEARCH

DATE:

AIM:

To write a java program to implement the binary search algorithm.

ALGORITHM:

1.Import the needed header files.

2.Declare an array variable arr.

3.Store the data to be searched in the key variable.

4.Find the element in the middle position .

5. Compare the key elements with the middle element.

6. If key = middle element, then return the mid index position.

7. If key>middle element, then the key lies in the right half of the array. Repeat steps 4,5 and 6

on the right half of the array.

8. If key<middle element, then the key lies in the left half of the array. Repeat steps 4,5 and 6

on the left half of the array.

Program:

import java.lang.*;

import java.util.*;

import java.io.*;

class Binary

public static void binarysearch(int arr[], int first, int last, int key)

int pos;

int mid = (first + last)/2;

while( first <last )

if ( arr[mid] <key )

first = mid + 1;

}else if ( arr[mid] == key )


{

pos=mid+1;

System.out.println("Element is found at index: " + pos);

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[])

Scanner sc=new Scanner(System.in);

int n;

System.out.println("Enter the number of elements");

n=sc.nextInt();

int arr[]=new int[n];

System.out.println("Enter the elements:");

for(int i=0;i<n;i++)

arr[i]=sc.nextInt();

System.out.println("Enter the element to be searched");

int key = sc.nextInt();


int last=arr.length-1;

binarysearch(arr,0,last,key);

}}

OUTPUT:

RESULT :

Thus the binary search algorithm is successfully implemented in java.


EX.NO:1.c) SELECTION SORT

DATE :

AIM:

To write a java program to implement Selection sort.

ALGORITHM:

1. Set MIN to location 0


2. Search the minimum element in the list
3.Swap with value at location MIN
4.Increment MIN to point to next element
5.Repeat until the list is sorted

PROGRAM:
import java.util.*;
class SelectionSort
{
public static void main(String args[])
{
int size;
Scanner in=new Scanner(System.in);
System.out.println("Enter the no.of elements in the array");
size=in.nextInt();
int array[]=new int[size];
System.out.println("Enter the elements");
for(int j=0;j<size;j++)
{
array[j]=in.nextInt();
}

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;
}
System.out.println("The sorted array is");
for (int i = 0 ;i< size; i++)
{
System.out.print(" "+array[i]);
}
}
}

OUTPUT:

RESULT:

Thus the java program to implement selection sort was executed successfully.
EX.NO: 1.d) INSERTION SORT
DATE :
AIM:
To write a java program to implement the insertion sort in java.

ALGORITHM:

1 . If the element is the first element, assume that it is already sorted. Return 1.

2 . Pick the next element, and store it separately in a key.

3. Now, compare the key with all elements in the sorted array.

4. If the element in the sorted array is smaller than the current element, then move to the next
element. Else, shift greater elements in the array towards the right.

5. Insert the value.

6. Repeat until the array is sorted.

PROGRAM:
import java.util.*;
class InsertionSort
{
public static void sortInsertion(int[] arr)
{

for(int i=0;i<arr.length;++i)
{

int j = i;

while(j > 0 && arr[j-1]>arr[j])


{

int key = arr[j];


arr[j] = arr[j-1];
arr[j-1] = key;
j = j-1;

}
}
}

public static void main( String args[] )


{
int n;
Scanner in=new Scanner(System.in);
System.out.println("Enter the no.of elements in the array");
n=in.nextInt();
int arr[]=new int[n];
System.out.println("Enter the elements in the array");
for(int j=0;j<n;j++)
{
arr[j]=in.nextInt();
}
sortInsertion(arr);
System.out.println("The Sorted array is");

for(int i=0;i<arr.length;++i)
{
System.out.print(arr[i] + " ");
}
}
}

OUTPUT:

RESULT:
Thus the java program for insertion sort has been implemented successfully.
EX.NO: 2.a) STACK

DATE :

AIM:

To design a java application to implement Stack using classes and objects.

ALGORITHM:

1. Start the program.


2. Include the needed header file.
3. Create a class named Operation.
4. Define all the stack operations.
5. Create a main class named StackExample.
6. Create a Stack.
7. Get the choice from the user for the stack operations.
8. Using the object invoke all the stack functions.
9. Stop the program.

PROGRAM:

import java.util.*;
class Operation
{
void push(Stack<Integer>stack,int a)
{
stack.push(a);
}
void pop(Stack<Integer> stack)
{
int ele=(Integer)stack.pop();
System.out.println("The popped element:"+ele);
}
void peek(Stack<Integer> stack)
{
int ele=(Integer)stack.peek();
System.out.println("The top element of the stack:"+ele);
}
void search(Stack<Integer>stack,int a)
{
int pos=(Integer)stack.search(a);
if(pos==0)
System.out.println("Element not found");
else
System.out.println("Element is found in the position"+pos);
}

class StackExample
{
public static void main(String[] args)
{
Stack<Integer> stack= new Stack<Integer>();
Scanner in=new Scanner(System.in);
Operation ob=new Operation();
int ch,data;
do
{
System.out.println("\n1.Push\n2.Pop\n3.Peek\n4.Search\n5.Exit\nEnter the choice:");
ch=in.nextInt();

switch(ch)
{
case 1:
System.out.println("Enter the data");
data=in.nextInt();
ob.push(stack,data);
System.out.println("Elements in the stack:"+stack);
break;
case 2:
ob.pop(stack);
System.out.println("Elements in the stack:"+stack);
break;
case 3:
ob.peek(stack);
break;
case 4:
System.out.println("Enter the data to be searched");
data=in.nextInt();
ob.search(stack,data);
break;
case 5:
ch=0;
break;
default:
System.out.println("Enter the proper choice");
}
}while(ch>0);
}
}
OUTPUT:

RESULT:
Thus the java application to implement Stack using classes and objects was executed successfully.
EX.NO: 2.b) QUEUE

DATE :

AIM:

To design a java application to implement Queue using classes and objects.

ALGORITHM:

1. Start the program.


2. Include the needed header file.
3. Create a class named Operation.
4. Define all the queue operations.
5. Create a main class named QueueExample.
6. Create a Queue.
7. Get the choice from the user for the queue operations.
8. Using the object invoke all the queue functions.
9. Stop the program.

PROGRAM:

import java.util.*;

class Operation

void enqueue(Queue<Integer> queue,int a)

queue.add(a);

void dequeue(Queue<Integer> queue)

int ele=(Integer)queue.poll();

System.out.println("The popped element:"+ele);

void peek(Queue<Integer> queue)

int ele=(Integer)queue.peek();

System.out.println("The head of the queue:"+ele);

class QueueExample
{

public static void main(String[] args)

Queue<Integer> queue= new LinkedList<Integer>();

Scanner in=new Scanner(System.in);

Operation ob=new Operation();

int ch,data,value;

do

System.out.println("\n1.Enqueue\n2.Dequeue\n3.Peek\n4.Exit\nEnter the choice:");

ch=in.nextInt();

switch(ch)

case 1:

System.out.println("Enter the data");

data=in.nextInt();

ob.enqueue(queue,data);

System.out.println("Elements in the queue:"+queue);

break;

case 2:

ob.dequeue(queue);

System.out.println("Elements in the queue:"+queue);

break;

case 3:

ob.peek(queue);

break;

case 4:

ch=0;

break;

default:

System.out.println("Enter the proper choice ");


}

}while(ch>0);

OUTPUT:
RESULT:
Thus the java application to implement Queue using classes and objects was executed successfully.
EX NO: 3 PAYSLIP GENERATION USING INHERITANCE
DATE:

AIM:

To develop a java application with 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 fund. Generate pay slips
for the employees with their gross and net salary.
.
ALGORITHM:

1. Create the class Employee with name,Empid,address,mailid,mobile no as data 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 as12%ofBP,Staffclubfund as 0.1% of BP.
5. Calculate grosssalary and netsalary.
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 sc = new Scanner(System.in);

void getdata()

System.out.println("Enter Name of the Employee");

name = sc.nextLine();

System.out.println("Enter Mail id");

mailid = sc.nextLine();

System.out.println("Enter Address of the Employee:");


address =sc.nextLine();

System.out.println("Enter employee id ");

empid = sc.nextInt();

System.out.println("Enter Mobile Number");

mobile = sc.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 = sc.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("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 Asstprofessor extends Employee

double salary,bp,da,hra,pf,club,net,gross;

void getasst()

System.out.println("Enter basic pay");

bp = sc.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 = sc.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 =sc.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

Scanner sc = new Scanner(System.in);

System.out.println("PAYROLL");

System.out.println(" 1.PROGRAMMER \n2.ASSISTANT PROFESSOR \n 3.ASSOCIATE


PROFESSOR \n 4.PROFESSOR ");

System.out.println("Enter Your Choice:");

choice=sc.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;

default:

System.out.println(“Enter the proper choice:”);


}

System.out.print("Please enter 0 to quit and 1 to continue: ");

cont=sc.nextInt();

}while(cont==1);

OUTPUT:
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 ABSTRACT CLASS

DATE :

AIM:

To write a Java Program to create an abstract class named Shape that contains two
integers and an empty method named printArea(). Provide three classes named Rectangle,
Triangle and Circle such that each one of the classes extends the class Shape. Each one of the
classes contains only the method printArea() that prints the area of the given shape.

ALGORITHM:
1. Start the program.
2. Create an abstract class as Shape it contains two integers and one abstact method as
printArea().
3. Create the classes Rectangle,Triangle and Circle which extends from the class Shape.
4. Calculate the area of the given shape in the method printArea().
5. Stop the program.

PROGRAM:

import java.util.*;

abstract class Shape

public int x, y;

public abstract void printArea();

class Rectangle extends Shape

public void printArea()

System.out.println("Area of Rectangle is " + x * y);

class Triangle extends Shape

public void printArea()

{
System.out.println("Area of Triangle is " + (x * y) / 2);

class Circle extends Shape

public void printArea()

System.out.println("Area of Circle is " + (22 * x * x) / 7);

class AbstractExample

public static void main(String[] args)

Scanner sc=new Scanner(System.in);

Rectangle r = new Rectangle();

System.out.println("Rectangle");

System.out.println("Enter the length:");

r.x = sc.nextInt();

System.out.println("Enter the breadth:");

r.y = sc.nextInt();

r.printArea();

System.out.println("Triangle");

Triangle t = new Triangle();

System.out.println("Enter the base:");

t.x = sc.nextInt();

System.out.println("Enter the height:");

t.y = sc.nextInt();
t.printArea();

System.out.println("Circle");

Circle c = new Circle();

System.out.println("Enter the radius:");

c.x = sc.nextInt();

c.printArea();

OUTPUT:
RESULT:

Thus the program for calculating the area of the given shape using abstract class was
executed successfully.
EX.NO : 5 INTERFACES

DATE :

AIM:

To write a Java Program to create interfaces named Data and Method that contains two
integers and an empty method named print Area(). Provide three classes named Rectangle,
Triangle and Circle such that each one of the classes implements the interfaces Data and
Method . Each one of the classes contains only the method print Area () that prints the area of
the given shape.

ALGORITHM:
1. Start the program.
2. Create an interface as Data with 2 integers and another interface as Method that contains
themethod printArea().
3. Create a classes as Rectangle,Triangle and Circle which implements the interfaces Data
and Method.
4. Calculate the area of the given shapes in the method printArea().
5. Stop the program.

PROGRAM:

interface Data

static final int x=24,y=45;

interface Method

public void printArea();

class Rectangle implements Data ,Method

public void printArea()

System.out.println("Area of Rectangle is " + (x * y));

class Triangle implements Data,Method

{
void printArea()

System.out.println("Area of Triangle is " + (x * y) / 2);

class Circle implements Data,Method

public void printArea()

System.out.println("Area of Circle is " + (22 * x * x) / 7);

class MainShape

public static void main(String[] args)

Rectangle r = new Rectangle();

Triangle t = new Triangle();

Circle c = new Circle();

r.printArea();

t.printArea();

c.printArea();

}
OUTPUT:

RESULT:

Thus the java program to implement the interface for calculating the area of the given shape
was executed successfully.
EX. NO:6 IMPLEMENT USER DEFINED EXCEPTION HANDLING

DATE :

AIM:

To write a Java program to implement user defined exception handling.

ALGORITHM:

1.Start the program .

2.Get input from the user.

3. If the number is negative throw a exception.

4.Else find the square root of the given number and display it.

5. Stop the program.

PROGRAM:

import java.util.*;

import java.io.*;

class MyException extends Exception

MyException(String str)

System.out.println(str);

class ExceptionExample

public static void main(String a[])

int no;

try

Scanner sc=new Scanner(System.in);


System.out.println("Enter a number :");

no=sc.nextInt();

if(no<0)

throw new MyException("Number must be positive");

else

System.out.println(" Square root :"+Math.sqrt(no));

catch(Exception e)

}
OUTPUT:

RESULT:

Thus the program for user defined exception handling was executed successfully.
EX. NO:7 MULTI THREADING

DATE :

AIM:

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

ALGORITHM:

1. Start the program.

2. First thread generates random number every 1 second.

3. If even, computes square of number

4. If odd, computes cube of number.

5. Display the square and cube values.

6. Stop the program.

PROGRAM:

import java.util.*;

import java.lang.*;

class EvenThread implements Runnable

public int x;

public EvenThread(int x)

this.x = x;

public void run()

System.out.println("New Thread "+ x +" is EVEN \n Square of " + x + " is: " + x *x);

class OddThread implements Runnable


{

public int x;

public OddThread(int x)

this.x = x;

public void run()

System.out.println("New Thread "+ x +" is ODD\n Cube of " + x + " is: " + x * x *x);

class RandomThread 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("Generated Random Number is " + num);

if (num % 2 == 0)

Thread t1 = new Thread(new EvenThread(num));

t1.start();

else

{
Thread t2 = new Thread(new OddThread(num));

t2.start();

Thread.sleep(1000);

System.out.println(" ");

catch (Exception e)

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

public class ThreadExample

public static void main(String args[])

RandomThread rt = new RandomThread();

rt.start();

}
OUTPUT:

RESULT:

Thus the program for multi threading was executed successfully .


EX. NO:8 FILE HANDLING
DATE:

AIM :

To write a Java program that performs different file operations

ALGORITHM:

1. Start the program .


2. Get the filename from the user and create a new file.
3. Read the data from the file student.txt
4. Copy the data in the created file.
5. Display the data in the file.

PROGRAM:

import java.io.*;

import java.util.*;

class FileOperation

public static void main(String args[]) throws IOException

Scanner sc=new Scanner(System.in);

System.out.println(“Enter the name of the file to be created”);

String str=sc.nextLine();

File f=new File(str);

InputStream in = new FileInputStream(“D:\\student.txt”);

OutputStream out=new FileOutputStream(f);

DataInputStream dis = new DataInputStream(in);

DataOutputStream dos=new DataOutputStream(out);

String filename=f.getPath();

System.out.println(“Name of the created file:”+filename);

String path=f.getAbsolutePath();

System.out.println(“Path of the created file:”+path);

int count = in.available();

System.out.println(“Size of the data in file:”+count);

byte[] data = new byte[count];


System.out.println(“Copying the datas to the file :”+f+”.txt”);

dis.read(data);

System.out.println(“Data in the file: “+filename);

for(int i=0;i<count;i++)

char k=(char)data[i];

out.write(data[i]);

System.out.print(k+””);

OUTPUT:

RESULT:

Thus the java program to implement the different file operations was executed
successfully.
EX. NO:9 GENERIC METHOD

DATE:

AIM:

To write a java program for finding the maximum value from the given integer and
float array using a generic method.

ALGORITHM:

1. Start the program.


2. Integer values are stored in an array.
3. Find maximum value of give n array.
4. Float values are stored in an array.
5. Find maximum value of the given array.
6. Stop the program.

PROGRAM:

import java.lang.*;

import java.util.*;

class Maximum

public static <E extends Comparable<E>> E max(E[] list)

E max = list[0];

for (int i = 1; i < list. length; i++)

if (list[i].compareTo(max)>0)

max = list[i];

return max;

class GenericExample

{
public static void main (String args[])

Integer arr1[] = {10, 15,45,34,75,25,89,64,72,5};

String str=arr1.toString();

System.out.println("\nElements in the integer array:\n");

for(int i=0;i<10;i++)

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

System.out.println("\nMaximum: "+Maximum.max(arr1));

Float arr2[] = {1.1f, 25.3f,3.1f,5.5f,13.7f,8.4f,15.6f,46.5f,76.5f,34.9f};

System.out.println("\nElements in the float array:\n");

for(int j=0;j<10;j++)

System.out.print(arr2[j]+" ");

System.out.println("\n\nMaximum: "+Maximum.max(arr2));

OUTPUT:
RESULT:

Thus the java program for finding the maximum value from the given integer and float
array using a generic method was implemented successfully.
EX.NO 10.a) JAVA FX CONTROLS

DATE :

AIM:
To write a Java program for javafx controls.

ALGORITHM:
1. Extend javafx.application.Application and override start ().
2.Create a button.
3. Create a layout and add button to it.
4. Create a scene.
5. Prepare the stage.
6. Create an event for the button.
7. Create the main method.

PROGRAM:

package application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import javafx.scene.layout.StackPane;
public class Hello_World extends Application
{
public void start(Stage primaryStage) throws Exception
{
// TODO Auto-generated method stub
Button btn1=new Button("Say, Hello World");
btn1.setOnAction(new EventHandler<ActionEvent>()
{
public void handle(ActionEvent arg0) {
// TODO Auto-generated method stub
System.out.println("hello world");
}
});
StackPane root=new StackPane();
root.getChildren().add(btn1);
Scene scene=new Scene(root,600,400);
primaryStage.setTitle("First JavaFX Application");
primaryStage.setScene(scene);
primaryStage.show();
}
publicstaticvoid main (String[] args)
{
launch(args);
}
}
OUTPUT:

RESULT:

Thus the program for javafx controls was executed successfully.


EX.NO:10.b) BORDER LAYOUTS

DATE :

AIM:
To write java program to create a layout.

ALGORITHM:
1.Start the program.
2. Call the constructors.
3. To arrange the components in Five Regions: North, South, East, West,
Center.
4.Each region may contain one Component only.
5. Display the layout.
6. Stop the program.

PROGRAM:
import java.awt.*;
import javax.swing.*;
public class Border
{
JFrame f;
Border()
{
f = new JFrame();
JButton b1 = new JButton("NORTH");;
JButton b2 = new JButton("SOUTH");;
JButton b3 = new JButton("EAST");;
JButton b4 = new JButton("WEST");;
JButton b5 = new JButton("CENTER");;
f.add(b1, BorderLayout.NORTH);
f.add(b2, BorderLayout.SOUTH);
f.add(b3, BorderLayout.EAST);
f.add(b4, BorderLayout.WEST);
f.add(b5, BorderLayout.CENTER);
f.setSize(300, 300);
f.setVisible(true);
}
public static void main(String[] args)
{
new Border();
}
}

OUTPUT:
RESULT:
Thus the program for border layout was executed successfully.
EX.NO: 10.c) MENUS

DATE :

AIM:
To write a java program to create a menu and display.

ALGORITHM:
1. Create a menu using JMenu class.
2. To create menu items use the JMenuItem class.
3. Create a menu bar using JMenuBar and add the menu to it.
4. Set the menu to the frame using setJMenuBar function.
5. Display the label of the menu item selected.

PROGRAM:

import javax.swing.*;
class MenuExample
{
JMenu menu, submenu;
JMenuItem i1, i2, i3, i4, i5;
MenuExample()
{
JFrame f= new JFrame("Menu and MenuIte m Example");
JMenuBar mb=new JMenuBar();
menu=new JMenu("Menu");
submenu=new JMenu("Sub Menu");
i1=new JMenuItem("Item 1");
i2=new JMenuItem("Item 2");
i3=new JMenuItem("Item 3");
i4=new JMenuItem("Item 4");
i5=new JMenuItem("Item 5");
menu.add(i1); menu.add(i2); menu.add(i3);
submenu.add(i4); submenu.add(i5);
menu.add(submenu);
mb.add(menu);
f.setJMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new MenuExample();
}}

OUTPUT:

RESULT:
Thus the program for menus was executed successfully.

You might also like