124k views
2 votes
Write a java program to calculate the amount of tax and amount of tip on a restaurant bill. Ask the user for the cost of the meal. The tax will be 9.75 percent of the cost of the meal. The tip amount will be 15 percent of total after tax. Output the cost of the meal, tax amount, tip amount, and the total bill.

User Borealis
by
7.4k points

1 Answer

0 votes

Final answer:

A Java program can be written to calculate the tax and tip on a restaurant bill by asking the user for the cost of the meal, computing the tax at 9.75%, calculating the tip at 15% of the total after tax, and then displaying all the amounts including the final total.

Step-by-step explanation:

To calculate the amount of tax and tip on a restaurant bill, you can write a simple Java program. Let's assume you request the cost of the meal as user input. The tax is 9.75% of this cost, and after adding the tax, the tip is 15% of the total. The program will output the cost of the meal, the tax amount, the tip amount, and the final total bill.

Here's an example:

import java.util.Scanner;
public class RestaurantBillCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the cost of your meal: ");
double mealCost = scanner.nextDouble();
double taxAmount = mealCost * 0.0975;
double totalWithTax = mealCost + taxAmount;
double tipAmount = totalWithTax * 0.15;
double totalBill = totalWithTax + tipAmount;
System.out.printf("Meal Cost: $%.2f\\Tax Amount: $%.2f\\Tip Amount: $%.2f\\Total Bill: $%.2f", mealCost, taxAmount, tipAmount, totalBill);
scanner.close();
}
}

This example asks the user for the meal cost, calculates the tax, and then adds the tip based on the total after tax. It uses the Scanner class to get the user input and printf to format the output with two decimal places.

User ShadowMare
by
7.3k points