Cs3381 Oops
Cs3381 Oops
LAB MANUAL
PAGE
S. NO. LIST OF EXPERIMENTS / PROGRAMS
NO.
1A SEQUENTIAL SEARCH 1
1B BINARY SEARCH 3
1C SELECTION SORT 5
1D INSERTION SORT 7
2A STACK USING CLASSES AND OBJECTS. 9
2B QUEUE USING CLASSES AND OBJECTS. 12
3 GENERATING PAY SLIP FOR EMPLOYEES 15
4 FINDING AREA USING ABSTRACT CLASS 19
5 FINDING AREA USING INTERFACE 22
6A JAVA EXCEPTION HANDLING 25
6B USER DEFINED EXCEPTION HANDLING 26
7 MULTITHREADING 28
8 INPUT/OUTPUT FILES 32
9 GENERIC CLASSES 35
10A JAVAFX CONTROLS AND LAYOUTS 37
10B JAVAFX MENUS 39
CS3381 / OBJECT ORIENTED PROGRAMMING LABORATORY
LIST OF EXPERIMENTS
COURSE OBJECTIVES:
To build software development skills using java programming for real-world applications.
To understand and apply the concepts of classes, packages, interfaces, inheritance, exception handling and file
processing.
To develop applications using generic programming and event handling
LIST OF EXPERIMENTS
1. Solve problems by using sequential search, binary search, and quadratic sorting algorithms
(selection,insertion)
2. Develop stack and queue data structures using classes and objects.
3. Develop a java application with an Employee class with Emp_name, Emp_id, Address, Mail_id,
Mobile_no as members. Inherit the classes, Programmer, Assistant Professor, Associate Professor and
Professor from employee class. Add Basic Pay (BP) as the member of all the inherited classes with
97% of BP as DA, 10 % of BP as HRA, 12% of BP as PF, 0.1% of BP for staff club funds. Generate
pay slips for the employees with their gross and net salary.
4. 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.
5. Solve the above problem using an interface.
6. Implement exception handling and creation of user defined exceptions.
7. Write a java program that implements a multi-threaded application that has three threads. First thread
generates a random integer every 1 second and if the value is even, the second thread computes the
square of the number and prints. If the value is odd, the third thread will print the value of the cube of
the number.
8. Write a program to perform file operations.
9. Develop applications to demonstrate the features of generics classes.
10. Develop applications using JavaFX controls, layouts and menus.
11. Develop a mini project for any application using Java concepts.
TOTAL: 45 PERIODS
COURSE OUTCOMES:
On completion of this course, the students will be able to:
CO1: Design and develop java programs using object oriented programming concepts
CO2: Develop simple applications using object oriented concepts such as package, exceptions
CO3: Implement multithreading, and generics concepts
CO4: Create GUIs and event driven programming applications for real world problems
CO5: Implement and deploy web applications using Java.
Aim
To write a Java program to solve problems in array by using sequential search
algorithm.
Algorithm:
1. Start theprogram
2. Create SequentialSearch class with one dimensional array.
3. Read the search element from the user.
4. Compare the search element with the first element in the list
5. If both are matched, then display "Given element is found!!!" and terminate the function
6. If both are not matched, then compare search element with the next element in the list.
7. Repeat steps 3 and 4 until search element is compared with last element in the list.
8. If last element in the list also doesn't match, then display "Element is not found!!!" and
terminate the function.
9. Stop the program.
Program:
public class SequentialSearch{
public static void main(String[]args){
int[]One={2,9,6,7,4,5,3,0,1};
int target=4;
sequentialSearch(One,target);
}
public static void sequentialSearch(int[]a,intb){
int index=-1;
for(int i=0;i<a.leangth;i++){
if(a[i]==b){
index=i;
break;
}
}
if(index==-1){
System.out.println("Your target integer does not exist in the array");
}
1
else{
System.out.println("Your target integer is in index"+index+"of the array");
}
}
}
Output:
Result:
Thus the program for solving problems in array using sequential search algorithm has been
executed and the output was verified.
2
EX.NO:1.B BINARY SEARCH
DATE:
Aim
To write a Java program to solve problems in array by using binary search algorithm.
Algorithm
1. Start theprogram
2. Create BinarySearch class with one dimensional array.
3. Read the search element from the user.
4. Find the middle element in the sorted list.
5. Compare the search element with the middle element in the sorted list.
6. If both are matched, then display "Given element is found!!!" and terminate the function.
7. If both are not matched, then check whether the search element is smaller or larger than the
middle element.
8. If the search element is smaller than middle element, repeat steps 2, 3, 4 and 5 for the left
sublist of the middle element.
9. If the search element is larger than middle element, repeat steps 2, 3, 4 and 5 for the right
sublist of the middle element.
10. Repeat the same process until we find the search element in the list or until sublist contains
only one element.
11. If that element also doesn't match with the search element, then display "Element is not found
in the list!!!" and terminate the function.
12. Stop the program.
Program:
class BinarySearch{
public static int binarySearch(int arr[],int first,int last,int key){
if(last>=first){
int mid=first+(last-first)/2;
if(arr[mid]==key){
return mid;
}
if(arr[mid]>key){
return binarySearch(arr,first,mid-1,key);??search in left subarray
}else{
return binarySearch(arr,mid+1,last,key);//search in right subarray
}
}
return-1;
}
3
public static void main(String args[]){
int arr[]={10,20,30,40,50};
int key=30;
int last=arr.length-1;
int result=binarySearch(arr,0,last,key);
if(result==-1)
System.out.println("Element is not found!");
else
System.out.println("Element is found at index:"+result);
}
}
Output:
Element is found at index : 2
Result:
Thus the program for solving problems in array using binary search algorithm has been executed
and the output was verified.
4
EX.NO:1.C. SELECTION SORT
DATE:
Aim:
To write a Java program to solve problems in array by using selection sort algorithm.
Algorithm:
1. Start the program.
2. Create a SelectionSort class with one dimensional array.
3. Select the first element of the list (i.e., Element at first position in the list).
4. Compare the selected element with all the other elements in the list.
5. In every comparision, if any element is found smaller than the selected element (for
Ascending order), then both are swapped.
6. Repeat the same procedure with element in the next position in the list till the entire list is
sorted.
7. Stop the program
Program:
public class SelectionSort{
public static void selectionSort(int[]arr){
for(int i=0;i<arr.leangth-1;i++){
int index=i;
for(int j=i+1;j<arr.length;j++){
if(arr[j]<arr[index]){
index=j;
}
}
int smallerNumber=arr[index];
arr[index]=arr[i];
arr[i]=smallerNumber;
}
}
public static void main(String a[]){
int[]arr1={9,14,3,2,43,11,58,22};
System.out.println("Befor Selection Sort");
for(int i:arr1){
System.out.print(i+"");
}
System.out.println();
selectionSort(arr1);
System.out.println("After Selection Sort");
5
for(int i:arr1){
System.out.print(i+"");
}
}
}
Output:
Result:
Thus the program for solving problems in array using selection sort algorithm has been
executed and the output was verified.
6
EX.NO:1.D INSERTION SORT
DATE:
Aim
To write a Java program to solve problems in array by using insertion sort algorithm.
Algorithm
Program:
public class InsertionSort{
public static void insertionSort(int array[]){
int n=array.length;
for(int j=1;j<n;j++){
int key=array[j];
int i=j-1;
while((i>-1)&&(array[i]>key)){
array[i+1]=array[i];
i--;
}
array[i+1]=key;
}
}
public static void main(String a[]){
int[]arr1={9,14,3,2,43,11,58,22};
System.out.println("Before Insertion Sort");
for(int i:arr1){
System.out.print(i+"");
}
System.out,println();
insertionSort(arr1);//sorting array using insertion sort
System.out.println("After Insertion Sort");
for(int i:arr1){
7
System.out.print(i+"");
}
}
}
Output:
Result:
Thus the program for solving problems in array using insertion sort algorithm has been
executed and the output was verified.
8
EX.NO:2.A STACK
DATE:
Aim
To write a Java program to develop a stack data structures using classes and objects.
Algorithm:
Program:
class Stack{
privateintarr[];
privateint top;
privateint capacity;
Stack(int size){
arr=new int[size];
capacity=size;
top=-1;
}
public void push(int x){
if(isFull()){
System.out.println("Overflow\nProgram Terminated\n");
System.exit(-1);
}
System.out.println("Inserting"+x);
arr[++top]=x;
}
9
publicint pop(){
if(isEmpty()){
System.out.println("Underflow\nProgram Terminated");
System.exit(-1);
}
System.out.println("Removing"+peek());
returnarr[top--];
}
publicint peek(){
if(!isEmpty()){
returnarr[top];
}
else{
System.exit(-1);
}
return-1;
}
publicint size(){
return top+1;
}
public boolean isFull(){
return top==capacity-1;
}
}
class Main{
public static void main(Sting[]args){
Stack stack=new Stack(3);
stack.push(1);
stack.push(2);
stack.pop();
stack.pop();
stack.push(3);
System.out.println("The top element is"+stack.peek());
System.out.println("The stack size is"+stack.size());
stack.pop();
if(stack.isEmpty()){
System.out.println("The stack is empty");
}
else{
System.out.println("The stack is not empty");
}
}
} 10
Output:
Inserting 1
Inserting 2
Removing 2
Removing 1
Inserting 3
Thr Top element is 3
The Stack size is 1
Removing 3
The Stack is empty
Result:
Thus the Java program to develop a stack data structures using classes and objects
has been executed and the output was verified.
11
EX.NO:2.B QUEUE
DATE:
Aim
To write a Java program to develop a queue data structures using classes and
objects.
Algorithm:
1. Create a class DemoQueue.
2. Declare all the user defined functions which are used in queue implementation.
3. Create a one dimensional array with above defined SIZE
4. Define two integer variables 'front' and 'rear' and initialize rear with '-1'.
5. Then implement main method by displaying menu of operations list and make suitable
function calls to perform operation selected by the user on queue.
6. Check whether queue is FULL. If it is FULL, then display "Queue is FULL!!! . If it is NOT
FULL, then increment rear value by one (rear++) and set queue[rear] = item.
7. Check whether queue is EMPTY. If it is EMPTY, then display "Queue is EMPTY!!!" and
terminate the function. If it is not empty, then define an integer variable 'i' and set
'i = front+1'.
8. Display 'queue[i]' value and increment 'i' value by one (i++). Repeat the same until 'i' value
reaches to rear .
9. Stop the program.
Program:
public class DemoQueue{
private int maxSize;
private int[]queueArray;
private int front;
private int rear;
private int currentSize;
public DemoQueue(int size){
this.maxSize=size;
this.queueArray=new int[size];
front=0;
rear=-1;
currentSize=0;
}
public void insert(int item){
if(isQueueFull()){
System.out.println("Queue is full!");
return;
}
12
if(rear==maxSize-1){
rear=-1;
}
queueArray[++rear]=item;
currentSize++;
System.out.println("Item added to queue:"+item);
}
public int delete(){
if(isQueueEmpty()){
throw new RuntimeException("Queue is empty");
}
int temp=queueArray[front++];
if(front==maxSize){
front=0;
}
currentSize--;
return temp;
}
public int peek(){
return queueArray[front];
}
public boolean isQueueFull(){
return(maxSize==currentSize);
}
public bollean isQueueEmpty(){
return(currentSize==0);
}
public static void main(String[]args){
DemoQueue queue=new DemoQueue(10);
queue.insert(2);
queue.insert(3);
System.out.println("Item deleted from queue:"+queue.delete());
System.out.println("Item deleted from queue:"+queue.delete());
queue.insert(5);
System.out.println("Item deleted from queue:"+queue.delete());
}
}
13
Output:
Result:
Thus the Java program to develop a queue data structures using classes and objects has been
executed and the output was verified.
14
EX.NO:3 GENERATING PAY SLIP FOR EMPLOYEES
DATE:
Aim
To develop a Java application with employee class and generate pay slips for the
employees with their gross and net salary.
Algorithm
13. Start theprogram
14. Create Employee class with Emp_name, Emp_id, Address, Mail_id,
Mobile_noas members.
15. Inherit the classes, Programmer, Assistant Professor, Associate
Professorand Professor from employeeclass.
16. Add Basic Pay (BP) as the member of all the inherited classes with 97% of BP
asDA, 10 % of BP as HRA, 12% of BP as PF, 0.1% of BP for staff clubfund.
17. Generate pay slips for the employees with their gross and netsalary.
18. Stop theprogram
PROGRAM:
import java.util.Scanner;
class Employee
{
int Emp_id;
String Emp_name;
Stringaddress;
Stringmail_id;
int mobile_no;
double bp,gross_salary,net_salary;
Employee()
{}
Employee(int id,String name,String addr,String mail, int mob)
{
this.Emp_id=id;
this.Emp_name=name;
this.address=addr;
this.mail_id=mail;
this.mobile_no=mob;
}
void computepay()
{ 15
System.out.println("Enter basic pay:");
Scanner input=new Scanner(System.in);
bp=input.nextDouble();
double da,hra,pf,fund;
da=(bp*97/100);
hra=(bp*10/100);
pf=(bp*12/100);
fund=(bp*0.1/100);
gross_salary=bp+da+hra;
net_salary=bp+da+hra-(pf+fund);
System.out.println("Emp_id:"+Emp_id);
System.out.println("Emp_name:"+Emp_name);
System.out.println("Address:"+address);
System.out.println("mail_id:"+mail_id);
System.out.println("mobile_number:"+mobile_no);
System.out.println("Gross_pay:"+gross_salary);
System.out.println("net pay:"+net_salary);
}
}
class Programmer extends Employee
{
public Programmer(int id,String name,String addr,String mail, int mob)
{
super(id,name,addr,mail,mob);
}
}
class Asst_prof extends Employee
{
public Asst_prof(int id,String name,String addr,String mail, int mob)
{
super(id,name,addr,mail,mob);
}
}
class Assoc_prof extends Employee
{
public Assoc_prof(int id,String name,String addr,String mail, int mob)
{
super(id,name,addr,mail,mob);
}
}
class Prof extends Employee
{
public Prof(int id,String name,String addr,String mail, int mob)
{
super(id,name,addr,mail,mob);
}
16
}
public class Payslip
{
public static void main(String[] args)
{
Programmer p = newProgrammer(10,"ABC","xxx","[email protected]",12345);
System.out.println("-------Programmer -------");
omputepay();
Asst_prof ap =newAsst_prof(20,"dab","Yyy","[email protected]",12346);
System.out.println("---------Assistantprofessor -------- ");
ap.computepay();
Assoc_prof ac=newAssoc_prof(30,"cccc","zzz","[email protected]",123487);
ac.computepay();
Prof pro=newProf(40,"DDD","www","[email protected]",23456);
System.out.println("----------Professor ----------- ");
pro.computepay();
}
}
17
OUTPUT:
RESULT:
The program for developing a Java application with employee class and generate
pay slips for the employees with their gross and net salary has been executed
successfully and output was verified.
18
EX.NO:4 FINDING AREA USING ABSTRACT CLASS
DATE:
Aim
To write a Java Program to create an abstract class named Shapes and provide three
classes named Rectangle, Triangle and Circle such that each one of the classes extends the
classShapes.
Algorithm
1. Start theprogram
2. Define the abstract classShapes.
3. Define the class Rectangle with PrintArea() method that extends(makes useof)
Shape.
4. Define the class Triangle with PrintArea() method that extends(makes use of)
Shape.
5. Define the class Circle with PrintArea() method that extends(makes use of)Shape.
6. Print the area of the Rectangle,Triangle and Circle.
7. Stop theProgram.
PROGRAM:
import java.util.*;
abstract class Shapes
{
double a,b;
abstract void printArea();
}
class Rectangle extends Shapes
{
void printArea()
{
System.out.println("\t\t Calculating Area ofRectangle");
Scanner in=new Scanner(System.in);
System.out.println("\nEnter length");
a=in.nextDouble();
System.out.println("\n Enter Breadth:");
b=in.nextDouble();
double area=a*b;
System.out.println("Area of Rectangle:"+area);
}
} 19
class Triangle extends Shapes
{
void printArea()
{
System.out.println("\t\t Calculating Area ofTriangle");
Scanner in=new Scanner(System.in);
System.out.println("\nEnter height");
a=in.nextDouble();
System.out.println("\n Enter Breadth:");
b=in.nextDouble();
double area=0.5*a*b;
System.out.println("Area of Triangle:"+area);
}
}
class Circle extends Shapes
{
void printArea()
{
System.out.println("\t\t Calculating Area of circle");
Scanner in=new Scanner(System.in);
System.out.println("\nEnter radius");
a=in.nextDouble();
double area=3.14*a*a;
System.out.println("Area of Circle:"+area);
}
}
public class AbstractClassDemo
{
public static void main(String[] args)
{
Shapes obj;
obj=new Rectangle();
obj.printArea();
obj=new Triangle();
obj.printArea();
obj=new Circle();
obj.printArea();
}
}
20
OUTPUT:
RESULT:
The program for a to create an abstract class named Shape and provide three classes
named Rectangle, Triangle and Circle such that each one of the classes extends the class
Shape has been executed successfully and output was verified.
21
EX.NO:5 FINDING AREA USING AN INTERFACE
DATE:
Aim
To write a Java Program to find area of shapes using an interface named Shapes and
provide three classes named Rectangle, Triangle and Circle such that each one of the
classes implements the interface Shapes.
Algorithm
1. Start the program
2. Define the interface Area.
3. Define the class Rectangle with PrintArea() method that implements the
interface Shapes.
4. Define the class Triangle with PrintArea() method that implements the
interface Shapes.
5. Define the class Circle with PrintArea() method that implements the
6. Interface Shapes.
7. Print the area of the Rectangle, Triangle and Circle.
8. Stop the Program.
PROGRAM:
import java.util.*;
interface Area
{
void printArea();
}
class Rectangle implements Area
{
private double a,b;
public void printArea()
{
System.out.println("\t\t Calculating Area ofRectangle");
Scanner in=new Scanner(System.in);
System.out.println("\nEnter length");
a=in.nextDouble();
System.out.println("\n Enter Breadth:");
b=in.nextDouble();
double area=a*b;
System.out.println("Area of Rectangle:"+area);
}
} 22
class Triangle implements Area
{
}
}
23
OUTPUT:
Enter Length
4
Enter Breadth
5
Area of Rectangle:20.0
Calculating Area of Triangle
Enter Height
5
Enter Breadth
3
Area of Triangle:7.5
Calculating Area of Circle
Enter Radius
4
Area of Circle:50.24
BUILD SUCCESSFUL (total time: 18 seconds)
RESULT:
The program for a to find area of shapes using an interface Shapes and provide three classes
named Rectangle, Triangle and Circle such that each one of the classes implements the interface Shapes
has been executed successfully and output was verified.
24
Ex No:6A JAVA EXCEPTION HANDLING
DATE:
Aim:
To write for a java program creating simple java exception handling.
Algorithm:
1. Start
2. Create an exception under the classExceptions
3. Initialize an string array and it can be iterated using for loop
4. In main method class, try and catch blocks are used to display the exception inthe
program.
5. Stop
PROGRAM:
class Exceptions {
public static void main(String[] args) {
String languages[] = { "C", "C++", "Java", "Perl", "Python" };
try {
for (int c = 1; c <= 5; c++) {
System.out.println(languages[c]);
}
}
catch (Exception e) {
System.out.println(e);
}
}
}
OUTPUT :
C++
Java
Perl
Python
java.lang.ArrayIndexOutOfBoundsException: 5
RESULT:
Thus the java program for exception handling was executed and output was verified.
25
EX.NO:6B USER DEFINED EXCEPTION HANDLING
DATE:
Aim
To write a Java program to implement user defined exception handling.
Algorithm
6. Start the program.
7. Create an exception under the classMyException
8. In exception block,assigns1=s2
9. Define a function StringToString() to display the outputmessage
10. In main method class, try and catch blocks are used to display the exception inthe
program.
11. Stop the program
PROGRAM:
import java.lang.*;
class MyException extends Exception {
String s1;
MyException(String s2) {
s1 = s2;
}
@Override
public String toString() {
return ("Output String = "+s1);
}
}
public class NewClass {
public static void main(String args[]) {
try {
throw new MyException("Custom message");
} catch(MyException exp) {
System.out.println(exp);
}}}
26
OUTPUT:
RESULT:
Thus the Java program to implement user defined exception handling has been executed
Aim
To write a java program that implements a multi-threaded application
Algorithm
PROGRAM:
import java.util.*;
class NumberGenerate
{
private int value;
private boolean flag;
public synchronized void put()
{
while(flag)
{
try
{
wait();
}
catch(InterruptedException e)
{}
}
flag=true;
Random random=new Random();
this.value=random.nextInt(10);
System.out.println("The Generated number is"+value);
notifyAll();
}
//consumer
public synchronized void get1()
{
28
while(!flag)
{
try
{
wait();
}
catch(InterruptedException e)
{}
}
if(value%2==0)
{
System.out.println("Second is executing now...");
int ans=value*value;
System.out.println(value+"is Even Number Square is:"+ans);
}
flag=false;
notifyAll();
}
public synchronized void get2()
{
while(flag)
{
try
{
wait();
}
catch(InterruptedException e)
{
}
}
if(value%2!=0)
{
System.out.println("Third is executing now...");
int ans=value*value*value;
System.out.println(value+"is odd number and its cube is:"+ans);
}
flag=false;
notifyAll();
}
}
public class TestNumber
{
public static void main(String[] args)
{
final NumberGenerate obj=new NumberGenerate();
Thread producerThread=new Thread()
{
29
obj.put();
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{}
}
}
};
Thread consumerThread1=new Thread()
{
public void run()
{
for(int i=1;i<=3;i++)
{
obj.get1();
try
{
Thread.sleep(2000);
}
catch(InterruptedException e)
{}}}
};
Thread consumerThread2=new Thread()
{
public void run()
{
for(int i=1;i<=3;i++)
{
obj.get2();
try
{
Thread.sleep(3000);
}
catch(InterruptedException e)
{}}}
};
producerThread.start();
consumerThread1.start();
consumerThread2.start();
}}
30
OUTPUT:
RESULT:
Thus the program that implements a multi-threaded application has been
executed successfully and output was verified.
31
EX.NO:8 INPUT/OUTPUT FILES
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. Start theprogram
2. Read the filename from theuser.
3. Use getName() Method to display thefilename.
4. Use getPath() Method to display the path of thefile.
5. Use getParent() Method to display its parent’sinformation.
6. Use exists() Method to display whether the file exist ornot
7. Use isFile() and isDirectory() Methods to display whether the file is fileor
directory.
8. 8.UsecanRead() and canWrite) methods to display whether the file isreadable
orwritable.
9. Use lastModified() Method to display the modifiedinformation.
10. Use length() method to display the size of thefile.
11. Use isHiddden() Method to display whether the file is hidden ornot.
12. Stop theProgram.
32
PROGRAM:
import java.util.*;
import java.io.*;
class FileDemo
{
public static void main(String[]args)
{
System.out.println("Enter the name of the file");
Scanner in=newScanner(System.in);
String s=in.nextLine();
File f1=new File(s);
System.out.println(" ");
System.out.println("FileName:"+f1.getName());
System.out.println("Path:"+f1.getPath());
System.out.println("Absolutepath:"+f1.getAbsolutePath());
System.out.println("This file is :"+(f1.exists()?"Exists":"Does not Exists"));
System.out.println("Is File:"+f1.isFile());
System.out.println("Is Directory:"+f1.isDirectory());
System.out.println("Is Readable:"+f1.canRead());
System.out.println("Is Writable:"+f1.canWrite());
System.out.println("Is Absolute:"+f1.isAbsolute());
System.out.println("FileSize:"+f1.length()+"bytes");
System.out.println("is Hidden:"+f1.isHidden());
}
}
33
OUTPUT:
RESULT:
Thus the 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 has been executed successfully and output was verified.
34
EX.NO:9 GENERIC CLASSES
DATE:
Aim
To write a Java program to develop applications to demonstrate the features of
generic classes.
Algorithm
1. Start theprogram
2. Create a generic class to find area with two types.
3. Create an object for generic class.
4. Create an object for integer type for rectangle.
5. Create an object for double type for circle.
6. Use get() Method to display values.
7. Stop the Program.
PROGRAM:
10
2.5
Result:
36
EX.NO:10A JAVAFX CONTROLS AND LAYOUTS
DATE:
Aim
To write a program to develop applications using JavaFX controls and
layouts.
Algorithm:
1. Start the program
2. Override start method and give title for stage.
3. Create a Flow pane layout by setting title, width and height .
4. Create a scene for Flow pane.
5. Add buttons to flow pane.
6. Handle the action events for buttons.
7. Show the stage and its scene.
8. Use launch(args) to start JavaFX application.
9. Stop the Program.
PROGRAM :
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
/*from w w w .java2 s.com*/
public class Main2 extends Application{
37
flow.getChildren().add(new Button("asdf"));
}
scene.setRoot(flow);
stage.setScene(scene);
stage.show();
}
public static void main(String[]args){
launch(args);
}
}
OUTPUT:
RESULT:
Thus the program to develop applications using JavaFX controls and layouts has been
executed and the output was verified.
38
EX.NO:10B JAVAFX MENUS
DATE:
Aim
To write a program to develop applications using JavaFX menus.
Algorithm:
1. Start the program
2. Use launch(args) to start JavaFX application.
3. Create a Border pane layout .
4. Create a scene for Border pane.
5. Add menu bar to border pane.
6. Add menu items for each menu.
7. Show the stage and its scene.
8. Stop the Program.
PROGRAM :
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Application1 extends Application{
public static void main(String[]args){
launch(args);
}
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");
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();
}
} 39
OUTPUT:
RESULT:
Thus the program to develop applications using JavaFX menus has been executed and
the output was verified.
40