I have this program I have to write that calculates the volume of a pool and then displays the results along with how much water is needed to fill the pool and the cost to fill the pool if the cost is .77 cents per cubic foot plus a one time fee of $100. I have the code that calculates the pool volume, but I cant figure out how to get it to calculate how much water is needed or the cost. I am stummped. If someone could direct me in the right way to do this I would be so greatful. Here is the code I have so far.

import java.io.*;
import java.text.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.text.DecimalFormat;

public class PoolVolume {
private JFrame mainFrame;
private JButton calcButton, exitButton;
private JTextField length, width, depth, volume;
private JLabel lengthLabel, widthLabel, depthLabel, volumeLabel;
public PoolVolume() {
mainFrame = new JFrame("Swimming Pool Volume Calculator");
calcButton = new JButton("Calculate volume");
exitButton = new JButton("Exit");
length = new JTextField(5);
width = new JTextField(5);
depth = new JTextField(5);
volume = new JTextField(5);
lengthLabel = new JLabel("Enter the length of the swimming pool: ");
widthLabel = new JLabel("Enter the width of the swimming pool: ");
depthLabel = new JLabel("Enter the depth dept of the swimming pool: ");
volumeLabel = new JLabel("Swimming pool volume: ");
JPanel c=new JPanel(new GridLayout(5,2));
c.add(lengthLabel);
c.add(length);
c.add(widthLabel);
c.add(width);
c.add(depthLabel);
c.add(depth);
c.add(volumeLabel);
c.add(volume);
c.add(calcButton);
c.add(exitButton);
calcButton.setMnemonic('C');
exitButton.setMnemonic('x');
mainFrame.setSize(500,200);
mainFrame.add(c);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}});
calcButtonHandler chandler =new calcButtonHandler();
calcButton.addActionListener(chandler);
exitButtonHandler ehandler =new exitButtonHandler();
exitButton.addActionListener(ehandler);
FocusHandler fhandler =new FocusHandler();
length.addFocusListener(fhandler);
width.addFocusListener(fhandler);
depth.addFocusListener(fhandler);
volume.addFocusListener(fhandler);
mainFrame.setVisible(true);
}
class calcButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
DecimalFormat num =new DecimalFormat(",###.##");
Double cLength, cWidth, cdepth, cVolume, Total;
String sLength, sWidth, sdepth, sVolume;
sLength =length.getText();
cLength = Double.parseDouble(sLength);
sWidth =width.getText();
cWidth = Double.parseDouble(sWidth);
sdepth =depth.getText();
cdepth = Double.parseDouble(sdepth);
if(e.getSource() == calcButton) {
Total = cLength * cWidth * cdepth;
volume.setText(num.format(Total));
try{
String value=volume.getText();
File file = new File("output.txt");
FileWriter fstream = new FileWriter(file,true);
BufferedWriter out = new BufferedWriter(fstream);
out.write("Length= "+sLength+", Width= "+sWidth+", Depth= "+sdepth+" so the volume of Swimming Pool is "+value);
out.newLine();
out.close();
}
catch(Exception ex){}
}
}
}
class exitButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
class FocusHandler implements FocusListener {
public void focusGained(FocusEvent e) {
}
public void focusLost(FocusEvent e) {
}
}
public static void main(String args[]){
new PoolVolume();
}
}

It looks like you calculate the volume correctly in there. From there, the cost should be a piece of cake. What's the specific trouble you're having?

Also, I see you're writing the output to a file - is this intentional? Normally, writing this output to the screen would be sufficient. (Unless your prof wants an output file to check your work, or something)

I suppose the problem that I am having is that I am not sure how to write out the code for the cost and how much water is needed. I am sure it is simple but I cant wrap my mind around it.

I'm mystified. The amount of water required should be equal to the volume of the pool, shouldn't it? Or am I missing something?

Yes all I need is the code that takes the computed volume and determines the cost, and then displays it in the text field next to cost. This is what I cant figure out.

Here is the new code.

import java.io.*;
import java.text.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.text.DecimalFormat;

public class PoolVolume {
private JFrame mainFrame;
private JButton calcButton, exitButton;
private JTextField length, width, depth, volume, cost;
private JLabel lengthLabel, widthLabel, depthLabel, volumeLabel, costLabel;
public PoolVolume() {
mainFrame = new JFrame("Swimming Pool Volume Calculator");
calcButton = new JButton("Calculate");
exitButton = new JButton("Exit");
length = new JTextField(3);
width = new JTextField(3);
depth = new JTextField(3);
volume = new JTextField(3);
cost = new JTextField(3);
lengthLabel = new JLabel("Length: ");
widthLabel = new JLabel("Width: ");
depthLabel = new JLabel("Depth ");
volumeLabel = new JLabel("Volume: ");
costLabel= new JLabel ("Cost:");
JPanel c=new JPanel(new GridLayout(3,2));
c.add(lengthLabel);
c.add(length);
c.add(widthLabel);
c.add(width);
c.add(depthLabel);
c.add(depth);
c.add(volumeLabel);
c.add(volume);
c.add(costLabel);
c.add(cost);
c.add(calcButton);
c.add(exitButton);
calcButton.setMnemonic('C');
exitButton.setMnemonic('x');
mainFrame.setSize(500,200);
mainFrame.add(c);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}});
calcButtonHandler chandler =new calcButtonHandler();
calcButton.addActionListener(chandler);
exitButtonHandler ehandler =new exitButtonHandler();
exitButton.addActionListener(ehandler);
FocusHandler fhandler =new FocusHandler();
length.addFocusListener(fhandler);
width.addFocusListener(fhandler);
depth.addFocusListener(fhandler);
volume.addFocusListener(fhandler);
mainFrame.setVisible(true);
}
class calcButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
DecimalFormat num =new DecimalFormat(",###.##");
Double cLength, cWidth, cdepth, cVolume, Total;
String sLength, sWidth, sdepth, sVolume;
sLength =length.getText();
cLength = Double.parseDouble(sLength);
sWidth =width.getText();
cWidth = Double.parseDouble(sWidth);
sdepth =depth.getText();
cdepth = Double.parseDouble(sdepth);
if(e.getSource() == calcButton) {
Total = cLength * cWidth * cdepth;
volume.setText(num.format(Total));
try{
String value=volume.getText();
File file = new File("output.txt");
FileWriter fstream = new FileWriter(file,true);
BufferedWriter out = new BufferedWriter(fstream);
out.write("Length= "+sLength+", Width= "+sWidth+", Depth= "+sdepth+" so the volume of Swimming Pool is "+value);
out.newLine();
out.close();
}
catch(Exception ex){}
}
}
}
class exitButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
class FocusHandler implements FocusListener {
public void focusGained(FocusEvent e) {
}
public void focusLost(FocusEvent e) {
}
}
public static void main(String args[]){
new PoolVolume();
}
}

Write down on a piece of paper the information that you have.

Then use algebra to compute the answer.

For example: a car is moving at 20 miles/hour and will travel for 10 hours
How far would the car have gone?

Ok I know that but still do not understand how you code that, so that the program displays it in the right place.

Ok, you now understand the algrebra and can compute the values you need.

Where is "the right place" to display the results? Can you explain that?

Show what is displayed now
and post what you'd like to have displayed.

Ok the program displays the volume, but not the cost to fill because that code has not been written yet. I dont know how to write that code. I am new to Java and that is why I have asked for help. I can input the algebra, but I dont know what to write to get it started. Does that make sense? I mean anyone can write out an algebraic equation, but that does not make it java code.

Okay, why don't you start by showing us the algebraic equation.

ok here it is:

Total cost = one time fee + volume x .7

sorry that is Total cost= one time fee + volume * .77

or Total Cost= 100.00+Volume*.77

Excellent, now we're getting somewhere. (by the way, you can edit your posts, if you find you've made a typo - look to the left of the text)

So let's say you have a variable to hold the total cost. Call it, I dunno, TotalCost. And, for the sake of style, declare your constants as final variables so you don't have "magic numbers" in your code.

double TotalCost = 0.0;
final double ONE_TIME_FEE = 100.00;    
final double PRICE_PER_CUBIC_FOOT = 0.77;

As a side note, it's not ideal to use floats or doubles for dollar amounts, but that's an issue for another day.

So, given these declarations, can you write the equation in java? (you'll want to use parentheses)

Thanks, That helped me a lot!

Excellent, glad to hear it. Any more questions on this one, or are we done here?
Do you understand why I want you to put the constants into "variables" instead of writing them directly in the code?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.