In: Computer Science
Tip Calculator Instructions
For this project you are to create a tip calculator that will take a decimal/or non decimal amount and calculate the different tip values of that amount:
10% 15% 20%
And display them appropriately. The values will be set to two decimal places. If you have an empty value, then a message will display informing the user of such. When the device is rotated the error message or tip information must continue to be displayed.
in Android/Java
Tip Calculator Using Java Program
In this program, accepting bill amount as user input and checking the user entered input is correct or not. If not display the proper message to the user. Finding the 10%, 15% and 20% of the bill amount as tip amount and also find the total amount.
Scanner is used for taking input from the user.
Here, if ...else is used for checking the user input.
%.2f is used for displaying the output in two decimal places.
Program in
Java
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
//input bill amount from user
Scanner scan = new
Scanner(System.in);
System.out.print("Bill Amount:
");
double billAmt = scan.nextDouble();
if(billAmt==0){
System.out.printf("Bill Amount cannot be
zero.");
}else if(billAmt<0){
System.out.printf("Bill Amount cannot be
negative.");
}
else{
//calculate the tip amount of 10%, 15% and 20% of bill
amount And Finding total amount.
//%.2f => values will be set to two decimal
places
System.out.printf("\nTip Amount of
10%% = %.2f and Bill Amount =%.2f", (billAmt*0.1),(billAmt +
(billAmt*0.1)));
System.out.printf("\nTip Amount of 15%% = %.2f and Bill Amount
=%.2f", (billAmt*0.15),(billAmt + (billAmt*0.15)));
System.out.printf("\nTip Amount of 20%% = %.2f and Bill Amount
=%.2f", (billAmt*0.2),(billAmt + (billAmt*0.2)));
}
}
}
/* ---------------END-------------- */