Make sure you are doing it right. Or else, it will be flagged. Use printf to con
ID: 3596007 • Letter: M
Question
Make sure you are doing it right. Or else, it will be flagged.
Use printf to control the display of the decimal point to 2 decimal places.
Write a program that will help two friends split the cost of a trip. There are four main expenses: the cost of the flight, cost of the hotel, cost of a tour, and the cost of meals. The program uses a Scanner object to read in user input. The program will first ask for the names of two friends. Then it will ask the cost for each expense and the who paid for which expense. The program will then calculate the total of all the expenses, what each person should pay if the costs were divided equally, and how much each friend actually paid. If one person paid less than the other, then they will owe their friend some money. See below for some sample outputs and how you should format your code.
Use printf to control the display of the decimal point on the following code to 2 decimal places.
import java.util.Scanner;
public class E6 {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.print("Enter the first name: ");
String first = kb.next();
System.out.print("Enter the second name: ");
String second = kb.next();
int choose,cost;
int firstCost = 0,secondCost = 0;
System.out.print("Enter cost of flights: $");
cost = kb.nextInt();
System.out.print("Who paid for the flights: Enter 1 for "+first+" or 2 for "+second+": ");
choose = kb.nextInt();
if(choose==1){
firstCost+=cost;
}
else{
secondCost+=cost;
}
System.out.print("Enter cost of hotel: $");
cost = kb.nextInt();
System.out.print("Who paid for the hotel: Enter 1 for "+first+" or 2 for "+second+": ");
choose = kb.nextInt();
if(choose==1){
firstCost+=cost;
}
else{
secondCost+=cost;
}
System.out.print("Enter cost of tour: $");
cost = kb.nextInt();
System.out.print("Who paid for the tour: Enter 1 for "+first+" or 2 for "+second+": ");
choose = kb.nextInt();
if(choose==1){
firstCost+=cost;
}
else{
secondCost+=cost;
}
System.out.print("Enter cost of meals: $");
cost = kb.nextInt();
System.out.print("Who paid for the meals: Enter 1 for "+first+" or 2 for "+second+": ");
choose = kb.nextInt();
if(choose==1){
firstCost+=cost;
}
else{
secondCost+=cost;
}
int total = firstCost+secondCost;
System.out.println("Total bill for the trip: $"+total);
System.out.println("Each person owes: $"+(total/2));
System.out.println(first+" paid $"+firstCost);
System.out.println(second+" paid $"+secondCost);
if(firstCost>secondCost){
System.out.println(second+" owes "+first+" $"+((total/2d)-secondCost));
}
else if(firstCost==secondCost){
System.out.println(first+" and "+second+" paid the same amount.");
}
else{
System.out.println(first+" owes "+second+" $"+((total/2d)-firstCost));
}
}
}
Explanation / Answer
HI
I have updated the code with printf statements.
E6.java
import java.util.Scanner;
public class E6 {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.print("Enter the first name: ");
String first = kb.next();
System.out.print("Enter the second name: ");
String second = kb.next();
int choose,cost;
int firstCost = 0,secondCost = 0;
System.out.print("Enter cost of flights: $");
cost = kb.nextInt();
System.out.print("Who paid for the flights: Enter 1 for "+first+" or 2 for "+second+": ");
choose = kb.nextInt();
if(choose==1){
firstCost+=cost;
}
else{
secondCost+=cost;
}
System.out.print("Enter cost of hotel: $");
cost = kb.nextInt();
System.out.print("Who paid for the hotel: Enter 1 for "+first+" or 2 for "+second+": ");
choose = kb.nextInt();
if(choose==1){
firstCost+=cost;
}
else{
secondCost+=cost;
}
System.out.print("Enter cost of tour: $");
cost = kb.nextInt();
System.out.print("Who paid for the tour: Enter 1 for "+first+" or 2 for "+second+": ");
choose = kb.nextInt();
if(choose==1){
firstCost+=cost;
}
else{
secondCost+=cost;
}
System.out.print("Enter cost of meals: $");
cost = kb.nextInt();
System.out.print("Who paid for the meals: Enter 1 for "+first+" or 2 for "+second+": ");
choose = kb.nextInt();
if(choose==1){
firstCost+=cost;
}
else{
secondCost+=cost;
}
int total = firstCost+secondCost;
System.out.printf("Total bill for the trip: $%.2f ",total/1d);
System.out.printf("Each person owes: $%.2f ",+(total/2d));
System.out.printf(first+" paid $%.2f ",firstCost/1d);
System.out.printf(second+" paid $%.2f ",secondCost/1d);
if(firstCost>secondCost){
System.out.printf(second+" owes "+first+" $%.2f ",((total/2d)-secondCost));
}
else if(firstCost==secondCost){
System.out.println(first+" and "+second+" paid the same amount.");
}
else{
System.out.printf(first+" owes "+second+" $%.2f ",((total/2d)-firstCost));
}
}
}
Output:
Enter the first name: AAAA
Enter the second name: BBBB
Enter cost of flights: $1111
Who paid for the flights: Enter 1 for AAAA or 2 for BBBB: 1
Enter cost of hotel: $222
Who paid for the hotel: Enter 1 for AAAA or 2 for BBBB: 2
Enter cost of tour: $333
Who paid for the tour: Enter 1 for AAAA or 2 for BBBB: 2
Enter cost of meals: $111
Who paid for the meals: Enter 1 for AAAA or 2 for BBBB: 1
Total bill for the trip: $1777.00
Each person owes: $888.50
AAAA paid $1222.00
BBBB paid $555.00
BBBB owes AAAA $333.50
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.