-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab12.java
More file actions
78 lines (52 loc) · 2.06 KB
/
Copy pathLab12.java
File metadata and controls
78 lines (52 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DivisionApp extends Frame implements ActionListener {
TextField num1Field;
TextField num2Field;
TextField resultField;
Button divideButton;
public DivisionApp() {
// Create components
num1Field = new TextField();
num1Field.setBounds(60, 50, 170, 20);
num2Field = new TextField();
num2Field.setBounds(60, 80, 170, 20);
resultField = new TextField();
resultField.setBounds(60, 110, 170, 20);
resultField.setEditable(false);
divideButton = new Button("Divide");
divideButton.setBounds(100, 150, 80, 30);
divideButton.addActionListener(this); add(num1Field);
add(num2Field);
add(resultField);
add(divideButton);
setSize(300, 300);
setLayout(null);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
try {
int num1 = Integer.parseInt(num1Field.getText());
int num2 = Integer.parseInt(num2Field.getText());
if (num2 == 0) {
throw new ArithmeticException("Division by zero is not allowed.");
}
int result = num1 / num2;
resultField.setText(String.valueOf(result));
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Please enter valid integers.", "Input Error", JOptionPane.ERROR_MESSAGE);
} catch (ArithmeticException ex) {
JOptionPane.showMessageDialog(this, ex.getMessage(), "Arithmetic Error", JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args) {
new DivisionApp();
}
}
//