Do not delete anything from this file. Copy and paste your code under Answer at the
bottom.
Design a GUI with a frame, three text fields, four radio buttons, and a label (for the equal sign) as follows.
Write a program such that if the user enters two Integers on the text fields and click on any radio buttons
the result of the operation appears in the third text field. Note that there is no command button here (after
entering integer data on text field one and field two, click on the radio button for ‘+’, sum will appear on
text field three. And so for the other operations.)
For the following cases you must display a dialog box with an appropriate message:
1. One or both text fields are empty or contain non numerical strings, and the user clicks on one of the radio
buttons.
2. When division by zero happens.
Hint: You need to convert the content of both text fields to integer by using Integer class. It is at the above
cases an exception must happen (need try-catch block) and your program must issues an error message by a
dialog box.
Answer:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
try {
String a = input1.getText(), b = input2.getText();
float i = Float.parseFloat(a), j = Float.parseFloat(b);
if (evt.getActionCommand().equals("+")) {
output.setText(String.valueOf(i + j));
} else if (evt.getActionCommand().equals("-")) {
output.setText(String.valueOf(i - j));
} else if (evt.getActionCommand().equals("*")) {
output.setText(String.valueOf(i * j));
} else if (evt.getActionCommand().equals("/")) {
if(input2.getText().equals("0")){
JOptionPane.showMessageDialog(null, "Division by zero is not possible");
}
else
output.setText(String.valueOf(i/j));
}
} catch (Exception e){
JOptionPane.showMessageDialog(null, "Enter the values in textfiled first");
}
}
}