Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

11.2: Prices Copy the following program into a file named Price.java : 1 2 3 4 5 6 7 8 9 10 11 12 13

11.2: Prices

  • Copy the following program into a file named Price.java:
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
    public static void main(String[] args) { String name; double price = 0; Scanner input = new Scanner(System.in); System.out.print("Enter the product name: "); name = input.nextLine(); System.out.print("Price of the " + name + ": "); price = input.nextDouble(); // Insert new statements here System.out.println("Total price: $" + price); } 
  • Alter the block comments to include your name and section information to the top of the program.
  • Compile and run the starter program to make sure you entered it correctly.

    When you run the program, the output should look like this:

    Enter the product name: iPod Nano Price of the iPod Nano: 149.50 Total price: $149.5 

    Note the format of the numbers output for the total price. We will address this formatting issue later in the exercise.

  • Run the program again for a product with a very high cost, like a Boeing 777:
    Enter the product name: Boeing 777 Price of the Boeing 777: 212345678 Total price: $2.12345678E8 

    Note the format of the numbers output for the total price. This format is called exponential notation. You may have encountered it in some of your math classes.

  • Let us correct the formatting of the total price. Alter the System.out statement that prints the total price:
    System.out.printf("Total price: $%.2f ", price); 
    Compile and run your program again and verify the output looks like:
    Enter the product name: Boeing 777 Price of the Boeing 777: 212345678 Total price: $212345678.00 
  • Let us add a final variable that we will use later in our program. Enter the following code after the printf statement and before the statement that prints the total price:
    final int percent = 100; 

    We are declaring this variable rather than using a magic number.

  • Now we will add sales tax to the price of the product. Enter the following code after the percent variable and before the statement that prints the total price:
    double taxRate = 0; System.out.print("Enter sales tax rate (%): "); taxRate = input.nextDouble(); double tax = price * taxRate / percent; price += tax; 

    Notice the last statement: price += tax;. This is an alternate way to code the statement: price = price + tax;.

  • Compile and run your modified program and verify the output looks like:
    Enter the product name: iPod nano Price of the iPod nano: 89.50 Enter sales tax rate (%): 9.5 Total price: $98.00 
  • Now we will find the whole dollars and cents of the amount to demonstrate casting. Enter the following code after the statement that prints the total price and before the closing curly brace of main:
    int dollars = (int) price; System.out.println("In whole dollars: $" + dollars); 

    Notice the (int) in the first statement. This is known as a type cast or just cast.

  • Compile and run your modified program and verify the output looks like. Then, submit Prices.java to Canvas.
    Enter the product name: iPod nano Price of the iPod nano: 89.50 Enter sales tax rate (%): 9.5 Total price: $98.00 In whole dollars: $98 

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

Recommended Textbook for

Understanding Oracle APEX 5 Application Development

Authors: Edward Sciore

2nd Edition

1484209893, 9781484209899

More Books

Students also viewed these Databases questions