/*
   Chapter 4:  Programming Assignment 12
   Programmer: Brad Shedd
   Date:    January 27, 2004
   Filename:   VolumeComputations.java
   Purpose: Allows a program to compute the volume of a box, cylinder, cone and sphere
*/

import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.applet.*;
import javax.swing.*;

public class VolumeComputations extends Applet implements ActionListener, ItemListener
{
  
// variables
   static double length, width, height, radius, volume;
  
static boolean validResponse = false;

  
// GUI components
   Label shapeLabel = new Label("Select a shape                                                     ");
   Label dimensionsLabel =
new Label("Enter the required dimensions ");
   Label lengthLabel =
new Label("Length: ");
   Label widthLabel  =
new Label("Width: ");
   Label heightLabel =
new Label("Height: ");
   Label radiusLabel =
new Label("Radius: ");

   TextField lengthField =
new TextField(10);
   TextField widthField =
new TextField(10);
   TextField heightField =
new TextField(10);
   TextField radiusField =
new TextField(10);

   Button calcButton =
new Button("Calculate");

   CheckboxGroup shapeGroup =
new CheckboxGroup();
      Checkbox boxBox =
new Checkbox("Box", true, shapeGroup);
      Checkbox cylinderBox =
new Checkbox("Cylinder", false, shapeGroup);
      Checkbox coneBox =
new Checkbox("Cone", false, shapeGroup);
      Checkbox sphereBox =
new Checkbox("Sphere", false, shapeGroup);

  
public void init()
   {
      add(shapeLabel);
      add(boxBox);
      boxBox.addItemListener(
this);
      add(cylinderBox);
      cylinderBox.addItemListener(
this);
      add(coneBox);
      coneBox.addItemListener(
this);
      add(sphereBox);
      sphereBox.addItemListener(
this);
      add(dimensionsLabel);
      add(lengthLabel);
      add(lengthField);
      add(widthLabel);
      add(widthField);
      add(heightLabel);
      add(heightField);
      add(radiusLabel);
      add(radiusField);
      radiusField.setEnabled(
false);
      add(calcButton);
      calcButton.addActionListener(
this);
   }

  
public void actionPerformed(ActionEvent e)
   {
     DecimalFormat twoDigits =
new DecimalFormat("##.00");

      String message =
new String("");

      validResponse =
true;

     
// try to get by with valid input
      try
      {
        
// only a box needs length and width
         if (boxBox.getState())
         {
            length = Double.parseDouble(lengthField.getText());
            width = Double.parseDouble(widthField.getText());
            height = Double.parseDouble(heightField.getText());
            volume = length * width * height;
            message +=
"Length:  " + twoDigits.format(length) + "\n";
            message +=
"Width:   " + twoDigits.format(width) + "\n";

           
// if length or width is zero, throw an error
            if ((length <= 0) || (width <= 0))throw new NumberFormatException();
         }
        
else
         {
           
// only a box disregards radius, checked here
            radius = Double.parseDouble(radiusField.getText());
            message +=
"Radius:  " + twoDigits.format(radius) + "\n";
           
if (radius <= 0) throw new NumberFormatException();
         }
           
// if the shape is not a sphere, check the height
            if ((height <= 0) && (!sphereBox.isEnabled())) throw new NumberFormatException();
         }
        
catch (NumberFormatException oops)
         {
            JOptionPane.showMessageDialog(
null, "Please enter valid dimensions", "Invalid Input", JOptionPane.ERROR_MESSAGE);
           validResponse =
false;
            message =
"";
         }

     
if (validResponse)
      {
        
if (coneBox.getState()) volume = Math.PI * Math.pow(radius, 2) * height;
        
if (cylinderBox.getState()) volume = (Math.PI * Math.pow(radius, 2) * height) / 3;
        
if (sphereBox.getState())
         {
            volume = 4 / 3 * Math.PI * Math.pow(radius, 3);
         }
        
else
         {
            message +=
"Height:  " + twoDigits.format(height) + "\n";
           
if (height <= 0) throw new NumberFormatException();
         }

         message +=
"Volume:  " + twoDigits.format(volume);

         JOptionPane.showMessageDialog(
null, message, "Volume Computations", JOptionPane.PLAIN_MESSAGE);
      }
   }
// end actionPerformed()

   public void itemStateChanged(ItemEvent c)
   {
      lengthField.setEnabled(boxBox.getState());
      widthField.setEnabled(boxBox.getState());
      heightField.setEnabled(!sphereBox.getState());
      radiusField.setEnabled(!boxBox.getState());
   }
}

Homepage