Java Ex 10-15
Java Ex 10-15
EX.NO:10 Copy the contents of one file to another using file stream.
DATE:
AIM:
To write a java program to copy the contents of one file to
another using file stream
ALGORITHM:
1. Start the algorithm.
2. Define input and output file paths.
3. Create FileInputStream and FileOutputStream for source and
destination files.
4. Read from source file and write to destination file byte by byte
using a buffer.
5. Close FileInputStream and FileOutputStream to release system
resources.
6. If the process completes successfully, print "File copied
successfully."
7. If any errors occur, catch and handle them by printing an error
message.
8. End the algorithm.
PROGRAM:
import java.io.*;
public class FileCopy {
public static void main(String[] args) {
// Input and output file paths
String sourceFilePath = "source.txt";
String destinationFilePath = "destination.txt";
try {
// Create FileInputStream and FileOutputStream for source
and destination files
FileInputStream fileInputStream = new
FileInputStream(sourceFilePath);
FileOutputStream fileOutputStream = new
FileOutputStream(destinationFilePath);
// Read from source file and write to destination file byte by byte
int bytesRead;
byte[] buffer = new byte[1024];
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, bytesRead);
}
// Close streams
fileInputStream.close();
fileOutputStream.close();
System.out.println("File copied successfully.");
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
RESULT:
Thus java program to copy the contents of one file to another using
file stream is executed successfully.
AIM:
To implement a java program with multi-threading application
that has three threads.
ALGORITHM:
PROGRAM:
import java.util.Random;
class R extends Thread {
public void run() {
Random r = new Random();
while (true) {
int n = r.nextInt(100);
if (n % 2 == 0)
synchronized (M.l) {
M.e = n;
M.l.notifyAll();
}
else
synchronized (M.l) {
M.o = n;
M.l.notifyAll();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class S extends Thread {
public void run() {
while (true) {
synchronized (M.l) {
while (M.e == -1) {
try {
M.l.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Square of " + M.e + ": " + (M.e * M.e));
M.e = -1;
}
}
}
}
class C extends Thread {
public void run() {
while (true) {
synchronized (M.l) {
while (M.o == -1) {
try {
M.l.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Cube of " + M.o + ": " + (M.o * M.o *
M.o));
M.o = -1;
}
}
}
}
class M {
static final Object l = new Object();
static int e = -1, o = -1;
public static void main(String[] args) {
new R().start();
new S().start();
new C().start();
}
}
RESULT:
Thus java program with multi-threading application that has three
threads is implemented successfully.
AIM:
To develop a java application that executes two threads.
ALGORITHM:
PROGRAM:
e.printStackTrace();
}
}
}
}
public class Main {
static final Object lock = new Object();
public static void main(String[] args) {
AlphabetPrinter firstThread = new AlphabetPrinter('A', 1000);
AlphabetPrinter secondThread = new AlphabetPrinter('Z', 2000);
firstThread.start();
secondThread.start();
try {
firstThread.join();
secondThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("All threads finished execution.");
}
}
RESULT:
Thus java application with two threads has been executed
successfully.
EXP. NO: 13
DATE:
JAVAFX CONTROLS
AIM:
ALGORITHM:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
launch(args);
@Override
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.add(scenetitle, 0, 0, 2, 1);
grid.add(userName, 0, 1);
grid.add(userTextField, 1, 1);
grid.add(pw, 0, 2);
grid.add(passwordBox, 1, 2);
passwordTextField = passwordBox;
showPasswordCheckBox.setOnAction(event -> {
if (showPasswordCheckBox.isSelected()) {
passwordBox.setPromptText(passwordBox.getText());
passwordBox.setText("");
passwordBox.setManaged(false);
passwordBox.setVisible(false);
passwordTextField.setPromptText("Password");
grid.add(passwordTextField, 1, 2);
} else {
grid.getChildren().remove(passwordTextField);
passwordBox.setText(passwordTextField.getText());
passwordBox.setManaged(true);
passwordBox.setVisible(true);
passwordTextField = passwordBox;
});
grid.add(showPasswordCheckBox, 1, 3);
hbBtn.setAlignment(Pos.CENTER);
hbBtn.getChildren().add(btn);
grid.add(hbBtn, 1, 4);
grid.add(actiontarget, 1, 6);
btn.setOnAction(e -> {
actiontarget.setText("");
if (enteredUsername.equals(username) &&
enteredPassword.equals(password)) {
actiontarget.setText("Login Successful");
actiontarget.setFill(Color.GREEN);
} else {
actiontarget.setFill(Color.RED);
});
primaryStage.setScene(scene);
primaryStage.show();
SAMPLE I/O:
RESULT:
EX.NO:14a
LAYOUT USING FLOWPANE
DATE:
AIM:
To Develop a Java program to use a FlowPane layout in JavaFX to organize and
display a set of Label nodes.
ALGORITHM:
1. Initialize JavaFX Application:
• Define a class FlowPaneWidthDemo that extends Application.
• Define the main method within FlowPaneWidthDemo class to start the
JavaFX application by calling launch(args).
2. Override start Method:
• Override the start method of the Application class.
• The start method takes a Stage parameter, which represents the primary
stage of the application.
3. Create nine Label objects, each initialized with a unique text from "Label 1" to
"Label 9".
4. Create FlowPane:
• Create a FlowPane with horizontal and vertical gaps of 10 pixels.
• Add all nine Label objects to the labelPane.
5. Set the preferred wrap length of the labelPane to 150 pixels, which will cause it
to wrap its content to the next row when the width exceeds this value.
6. Create another FlowPane named rootNode and add the labelPane to it.
7. Create a Scene named myScene with the rootNode as its root node and a
width of 230 pixels and a height of 140 pixels.
8. Set myScene as the scene of the myStage.
9. Show the myStage along with its scene by calling myStage.show().
PROGRAM:
import javafx.application.*;
import javafx.scene.*;
import javafx.stage.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
labelPane.setPrefWrapLength (150);
myStage . setScene(myScene);
myStage.show();
}
}
RESULT:
Thus the Java program to use a FlowPane layout in JavaFX to organize and display
a set of Label nodes is successfully written and executed.
EX.NO:14b
LAYOUT USING BORDERPANE
DATE:
AIM:
To Develop a Java program showcasing the usage of BorderPane layout in JavaFX
for organizing controls in specific regions of the pane.
ALGORITHM:
1. Import necessary JavaFX packages for GUI implementation.
2. Define a class named BorderPaneDemo that extends Application.
3. Implement the main method to launch the JavaFX application (launch(args)).
4. Override the start method inherited from Application.
5. Initialize the stage (myStage) and set its title to "Demonstrate Border Pane".
6. Create a BorderPane (rootNode) to serve as the root layout container.
7. Create a Scene (myScene) with specified dimensions (340x200) and set it on the
stage.
8. Add graphical controls (TextField, Slider, Button, Label) to different regions
(Center, Right, Left, Top, Bottom) of the BorderPane.
9. Configure alignments and margins for controls within the BorderPane.
10. Display the stage (myStage) to show the GUI with the configured BorderPane
layout.
PROGRAM:
import javafx.application.*;
import javafx.scene.*;
import javafx.stage.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.geometry.*;
//Center
TextField tfCenter = new TextField("This text field is in the center");
Border Pane.setAlignment (tfCenter, Pos.CENTER);
rootNode.setCenter(tfCenter);
//Right
Slider SldrRight = new Slider (0.0, 100.0, 50.0);
sldrRight.setOrientation (Orientation. VERTICAL);
sldrRight.setPrefWidth (60);
sldrRight. setShowTickLabels (true);
sldrRight.setShowTickMarks (true);
BorderPane.setAlignment (sldrRight, Pos.CENTER);
rootNode.setRight (sldrRight),
//Left
Button btnAlpha = new Button("Alpha");
Button btnBeta = new Button("Beta");
Button btnGamma = new Button("Gamma");
btnAlpha.setPrefWidth(60);
btaBeta.setPrefWidth (60):
btnGamma.setPrefWidth(60);
//Top
Label lblTop = new Label ("This label is displayed along the top.");
BorderPane.setAlignment (lblTop, Pos.CENTER);
rootNode.setTop (lblTop);
//Bottom
Label lblBottom = new Label ("This label is displayed along the bottom.");
Border Pane.setAlignment (lblBottom, Pos.CENTER);
rootNode.setBottom (lblBottom);
myStage.show();
}
}
RESULT:
The Java program demonstrating the usage of BorderPane layout in JavaFX has
been executed successfully.
EX.NO:14c
LAYOUT USING GRIDPANE
DATE:
AIM:
To write a Java program demonstrating the usage of the GridPane layout in JavaFX.
ALGORITHM:
1. Import necessary JavaFX packages for GUI implementation.
2. Define a class named GridPaneDemo that extends Application.
3. Implement the main method to launch the JavaFX application (launch(args)).
4. Override the start method inherited from Application.
5. Initialize the stage (myStage) and set its title ("Demonstrate GridPane").
6. Create TextField and Label objects for user input and descriptions.
7. Create a GridPane (rootNode) to organize controls in a grid layout.
8. Set padding around the GridPane using setPadding() method.
9. Set vertical and horizontal gaps between grid cells using setVgap() and setHgap()
methods.
10. Add labels and text fields to specific positions within the GridPane using add()
method with row and column indices.
11. Create a Scene (myScene) with the GridPane as its root node and set preferred
dimensions.
12. Set the created scene on the stage using setScene() method.
13. Display the stage to show the GUI with the configured GridPane layout and
controls.
PROGRAM:
import javafx.application.*;
import javafx.scene.*;
import javafx.stage.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.geometry.*;
// Create a scene.
Scene myScene = new Scene (rootNode, 300, 120);
RESULT:
The Java program showcasing the usage of the GridPane layout in JavaFX has
been successfully executed.
EX.NO:15
JAVAFX MENU
DATE:
AIM:
To write a java program to showcase menu creation in javafx.
ALGORITHM:
1. Import necessary JavaFX packages for GUI implementation and define a class
named MenuDemo that extends Application.
2. Implement the main method, calling launch(args) to start the JavaFX
application and override the start method inherited.
3. Create a new Stage (myStage) for the application and set its title to
"Demonstrate Menus".
4. Create a BorderPane (rootNode) as the root layout container.
5. Create a Scene (myScene) with dimensions of 300x300 and set it for the stage
(myStage) and a MenuBar(mb) to hold menu items.
6. Define various Menu objects for organizing menu items.
7. Create MenuItem objects (open, close, save, exit, red, green, blue, high, low,
reset, about) representing actions in menus.
8. Configure menu hierarchy using getItems().addAll() and getMenus().add()
methods.
9. Implement an EventHandler<ActionEvent> (MEHandler) to handle menu item
selections and identify selected menu item using getText().
10.Perform specific actions based on the selected menu item.
11.Attach MEHandler to each menu item's setOnAction() method to respond to
user interactions.
12.Set the menu bar (mb) at the top and a Label (response) at the center of the
BorderPane (rootNode).
13.Display the stage (myStage) to show the GUI to the user.
PROGRAM:
import javafx.application.*;
import javafx.scene.*;
import javafx.stage.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.event.*;
import javafx.geometry.*;
// Add a separator.
optionsMenu.getItems().add(new SeparatorMenuItem());
EventHandler<ActionEvent> MEHandler =
new EventHandler<ActionEvent>() { public void handle(ActionEvent ae) {
String name = ((MenuItem)ae.getTarget()).getText();
// If Exit is chosen, the program is terminated.
if(name.equals("Exit")) Platform.exit();
response.setText( name + " selected");
}
};
reset.setOnAction(MEHandler);
about.setOnAction(MEHandler);
//Add the menu bar to the top of the border pane and
//the response label to the center position.
rootNode.setTop(mb);
rootNode.setCenter(response);
myStage.show();
}
}
RESULT:
Thus the java program to showcase menu creation in javafx has been
implemented and the output has been verified successfully