5. (TCO 3, 4) Assume the existence of the following Java GUI application. There
ID: 1812475 • Letter: 5
Question
5. (TCO 3, 4) Assume the existence of the following Java GUI application. There are two text fields for user input. The first JTextField variable is named radius and is used to input a radius value. The second JTextField variable named height is used to input a height value. All input values and calculations use floating point values. The user clicks a JButton to perform a calculation. One JButton called area causes the program to calculate and display the surface area of a cylinder, A = 2 * pi * r * r + 2 * pi * r * h. Another JButton called volume causes the program to calculate the volume of the cylinder, volume = pi * r * r * h. The result of the selected calculation is displayed in an output text area variable named output. The output string should say something like this: "Surface area of a cylinder with radius 1 and height 1 is 12.57." The output should display two digits after the decimal point. 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
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ButtonHandler {
//assuming two Jbuttons: area and volume
//Two Text fields radius and height
//One Text Area Output
/*
JButton area, volume;
JTextArea output;
JTextField radius, height;
double r, h;
*/
public ButtonHandler() {
area.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent areaevent) {
load_r_and_h();
calculateanddisplayarea(areaevent);
}
});
volume.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent volumeEvent) {
load_r_and_h();
calculateanddisplayvolume(volumeEvent);
}
});
}
public void load_r_and_h() {
r = Double.parseDouble(radius.getText());
h = Double.parseDouble(height.getText());
}
public void calculateanddisplayarea(ActionEvent areaevent) {
double area_of_cylinder = 0.0;
area_of_cylinder = (2 * Math.PI * r * r) + (2 * Math.PI * r * h);
DecimalFormat twoD = new DecimalFormat("#.00");
String out_area;
out_area = twoD.format(area_of_cylinder);
String output_string = "Surface area of a cylinder with radius " + r + " and height " + h + " is " + out_area;
output.setText(output_string);
}
public void calculateanddisplayvolume(ActionEvent volumeEvent) {
double volume_of_cylinder = 0.0;
volume_of_cylinder = Math.PI * r * r * h;
DecimalFormat twoD = new DecimalFormat("#.00");
String out_volume;
out_volume = twoD.format(volume_of_cylinder);
String output_string = "volume of a cylinder with radius " + r + " and height " + h + " is " + out_volume;
output.setText(output_string);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.