Java Project Calculator for Shipping Costs

In summary, the conversation discusses creating a GUI project to display a package ID and calculate its shipping cost. The shipping cost is calculated per ounce and the code includes a method to convert pounds to ounces. However, there is an issue with the calculation always showing up as 0. The conversation also explores the possibility of the formula not being read because it is within a method. The summary also includes a suggestion to call the calculateShipping method before displaying the output. Additionally, the conversation mentions attempting to display the output in a JLabel, but the labels are not appearing.
  • #1
clook
35
0
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:
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');
	
	}
}
 
Physics news on Phys.org
  • #2
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.
 
  • #3
how can i put it within displayoutput?
 
  • #4
its been a while since i did ne java, but if u just put:

Code:
        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.
 
  • #5
thanks.. works now.
 
Last edited:
  • #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:
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:

1. What is a Java project calculation?

A Java project calculation is a process of using the Java programming language to create a computer program or application that performs mathematical calculations. This can involve writing code to perform complex calculations, creating algorithms, or using built-in Java libraries for mathematical operations.

2. What are the benefits of using Java for project calculations?

Java is a powerful programming language with many features that make it well-suited for performing project calculations. Some of the benefits include its ability to handle complex mathematical operations, its built-in libraries for scientific and statistical calculations, its portability across different platforms, and its support for object-oriented programming.

3. How do I get started with a Java project calculation?

To get started with a Java project calculation, you will need to have a basic understanding of the Java programming language. This can be achieved through online tutorials, books, or courses. Once you have a good grasp of the language, you can begin by defining your project requirements and then writing the code to perform the necessary calculations.

4. Can Java be used for both simple and complex project calculations?

Yes, Java can be used for both simple and complex project calculations. It has a wide range of built-in functions and libraries that can handle simple calculations like addition, subtraction, and multiplication, as well as more complex calculations like trigonometry, logarithms, and statistical analysis.

5. Are there any challenges to consider when using Java for project calculations?

While Java is a powerful language for project calculations, there are a few challenges to keep in mind. One is the learning curve, as it may take some time to become proficient in the language. Additionally, Java can be memory-intensive, so efficient coding practices are important. Finally, debugging can be more challenging in Java compared to other languages, so it's important to write clean and organized code.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
Back
Top