You have been in a fancy restaurant with four of your best friends. All the indi
ID: 3668608 • Letter: Y
Question
You have been in a fancy restaurant with four of your best friends. All the individual bills are brought to the table and it is time to add the tip and to compute everyone’s share. Since nobody ordered anything extravagantly expensive, you decide that everyone will pay an equal share of the bill.
Inside your program, you have to declare eight variables: one for each of the names and one for each of the bills. Assume the group has five people. Request names of the five attendees and store them in variables ‘name1’, …, ‘name5’. Then assign bills for each person and store each of them in the corresponding variables: ‘bill1’, …,’bill5’. Print out both of those sets of variables. Next ask for input for the tip percentage and compute and print the total bill and the amount each person must pay.
Explanation / Answer
// header files
import java.util.Date;
import java.util.Scanner;
import java.text.DecimalFormat;
// create class
public class HelloWorld
{
public static void main(String []args)
{
// declare variables
String name;
float bill, tip, tipamt;
int people;
DecimalFormat df = new DecimalFormat("0.00");
Scanner scan = new Scanner(System.in);
// getting name from user
System.out.print("Please enter your name: ");
name = scan.nextLine();
System.out.println( name + ", welcome to the Tip Calculator!");
// asking to enter amount of bill
System.out.print("Please enter the amount of your bill: ");
bill = scan.nextFloat();
// percentage as input
System.out.print("Please enter the percentage that you want to tip: ");
tip = scan.nextFloat();
System.out.println("Your original bill was $"+bill);
// calculate tip
tipamt = (bill*tip/100);
// display output
System.out.println("Your tip amount is $"+df.format(tipamt));
System.out.println("Your total bill is $"+df.format(bill+tipamt));
System.out.print("How many people will be splitting the bill? ");
people = scan.nextInt();
System.out.println("Each person should pay $"+df.format((bill+tipamt)/people));
}
}
output
Please enter your name: Ram
Ram, welcome to the Tip Calculator!
Please enter the amount of your bill: 1000
Please enter the percentage that you want to tip: 10
Your original bill was $1000.0
Your tip amount is $100.00
Your total bill is $1100.00
How many people will be splitting the bill? 5
Each person should pay $220.00
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.