Jump to content

Please help with Java HomeWork

I am having alot of problems with my java homework program and though maybe someone here might be able to help me out.

The assignment is:

CPS 121 – Program 5, Postage

20 points

Due: Monday, February 18

Task: Write a program that will calculate the length-plus-girth measurement that is used in post office regulations. Assume that a customer wants to mail several packages of the same size. Your program will verify that the packages qualify for first-class mail, and it will calculate the total postage due.

The length of a three-dimensional rectangular object is its longest dimension. The girth of the object is the distance around it, measured perpendicular to the length. In postal regulations, the length-plus-girth of a package cannot exceed 100 inches if the package is to be sent by first class mail.

Input: Using dialog boxes, input these items:

· 3 dimensions (in inches) of one package

· the weight (in ounces) of that package

· the number of that kind of package

Note: the user will provide 3 dimensions for the package, but you need to determine which of the three numbers is the longest. In other words, the user will give you 3 package measurements, but may enter them in any order.

Processing: If a package qualifies for first-class mail, assume that the postage due will be calculated as $0.42 per ounce. If a package does not qualify for first-class, it will be mailed by slower delivery bulk rate, which will be calculated as $0.23 per ounce.

Output: In one dialog box, display the length-plus-girth measurement of a package, the weight of the package, the number of packages, and the total postage due. Format money amounts neatly to 2 decimal places.

Here is my code:

//CPS 121, Assignment 5

//Postage Calculator

import javax.swing.JOptionPane;

public class PostageCalc

{

public static void main (String[] args)

{

final double firstclass, normal;

double dimension1, dimension2, dimension3, weight, //Input data

length, girth, size, postage;

String dimension1Str, dimension2Str, dimension3Str, weightStr;

firstclass = .42;

normal = .23;

dimension1Str = JOptionPane.showInputDialog("Enter the first dimension: " );

dimension1 = Integer.parseInt(dimension1Str);

dimension2Str = JOptionPane.showInputDialog("Enter the second dimension: " );

dimension2 = Integer.parseInt(dimension2Str);

dimension3Str = JOptionPane.showInputDialog("Enter the third dimension: " );

dimension3 = Integer.parseInt(dimension3Str);

weightStr = JOptionPane.showInputDialog("Enter the weight: ");

weight = Integer.parseInt(weightStr);

if ((dimension1 > dimension2) && (dimension1 > dimension3))

length = dimension1;

girth = (2 * dimension2) + (2 * dimension3);

if ((dimension2 > dimension1) && (dimension2 > dimension3))

length = dimension2;

girth = (2 * dimension1) + (2 * dimension3);

if ((dimension3 > dimension1) && (dimension3 > dimension2))

length = dimension3;

girth = (2 * dimension1) + (2 * dimension2);

size = length + girth;

if (size < 100 )

postage = size * firstclass;

JOptionPane.showMessageDialog(null, "Your package will be sent first class. ");

JOptionPane.showMessageDialog(null, "Your shipping cost is: " + postage);

if (size > 100)

postage = size * normal;

JOptionPane.showMessageDialog(null, "Your package will be sent through normal shipping." );

JOptionPane.showMessageDialog(null, "Your shipping cost is: " + postage);

System.exit(0);

}

}

Link to comment
https://linustechtips.com/topic/5012-please-help-with-java-homework/
Share on other sites

Link to post
Share on other sites

Yea I think we're missing some brackets here, you can put your code in-between the CODE tags. Also you need to be more specific about what doesn't work, and what is your question.

But for starter, I can tell you a good way to find out the length would be to use arrays :

	int[ ] dimensions = { d1, d2, d3 } ;	Arrays.sort(dimensions);	// So length will be equal to :	// dimensions[dimensions.length-1]);	// because it's the last value (sorted in ascending order)
Link to post
Share on other sites

Yea I think we're missing some brackets here, you can put your code in-between the CODE tags. Also you need to be more specific about what doesn't work, and what is your question.

But for starter, I can tell you a good way to find out the length would be to use arrays :


int[ ] dimensions = { d1, d2, d3 } ;

Arrays.sort(dimensions);

// So length will be equal to :

// dimensions[dimensions.length-1]);

// because it's the last value (sorted in ascending order)

Thanks for the tips. I will try them.
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×