Wa0006.
Wa0006.
Divya
Rollno:22124024
public AdditionGUI() {
setLayout(new FlowLayout());
setTitle("Addition Program");
setSize(250, 150);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
dispose();
}
});
}
resultField.setText(Integer.toString(result));
} catch (NumberFormatException e) {
resultField.setText("Invalid input");
}
}
}
SimpleCalculator() {
setTitle("Simple Calculator");
setSize(300, 400);
setLayout(null);
setVisible(true);
tf = new TextField();
tf.setBounds(50, 50, 200, 50);
add(tf);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
}
public SimpleCalculator() {
setLayout(new FlowLayout());
btnCalculate.addActionListener(this);
setTitle("Simple Calculator");
setSize(300, 150);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
});
}
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");
}
}