Skip to content

Commit fd0095f

Browse files
committed
Create swing.java
1 parent cc2feac commit fd0095f

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

Integer_Division/swing.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Create a User Interface to perform Integer Division. The user enters two
3+
* numbers in text fields, Num1 and Num2. The division of Num1 and Num2 is
4+
* displayed in the result field when the divide button is clicked. If Num1 or
5+
* Num2 were not integer, the program would throw a NumberFormatException, If
6+
* Num2 is Zero, the program would throw an Arithmetic Exception. Display the
7+
* Exception in message box.
8+
*/
9+
10+
package Integer_Division;
11+
12+
import java.awt.*;
13+
import java.awt.event.*;
14+
import javax.swing.*;
15+
16+
class A extends JFrame implements ActionListener {
17+
JLabel l1, l2, l3;
18+
JTextField tf1, tf2, tf3;
19+
JButton b1;
20+
21+
A() {
22+
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
23+
setLayout(new FlowLayout());
24+
setSize(500, 200);
25+
l1 = new JLabel("Enter Number 1");
26+
add(l1);
27+
tf1 = new JTextField(10);
28+
add(tf1);
29+
l2 = new JLabel("Enter Number 2");
30+
add(l2);
31+
tf2 = new JTextField(10);
32+
add(tf2);
33+
l3 = new JLabel("Result");
34+
add(l3);
35+
tf3 = new JTextField(10);
36+
add(tf3);
37+
b1 = new JButton("Divide");
38+
add(b1);
39+
b1.addActionListener(this);
40+
setVisible(true);
41+
}
42+
43+
public void actionPerformed(ActionEvent ae) {
44+
try {
45+
int a = Integer.parseInt(tf1.getText());
46+
int b = Integer.parseInt(tf2.getText());
47+
if (b == 0)
48+
throw new ArithmeticException("Divide by Zero Error");
49+
float c = (float) a / b;
50+
tf3.setText(String.valueOf(c));
51+
} catch (NumberFormatException ex) {
52+
JOptionPane.showMessageDialog(this, ex.getMessage());
53+
} catch (ArithmeticException ex) {
54+
JOptionPane.showMessageDialog(this, ex.getMessage());
55+
}
56+
}
57+
}
58+
59+
public class swing {
60+
public static void main(String[] args){
61+
new A();
62+
}
63+
}

0 commit comments

Comments
 (0)