Name: B.H.M.S.
Divya
Rollno:22124024
JAVA ASSIGNMENT WEEK 9
1. Write a GUI-based program in java to perform the addition of two
numbers. Take input from two text boxes and show output in the third
text box.
import java.awt.*;
import java.awt.event.*;
public class AdditionGUI extends Frame implements ActionListener {
TextField num1Field, num2Field, resultField;
public AdditionGUI() {
setLayout(new FlowLayout());
Label num1Label = new Label("Enter first number: ");
add(num1Label);
num1Field = new TextField(10);
add(num1Field);
Label num2Label = new Label("Enter second number: ");
add(num2Label);
num2Field = new TextField(10);
add(num2Field);
Button addButton = new Button("Add");
add(addButton);
addButton.addActionListener(this);
Label resultLabel = new Label("Result: ");
add(resultLabel);
resultField = new TextField(10);
resultField.setEditable(false);
add(resultField);
setTitle("Addition Program");
setSize(250, 150);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
dispose();
}
});
}
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("Add")) {
try {
int num1 = Integer.parseInt(num1Field.getText());
int num2 = Integer.parseInt(num2Field.getText());
int result = num1 + num2;
resultField.setText(Integer.toString(result));
} catch (NumberFormatException e) {
resultField.setText("Invalid input");
}
}
}
public static void main(String[] args) {
new AdditionGUI();
}
}
OUTPUT:
2. Write a program in java to create a simple calculator with basic
operations like +, -, /, *, %. Use
buttons for input and TextField for Output. It should be similar to a
traditional simple calculator.
import java.awt.*;
import java.awt.event.*;
public class SimpleCalculator extends Frame implements ActionListener {
TextField tf;
String operator;
double num1, num2, result;
SimpleCalculator() {
setTitle("Simple Calculator");
setSize(300, 400);
setLayout(null);
setVisible(true);
tf = new TextField();
tf.setBounds(50, 50, 200, 50);
add(tf);
Button[] buttons = new Button[16];
String[] buttonLabels = {"7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "C",
"0", "=", "/"};
int x = 50, y = 120, width = 50, height = 50;
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new Button(buttonLabels[i]);
buttons[i].setBounds(x, y, width, height);
buttons[i].addActionListener(this);
add(buttons[i]);
x += width + 10;
if ((i + 1) % 4 == 0) {
x = 50;
y += height + 10;
}
}
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("+") || command.equals("-") || command.equals("*") ||
command.equals("/") || command.equals("%")) {
operator = command;
num1 = Double.parseDouble(tf.getText());
tf.setText("");
} else if (command.equals("=")) {
num2 = Double.parseDouble(tf.getText());
switch (operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
if (num2 != 0)
result = num1 / num2;
else
tf.setText("Cannot divide by zero");
break;
case "%":
result = num1 % num2;
break;
}
tf.setText(String.valueOf(result));
} else if (command.equals("C")) {
tf.setText("");
} else {
String currentText = tf.getText();
tf.setText(currentText + command);
}
}
public static void main(String[] args) {
new SimpleCalculator();
}
}
OUTPUT:
3. Implement exception handling for invalid inputs (e.g., 7*+=9 is an
invalid input) in the previous
program.
import java.awt.*;
import java.awt.event.*;
public class SimpleCalculator extends Frame implements ActionListener {
private TextField tfInput;
private Label lblResult;
public SimpleCalculator() {
setLayout(new FlowLayout());
Label lblInput = new Label("Enter an arithmetic expression:");
add(lblInput);
tfInput = new TextField(20);
add(tfInput);
Button btnCalculate = new Button("Calculate");
add(btnCalculate);
lblResult = new Label("Result:");
add(lblResult);
btnCalculate.addActionListener(this);
setTitle("Simple Calculator");
setSize(300, 150);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
String expression = tfInput.getText();
try {
int result = evaluateExpression(expression);
lblResult.setText("Result: " + result);
} catch (NumberFormatException ex) {
lblResult.setText("Error: Invalid input");
} catch (ArithmeticException ex) {
lblResult.setText("Error: Division by zero");
}
}
private int evaluateExpression(String expression) throws NumberFormatException,
ArithmeticException {
String[] tokens = expression.split(" ");
int operand1 = Integer.parseInt(tokens[0]);
char operator = tokens[1].charAt(0);
int operand2 = Integer.parseInt(tokens[2]);
switch (operator) {
case '+':
return operand1 + operand2;
case '-':
return operand1 - operand2;
case '*':
return operand1 * operand2;
case '/':
if (operand2 == 0) {
throw new ArithmeticException("Division by zero");
}
return operand1 / operand2;
default:
throw new NumberFormatException("Invalid operator");
}
}
public static void main(String[] args) {
new SimpleCalculator();
}
}
Output: