Computer Program for Pool Volume and Cost Calculator

Only available on StudyMode
  • Download(s) : 62
  • Published : February 7, 2011
Open Document
Text Preview
package poolvolume;

/**PRG/421
*
* @author Mike
*/
//Next four lines import the classes needed for the program's functions import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.text.DecimalFormat;

public class PoolVolume extends JFrame
{
//declaring varibales for program
private JFrame mainFrame;
private JButton calcButton;
private JButton clearButton;
private JButton exitButton;
private JTextField length;
private JTextField width;
private JTextField average;
private JTextField rate;
private JLabel volume;
private JLabel cost;
private JLabel lengthLabel;
private JLabel widthLabel;
private JLabel averageLabel;
private JLabel volumeLabel;
private JLabel rateLabel;
private JLabel costLabel;

public PoolVolume()
{
//setting up the layout for the frame
this.setLayout(new BorderLayout());
mainFrame = new JFrame("Swimming Pool Filling Cost Analyzer"); calcButton = new JButton("Calculate");
clearButton = new JButton("Clear");
exitButton = new JButton("Exit");
length = new JTextField(5);
width = new JTextField(5);
average = new JTextField(5);
volume = new JLabel();
rate = new JTextField(5);
cost = new JLabel("");
lengthLabel = new JLabel("Enter the length of the swimming pool: "); widthLabel = new JLabel("Enter the width of the swimming pool: "); averageLabel = new JLabel("Enter the average depth of the swimming pool: "); volumeLabel = new JLabel("The Volume of your swimming pool is: "); rateLabel = new JLabel("Enter in cost of water per cubic foot: "); costLabel = new JLabel("Cost to fill your swimming pool: "); Container c = mainFrame.getContentPane();

c.setLayout(new GridLayout(8,2));//sets up a grid layout of 8 row and 2 columns //adds the objects to the frame
c.add(lengthLabel, BorderLayout.SOUTH);
c.add(length);
c.add(widthLabel);
c.add(width);
c.add(averageLabel);
c.add(average);
c.add(rateLabel);
c.add(rate);
c.add(volumeLabel);
c.add(volume);
c.add(costLabel);
c.add(cost);
c.add(calcButton);
c.add(clearButton);
c.add(exitButton);
//sets shortcut keys for the buttons.
calcButton.setMnemonic('C');
clearButton.setMnemonic('l');
exitButton.setMnemonic('x');
mainFrame.setSize(550,300);
mainFrame.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {System.exit(0);}}); //creates objects for calcButtonHandler
calcButtonHandler chandler =new calcButtonHandler();
calcButton.addActionListener(chandler);
clearButtonHandler lHandler =new clearButtonHandler();
clearButton.addActionListener(lHandler);
exitButtonHandler ehandler =new exitButtonHandler();
exitButton.addActionListener(ehandler);

//creates objects for FoucusHandler
FocusHandler fhandler =new FocusHandler();
length.addFocusListener(fhandler);
width.addFocusListener(fhandler);
average.addFocusListener(fhandler);
volume.addFocusListener(fhandler);
mainFrame.setVisible(true);
}

class calcButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {

try//sets what happens when the user clicks the calculate buttone {

DecimalFormat num =new DecimalFormat(",###.##");
Double cLength, cWidth, cAverage,cRate, cCost, Total,cVolume; String sLength, sWidth, sAverage,sRate,sVolume;

sLength =length.getText();
cLength = Double.parseDouble(sLength);
sWidth =width.getText();
cWidth = Double.parseDouble(sWidth);
sAverage =average.getText();
cAverage = Double.parseDouble(sAverage);
sRate = rate.getText();
cRate = Double.parseDouble(sRate);
Total = cLength * cWidth * cAverage;
volume.setText(num.format(Total));
sVolume = volume.getText();...
tracking img