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

Need help with this. I have tried and follow the instructions below but keep get

ID: 3880474 • Letter: N

Question

Need help with this. I have tried and follow the instructions below but keep getting errors. Need one to compare with mine to see what I am doing wrong.

You need to write a program in Java that will tell the user how much to tip a waiter or waitress at a restaurant. You will just make it run on your computer console instead of on an Android device.

Objectives:

•• To create both types of comments

•• To learn standard documentation techniques

•• To learn about the Math class, particularly round

•• To handle integer and double literals and variables

•• To input values from the console using the Scanner class

•• To print formatted output using the printf statement

•• To learn standard conventions with regard to variable names

•• To gain more experience with integer and floating point arithmetic

Instructions:

1. Call your class Program2, so your filename will be Program2.java. It is essential for grading purposes that everyone have the same class name.

2. Create several lines of comments of identification and description at the top of the file (it doesn’t have to look exactly like this, in particular, if your editor wants to put * in front of every line, that’s fine):

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

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

5. Ask the user for the total cost of the meal.

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

7. Ask the user for the percent tip.

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

9. 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.

10. 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.

11. 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.

12. 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). You will need to use the printf statement with format specifier “Tip amount: $%.2f ” , or you could use %n instead of , but all other languages use , which is the end of line character. The backslash indicates an escape character, which, when combined with another character, creates one special character.

13. 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.

14. 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). Again, use printf with format specifier “Meal total : $%.2f” . Be sure to put two blanks after the colon so that your $ signs line up.

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

Notes:

If your tip amount ends up as zero, reread instruction #8 above carefully because it is likely that you did integer arithmetic instead of floating-point arithmetic.

To format the dollar and cents, read Section 4.6 in your Liang book about the printf statement. That will tell you how to make the output have exactly two digits after the decimal point.

Explanation / Answer

package org.students;

import java.util.Scanner;

public class CalMealTot {

public static void main(String[] args) {

//Declaring variables
double totCostOfMeal, mealTotal = 0.0, tipAmt;
int tipPercent;


/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);

//Getting the input entered by the user
System.out.print("Enter total meal cost :");
totCostOfMeal = sc.nextDouble();

System.out.print("Enter tip in percent :");
tipPercent = sc.nextInt();

//Calculating the tip amount
tipAmt = (tipPercent / (double)(100)) * totCostOfMeal;

//calculating the meal cost including the tip
mealTotal = tipAmt + totCostOfMeal;

//Displaying the tip amount
System.out.printf("Tip amount: $%.2f ", tipAmt);

//Displaying the total meal cost including the tip amount
System.out.printf("Meal total : $%.2f ", mealTotal);

}

}

___________________

Output:

Enter total meal cost :54.27
Enter tip in percent :17
Tip amount: $9.23
Meal total : $63.50

___________________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote