Assume the existence of the following Java GUI application. There are two text f
ID: 3544930 • Letter: A
Question
Assume the existence of the following Java GUI application. There are two text fields for user input. The first JTextField variable is named res1 and is used to input a resistance value in ohms. The second JTextField variable named tol is used to input a tolerance percentage. All input values and calculations use integer values. The tolerance value is entered as an integer percentage between 1 and 100. The user clicks a JButton to perform a calculation. One JButton called min causes the program to calculate and display the minimum value of the resistor, Rmin = R - R * tolerance. Another JButton called max causes the program to calculate the maximum value of the resistor, Rmax = R + R * tolerance. The result of the selected calculation is displayed in an output text area variable named output. The output string should say something like this: "The max resistance value of a 100 ohm resistor with a 10 percent tolerance is 110 ohms". Clicking one of the buttons generates the event which causes the program to do the selected calculation and update the output area. Assume the user interface has already been implemented and the member variables listed above exist. Your job is to write the ButtonHandler inner class which handles the events from the two buttons, does the requested calculation and displays the appropriate result.
Explanation / Answer
// insert these code in the class and compile it any doubt please ask and do rate best please
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==min)// say your button named as min
{
try
{
String a1 = res1.getText();
String b1 = tol.getText();
if( !(a1.equals("") || b1.equals("")) ) {
int res11 = Integer.parseInt(a);
int tol1 = Integer.parseInt(b);
int Rmin = res11 - (res11*(tol1*0.01));
JOptionPane.showMessageDialog(null,"The min resistance value of a "+res11+" ohm resistor with a "+tol1+" percent tolerance is "+Rmin+" ohms");
}
else {
JOptionPane.showMessageDialog(null,"Wrong Input");
res1.setText("");
tol.setText("");
res1.requestFocus();
}
}
catch(Exception e) {
e.printStackTrace();
}
}
if(ae.getSource()==max)// say your button named as max
{
try
{
String a = res1.getText();
String b = tol.getText();
if( !(a.equals("") || b.equals("")) ) {
int res1 = Integer.parseInt(a);
int tol = Integer.parseInt(b);
int Rmax = res1 + (res1*(tol*0.01));
JOptionPane.showMessageDialog(null,"The max resistance value of a "+res1+" ohm resistor with a "+tol+" percent tolerance is "+Rmax+" ohms");
}
else {
JOptionPane.showMessageDialog(null,"Wrong Input");
res1.setText("");
tol.setText("");
res1.requestFocus();
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.