Question: // Step 1: import class Scanner import java.util.Scanner; /** * Ask the user for product name, its original price, * and the discount rate, then
// Step 1: import class Scanner import java.util.Scanner; /** * Ask the user for product name, its original price, * and the discount rate, then calculate and display * the discount price for the product. * * Step 2: Enter your name for @author and the password for @version * @author * @version */ public class DiscountPrice { public static void main(String[] args) { // Step 3: Create a Scanner object to get // input from the keyboard
// Step 4: Display an input prompt // "Enter the product name: " // Step 5: Read in one line of input and store it // in a variable // Step 6: Display an input prompt // "Enter the original price: " // Step 7: If the next input token is not a double number // Read in the input // Display a message on one line // Sample message assuming the input is "Apple" // "Invalid price: Apple." // Display a message on one line // "Program terminated." // Terminate the program
// Step 8: Read in the price and store it in a variable
// Step 9: Display an input prompt "Enter the discount rate: "
// Step 10: If the next input token is not a double number // Read in the input // Display a message on one line // Sample message assuming the input is "rate" // "Invalid discount rate: rate." // Display a message on one line // "Program terminated." // Terminate the program
// Step 11: Read in the discount rate and store it in a variable
// Step 12: Calculate the discount price // discount price = price * (1 - discount rate)
// Step 13: Display the product name within double quotes // and the discount price with a dollar sign and // two decimal digits // Sample output: // The discount price for "iPad Pro" is $343.99.
} }
Output 1
Enter the product name: iPhone XS Max Enter the original price: 249.99 Enter the discount rate: 0.10 The discount price for iPhone XS Max is $224.99.
Output 2
Enter the product name: iPad Enter the original price: Apple Invalid price!
Output 3
Enter the product name: iPad Pro Enter the original price: 429.99 Enter the discount rate: rate Invalid discount rate!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
