Upload files to "OOP/Java/Lab/Week13"

This commit is contained in:
Aadit Agrawal 2024-10-26 02:15:11 +05:30
parent 0909e7c1ae
commit 8fbdd81a2b
5 changed files with 402 additions and 0 deletions

View File

@ -0,0 +1,122 @@
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField display;
private double result = 0;
private String operator = "=";
private boolean calculating = true;
public Calculator() {
display = new JTextField("0", 20);
display.setHorizontalAlignment(JTextField.RIGHT);
display.setEditable(false);
// Create panel with grid layout for buttons
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4, 5, 5));
String[] buttonLabels = {
"7",
"8",
"9",
"/",
"4",
"5",
"6",
"*",
"1",
"2",
"3",
"-",
"0",
".",
"=",
"+",
};
for (String label : buttonLabels) {
JButton button = new JButton(label);
buttonPanel.add(button);
button.addActionListener(this);
}
JButton clearButton = new JButton("C");
clearButton.addActionListener(e -> {
result = 0;
operator = "=";
calculating = true;
display.setText("0");
});
// Layout
setLayout(new BorderLayout(5, 5));
add(display, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.CENTER);
add(clearButton, BorderLayout.SOUTH);
setTitle("Simple Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (
(command.charAt(0) >= '0' && command.charAt(0) <= '9') ||
command.equals(".")
) {
if (calculating) {
display.setText(command);
} else {
display.setText(display.getText() + command);
}
calculating = false;
} else {
if (calculating) {
if (command.equals("-")) {
display.setText(command);
calculating = false;
} else {
operator = command;
}
} else {
double x = Double.parseDouble(display.getText());
calculate(x);
operator = command;
calculating = true;
}
}
}
private void calculate(double n) {
switch (operator) {
case "+":
result += n;
break;
case "-":
result -= n;
break;
case "*":
result *= n;
break;
case "/":
result /= n;
break;
case "=":
result = n;
break;
}
display.setText("" + result);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new Calculator().setVisible(true);
});
}
}

View File

@ -0,0 +1,54 @@
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Credentials extends Application {
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Login Form");
GridPane grid = new GridPane();
grid.setPadding(new Insets(10, 10, 10, 10));
grid.setVgap(5);
grid.setHgap(5);
Label username = new Label("Username:");
grid.add(username, 0, 0);
TextField userTextField = new TextField();
grid.add(userTextField, 1, 0);
Label password = new Label("Password:");
grid.add(password, 0, 1);
PasswordField passwordField = new PasswordField();
grid.add(passwordField, 1, 1);
Button signInButton = new Button("Sign in");
grid.add(signInButton, 1, 2);
Text welcomeText = new Text();
grid.add(welcomeText, 1, 3);
signInButton.setOnAction(e -> {
String name = userTextField.getText();
welcomeText.setText("Welcome " + name);
});
Scene scene = new Scene(grid, 300, 200);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

View File

@ -0,0 +1,95 @@
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
class Employee {
private String name;
private String empID;
private String designation;
private double basicPay;
private double DA;
private double HRA;
private double PF;
private double LIC;
private double netSalary;
public Employee(
String name,
String empID,
String designation,
double basicPay,
double LIC
) {
this.name = name;
this.empID = empID;
this.designation = designation;
this.basicPay = basicPay;
this.LIC = LIC;
this.DA = 0.4 * basicPay;
this.HRA = 0.15 * basicPay;
this.PF = 0.12 * basicPay;
this.netSalary = basicPay + DA + HRA - PF - LIC;
}
public String getEmployeeInfo() {
return String.format(
"""
Employee Details:
Name: %s
Employee ID: %s
Designation: %s
Basic Pay: $%.2f
DA: $%.2f
HRA: $%.2f
PF: $%.2f
LIC: $%.2f
Net Salary: $%.2f
""",
name,
empID,
designation,
basicPay,
DA,
HRA,
PF,
LIC,
netSalary
);
}
}
public class EmployeeInfoApp extends Application {
@Override
public void start(Stage primaryStage) {
Employee emp = new Employee(
"John Doe",
"E123",
"Software Engineer",
50000,
2000
);
Label infoLabel = new Label(emp.getEmployeeInfo());
infoLabel.setStyle("-fx-font-family: monospace;");
VBox root = new VBox(10);
root.setPadding(new Insets(10));
root.getChildren().add(infoLabel);
Scene scene = new Scene(root, 300, 400);
primaryStage.setTitle("Employee Information");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

View File

@ -0,0 +1,65 @@
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class GCDCalculator extends Application {
@Override
public void start(Stage primaryStage) {
TextField num1Field = new TextField();
num1Field.setPromptText("Enter first number");
TextField num2Field = new TextField();
num2Field.setPromptText("Enter second number");
Label resultLabel = new Label();
Button calculateButton = new Button("Calculate GCD");
calculateButton.setOnAction(e -> {
try {
int num1 = Integer.parseInt(num1Field.getText());
int num2 = Integer.parseInt(num2Field.getText());
if (num1 <= 0 || num2 <= 0) {
resultLabel.setText("Please enter positive integers only");
return;
}
int gcd = calculateGCD(num1, num2);
resultLabel.setText(
String.format("GCD of %d and %d is: %d", num1, num2, gcd)
);
} catch (NumberFormatException ex) {
resultLabel.setText("Please enter valid integers");
}
});
VBox root = new VBox(10);
root.setAlignment(Pos.CENTER);
root
.getChildren()
.addAll(num1Field, num2Field, calculateButton, resultLabel);
Scene scene = new Scene(root, 300, 200);
primaryStage.setTitle("GCD Calculator");
primaryStage.setScene(scene);
primaryStage.show();
}
private int calculateGCD(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
public static void main(String[] args) {
launch(args);
}
}

View File

@ -0,0 +1,66 @@
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class Multiplier extends Application {
@Override
public void start(Stage primaryStage) {
FlowPane root = new FlowPane();
root.setPadding(new Insets(10));
root.setHgap(10);
root.setVgap(10);
Label inputLabel = new Label("Enter a number:");
TextField numberInput = new TextField();
Button generateButton = new Button("Generate Table");
TextArea resultArea = new TextArea();
resultArea.setPrefRowCount(12);
resultArea.setPrefColumnCount(20);
resultArea.setEditable(false);
generateButton.setOnAction(e -> {
try {
int number = Integer.parseInt(numberInput.getText());
StringBuilder table = new StringBuilder();
table
.append("Multiplication Table for ")
.append(number)
.append("\n\n");
for (int i = 1; i <= 10; i++) {
table
.append(number)
.append(" x ")
.append(i)
.append(" = ")
.append(number * i)
.append("\n");
}
resultArea.setText(table.toString());
} catch (NumberFormatException ex) {
resultArea.setText("Please enter a valid integer");
}
});
root
.getChildren()
.addAll(inputLabel, numberInput, generateButton, resultArea);
Scene scene = new Scene(root, 300, 400);
primaryStage.setTitle("Multiplication Table Generator");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}