Your job is to create this applet.
When a button, like cylinder, is pressed a dialog appears for each piece of information needed to calculate the volume of that shape:
Then another,
Finally the volume is calculated and rounded. The labels are all updated to show the calculations.
Note that the picture changed from:
to
You need to make each button work. Use a method to calculate the volume of each shape and call it in the button's section of actionPerformed.
For example, the cylinder's actionPerformed code and cylinderVolume method is as follows:
public void actionPerformed (ActionEvent e)
{
if (e.getActionCommand ().equals ("Cy"))
{
int height = Integer.parseInt (JOptionPane.showInputDialog ("Enter a height:"));
int radius = Integer.parseInt (JOptionPane.showInputDialog ("Enter a radius:"));
double volume = cylinderVolume (height, radius);
volume = round (volume,1);
l3.setText ("Height = " + height + " cm. Radius = " + radius + "cm.");
l4.setText ("The volume is " + volume + " cm^3.");
}
}
public double cylinderVolume (int h, int r)
{
return r * r * Math.PI * h;
}
public double round (double num, int digit)
{
double num2 = num * Math.pow (10, digit);
double num3 = (num2 - ((int) num2)) * 10;
num2 = ((int) num2);
if (num3 >= 5)
num2++;
return num2 /= Math.pow (10, digit);
}
When you are finished, add comments. Put the title comments in (name, date, purpose). Put a comment before each if. Put a comment at the start of each method to explain what it does.
To get extra levels add some extra features. For example, you chould also calculate the Surface Areas of the shapes. You could add some other shapes (triangular prism, square based pyramid, hexagonal prism, silo shape...).