0% found this document useful (0 votes)
2 views3 pages

Java Programs

Uploaded by

pshubh3113
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Java Programs

Uploaded by

pshubh3113
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Programs

Read Characters from Console Using BufferedReader

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ReadFromConsole {


public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a character: ");
char ch = (char) reader.read();
System.out.println("You entered: " + ch);
}
}

Copy Contents from One File to Another

import java.io.*;

public class FileCopy {


public static void main(String[] args) {
try {
FileReader fr = new FileReader("source.txt");
FileWriter fw = new FileWriter("destination.txt");
int ch;
while ((ch = fr.read()) != -1) {
fw.write(ch);
}
fr.close();
fw.close();
System.out.println("File copied successfully.");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}

JavaFX Add Operation

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class AddOperationFX extends Application {


public void start(Stage stage) {
TextField num1 = new TextField();
TextField num2 = new TextField();
Button addButton = new Button("Add");
Java Programs

Label resultLabel = new Label("Result:");

addButton.setOnAction(e -> {
try {
int a = Integer.parseInt(num1.getText());
int b = Integer.parseInt(num2.getText());
int sum = a + b;
resultLabel.setText("Result: " + sum);
} catch (NumberFormatException ex) {
resultLabel.setText("Invalid input!");
}
});

VBox vbox = new VBox(10, new Label("Enter first number:"), num1, new
Label("Enter second number:"), num2, addButton, resultLabel);
vbox.setStyle("-fx-padding: 20;");
stage.setScene(new Scene(vbox, 300, 250));
stage.setTitle("Add Operation");
stage.show();
}

public static void main(String[] args) {


launch(args);
}
}

Declare Person Class and Display Variables

class Person {
String name;
int age;
double salary;

Person(String name, int age, double salary) {


this.name = name;
this.age = age;
this.salary = salary;
}

void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}
}

public class PersonTest {


public static void main(String[] args) {
Person p = new Person("Alice", 30, 50000.00);
p.displayInfo();
Java Programs

}
}

You might also like