The Fast Freight Shipping Company charges the following rates: Weight of Package (in Kilograms) 2 kg or less Over 2 kg but not more than 6 kg Over 6 kg but not more than 10 kg Over 10 kg but not more than 20 kg Rate per 500 Miles Shipped $1.10 $2.20 $3.70 $4.80 Write a program that asks for the weight of the package and the distance it is to be shipped, and then displays the charges. Input Validation: Do not accept values of 0 or less for the weight of the package. Do not accept weights of more than 20 kg (this is the maximum weight the company will ship). Do not accept distances of less than 10 miles or more than 3,000 miles. These are the company's minimum and maximum shipping distances.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter4: Control Structures I (selection)
Section: Chapter Questions
Problem 20PE: The cost of renting a room at a hotel is, say $100.00 per night. For special occasions, such as a...
icon
Related questions
Question

I have to do this exercise in JAVA, I already have most of the code but I don't know how to calculate the distance, for every 500 miles an amount is charged, so for example if the user chooses 520 miles it would be rounded to 1000 miles and the price would be double. So for a 6kg package the price is $2.20 but the package will be ship 520 miles so the price would be $4.40.

import java.util.Scanner;
class Main {
  public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
int weight, miles;
    double r;
    
             /* Input - weight */    
    System.out.print("What is the weight of the package: ");
    weight = input.nextInt();
    
            /* Input validation for the weight */
while (weight < 0 || weight > 20){
  System.out.print("invalid imput, plis input the correct weight: ");
    weight = input.nextInt();
}
            /* Input - distance */    
    System.out.print("What is the distance: ");
    miles = input.nextInt();

            /* Input validation for the distance */ 
while (miles < 10 || miles > 3000){
    System.out.print("invalid imput, plis input the correct distance: ");
   miles = input.nextInt(); 
}
      if (weight <= 2){
        r = 1.10;
        System.out.print(" You have to pay: $" + r + " for you package");
      }
      else if (weight > 2 && weight <= 6 ){
        r = 2.20;  
        System.out.print(" You have to pay: $" + r + " for you package");
      }
      else if (weight > 6 && weight <= 10 ){
        r = 3.70;
        System.out.print(" You have to pay: $" + r + " for you package");
      }
      else if (weight > 10 && weight <= 20 ){
        r = 4.80;
        System.out.print(" You have to pay: $" + r + " for you package");
      }
    }
  }  

The Fast Freight Shipping Company charges the following rates:
Weight of Package (in Kilograms)
2 kg or less
Over 2 kg but not more than 6 kg
Over 6 kg but not more than 10 kg
Over 10 kg but not more than 20 kg
Rate per 500 Miles Shipped
$1.10
$2.20
$3.70
$4.80
Write a program that asks for the weight of the package and the distance it is to be
shipped, and then displays the charges.
Input Validation: Do not accept values of 0 or less for the weight of the package. Do
not accept weights of more than 20 kg (this is the maximum weight the company will
ship). Do not accept distances of less than 10 miles or more than 3,000 miles. These
are the company's minimum and maximum shipping distances.
Transcribed Image Text:The Fast Freight Shipping Company charges the following rates: Weight of Package (in Kilograms) 2 kg or less Over 2 kg but not more than 6 kg Over 6 kg but not more than 10 kg Over 10 kg but not more than 20 kg Rate per 500 Miles Shipped $1.10 $2.20 $3.70 $4.80 Write a program that asks for the weight of the package and the distance it is to be shipped, and then displays the charges. Input Validation: Do not accept values of 0 or less for the weight of the package. Do not accept weights of more than 20 kg (this is the maximum weight the company will ship). Do not accept distances of less than 10 miles or more than 3,000 miles. These are the company's minimum and maximum shipping distances.
19 2012345678290123455劲8羽♀钍2钌455钉8p丽弘貂貉科5%刃890碰成
import java.util.Scanner;
21 class Main {
22 - public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int weight, miles;
double r;
45
32 while (weight < 0 || weight > 20){
49
9
53 -
57 ~
| || /* Input weight */
System.out.print("What is the weight of the package: ");
weight = input.nextInt();
41 ~ while (miles < 10 || miles > 3000){
}
/* Input validation for the weight */
System.out.print("invalid imput, plis input the correct weight: ");
weight = input.nextInt();
/* Input distance */
System.out.print("What is the distance: ");
miles = input.nextInt();
}
/* Input validation for the distance */
System.out.print("invalid imput, plis input the correct distance: ");
miles = input.nextInt ();
}
if (weight <= 2){
r = 1.10;
System.out.print(" You have to pay: $" + r + for you package");
else if (weight > 2 && weight <= 6 ){
r = 2.20;
System.out.print(" You have to pay:
+r+ " for you package");
}
else if (weight > 6 && weight <= 10 ){
r = 3.70;
System.out.print(" You have to pay: $" + r +
}
else if (weight > 10 && weight <= 20 ) {
r = 4.80;
System.out.print(" You have to pay: $" + r +
for you package");
for you package");
Transcribed Image Text:19 2012345678290123455劲8羽♀钍2钌455钉8p丽弘貂貉科5%刃890碰成 import java.util.Scanner; 21 class Main { 22 - public static void main(String[] args) { Scanner input = new Scanner(System.in); int weight, miles; double r; 45 32 while (weight < 0 || weight > 20){ 49 9 53 - 57 ~ | || /* Input weight */ System.out.print("What is the weight of the package: "); weight = input.nextInt(); 41 ~ while (miles < 10 || miles > 3000){ } /* Input validation for the weight */ System.out.print("invalid imput, plis input the correct weight: "); weight = input.nextInt(); /* Input distance */ System.out.print("What is the distance: "); miles = input.nextInt(); } /* Input validation for the distance */ System.out.print("invalid imput, plis input the correct distance: "); miles = input.nextInt (); } if (weight <= 2){ r = 1.10; System.out.print(" You have to pay: $" + r + for you package"); else if (weight > 2 && weight <= 6 ){ r = 2.20; System.out.print(" You have to pay: +r+ " for you package"); } else if (weight > 6 && weight <= 10 ){ r = 3.70; System.out.print(" You have to pay: $" + r + } else if (weight > 10 && weight <= 20 ) { r = 4.80; System.out.print(" You have to pay: $" + r + for you package"); for you package");
Expert Solution
Step 1

Here is the java program for the above requirements:

 

import java.util.Scanner;

public class Main {
   public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      double weight, distance, rate;

      // Ask for weight of the package
      System.out.print("Enter the weight of the package (in kilograms): ");
      weight = input.nextDouble();

      // Input validation for weight
      while (weight <= 0 || weight > 20) {
         System.out.print("Invalid weight. Enter a weight between 0 and 20 kilograms: ");
         weight = input.nextDouble();
      }

      // Ask for distance
      System.out.print("Enter the distance to be shipped (in miles): ");
      distance = input.nextDouble();

      // Input validation for distance
      while (distance < 10 || distance > 3000) {
         System.out.print("Invalid distance. Enter a distance between 10 and 3000 miles: ");
         distance = input.nextDouble();
      }

      // Round up distance to nearest 500
      int dist =(int) Math.ceil(distance/500);

      // Calculate rate based on weight of package
      if (weight <= 2) {
         rate = 1.10;
      } else if (weight <= 6) {
         rate = 2.20;
      } else if (weight <= 10) {
         rate = 3.70;
      } else {
         rate = 4.80;
      }

      // Display charges
      System.out.println("The charges for shipping a package weighing " + weight + " kilograms for " + distance + " miles is $" + dist * rate);
   }
}

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Mathematical functions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning