Task 1
package simpleCalculator;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class SimpleCalculator extends Frame {
private TextField tfX;
private TextField tfY;
private TextField tfResult;
SimpleCalculator(String title) { super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ev) {
System.exit(0);
}
});
setSize(500, 150);
init();
setVisible(true);
}
public static void main(String[] args) throws Exception {
SimpleCalculator myCalculator = new SimpleCalculator("Simple
Calculator");
}
public void init() {
setLayout(new BorderLayout(10, 10));
Panel topPanel = new Panel();
topPanel.setLayout(new GridLayout(1, 4, 10, 10));
Button btnAdd = new Button("+");
Button btnSubtract = new Button("-");
Button btnMultiply = new Button("*");
Button btnDivide = new Button("/");
topPanel.add(btnAdd);
topPanel.add(btnSubtract);
topPanel.add(btnMultiply);
topPanel.add(btnDivide);
Panel bottomPanel = new Panel();
bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10,
10));
tfX = new TextField("X", 8);
tfY = new TextField("Y", 8);
tfResult = new TextField("RESULT", 8);
tfResult.setEditable(false);
bottomPanel.add(tfX);
bottomPanel.add(tfY);
bottomPanel.add(tfResult);
add(topPanel, BorderLayout.NORTH);
add(bottomPanel, BorderLayout.CENTER);
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
calculate('+');
}
});
btnSubtract.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
calculate('-');
}
});
btnMultiply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
calculate('*');
}
});
btnDivide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
calculate('/');
}
});
pack();
}
private void calculate(char operation) {
try {
int x = Integer.parseInt(tfX.getText());
int y = Integer.parseInt(tfY.getText());
int result = 0;
switch (operation) {
case '+':
result = x + y;
break;
case '-':
result = x - y;
break;
case '*':
result = x * y;
break;
case '/':
if (y == 0) {
tfResult.setText("Error: Div by zero");
return;
}
result = x / y;
break;
}
tfResult.setText(String.valueOf(result));
} catch (NumberFormatException e) {
tfResult.setText("Error: Invalid input");
}
}
}
Task 2
package calculatorSimple;
import java.awt.*;
import java.awt.event.*;
public class CalculatorSimple extends Frame {
private TextField xField;
private TextField yField;
private TextField resultField;
private Label operatorLabel;
private int mode = 0; // 0: +, 1: -, 2: *, 3: /
private final String[] modes = {"+", "-", "*", "/"};
public CalculatorSimple(String title) {
super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ev) {
System.exit(0);
}
});
setLayout(new FlowLayout());
Button actionButton = new Button("ACTION");
xField = new TextField("0", 5);
operatorLabel = new Label("+");
yField = new TextField("0", 5);
resultField = new TextField("0", 5);
resultField.setEditable(false);
Button modeButton = new Button("MODE");
add(actionButton);
add(xField);
add(operatorLabel);
add(yField);
add(new Label("="));
add(resultField);
add(modeButton);
actionButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calculate();
}
});
modeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
switchMode();
}
});
setSize(400, 100);
setVisible(true);
}
private void calculate() {
try {
int x = Integer.parseInt(xField.getText());
int y = Integer.parseInt(yField.getText());
int result = 0;
if (mode == 0) {
result = x + y;
} else {
if (mode == 1) {
result = x - y;
} else {
if (mode == 2) {
result = x * y;
} else {
if (y != 0) {
result = x / y;
} else {
resultField.setText("Error");
return;
}
}
}
}
resultField.setText(String.valueOf(result));
} catch (NumberFormatException ex) {
resultField.setText("Invalid input");
}
}
private void switchMode() {
mode = (mode + 1) % modes.length;
operatorLabel.setText(modes[mode]);
}
public static void main(String[] args) {
new CalculatorSimple("Simple Calculator");
}
}