Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use comments liberally to document exactly what you are doing in the program, es

ID: 3831039 • Letter: U

Question

Use comments liberally to document exactly what you are doing in the program, especially explain about the rounding in #11.

Use descriptive variable names in camel-case with the first letter in lowercase.

Ask the user for the total cost of the meal.

Read in a double number and assign it to a variable of type double.

Ask the user for the percent tip.

Read in an integer number and assign it to a variable of type integer.

Divide that percent tip by a double 100 to get the tip rate in a decimal form instead of percent and assign that to a variable of type double.

Multiply the variable that represents the cost of the meal by the variable that represents the tip rate in decimal form. This will be the tip amount in dollars and cents. Assign this result to a variable of type double.

Extra Credit: Round that last result to the nearest cent using the Math.round method described in Chapter 4 amount in the original variable for the tip amount in dollars and cents. Note that if you use the printf statement in #12 correctly, it will be displayed as rounded to the nearest cent, but the variable itself will NOT have been rounded unless you use Math.round or some other rounding technique. Be sure to make a clear comment for this step.

Tell the user how much the tip should be in dollars and cents. Make sure that you display exactly 2 digits after the decimal point (this will cause the display amount to round, but the variable will NOT be changed by this).

Add that tip amount to the total cost of the meal and store that total in the same variable as the total cost of the meal. Use the addition assignment operator += to do this.

Tell the user what the new total is. Make sure that you display exactly 2 digits after the decimal point (this will cause the display amount to round, but the variable will NOT be changed by this).

Sample Run:

Enter total meal cost (example 43.37): 54.27

Enter tip in percent (example 15 means 15%): 17

Tip amount: $9.23

Meal total : $63.50

Explanation / Answer

Language in the question is not given, so implementing this in java. Below is your code.

BillDetails.java


import java.util.Scanner;

public class BillDetails {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in); //Initializing scanner to get user input
       System.out.print("Enter total meal cost (example 43.37): ");
       double totalMealCost = sc.nextDouble(); // Getting total meal cost from the user
       System.out.print("Enter tip in percent (example 15 means 15%): ");
       int percentTip = sc.nextInt(); // Getting tip percent
      
       sc.close();// closing the scanner.
       double tipRate = percentTip/100.0;//Converting it to tip rate and saving it to double
       double tipAmount = totalMealCost*tipRate; // Calculating total meal cost in Dollors.
       double tipAmountInDollors = Math.round(tipAmount*100.0)/100.0;
       // Converting to nearest decimal upto two decimal places in $ by using Math.round buy first multiplying with 100
       // and rounding and then divide by 100.
       System.out.println("Tip amount: $"+(tipAmountInDollors));
      
       totalMealCost+=tipAmountInDollors; // Adding totalMealCost with TotalTip
      
       System.out.println("Meal total : $"+Math.round(totalMealCost*100.0)/100.0); //Rounding the digits and displaying.
      
   }
}

Sample Run: -

Enter total meal cost (example 43.37): 54.27
Enter tip in percent (example 15 means 15%): 17
Tip amount: $9.23
Meal total : $63.5