The problem
Write a program that reads the subtotal and the gratuity rate, then computes the gratuity and total. For example, if the user enters 10 for subtotal and 15% for gratuity rate, the program displays $1.50 as gratuity and $11.50 as total. Here is a sample run:
Enter the subtotal and a gratuity rate: 10 15
The gratuity is $1.5 and total is $11.5
Breaking it down
First, the program will asking the user to enter subtotal and gratuity while storing the values in two local variables gratuityTotal
and gratuityRate
. Next a method gratuity
will calculate the gratuity for the bill and total
will add the subtotal and gratuity total together. Finally a message will be outputted to the users.
public static void main(String[] Strings) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter the subtotal and gratuity rate: ");
double subtotal = input.nextDouble();
double gratuityRate = input.nextDouble();
input.close();
double gratuityTotal = gratuity(subtotal, gratuityRate);
double total = total(subtotal, gratuityTotal);
System.out.print("The gratuity is $" + gratuityTotal
+ " and total is $" + total);
}
private static double total(double subtotal, double gratuityTotal) {
double total = subtotal + gratuityTotal;
return total;
}
private static double gratuity(double subtotal, double gratuityRate) {
double gratuityTotal = subtotal * gratuityRate * .01;
return gratuityTotal;
}
Output
Please enter the subtotal and gratuity rate: 100 10
The gratuity is $10.0 and total is $110.0