Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

completely lost  RSS feed

 
jamie keene
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so I have this assignment, but I am completely lost,
how do i calculate without a prorate,
for example it costs $1.10 to ship an item that is under 2 kg for every 500 miles it is shipped, but if it is only shipped 600 miles they are still charged 2.20.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jamie keene wrote:...for example it costs $1.10 to ship an item that is under 2 kg for every 500 miles it is shipped, but if it is only shipped 600 miles they are still charged 2.20.

Welcome to the Ranch!

In other words, the cost is $1.10 for each 500 miles or portion thereof. Sounds like you want to divide the total distance by 500 and always round up, right?

Have you written any code for this yet? How far have you gotten?
 
jamie keene
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



but it doesn't give the right output. if i run a sample case of weight 9.5 and miles 750 i get 3.5 instead of 7
 
jamie keene
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and this is the original assignment
Lab 9 is based on Programming Challenge #6: ShippingCharges class.
The Fast Freight Shipping Company charges the following rates:
Shipping Rates
Weight of Package (in kilograms) Rate per 500 Miles Shipped
2 Kg or less $1.10
Over 2 Kg but not more than 6 Kg $2.30
Over 6 Kg but not more than 10 Kg $3.50
Over 10 Kg $4.70
The shipping charges per 500 miles are not prorated. For example if a 2 Kg package is shipped 550 miles, the charges would be $2.20
Test your understanding by completing the test case table for various weights and miles shipped
Test Cases
Weight (in Kg) Miles Shipped Total Cost
1.6 Kg 250 miles
2.3 Kg 750 miles
7.4 Kg 1000 miles
9.8 Kg 1250 miles
12.5 Kg 1550 miles
Create the Java ShippingCharges class
1. Design and implement the Java class ShippingCharges.java that stores the weight of a package and the miles shipped and has a method that returns the shipping charges
◦ You need two private instance variables:
1. one to store the weight (double)
2. and the other to store the miles shipped (int)
◦ The constructor method should accept two parameters:
1. package weight (double)
2. miles to be shipped (int)
◦ The method getShippingCharges() should calculate and return the total shipping charges (as a double) based on the values saved in the object's instance variables
Change Pair Partner Roles
Change pair partner roles. The driver should become the navigator, the navigator should become the driver.
Test your ShippingCharges class with a main method
1. Create a new Java class, ShippingChargesDemo.java, and copy and paste the main method below that reads in a weight and miles shipped from the user and then displays the total charges.
Note: this code is provided below. Test and ensure that it works correctly with your ShippingCharges class.
import java.util.Scanner;
import java.text.DecimalFormat;

public class ShippingChargesDemo
{
public static void main(String [] args)
{
// local variables
double weight;
int miles;

// Create a Scanner object
Scanner keyboard = new Scanner(System.in);

// Create a DecimalFormat object
DecimalFormat money = new DecimalFormat("0.00");

// Get the package weight and miles shipped
System.out.print("Enter the package weight: ");
weight = keyboard.nextDouble();
System.out.print("Enter the miles shipped: ");
miles = keyboard.nextInt();

// Create a ShippingCharges object
ShippingCharges myPackage = new ShippingCharges(weight, miles);

// Display the total charges
System.out.println("Total Charges will be $" +
money.format(myPackage.getShippingCharges()));
}
}
2. Test your program by running it and verifying that the answers your program produces match the answers you hand-calculated as the test cases.
Sample session
Enter the package weight: 9.5
Enter the miles shipped: 750
Total Charges will be $7.00
Change Pair Partner Roles
Change pair partner roles. The driver should become the navigator, the navigator should become the driver.
Use your class with a windows style program
1. Create a third new Java class, WindowsDemo.java, that duplicates the functionality of the ShippingChargesDemo program using the JOptionPane.




 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a hint :
 
jamie keene
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i don't know how to make it so that it rounds up with the int numbers.
 
Rob Spoor
Sheriff
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.lang.Math has some nice methods for rounding. Note that the int division will still round down. To solve this, cast one of the two to a double: (double)750 / 500. This will also work with variables. Constant values can also be turned into a double by appending .0 or d: 750.0 and 750d are both double representations of 750.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(distance + 499) / 500 ???
 
Steven Eck
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Complete code solution removed]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!