Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Overview This problem provides additional practice with conditional execution and static methods, and it also gives you a chance to write a simple program with

Overview This problem provides additional practice with conditional execution and static methods, and it also gives you a chance to write a simple program with repeated user interactions.

The program will allow the user to compute the shipping charge for a collection of one or items. The shipping charge for each item will be based on the following characteristics:

the type of shipping (one-day, two-day, or standard)

the type of the item being shipped (book, clothing, electronics, or toy)

the weight of the item, rounded to the nearest pound

Rules Here are the rules for determining the charge for a single item:

one-day shipping

toys: $4.99, plus $1.99 per pound

electronics: $5.99, plus $1.99 per pound

books and clothing:

if the item weighs 2 pounds or more: $3.99, plus $0.60 per pound

otherwise: $4.99 (with no per-weight charge)

two-day shipping

toys: $2.99, plus $0.99 per pound

electronics: $3.99, plus $0.89 per pound

books and clothing:

if the item weighs 2 pounds or more: $1.99, plus $0.75 per pound

otherwise: $2.99 (with no per-weight charge)

standard shipping

toys and electronics: $1.99, plus $0.80 per pound

books and clothing:

if the item weighs 2 pounds or more: $0.99, plus $0.70 per pound

otherwise: $1.99 (with no per-weight charge)

Example Lets say that the user wants to use one-day shipping to ship three items: a 10-pound toy, a 2-pound book, and a piece of clothing that weighs less than 2 pounds. Using the rules for one-day shipping found above:

The 10-pound toy would have a shipping cost of $24.89 ($4.99, plus $1.99*10).

The 2-pound book would have a shipping cost of $5.19 ($3.99, plus $0.60*2).

The piece of clothing would have a shipping cost of $4.99 (with no per-weight charge because it weighs less than 2 pounds).

Thus, the total cost would be $24.89 + $5.19 + $4.99 = $35.07.

(Note: For the sake of greater precision, you will actually perform most of the computations in cents e.g., 499 instead of 4.99, and 99 instead of 0.99. Then, at the end of the program, you will convert the total cost to dollars. See below for more detail.)

Sample runs Your final program should match the behavior of the sample runs available here. Note that these sample runs are not comprehensive, and you should make sure to test other cases as well.

Getting started

Download the following file: TerrierShipping.java Make sure to put it in your ps2 folder.

As needed, open your ps2 folder using the File->Open Folder or File->Open menu option in VS Code. The name of the folder should appear in the Explorer pane on the left-hand side of the VS Code window, along with the name of the TerrierShipping.java file that you downloaded in step 1.

Click on the name TerrierShipping.java, which will open an editor window for that file. You will see some starter code that weve provided. In particular, weve given you the beginnings of the main method, along with static methods for getting the type of shipping and the type of an item from the user.

Your tasks

Start by reading over the starter code that weve given you. Make sure that you understand it.

Complete the assignment statement in the main method for the variable weight so that it assigns an integer value obtained from the user. At the moment, the statement looks like this:

int weight = 0; 

You should replace the 0 with the appropriate method call.

Important: You must use the Scanner object created at the start of the main method, and you may not create an additional Scanner object.

Add the code needed to compute the charge for a single item, based on the inputs provided by the user (which you may assume are all valid) and the rules found above. In particular, you must:

Add at least three additional static helper methods, each of which handles the computation of one type of shipping charge. These new methods should go between the getItemType and main methods that we have given you.

There is more than one possible way to divide up the work among these helper methods. For example, you could have:

one method for each type of shipping (one-day, two-day, and standard)

or

one method for each type of item (book, clothing, electronics, and toy).

See below for some important requirements that your methods must satisfy.

Add code to main that uses conditional execution to call the appropriate helper method and assign its return value to the variable itemCharge.

Important requirements Each of your static helper methods must:

take one or more parameters that provide the necessary information about a single item

compute and return the appropriate charge for that item in cents. For example, instead of using a cost of 4.99 dollars, your method should use 499 cents. Keeping the charges in cents until the end of the program will prevent the loss of precision that can result when floating-point numbers (i.e., doubles) are added together.

The names of the methods are up to you, but you should pick names that describe what the methods are supposed to do.

Complete the line of code provided near the end of main so that it will convert the total charge from cents to dollars. At the moment, the statement looks like this:

double totalDollars = 0.0; 

You should replace the 0.0 with the appropriate expression. Make sure that your computation is as precise as possible.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions