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
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)); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
