KEMBAR78
Oop Lab | PDF | String (Computer Science) | Queue (Abstract Data Type)
0% found this document useful (0 votes)
4 views51 pages

Oop Lab

lab manual

Uploaded by

Divya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views51 pages

Oop Lab

lab manual

Uploaded by

Divya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 51

Java Program for Sequential Search (Linear Search)

Coding:

class Linear
{
public static int search(int arr[], int x)
{
Int n = arr.length;
for (int i = 0; i < n; i++)
{
if (arr[i] == x)
return i;
}
return -1;
}
public static void main(String args[])
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int result = search(arr, x);
if (result == -1)
System.out.print( "Element is not present in array");
else
System.out.print("Element is present at Index " +search(arr,x));
}
}
Output :

Element is present at index 3


Java Program for Binary Search

Coding:

class BinarySearch
{
int binarySearch(int arr[], int l,int r, int x)
{
if (r >= l)
{
int mid = l + (r - l) /2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l,mid - 1, x);
return binarySearch(arr, mid + 1,r, x);
}
return -1;
}
public static void main(String args[])
{
BinarySearch ob = new BinarySearch();
int arr[] = { 2, 3, 4, 10, 40 };
int n = arr.length;
int x = 40;
int result = ob.binarySearch(arr, 0,n - 1, x);
if (result == -1)
System.out.println("Element "+ "not present");
else
System.out.println("Element found"+ " at index "+ result);
}
}
Output :

Element is present at index 4


Java Program for Selection Sort

Coding:

class SelectionSort
{
void sort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
{
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
public static void main(String args[])
{
SelectionSort ob = new SelectionSort();
int arr[] = {64,25,12,22,11};
ob.sort(arr);
System.out.println("Sorted array");
ob.printArray(arr);
}
}
Output :

Sorted array:
11 12 22 25 64
Java Program for Insertion Sort

Coding:
package insertionsort;
class InsertionSort
{
void sort(int arr[])
{
int n = arr.length;
for (int i = 1; i < n; ++i)
{ int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
static void printArray(int arr[])
{
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String args[])
{
int arr[] = { 12, 11, 13, 5, 6 };
InsertionSort ob = new InsertionSort();
ob.sort(arr);
printArray(arr);
}
}
Output :

5 6 11 12 13
Java Application for Stack Data Structure

Coding:

package stack;
class Stack
{
private int arr[];
private int top;
private int capacity;
Stack(int size)
{
arr = new
int[size]; capacity
= size; top = -1;
}
public void push(int x)
{
if (isFull()) {
System.out.println("Stack
OverFlow"); System.exit(1);
}
System.out.println("Inserting " + x);
arr[++top] = x;
}
public int pop()
{
if (isEmpty())
{
System.out.println("STACK EMPTY");
System.exit(1);
}
return arr[top--];
}
public int getSize()
{
return top + 1;
}
public Boolean isEmpty()
{
return top == -1;
}
public Boolean isFull()
{
return top == capacity - 1;
}

public void printStack()


{
for (int i = 0; i <= top; i++)
{
System.out.print(arr[i] + ", ");
}
}
public static void main(String[] args)
{
Stack stack = new
Stack(5); stack.push(1);
stack.push(2);
stack.push(3);
System.out.print("Stack: ");
stack.printStack();
stack.pop();
System.out.println("\nAfter popping out");
stack.printStack();
}
}
Output :

Inserting 1
Inserting 2
Inserting 3
Stack: 1, 2, 3,
After popping out
1, 2,
Java Application for Queue Data Structure

Coding:
package queue;
public class Queue
{
int SIZE = 5;
int items[] = new int[SIZE];
int front, rear;
Queue()
{ front = -1;
rear = -1;
}
boolean isFull() {
if (front == 0 && rear == SIZE - 1)
{
return true;
}
return false;
}
boolean isEmpty()
{
if (front == -1)
return true;
else
return false;
}
void enQueue(int element)
{
if (isFull())
{
System.out.println("Queue is full");
}
else {
if (front == -1)
{
front = 0;
}
rear++;
items[rear] = element;
System.out.println("Insert " + element);
}
}
int deQueue()
{
int element;
if (isEmpty())
{
System.out.println("Queue is empty");
return (-1);
}
else
{
element = items[front];
if (front >= rear)
{
front = -1;
rear = -1;
}
else
{
front++;
}
System.out.println( element + " Deleted");
return (element);
}
}
void display()
{
int i;
if (isEmpty())
{
System.out.println("Empty Queue");
}
else
{
System.out.println("\nFront index-> " + front);
System.out.println("Items -> ");
for (i = front; i <= rear; i++)
System.out.print(items[i] + " ");
System.out.println("\nRear index-> " + rear);
}
}
public static void main(String[] args)
{
Queue q = new Queue();
q.deQueue();
for(int i = 1; i < 6; i ++)
{
q.enQueue(i);
}
q.enQueue(6);
q.display();
q.deQueue();
q.display();
}
}
Output :

Queue is empty
Insert 1
Insert 2
Insert 3
Insert 4
Insert 5
Queue is full
Front index-> 0
Items ->
12345
Rear index-> 4
1 Deleted
Front index-> 1
Items ->
2345
Rear index-> 4
Java Program for Employee Payroll System

Coding:

Employee.java
package main;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;
class Employee
{ String emp_name;
String emp_id;
String emp_address;
String emp_mail_id;
String emp_mobile_no;
int basic_pay;
int per_day_pay;
int current_basic_pay;
int da, hra, pf,
gross_pay; int net_pay;
int no_of_days_in_current_month;
int no_of_days_worked;
Calendar cal;
Scanner input;
Employee() {
input = new Scanner(System.in);
cal = new GregorianCalendar();
no_of_days_in_current_month =
cal.getActualMaximum(Calendar.DAY_OF_MONTH);
getUserBasicDetails();
}
public void generatePaySlip() {
this.da = (this.current_basic_pay / 100) * 97;
this.hra = (this.current_basic_pay / 100) * 12;
this.pf = (int) ((this.current_basic_pay / 100) * 0.1);
this.gross_pay = this.current_basic_pay + da + hra + pf;
this.net_pay = gross_pay - pf;
}
public void displayPaySlip()
{ System.out.println("Name: " + this.emp_name);
System.out.println("ID: " + this.emp_id);
System.out.println("Address: " + this.emp_address);
System.out.println("MailID: " + this.emp_mail_id);
System.out.println("Mobile No: " + this.emp_mobile_no);
System.out.println("\nEarnings");
System.out.println("-------");
System.out.println("BASIC Pay: " + current_basic_pay + "Rs");
System.out.println("DA : " + da + " Rs");
System.out.println("HRA : " + hra + " Rs"); System.out.println("\
nDeductions");
System.out.println("---------");
System.out.println("PF : " + pf + " Rs");
System.out.println("GROSS Pay: " + gross_pay + " Rs");
System.out.println("NET Pay: " + net_pay + " Rs");
}
public void getUserBasicDetails() {
System.out.println("Enter Details");
System.out.println("Name: ");
this.emp_name = input.next();
System.out.println("ID: ");
this.emp_id = input.next();
System.out.println("Address: ");
this.emp_address = input.next();
System.out.println("MailID: ");
this.emp_mail_id = input.next();
System.out.println("Mobile No:");
this.emp_mobile_no =
input.next();
}
public void computeCurrentBasicPay(String empType)
{ this.per_day_pay = this.basic_pay / no_of_days_in_current_month;
System.out.println("\nBasic Pay of " + empType + " " + this.basic_pay + " for "
+ this.no_of_days_in_current_month + " days");
System.out.println("This month this " + empType + " gets " + this.per_day_pay + "INR as basic
pay per day");
System.out.println("Enter no.of days worked by " + empType + " including CL,WH, FH and
excluding LWP:");
this.no_of_days_worked = input.nextInt();
if (no_of_days_worked <= no_of_days_in_current_month)
{
this.current_basic_pay = this.per_day_pay * no_of_days_worked;
System.out.println("Programmer");
System.out.println("---------");
generatePaySlip();
}
else
{
System.out.println("Sorry Please Enter Valid Days");
}
}
protected void finalize()
{ input.close();
System.exit(0);
}
}
Programmer.java
package payscale;
public class Programmer extends Employee
{ public Programmer() {
super();
computeProgrammerPay();
}
public void computeProgrammerPay() {
System.out.println("Enter Basic pay of Programmer [enter -1 for Default [BP = 30000]]:");
this.basic_pay = input.nextInt();
if (this.basic_pay == -1)
{
this.basic_pay = 30000;
System.out.println("Default Pay Taken");
}
computeCurrentBasicPay("Programmer");
generatePaySlip();
displayPaySlip();
}
}
Assistant Professor.java
package payscale;
public class AssistantProfessor extends Employee
{ public AssistantProfessor() {
super();
computeAssistantProfessorPay();
}
public void computeAssistantProfessorPay() {
System.out.println("Enter Basic pay of AssistantProfessor [enter -1 for Default [BP =
25000]]:");
this.basic_pay = input.nextInt();
if (this.basic_pay == -1) {
this.basic_pay = 25000;
System.out.println("Default Pay Taken");
}
computeCurrentBasicPay("AssistantProfessor");
generatePaySlip();
displayPaySlip();
}
}
Associate Professor.java
package payscale;
public class AssociateProfessor extends Employee
{ public AssociateProfessor() {
super();
computeAssociateProfessorPay();
}
public void computeAssociateProfessorPay() {
System.out.println("Enter Basic pay of AssociateProfessor [enter -1 for Default [BP =
40000]]:");
this.basic_pay = input.nextInt();
if (this.basic_pay == -1)
{ this.basic_pay = 40000;
System.out.println("Default Pay Taken");
}
computeCurrentBasicPay("AssociateProfessor");
generatePaySlip();
displayPaySlip();
}
}
Professor.java
package payscale;
public class Professor extends Employee
{
public Professor()
{
super();
computeProfessorPay();
}
public void computeProfessorPay() {
System.out.println("Enter Basic pay of Professor [enter -1 for Default [BP = 70000]]:");
this.basic_pay = input.nextInt();
if (this.basic_pay == -1)
{
this.basic_pay = 70000;
System.out.println("Default Pay Taken");
}
computeCurrentBasicPay("Professor");
generatePaySlip();
displayPaySlip();
}
}
Main.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import payscale.AssistantProfessor;
import payscale.AssociateProfessor;
import payscale.Programmer;
import payscale.Professor;
public class Main {
public static void main(String[] args) throws IOException
{
Programmer aProgrammer;
AssistantProfessor aAssistantProfessor;
AssociateProfessor aAssociateProfessor;
Professor aProfessor;
String choice;
int n_choice =
0;
Scanner userInput = new Scanner("System.in");
while (n_choice != 5) { System.out.println("\n\
nEmployee Payroll System");
System.out.println("***********************\n");
System.out.println("1. Programmer\n2. Assistant Professor\n" + "3. Associate Professor\n4.
Professor\n"
+ "5. Exit\n\nEnter Your Choice");
choice = new BufferedReader(new InputStreamReader(System.in)).readLine();
n_choice = Integer.parseInt(choice);
switch (n_choice)
{ case 1:
System.out.println("Programmer Selected");
aProgrammer = new Programmer();
break;
case 2:
System.out.println("AssistantProfessor Selected");
aAssistantProfessor = new AssistantProfessor();
break;
case 3:
System.out.println("AssociateProfessor Selected");
aAssociateProfessor = new AssociateProfessor();
break;
case 4:
System.out.println("Professor Selected");
aProfessor = new Professor();
case 5:
System.out.println("Thank You !!!");
userInput.close();
break;
default:
System.out.println("Enter valid choice !!!");
break;
}
}
}
}
Output :

Choices

Basic Details

Programmer
Assistant Professor

Associate Professor
Professor
Java Application to Find the Area of different Shapes

Coding :
Shape.java
package shape;
public abstract class Shape
{
double length = 0.0;
double hight = 0.0;
public abstract void printArea();
}
Rectangle.java
package shapes;
import java.util.Scanner;
public class Rectangle extends Shape
{
double area = 0.0;
public void printArea()
{
System.out.println("\nRectangle");
System.out.println("--------\n");
Scanner input = new Scanner(System.in);
System.out.println("Enter Length of Rectangle : ");
this.length = input.nextDouble();
System.out.println("Enter Breadth of Rectangle :");
this.hight = input.nextDouble();
this.area = this.length * this.hight;
System.out.println("Area of the Rectangle is : " + this.area);
}
}
Triangle.java
package shapes;
import java.util.Scanner;
public class Triangle extends Shape
{
double area = 0.0;
public void printArea()
{
System.out.println("\nTriangle");
System.out.println("--------\n");
Scanner input = new Scanner(System.in);
System.out.println("Enter Length of Triangle : ");
this.length = input.nextDouble();
System.out.println("Enter Hight of Triangle : ");
this.hight = input.nextDouble();
this.area = 0.5 * this.length * this.hight;
System.out.println("Area of the Triangle is : " + this.area);
}
}
Circle.java
package shapes;
import java.util.Scanner;
public class Circle extends Shape
{
double area = 0.0;
public void printArea()
{
System.out.println("\nCircle");
System.out.println("------\n");
Scanner input = new Scanner(System.in);
System.out.println("Enter Radius of Circle : ");
this.length = input.nextDouble();
this.area = Math.PI * this.length * this.length;
System.out.println("Area of the Circle is : " + this.area);
}
}
Main.java
import shapes.Rectangle;
import shapes.Shape;
import shapes.Triangle;
import java.util.Scanner;
import shapes.Circle;
public class Main
{
public static void main(String[] args)
{
Scanner userInput = new Scanner(System.in);
int choice = 0;
do
{
System.out.println("Finding Area");
System.out.println("************");
System.out.println( "\n1. Rectangle" + "\n2. Triangle" + "\n3. Circle" + "\n4. Exit"
+ "\n\nEnter your choice: ");
choice = userInput.nextInt();
switch (choice)
{
case 1:
Shape rt = new Rectangle();
rt.printArea();
break;
case 2:
Shape tr = new Triangle();
tr.printArea();
break;
case 3:
Shape cr = new Circle();
cr.printArea();
break;
case 4:
System.out.println("\n\nThank You !!!");
userInput.close();
break;
default:
System.out.println("Please enter valid input");
break;
}
} while (choice != 4);
}
}
Output :
Choice

Rectangle

Triangle

Circle
Java Application to Find the Area of different Shapes - Interface

Coding:
Shape.java
package com.raja.oopslanb.shapes;
interface Shape
{
double length = 0.0;
double hight = 0.0;
public void printArea();
}
Rectangle.java
package com.raja.oopslanb.shapes;
import java.util.Scanner;
public class Rectangle implements Shape
{
double area = 0.0;
public void printArea()
{
System.out.println("\nRectangle");
System.out.println("--------\n");
Scanner input = new Scanner(System.in);
System.out.println("Enter Length of Rectangle : ");
this.length = input.nextDouble();
System.out.println("Enter Breadth of Rectangle :");
this.hight = input.nextDouble();
this.area = this.length * this.hight;
System.out.println("Area of the Rectangle is : " + this.area);
}
}
Triangle.java
package com.raja.oopslanb.shapes;
import java.util.Scanner;
public class Triangle implements Shape
{
double area = 0.0;
public void printArea()
{
System.out.println("\nTriangle");
System.out.println("--------\n");
Scanner input = new Scanner(System.in);
System.out.println("Enter Length of Triangle : ");
this.length = input.nextDouble();
System.out.println("Enter Hight of Triangle : ");
this.hight = input.nextDouble();
this.area = 0.5 * this.length * this.hight;
System.out.println("Area of the Triangle is : " + this.area);
}
}
Circle.java
package com.raja.oopslanb.shapes;
import java.util.Scanner;
public class Circle implements Shape
{
double area = 0.0;
public void printArea() {
System.out.println("\nCircle");
System.out.println("------\n");
Scanner input = new Scanner(System.in);
System.out.println("Enter Radius of Circle : ");
this.length = input.nextDouble();
this.area = Math.PI * this.length * this.length;
System.out.println("Area of the Circle is : " + this.area);
}
}
Main.java
import com.raja.oopslanb.shapes.Rectangle;
import com.raja.oopslanb.shapes.Shape;
import com.raja.oopslanb.shapes.Triangle;
import java.util.Scanner;
import com.raja.oopslanb.shapes.Circle;
public class Main
{
public static void main(String[] args)
{
Scanner userInput = new Scanner(System.in);
int choice = 0;
do
{
System.out.println("Finding Area");
System.out.println("************");
System.out.println("\n1. Rectangle" + "\n2. Triangle" + "\n3. Circle" + "\n4. Exit"
+ "\n\nEnter your choice: ");
choice = userInput.nextInt();
switch (choice)
{
case 1:
Shape rt = new
Rectangle();
rt.printArea();
break;
case 2:
Shape tr = new Triangle();
tr.printArea();
break;
case 3:
Shape cr = new Circle();
cr.printArea();
break;
case 4:
System.out.println("\n\nThank You !!!");
userInput.close();
break;
default:
System.out.println("Please enter valid input");
break;
}
} while (choice != 4);
}
}
Output :
Choice

Rectangle

Triangle

Circle
Bank Transaction System with User Exceptions

Coding:

Customer.java
package com.raja.oopslab.exception.bank;
import java.util.Scanner;
public class Customer
{
String name;
int accNo;
int balance;
public Customer(String name, int accNo)
{
this.name = name;
this.accNo = accNo;
this.balance = 0;
}
public void creditTransaction(int amount)
{
Scanner input = new
Scanner(System.in); try
{
if (amount < 0)
throw new InvalidCredit();
else
balance = balance + amount;
}
catch (InvalidCredit e)
{
amount = input.nextInt();
creditTransaction(amount);
}
}
public void debitTransaction(int amount)
{
Scanner input = new Scanner(System.in);
try
{
if (amount > balance)
throw new InvalidDebit();
else
balance = balance - amount;
}
catch (InvalidDebit e)
{
amount = input.nextInt();
debitTransaction(amount);
}
}
public void displayDetails(){ System.out.println("Customer Details");
System.out.println("****************");
System.out.println("Customer Name : "+this.name);
System.out.println("Customer AccNo : "+this.accNo);
System.out.println("Customer Current Balance : "+this.balance);
}
}
InvalidCredit.java
package com.raja.oopslab.exception.bank;
public class InvalidCredit extends Exception
{
public InvalidCredit() {
System.out.print("Please enter valid credit amount");
}
}
InvalidDebit.java
package com.raja.oopslab.exception.bank;
public class InvalidDebit extends Exception
{
public InvalidDebit()
{
System.out.print("Please enter valid debit amount");
}
}
Main.java
import java.util.Scanner;
import com.raja.oopslab.exception.bank.*;
public class Main {
public static void main(String[] args)
{
Scanner input = new
Scanner(System.in);
String name;
int acc_no;
System.out.println("Enter Customer Name");
name = input.next();
System.out.println("Enter account number");
acc_no = input.nextInt();
Customer aCustomer = new Customer(name, acc_no);
int choice = 0;
while(choice != 4){
System.out.println("\n1. Add Money\n2. Get Money\n3. Details\n4. Exit");
choice = input.nextInt();
switch(choice){
case 1:
System.out.println("Enter the amount");
aCustomer.creditTransaction(input.nextInt());
break;
case 2:
System.out.println("Enter the amount");
aCustomer.debitTransaction(input.nextInt());
break;
case 3:
aCustomer.displayDetails();
break;
case 4:
System.out.println("Thank You !!!");
break;
}
}
}
}
Output:
Choice:

Display Details:
Add Money:

Get Money:
Exception in Add Money:

Exception in Get Money:


Java Application for Multi threading

Coding:

RandomNumberThread.java
package com.raja.oopslab.threading;
import java.util.Random;
public class RandomNumberThread extends Thread
{
Random num = new Random();
int value;
public void run()
{
while(true)
{
try
{
this.sleep(1000);
}
catch (InterruptedException e)
{
}
value = num.nextInt(1000);
System.out.println("RandomNumberThread generated a number "+value);
if(value % 2 == 0)
new SquareGenThread(value).start();
else
}
}
}
new CubeGenThread(value).start();
SquareGenThread.java
package com.raja.oopslab.threading;
public class SquareGenThread extends
Thread
{
int number;
int squre;
public SquareGenThread(int number)
{
this.number = number;
}
public void run()
{
try
{
this.sleep(3000);
}
catch (InterruptedException e)
{
}
this.squre = this.number * this.number;
System.out.println("SquareGenThread--> Square of "+number+" is "+squre);
}
}
CubeGenThread.java
package com.raja.oopslab.threading;
public class CubeGenThread extends Thread
{
int number;
int squre;
public CubeGenThread(int number)
{
this.number = number;
}
public void run()
{
try
{
this.sleep(2000);
}
catch (InterruptedException e)
{
}
this.squre = this.number * this.number * this.number;
System.out.println("CubeGenThread--> Square of "+number+" is "+squre);
}
}
Main.java
import java.util.Random;
import com.raja.oopslab.threading.RandomNumberThread;
public class Main
{
public static void main(String[] args)
{
new RandomNumberThread().start();
}
}
Output:
Java Application for File Handling

Coding:

UserFileHandler.java
package com.raja.oopslab.files;
import java.io.File;
public class UserFileHandler
{
File aFile;
boolean isReadable = false;
boolean isWriteable = false;
boolean isExists = false;
int length = 0;
public UserFileHandler(String path)
{
aFile = new File(path);
this.isExists = aFile.exists();
this.isReadable = aFile.canRead();
this.isWriteable =aFile.canWrite();
this.length = (int) aFile.length();
}
public void fileDetails(){
if(isExists){
System.out.println("The File "+aFile.getName()+" is Available at:"+aFile.getParent());
if(isReadable && isWriteable)
System.out.println("File is Readable and Writeable");
else if(isReadable)
System.out.println("File is Only Readable");
else if(isWriteable)
System.out.println("File is Only Writeable");
System.out.println("Total length of the file is :"+this.length+" bytes");
}
else
}
System.out.println("File does not exists ");
}
Main.java
import java.io.File;
import java.util.Scanner;
import com.raja.oopslab.files.*;
public class Main
{
public static void main(String[] args)
{
String file_path = null;
Scanner input = new Scanner(System.in);
System.out.println("File Handler");
System.out.println("************");
System.out.println("Enter the file path");
file_path = input.next();
new UserFileHandler(file_path).fileDetails();
}
}
Output:

Availability of File:

File Read and Writeable:


File Size:

File Not Exists:


Java Application for Generic Max Finder

Coding :

class GenericMax
{
public <T extends Comparable<T>> void maxFinder (T[] array)
{
T max = array[0];
for(T element: array)
{
System.out.println(element);
if(element.compareTo(max) > 0)
max = element;
}
System.out.println("Max is : "+max);
}
}
public class Main {
public static void main(String[] args)
{
GenericMax max = new GenericMax();
Integer[] numbers = {14,3,42,5,6,10};
String[] strings = {"R","Ra","Raj"};
max.maxFinder(numbers);
max.maxFinder(strings);
}
}
Output:

14
3
42
5
6
10
Max is: 42
R
Ra
Raj
Max is : Raj
BUILD SUCCESSFUL (total time: 0 seconds)
Java applications using JavaFX controls, layouts and menus

Coding:

package menubar_2;
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;
public class MenuBar_2 extends Application
{
public void start(Stage s)
{
s.setTitle("creating MenuBar");
Menu m = new Menu("Menu");
MenuItem m1 = new MenuItem("menu item 1");
MenuItem m2 = new MenuItem("menu item 2");
MenuItem m3 = new MenuItem("menu item 3");
m.getItems().add(m1);
m.getItems().add(m2);
m.getItems().add(m3);
Label l = new Label("\t\t\t\t"+ "no menu item selected");
EventHandler<ActionEvent> event = new
EventHandler<ActionEvent>()
{
public void handle(ActionEvent e)
{
1.setText("\t\t\t\t" + ((MenuItem)e.getSource()).getText() +" selected");
}
};
m1.setOnAction(event);
m2.setOnAction(event);
m3.setOnAction(event);
MenuBar mb = new MenuBar();
mb.getMenus().add(m);
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);
}
}
Output

You might also like