Oops Program 8,9,10 & 11
Oops Program 8,9,10 & 11
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
Algorithm:
Step 4 If number is even then create and start thread that computes square of a
number
Step 7 If number is odd then create and start thread that computes cube of a number
Step 10 Wait for 1 Second and Continue to Step 3until user wants to exits
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
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:
Java.
Algorithm:
Step1 StarttheProcess
Step3.2Compare[NumericPerspetive]thiselementwithMAX
Step3.3IfitisgreaterthanMAXthenstorecurrentelementasMAX
Step3.4Else do nothing
Step3.5Gotostep3.1untilalltheelementshasbeenprocessed.
Step4 Ifthearrayis Stringtype
Step4.1AssumefirstelementasMAX
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{
}
Output:
Result:
Aim:
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.
// 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");
// create a menubar
MenuBar mb = new MenuBar();
// create a VBox
12
VBox vb = new VBox(mb);
// create a scene
Scene sc = new Scene(vb, 500, 300);
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