1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java project calculation

  1. Mar 27, 2008 #1
    i'm supposed to create a GUI project that will display a package ID and calculate its shipping cost. the shipping cost is calculated per ounce, and i must convert the pounds to ounces for the shipping cost. everything seems to be working fine except for the calculation. the shipping cost is always showing up as 0, and i am pretty confident my formulas are right. is the formula not being read since it is only in a method? if so, how should i go about doing this?

    here is my code:
    Code (Text):

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



    public class shippingCharge extends JFrame implements ActionListener
    {

        //objects and data types created here
        JPanel mainPanel = new JPanel();
        JTextField packageIdentificationTextField = new JTextField(6);
        JTextField poundsTextField = new JTextField(10);
        JTextField ouncesTextField = new JTextField(10);
        JButton displayButton = new JButton("Calculate  ");
        JTextArea outputTextArea = new JTextArea("Your shipping charge ", 10,30);
       
        //Variables
        String packageIdentificationString;
        double poundDouble, ouncesDouble, poundToOunceOuncesDouble, shippingCostDouble;
     

        public static void main(String[] args)
        {
            shippingCharge shippingTotal = new shippingCharge();
        }
           
        public shippingCharge()
        {
            designFrame();
            setSize(500,500);
            setVisible(true);
           
        }
       
        public void designFrame()
        {
            mainPanel.add(new JLabel("Package ID"));
            mainPanel.add(packageIdentificationTextField);
            mainPanel.add(new JLabel("Pounds"));
            mainPanel.add(poundsTextField);
            mainPanel.add(new JLabel("Ounces"));
            mainPanel.add(ouncesTextField);
            mainPanel.add(displayButton);
            mainPanel.add(outputTextArea);
           
            add(mainPanel);
            //add listener to the  object
            packageIdentificationTextField.addActionListener(this);
            displayButton.addActionListener(this);
           
        }
       
        public void getInput()
        {
            packageIdentificationString = packageIdentificationTextField.getText();
            poundDouble = Double.parseDouble(poundsTextField.getText());
            ouncesDouble = Double.parseDouble(ouncesTextField.getText());
           
        }
       
        public void actionPerformed(ActionEvent evt)
        {
            getInput();
            displayOutput();
        }
       
        public void calculateShipping()
        {
            final double SHIPPING_RATE = .12;
            final double OUNCES_PER_POUND = 16;
            poundToOunceOuncesDouble = poundDouble * OUNCES_PER_POUND;
            shippingCostDouble = (poundToOunceOuncesDouble + ouncesDouble) * SHIPPING_RATE;
           
        }

        public void displayOutput()
        {
            outputTextArea.append('\n' + "Package ID  : " + packageIdentificationString + '\n' +
                    "Pounds  : " + poundDouble + '\n' +
                    "Ounces  : " + ouncesDouble + '\n'+
                    "Shipping Cost  : " + shippingCostDouble + '\n');
       
        }
    }
     
  2. jcsd
  3. Mar 28, 2008 #2

    Vid

    User Avatar

    You never called calculuateShipping. It's been at least a year and a half since I've done any JAVA programming, but I think you could put it either within the actionlisten or within displayOutput.
     
  4. Mar 28, 2008 #3
    how can i put it within displayoutput?
     
  5. Mar 28, 2008 #4
    its been a while since i did ne java, but if u just put:

    Code (Text):

            public void actionPerformed(ActionEvent evt)
        {
            getInput();
                    calculateShipping();
            displayOutput();
        }


     
    Just adding it before u call displayOutput should calculate the shipping, then u can display it... i believe that is how it would work.
     
  6. Mar 30, 2008 #5
    thanks.. works now.
     
    Last edited: Mar 30, 2008
  7. Mar 30, 2008 #6
    i am trying to display the output in a jlabel. I have created individual jlabels for the package ID, weight, and shipping cost and tried to display them with the mainPanel:

    Code (Text):

    import javax.swing.*;

    import java.awt.event.*;



    public class shippingCharge extends JFrame implements ActionListener
    {

        //objects and data types created here
        JPanel mainPanel = new JPanel();
        JTextField packageIdentificationTextField = new JTextField(6);
        JTextField poundsTextField = new JTextField(10);
        JTextField ouncesTextField = new JTextField(10);
        JButton displayButton = new JButton("Calculate  ");
       
       
        //Variables
        String packageIdentificationString;
        double poundDouble, ouncesDouble, poundToOunceOuncesDouble, shippingCostDouble;


        public static void main(String[] args)
        {
            shippingCharge shippingTotal = new shippingCharge();
        }
           
        public shippingCharge()
        {
            designFrame();
            setSize(500,500);
            setVisible(true);
           
        }
       
        public void designFrame()
        {
            mainPanel.add(new JLabel("Package ID"));
            mainPanel.add(packageIdentificationTextField);
            mainPanel.add(new JLabel("Pounds"));
            mainPanel.add(poundsTextField);
            mainPanel.add(new JLabel("Ounces"));
            mainPanel.add(ouncesTextField);
            mainPanel.add(displayButton);
           
           
            add(mainPanel);
            //add listener to the  object
            packageIdentificationTextField.addActionListener(this);
            displayButton.addActionListener(this);
           
        }
       
        public void getInput()
        {
            packageIdentificationString = packageIdentificationTextField.getText();
            poundDouble = Double.parseDouble(poundsTextField.getText());
            ouncesDouble = Double.parseDouble(ouncesTextField.getText());
           
        }
       
        public void calculateShipping()
        {
            final double SHIPPING_RATE = .12;
            final double OUNCES_PER_POUND = 16;
            poundToOunceOuncesDouble = poundDouble * OUNCES_PER_POUND;
            shippingCostDouble = (poundToOunceOuncesDouble + ouncesDouble) * SHIPPING_RATE;
           
        }

        public void actionPerformed(ActionEvent evt)
        {
            getInput();
            calculateShipping();
            displayOutput();
           
           
        }
       
        public void displayOutput()
        {
           
            mainPanel.add(new JLabel("Package ID:" + packageIdentificationString));
            mainPanel.add(new JLabel("Weight:" + poundDouble + "lbs" + ouncesDouble + "oz."));
            mainPanel.add(new JLabel("Shipping Cost:" + shippingCostDouble));
       
               
        }
    }
    here is the revised code, but the output labels aren't showing up. what gives?
     
    Last edited: Mar 30, 2008
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Java project calculation
Loading...