0% found this document useful (0 votes)
16 views

Oops Program 8,9,10 & 11

object oriented lab program
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Oops Program 8,9,10 & 11

object oriented lab program
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

EX NO:8 Java Application for Multithreading

DATE:

Aim:

To create a Java console application the uses the multi threading concepts in java. The

application has 3 threads one creates random number, one thread computes square of that number

and another one computes the cube of that number.

Algorithm:

Step 1 Start the Process

Step 2 Create a thread that generates random number

Step 3 Obtain one random number and check is odd or even

Step 4 If number is even then create and start thread that computes square of a

number

Step 5 Compute number * number and display the answer

Step 6 Notify to Random number thread and goto step 4

Step 7 If number is odd then create and start thread that computes cube of a number

Step 8 Compute number * number * number and display the answer

Step 9 Notify to Random number thread and goto step 4

Step 10 Wait for 1 Second and Continue to Step 3until user wants to exits

Step 11 Stop the process


Program:

import java.util.Random;
class Square extends Thread
{
int x;
Square(int n)
{
x = n;
}
public void run()
{
int sqr = x * x;
System.out.println("Square of " + x + " = " + sqr );
}
}
class Cube extends Thread
{
int x;
Cube(int n)
{
x = n;
}
public void run()
{
int cub = x * x * x;
System.out.println("Cube of " + x + " = " + cub );
}
}
class Number extends Thread
{
public void run()
{
Random random = new Random();
for(int i =0; i<10; i++)
{
int randomInteger = random.nextInt(100);
System.out.println("Random Integer generated : " + randomInteger);
Square s = new Square(randomInteger);
s.start();
Cube c = new Cube(randomInteger);
c.start();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
System.out.println(ex);
}
}
}
}
public class ThreadCreation {
public static void main(String args[])
{
Number n = new Number();
n.start();
}
}
Output:

Result:

Thus the Java console application for implementation of Multithreading was developed

and executed successfully.


EX NO:9 Java Application for File Handling
DATE:

Aim:

TocreateaJavaconsoleapplicationtohandlethefilesandfindthefileproperties
[Availability,ReadableorWriteableorBoth,LengthoftheFile].

Algorithm:

Step1 StarttheProcess

Step2 Prompttheusertoenterthefilenamewithpath
Step3 Getthefilename

Step3.1 Checkthefileisexists

Step3.2 Iffileexiststhenproceedtostep3.3elseproceedtostep3.8
Step3.3 ChecktheFileis BothReadableandWriteable
Step3.4 Ifyesdisplayfileis“ReadandWriteable”
Step3.5 Elsecheck isreadableifyesdisplay“ReadOnly” elsemovetostep3.6
Step3.6 Elsecheckiswriteableifyesdisplay“WriteOnly”
Step3.7 Computefilesizeanddisplay

Step3.8 Iffilenotexistingthendisplay“FileNotFound”
Step4 StoptheProcess

Program:

UserFileHandler.java
package
com.raja.oopslab.files;importjava
.io.File;
public class UserFileHandler{
File aFile;
boolean isReadable =
false;boolean isWriteable =
false;booleanisExists=false;
intlength=0;
public UserFileHandler(String path) {
aFile = new File(path);
this.isExists = aFile.exists();this.isReadable
= aFile.canRead();this.isWriteable
=aFile.canWrite();this.length=(int)aFi
le.length();
}
public void fileDetails()
{if(isExists){
System.out.println("TheFile"+aFile.getName()+"isAvailableat:"+aFile.getParent());
if(isReadable&&isWriteable)
System.out.println("File is Readable and
Writeable");elseif(isReadable)
System.out.println("File is Only
Readable");elseif(isWriteable)
System.out.println("File is Only Writeable");
System.out.println("Totallengthofthefileis:"+this.length+"bytes");
} e
els
System.out.println("Filedoesnotexists");
}
}

Main.java

import
java.io.File;importjava.util
.Scanner;
importcom.raja.oopslab.files.*;
publicclassMain{

publicstaticvoidmain(String[]args)
{Stringfile_path=null;
Scannerinput=newScanner(System.in);Syst
em.out.println("File
Handler");System.out.println("************");
System.out.println("Enter the file
path");file_path =input.next();
newUserFileHandler(file_path).fileDetails();
}
}
Output:

AvailabilityofFile:

FileReadandWriteable:
FileSize:

FileNotExists:

Result:

Thejavaconsoleapplicationforhandlingfileswasdevelopedandtestedsuccessfully.
EX NO:10 Java Application for Generic Max Finder
DATE:

Aim:

To createaJava consoleapplicationthatfindsthemaximum elementin an arrayusinggenericfunctionsin

Java.

Algorithm:

Step1 StarttheProcess

Step2 Create an arrayof numbersandarrayof stringsand pass itto genericfunction.


Step3 Ifthe arrayisIntegertype
Step3.1AssumefirstelementasMAX

Step3.2Compare[NumericPerspetive]thiselementwithMAX
Step3.3IfitisgreaterthanMAXthenstorecurrentelementasMAX
Step3.4Else do nothing

Step3.5Gotostep3.1untilalltheelementshasbeenprocessed.
Step4 Ifthearrayis Stringtype

Step4.1AssumefirstelementasMAX

Step4.2Compare [Dictionary Perspective] this element with MAX


Step4.3IfitisgreaterthanMAXthenstorecurrentelementasMAX
Step4.4Else do nothing
Step4.5Gotostep3.1untilalltheelementshasbeenprocessed.
Step5 StoptheProcess
Program:

classGenericMax{
public<TextendsComparable<T>>voidmaxFinder(T[]array){
Tmax=array[0];
for(Telement:array){
System.out.println(element);if(element.compareTo(ma
x)>0)
max=element;
}
System.out.println("Maxis:"+max);
}
}

publicclassMain3{

public static void main(String[] args) {


GenericMaxmax=newGenericMax();
Integer[] numbers = {14,3,42,5,6,10};
String[] strings =
{"R","Ra","Raj"};max.maxFinder(numbers);
max.maxFinder(strings);
}

}
Output:

Result:

Thejavaconsoleapplicationforfindinggenericmaxofgivenelements and stringswas


developed and executed successfully.
EX NO:11 Java Applications using JavaFX controls, layouts and Menus
DATE:

Aim:

TocreateaJavaapplicationusingJavaFXlayoutsandMenusto createamenubarand add a menu to it and also


add menu items to menus.

Algorithm:

Step1:Starttheprogram
Step 2:Importthenecessaryjavafxfiles.
Step3:Create theclassmenubarandset the titleforit.
Step 4: Create object for the class and create the necessary menu items in it.
Step5: Add themenu itemsusingthegetitem() and add()functions.

Step 6: Create the label and events for the menu.


Step 7: Create the objects for the necessary classes.
Step 8: In themainfunction, launchthe menu.
Step9:Stoptheprogram.
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;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.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
MenuBar mb = new MenuBar();

// add menu to menubar


mb.getMenus().add(m);

// create a VBox

12
VBox vb = 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);
}
}

13
Output:

Result:
The Java application using JavaFX layouts and Menus to create a menu bar and add a menu to it
And also add menu items to menu were developed and tested successfully.

14

You might also like