import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class FindVolume extends Applet implements ItemListener
{
//declare variables and construct a color
double rUnits, lUnits, wUnits, hUnits, answer;
int objCode;
Color custColor = new Color(0, 50, 160);
// create components for applet
Label radiusLabel = new Label("Enter radius of object:");
TextField radiusField = new TextField(15);
Label lengthLabel = new Label("Enter length of object:");
TextField lengthField = new TextField(15);
Label widthLabel = new Label("Enter width of object:");
TextField widthField = new TextField(15);
Label heightLabel = new Label("Enter height of object:");
TextField heightField = new TextField(15);
Label codeLabel = new Label("Select the object you want volume for:");
CheckboxGroup codeGroup = new CheckboxGroup();
Checkbox boxBox = new Checkbox("Box",false,codeGroup);
Checkbox cylinderBox = new Checkbox("Cylinder",false,codeGroup);
Checkbox coneBox = new Checkbox("Cone",false,codeGroup);
Checkbox sphereBox = new Checkbox("Sphere",false,codeGroup);
Checkbox hiddenBox = new Checkbox("",true,codeGroup);
Label outputLabel = new Label("Click an option to calculate volume.");
public void init()
{
setBackground(custColor);
setForeground(Color.white);
add(radiusLabel);
add(radiusField);
radiusField.requestFocus();
radiusField.setForeground(Color.black);
add(lengthLabel);
add(lengthField);
lengthField.requestFocus();
lengthField.setForeground(Color.black);
add(widthLabel);
add(widthField);
widthField.requestFocus();
widthField.setForeground(Color.black);
add(heightLabel);
add(heightField);
heightField.requestFocus();
heightField.setForeground(Color.black);
add(codeLabel);
add(boxBox);
boxBox.addItemListener(this);
add(cylinderBox);
cylinderBox.addItemListener(this);
add(coneBox);
coneBox.addItemListener(this);
add(sphereBox);
sphereBox.addItemListener(this);
add(outputLabel);
}
// this method is triggered by the user clicking the option button
public void itemStateChanged(ItemEvent choice)
{
try
{
rUnits = getRadius();
lUnits = getLength();
wUnits = getWidth();
hUnits = getHeight();
objCode = getCode();
answer = getVol(rUnits,lUnits,wUnits,hUnits,objCode);
output(answer, rUnits, lUnits, wUnits, hUnits);
}
catch(NumberFormatException e)
{
outputLabel.setText("You must enter a number >= zero.");
hiddenBox.setState(true);
radiusField.setText("");
radiusField.requestFocus();
lengthField.setText("");
lengthField.requestFocus();
widthField.setText("");
widthField.requestFocus();
heightField.setText("");
heightField.requestFocus();
}
}
public double getRadius()
{
double radius = Double.parseDouble(radiusField.getText());
if (radius <= 0) throw new NumberFormatException();
return radius;
}
public double getLength()
{
double length = Double.parseDouble(lengthField.getText());
if (length <= 0) throw new NumberFormatException();
return length;
}
public double getWidth()
{
double width = Double.parseDouble(widthField.getText());
if (width <= 0) throw new NumberFormatException();
return width;
}
public double getHeight()
{
double height = Double.parseDouble(heightField.getText());
if (height <= 0) throw new NumberFormatException();
return height;
}
public int getCode()
{
int code = 0;
if (boxBox.getState()) code = 1;
else
if (cylinderBox.getState()) code = 2;
else
if (coneBox.getState()) code = 3;
else
if (sphereBox.getState()) code = 4;
return code;
}
public double getVol(double radius, double length, double width, double height, int code)
{
double volume = 0.0;
switch(code)
{
case 1:
volume = length * width * height;
break;
case 2:
volume = Math.PI * height * radius * radius;
break;
case 3:
volume = (Math.PI * radius * radius * height) / 3;
break;
case 4:
volume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( radius, 3 );
}
return volume;
}
public void output(double volume, double radius, double length, double width, double height)
{
DecimalFormat twoDigits = new DecimalFormat("###.##");
outputLabel.setText("Volume of selected object " + " = " + twoDigits.format(volume));
}
}