[go: up one dir, main page]

0% found this document useful (0 votes)
10 views82 pages

OOP Lab Record 1-10

Uploaded by

21eca50
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)
10 views82 pages

OOP Lab Record 1-10

Uploaded by

21eca50
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/ 82

Exp.

No: 1a SEQUENTIAL SEARCH USING JAVA

AIM:

Write a java program to implement sequential search in an array using java.

ALGORITHM:

Step 1: Start from the leftmost element of array[] and one by one compare element with each
element of array[]

Step 2: If element matches with an element, return the index.

Step 3: If element doesn’t match with any of the elements, return -1.

PROGRAM:

import java.util.Scanner;

class Array

public static void main(String args[])

intn,element,pos=-1;

Scanner input = new Scanner(System.in);

System.out.println("Enter the size of Array");

n = input.nextInt();

int [] marks = new int[n];

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

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

marks[i] = input.nextInt();

System.out.println("Enter the Element to find");

element = input.nextInt();

//Linear Search

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

if(marks[i] == element)

pos = i;

break;

if(pos==-1)

System.out.println("Element not found");

else

{
System.out.println("Element is at " + pos);

RESULT:

The above program to implement sequential search is executed and verified successfully.
SAMPLE INPUT
Exp. No: 1b BINARY SEARCH USING JAVA
AIM:

Write a java program to implement sequential search in an array using java.

ALGORITHM:

Step 1: Begin with the mid element of the whole array as a search element.

Step 2: If the value of the search element is equal to the item, then return an index of the search
element.

Step 3: Or if the value of the search element is less than the item in the middle of the interval,
narrow the interval to the lower half.

Step 4: Otherwise, narrow it to the upper half.

Step 5: Repeatedly check from the second point until the value is found or the interval is empty.

PROGRAM:

import java.util.Scanner;

class Array

public static void main(String args[])

intn,element,pos=-1;

Scanner input = new Scanner(System.in);

System.out.println("Enter the size of Array");

n = input.nextInt();

int [] marks = new int[n];

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

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

marks[i] = input.nextInt();

System.out.println("Enter the Element to find");

element = input.nextInt();

//Binary Search

int first = 0;

int last = n-1;

int mid = (first + last)/2;

while(first<=last)

if(marks[mid] == element)

pos = mid;

break;

else if(marks[mid]>element)

last = mid-1;

else
{

first = mid + 1;

mid = (first + last)/2;

if(pos==-1)

System.out.println("Element not found");

else

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

RESULT:

The above program to implement binary search is executed and verified successfully.

SAMPLE INPUT

Enter the size of Array 6


7

Enter the Element to find

SAMPLE OUTPUT

Element is at 2
Exp. No: 1c SELECTION SORT USING JAVA

AIM:

Write a java program to implement selection sort.

ALGORITHM:

Step 1: Set minIndex to position 0

(minIndex will hold the index of the smallest number in the unsorted subarray)

Step 2: Search for the smallest element in the unsorted subarray and update minIndex

Step 3: Swap the element at the position minIndex with the first element of the unsorted subarray.

Step 4: Again set minIndex to the first position of the unsorted subarray

Step 5: Repeat steps 2 to 4 until the array gets sorted

PROGRAM:

public class Demo{

// Function to swap elements


public static void swap(int a[], int i, int j){

int temp = a[i];

a[i] = a[j];

a[j] = temp;

// Selection Sort function

public static void selectionSort(int a[], int n){

// Iterate from 0 to n - 1

for( int i = 0; i < n - 1; i++ ){

intminIndex = i;

// Iterating from i + 1 to n

for( int j = i + 1; j < n; j++){

// If a[j] is smaller than a[minIndex]

// then updating the minIndex.

if ( a[j] < a[minIndex] )

minIndex = j;

// Swapping a[i] with a[minIndex].

swap(a, i, minIndex);

public static void main(String args[]){

// Defining the array 'a'.


int a[]={3,54,2,6,4,23,43};

// Printing the initial array.

System.out.print("Array before Sorting - ");

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

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

System.out.println();

// Calling the Selection Sort function

selectionSort(a, a.length);

// Printing the sorted Array

System.out.print("Array after Sorting - ");

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

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

System.out.println();

}
RESULT:

The above program was written and executed successfully.

SAMPLE OUTPUT

Array before Sorting:

3,54,2,6,4,23,43

Array After Sorting:

2,3,4,6,23,43,54
Exp. No: 1d INSERTION SORT USING JAVA
AIM:

Write a java program to implement insertion sort.

ALGORITHM:

Step 1: The first element is assumed to be sorted.

Step2:Takethesecondelementandstoreit separately

Step 3:Call the stored element as a key element.

Step 4:Compare key element with the first element.

Step 5:If the first element is greater than key , then key is placed in front of the first element

PROGRAM:

import java.util.Scanner;

class Array

public static void main(String args[])

intn,element,pos=-1;

Scanner input = new Scanner(System.in);

System.out.println("Enter the size of Array");

n = input.nextInt();

int [] marks = new int[n];

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

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

marks[i] = input.nextInt();

}
for(int i = 1; i<n;i++)

intind = i - 1;

int key = marks[i];

while(ind>= 0 && marks[ind] > key)

marks[ind+1] = marks[ind];

ind = ind -1;

marks[ind+1] = key;

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

System.out.println(marks[i]);

}
RESULT:

The above program written and executed successfully.

SAMPLE OUTPUT

Array before Sorting:

3,54,2,6,4,23,43

Array After Sorting:

2,3,4,6,23,43,54
Exp. No: 2a IMPLEMENTATION OF STACK USING CLASS AND OBJECTS

AIM:

Write a program to implement Stack data structure using class and Objects in Java

ALGORITHM:

Step 1 : Create a Stack Class with essential Data Members

Step 2: Implement the methods push(), pop(), isEmpty(), isFull(), printStack() for stack ADT
operations.

Step 3: write main() method and create object for stack class

Step 4: call the necessary methods to perform stack operations

Step 5: call printstack() method to check the results.


PROGRAM:

// Stack implementation in Java

class Stack {

private intarr[];

private int top;

private int capacity;

Stack(int size) {

// initialize the array

// initialize the stack variables

arr = new int[size];

capacity = size;

top = -1;

public void push(int x) {

if (isFull()) {

System.out.println("Stack OverFlow");

System.exit(1);

// insert element on top of stack

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 intgetSize() {

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();

}
RESULT:

The above program written and executed successfully.

SAMPLE OUTPUT

Inserting 1

Inserting 2

Inserting 3

Stack: 1, 2, 3,

After popping out

1, 2,
Exp. No: 2b IMPLEMENTATION OF QUEUE USING CLASS AND OBJECTS

AIM:

Write a program to implement Queue data structure using class and Objects in Java

ALGORITHM:

Step 1 : Create a Queue Class with essential Data Members

Step 2: Implement the methods enQueue(), deQueue(), isEmpty(), isFull(), display() for stack
ADT operations.

Step 3: write main() method and create object for stack class

Step 4: call the necessary methods to perform stack operations

Step 5: call printstack() method to check the results.

PROGRAM:

public class Queue {

int SIZE = 5;

int items[] = new int[SIZE];

int front, rear;

Queue() {

front = -1;

rear = -1;

}
// check if the queue is full

booleanisFull() {

if (front == 0 && rear == SIZE - 1) {

return true;

return false;

booleanisEmpty() {

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);


}

intdeQueue() {

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();
// deQueue removes element entered first i.e. 1

q.deQueue();

q.display();

}
RESULT:

The above program written and executed successfully.

SAMPLE 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
Ex. No: 3 INHERITANCE

AIM:

To develop a java application to implement inheritance.

ALGORITHM:

Step 1: Start

Step 2: Create the class Employee with eId, eDId, eName, eAddr, eEmail, eMob as

data members.

Step 3: Inherit the classes Programmer, Asstprofessor, Associateprofessor and

Professor from employee


class.

Step 4: Add Basic Pay (BP) as the member of all the inherited classes.

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

Step 6: Calculate gross salary and net salary.

Step 7: Generate payslip for all categories of employees.

Step 8: Create the objects for the inherited classes and invoke the necessary methods

to display the Payslip

Step 9: Stop

PROGRAM:

import java.util.Scanner;

class Employee

inteId, eDId;

String eName, eAddr, eEmail, eMob;

String desgn[] = {"", "Programmer", "Assistant Professor","Associate Professor",


"Professor"};

double bp;

Employee(inteId, String eName, String eAddr, String eEmail, String eMob, inteDId)

this.eId=eId; this.eName=eName;

this.eAddr=eAddr; this.eEmail=eEmail;

this.eMob=eMob; this.eDId=eDId;

void display()

{
double da = Math.round((bp*0.97*100.0)/100.0);

double hra = Math.round((bp*0.1*100.0)/100.0);

double pf = Math.round((bp*0.12*100.0)/100.0);

double scf = Math.round((bp*0.01*100.0)/100.0);

System.out.println("******************************\n"

+ "* EMPLOYEE PAY SLIP *\n"

+ "******************************");

System.out.println("ID : "+eId);

System.out.println("Name : "+eName);

System.out.println("Designation : "+desgn[eDId] + " - "+ eDId);

System.out.println("Address : "+eAddr);

System.out.println("Email : "+eEmail);

System.out.println("Mobile : "+eAddr);

System.out.println("******************************");

System.out.println("Earnings ");

System.out.printf("Basic Pay : %8.2f \n",bp);

System.out.printf("DA : %8.2f \n",da);

System.out.printf("HRA : %8.2f \2

System.out.println("******************************");

System.out.printf("Earning (gross) : %8.2f \n",(bp+da+hra));

System.out.println("******************************");

System.out.println("Deduction ");

System.out.printf("DA : %8.2f \n",pf);

System.out.printf("HRA : %8.2f \n",scf);

System.out.println("******************************");

System.out.printf("Total Deduction : %8.2f \n",(pf+scf));


System.out.println("******************************");

System.out.printf("Net Salary : %8.2f \n",((bp+da+hra)-(pf+scf)));

System.out.println("******************************");

class Programmer extends Employee

Programmer(inteId, String eName, String eAddr, String eEmail, String eMob, inteDId)

super(eId, eName, eAddr, eEmail, eMob, eDId);

bp=10000;

class AssistantProfessor extends Employee

AssistantProfessor(inteId, String eName, String eAddr, String eEmail, String


eMob,inteDId)

super(eId, eName, eAddr, eEmail, eMob, eDId);

class AssociateProfessor extends Employee

AssociateProfessor(inteId, String eName, String eAddr, String eEmail, String eMob,


inteDId)

super(eId, eName, eAddr, eEmail, eMob, eDId);


bp=35000;

class Professor extends Employee

Professor(inteId, String eName, String eAddr, String eEmail, String eMob, inteDId)

super(eId, eName, eAddr, eEmail, eMob, eDId);

bp=52000;

public class OOPL03Emp

public static void main(String[] args)

inteId,eDId;

Scanner s = new Scanner(System.in);

String eName, eAddr, eEmail, eMob;

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

System.out.println("Enter the following Employee Details");

System.out.println("ID : ");

eId = s.nextInt();

System.out.println("Name : ");

eName = s.next();

System.out.println("Address : ");
eAddr=s.next();

System.out.println("Email : ");

eEmail=s.next();

System.out.println("Mobile : ");

eMob=s.next();

System.out.println("1. Programmer " + "2. Assistant Professor "

+ "3. Associate Professor " + "4. Professor");

System.out.println("Designation ID : ");

eDId=s.nextInt();

int c = eDId;

switch(c)

case 1:

Programmer p = new Programmer(eId, eName, eAddr,

eEmail, eMob, eDId);

p.display();

break;

case 2:

AssistantProfessorap = new AssistantProfessor(eId, eName,


eAddr, eEmail, eMob,

eDId);

ap.display();

break;

case 3:

AssociateProfessor asp = new AssociateProfessor(eId,


eName, eAddr, eEmail, eMob,

eDId);
asp.display();

break;

case 4:

Professor pr = new Professor(eId, eName, eAddr, eEmail,


eMob, eDId);

pr.display();

break;

default:

System.out.println("Invalid Designation");

break;

}
RESULT:

The above program written and executed successfully.


SAMPLE OUTPUT

Enter the following Employee Details

ID :

001

Name :

Shankar

Address :

123,GandhiNagar,Covai

Email :

abc12@gmail.com

Mobile :

8661237980

1. Programmer 2. Assistant Professor 3. Associate Professor 4. Professor

Designation ID :

******************************

* EMPLOYEE PAY SLIP *

******************************

ID : 1

Name : Shankar

Designation : Professor - 4

Address : 123,GandhiNagar,Covai

Email : abc12@gmail.com

Mobile : 8661237980

******************************
Earnings

Basic Pay : 52000.00

DA : 50440.00

HRA : 5200.00

******************************

Earning (gross) : 107640.00

******************************

Deduction

DA : 6240.00

HRA : 520.00

******************************

Total Deduction : 6760.00

******************************

Net Salary : 100880.00

******************************

Ex. No:4 ABSTRACT CLASS

AIM:
To write a java program to create an abstract class name shape.

ALGORITHM:

Step 1: Start

Step 2: Create an abstract class named Shape that contains abstract method named

printArea(), and the variables l and b for process

Step 3: Create a class Rectangle which should inherit abstract class Shape and

implement all the abstract methods with constructor to store data.

Step 4: Create a class Triangle which should inherit abstract class Shape and

implement all the abstract methods with constructor to store data.

Step 5: Create a class Circle which should inherit abstract class Shape and implement

all the abstract methods with constructor to store data.

Step 6: Create a class Demo with main method to access the printArea

method.

Step 7: Stop.

Program:

abstract class Shape {

intl,b;

public abstract void printArea();

class Rectangle extends Shape {

public Rectangle(intl,int b) {

this.l = l;

this.b = b;

}
public void printArea() {

System.out.println("Area of Rectangle = " + (this.l * this.b));

public class Triangle extends Shape{

public Triangle(inth,int b) {

this.l = h;

this.b = b;

public void printArea() {

System.out.println("Area of Triangle = " + ((0.5)*(this.l * this.b)));

public class Circle extends Shape{

public Circle(int r) {

this.l = r;

public void printArea() {

System.out.println("Area of Circle = " + ((3.14)*(this.l * this.l)));

public class Demo {

public static void main(String[] args) {

Rectangle r = new Rectangle(10, 20);

Triangle t = new Triangle(10, 20);

Circle c = new Circle(10);


r.printArea();

t.printArea();

c.printArea();

}
RESULT:

The above program written and executed successfully

SAMPLE OUTPUT

Area of Rectangle=200

Area of Triangle=100.0

Area of Circle =314.0

.
Ex. No: 5 IMPLEMENTATION OF INTERFACE

Aim:

Write a java program to create and implement interface for the given requirement.

Algorithm:

Step 1: Start

Step 2: Create an Interface named Shape that contains abstract method named

printArea().

Step 3: Create a class Rectangle which should implement Interface Shape and

implement all the methods with constructor to store data.

Step 4: Create a class Triangle which should implement Interface Shape and

implement all the methods with constructor to store data.

Step 5: Create a class Circle which should which should implement Interface Shape and

implement all the methods with constructor to store data.


Step 6: Create a class Demo with main method to access the printArea

method.

Step 7: Stop.

Program:

interface Shape {

public void printArea();

class Rectangle implements Shape {

intl,b;

public Rectangle(intl,int b) {

this.l = l;

this.b = b;

public void printArea() {

System.out.println(“Area of Rectangle = “ + (this.l * this.b));

class Triangle implements Shape{

intl,b;

public Triangle(inth,int b) {

this.l = h;

this.b = b;

}
public void printArea() {

System.out.println(“Area of Triangle = “ + ((0.5)*(this.l * this.b)));

class Circle implements Shape{

intl,b;

public Circle(int r) {

this.l = r;

public void printArea() {

System.out.println(“Area of Circle = “ + ((3.14)*(this.l * this.l)));

class Demo {

public static void main(String[] args) {

Rectangle r = new Rectangle(10, 20);

Triangle t = new Triangle(10, 20);

Circle c = new Circle(10);

r.printArea();

t.printArea();

c.printArea();

}
RESULT:

The above program written and executed successfully.

SAMPLE OUTPUT

Area of Rectangle=200

Area of Triangle=100.0

Area of Circle =314.0


Ex. No: 6 IMPLEMENTATION OF EXCEPTION HANDLING

Aim:

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

Algorithm:

Step 1: Start

Step 2: Create a class NegativeAmtException which extends Exception class.

Step 3: Create a constructor which receives the string as argument.

Step 4: Get the Amount as input from the user.

Step 5: If the amount is negative, the exception will be generated.

Step 6: Using the exception handling mechanism , the thrown exception is handled by

the catch construct.

Step 7: After the exception is handled, the string “invalid amount “ will be displayed.

Step 8: If the amount is greater than 0, the message “Amount Deposited “will be

displayed

Step 9: Stop.

Program:

import java.util.Scanner;

class NegativeAmtException extends Exception

String msg;

NegativeAmtException(String msg)

this.msg = msg;
}

public String toString()

return msg;

public class DemoException

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)

e.printStackTrace();

}
}
RESULT:

The above program was written and executed successfully

SAMPLE OUTPUT

Enter Amount:-500
Invalid Amount

Ex. No: 7 MULTI-THREADED PROGRAMMING


AIM:

To write a java program to implements a multithreaded application that

contains methods.

ALGORITHM:

Step 1: Create a class even which implements first thread that computes the square ofthe number

Step 2: run() method implements the code to be executed when thread gets executed.

Step 3: Create a class odd which implements second thread that computes the cube ofthe number.

Step 4: The Multithreading is performed and the task switched between multiplethreads.

Step 5: The sleep () method makes the thread to suspend for the specified time.

PROGRAM:

importjava.util.*;

class even implements Runnable

public int x;

public even(int x)

this.x = x;

public void run()

System.out.println("New Thread "+ x +" is EVEN and Square of "

+ x + " is: " + x * x);

}
}

class odd implements Runnable

public int x;

public odd(int x)

this.x = x;

public void run()

System.out.println("New Thread "+ x +" is ODD and Cube of " + x + "

is: " + x * x * x);

class A extends Thread

public void run()

intnum = 0;

Random r = new Random();

try

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

num = r.nextInt(100);

System.out.println("Main Thread and Generated Number is " + num);


if (num % 2 == 0)

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

t1.start();

else {

Thread t2 = new Thread(new odd(num));

t2.start();

Thread.sleep(1000);

System.out.println("--------------------------------------");

catch (Exception ex)

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

public class Demo

public static void main(String[] args)

A a = new A();

a.start();

}
}

RESULT:

Thus the java program to implement a multi-threaded application with three methods is executed
successfully and the output is verified.

SAMPLE OUTPUT

Main Thread and Generated Number is 10

New Thread 10 is EVEN and Square of 10 is: 100

--------------------------------------

Main Thread and Generated Number is 14

New Thread 14 is EVEN and Square of 14 is: 196


--------------------------------------

Main Thread and Generated Number is 83

New Thread 83 is ODD and Cube of 83 is: 571787

--------------------------------------

Main Thread and Generated Number is 1

New Thread 1 is ODD and Cube of 1 is: 1

--------------------------------------

Main Thread and Generated Number is 20

New Thread 20 is EVEN and Square of 20 is: 400

--------------------------------------

Ex. No: 8 WRITE A JAVA PROGRAM TO IMPLEMENT FILE OPERATIONS


AIM:

To write a java program to implement file operations.

ALGORITHM:

Step 1: Create a class and get the file name from the user .

Step 2: Use the file functions and display the information about the file.

Step 3: Read()-This method is basically a check if the file can be read.

Step 4: Write()-verifies whether the application can write to the file.

Step 5: Open a notepad- type any text and save the file in c:\

Step 6: Give the location path of the file as input to this program

PROGRAM:

import java.io.File;

import java.util.Scanner;

public class Demo

public static void main(String args[])

Scanner s = new Scanner(System.in);

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

String fileName= s.next();

File file = new File("C:\\"+fileName);

if (!file.exists() || !file.isFile())

System.out.println("File doesn\'t exist");


}

else

System.out.println(fileName+ " exists");

System.out.println("File is readable: "+file.canRead());

System.out.println("File is writable: "+file.canWrite());

String spl[] = fileName.split("\\.");

System.out.println("File type is: "+spl[1]);

System.out.println(fileName+ " length is "

+ file.length() + " bytes");

RESULT:

Thus the java program to implement file operations is executed successfullyand the output is verified.

SAMPLE OUTPUT
Enter the Filename:

test.txt

test.txt exists

File is readable: true

File is writable: true

File type is: txt

test.txt length is 17 bytes

Ex. No: 9 GENERIC CLASS & FUNCTION


AIM:

To write a java program to find the maximum value using a generic function.

ALGORITHM:

Step 1: Create a class to implement generic class and generic methods.

Step 2: Get the set of the values belonging to specific data type.

Step 3: Create the objects of the class to hold integer, character and double values.

Step 4: Create the method to compare the values and find the maximum value stored

in the array.

Step 5: Invoke the method with integer, character or double values . The output will

be displayed based on the data type passed to the method.

PROGRAM:

public class GenericDemo

public static <T extends Comparable<T>> T max(T... elements)

T max = elements[0];

for (T element : elements)

if (element.compareTo(max) > 0)

max = element;

}
return max;

public static void main(String[] args)

System.out.println("Integer Max: " + max(Integer.valueOf(32),

Integer.valueOf(56), Integer.valueOf(89), Integer.valueOf(3),

Integer.valueOf(456), Integer.valueOf(78), Integer.valueOf(45)));

System.out.println("Double Max: " + max(Double.valueOf(5.6),

Double.valueOf(7.8), Double.valueOf(2.9), Double.valueOf(18.6),

Double.valueOf(10.25), Double.valueOf(18.6001)));

System.out.println("String Max: " + max("Strawberry", "Mango",

"Apple", "Pomegranate", "Guava", "Blackberry", "Cherry", "Orange",

"Date"));

System.out.println("Boolean Max: " + max(Boolean.TRUE,

Boolean.FALSE));

System.out.println("Byte Max: " + max(Byte.MIN_VALUE,

Byte.MAX_VALUE));

}
RESULT:

Thus the java program to find the maximum value using a generic function isexecuted successfully and
the output is verified.

SAMPLE OUTPUT

Integer Max: 456

Double Max: 18.6001

String Max: Strawberry

Boolean Max: true

Byte Max: 127


Ex. No: 10a JAVAFX CONTROLS AND LAYOUTS
AIM:

Write a java Program to make use of Controls and Events.

ALGORITHM:

Step 1: Create a Button using javafx package

Step 2: Add Event Handler to the Button

Step 3: Create a stackpane layout from javafx package

Step 4: add button to the stackpane layout

Step 5: set title for the Layout.

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{

@Override

public void start(Stage primaryStage) throws Exception {

// TODO Auto-generated method stub

Button btn1=new Button("Say, Hello World");


btn1.setOnAction(new EventHandler<ActionEvent>() {

@Override

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);

}
RESULT:

The above program was written and executed successfully

SAMPLE OUTPUT
Ex. No: 10b JAVAFX MENUS
AIM:

Write a java Program to make use of Menus.

ALGORITHM:

Step 1: Create a Menu using javafx package

Step 2:Add menu items to the menu

Step 3: Add Event Handler to the Menus

Step 4: Create a VBoxlayout from javafx package

Step 5: add button to the VBox layout

Step 6: set title for the Layout.

PROGRAM:

// Java program to create a menu bar and add

// menu to it and also add menuitems to menu

import javafx.application.Application;

import javafx.scene.Scene;

import javafx.scene.control.Button;

importjavafx.scene.layout.*;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

importjavafx.scene.control.*;

import javafx.stage.Stage;

import javafx.scene.control.Alert.AlertType;

import java.time.LocalDate;

public class MenuBar_1 extends Application {


// launch the application

public void start(Stage s)

// set title for the stage

s.setTitle("creating MenuBar");

// create a menu

Menu m = new Menu("Menu");

// create menuitems

MenuItem m1 = new MenuItem("menu item 1");

MenuItem m2 = new MenuItem("menu item 2");

MenuItem m3 = new MenuItem("menu item 3");

// add menu items to menu

m.getItems().add(m1);

m.getItems().add(m2);

m.getItems().add(m3);

// create a menubar

MenuBarmb = new MenuBar();

// add menu to menubar

mb.getMenus().add(m);
// create a VBox

VBoxvb = new VBox(mb);

// create a scene

Scene sc = new Scene(vb, 500, 300);

// set the scene

s.setScene(sc);

s.show();

public static void main(String args[])

// launch the application

launch(args);

}
RESULT:

The above program was written and executed successfully.

SAMPLE OUTPUT

You might also like