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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education