Question
Write a Java application program that will add up the cost of three items, then print the final total with sales tax. You should begin
Write a Java application program that will add up the cost of three items, then print the final total with sales tax.
You should begin by prompting the user to enter three separate prices for three items that are being purchased. For each item you should ask for (in this order) the quantity of the product and the price of the product.
The program should compute, and print to the screen:
the subtotal (the total amount due before tax)
sales tax (the amount of sales tax that will be added, assume 7% tax rate)
total due (subtotal + sales tax)
The following is an example of what your MIGHT see on the screen when your program runs. The exact output depends on what values that the user types in while the program runs. The user's values are shown below in italics:
Enter the quantity of the first product: 3 Enter the price of the first product: $5.25 Enter the quantity of the second product: 2 Enter the price of the second product: $1.83 Enter the quantity of the third product: 7 Enter the price of the third product: $0.89 Subtotal: $25.64 Sales Tax: $1.79 Total Due: $27.43
Notes:
When asking the user to enter a price, make your prompt print the dollar sign to the screen, then put the cursor right behind the dollar sign. By doing this, the user will be less likely to try to enter a dollar sign, which will actually cause your program to crash. Remember, to leave the cursor right after the printed dollar sign, use System.out.print instead of System.out.println. The later version causes a line break after the dollar sign so the cursor will appear on the next line instead of behind the dollar sign.
When computing the sales tax amount, you are most likely to end up with a value that is more than two decimal places (in my example above, the sales tax computed was actually $1.7948). For this reason you will need to round this answer to the nearest penny. To perform the rounding, you should do the following:
multiply the amount by 100 ------------ Example: --- 1.7948 would become 179.48 ------------ Example: --- 1.7968 would become 179.68
add 0.5 to the result ------------ Example: --- 179.48 + 0.5 = 179.98 ------------ Example: --- 179.68 + 0.5 = 180.18
type cast the answer as an integer ------------ Example: --- 179.98 type cast as an integer gives 179 ------------ Example: --- 180.18 type cast as an integer gives 180
divide the resulting integer by 100 to put the two decimal places back where they should be ------------ Example: --- 179 becomes 1.79 ------------ Example: --- 180 becomes 1.80
You must ensure that all of your numbers display with exactly two decimal places (for example the number 1.80 will normally print as 1.8) To make your numbers print with two decimal places you must use the printf command rather than the print or println commands. Here is how to use the printf command: System.out.printf("Subtotal: $%.2f ", subtotalVariable); This command will cause the words "Subtotal: $" to be printed to the screen. At the point where the % is located in the string, the contents of the subtotalVariable will be inserted and forced to display with two decimal places.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started