Question

In: Computer Science

Objectives: Write a program which reads User Input using Scanner Print formatted output using printf or...

Objectives:

Write a program which reads User Input using Scanner

Print formatted output using printf or DecimalFormat

Practice programming simple mathematical calculations

Instructions:

Samwise Gamgee has finally decided to submit his expense report for all of his adventures regarding his travels to Mordor. Part of those expenses are his stay at the Prancing Pony Inn located in Bree.

You are to write a simple Java program which will generate an Invoice for his stay at the Inn.

Your program should ask for user input which represents the following information:

the number of nights Sam stayed at the Inn

the tax rate for the taxes collected by the Shire

the tax rate for the taxes collected by the City of Bree

cost per night of staying at the Inn

The program should then print out an Invoice which is similar to:

The Prancing Pony Inn, City of Bree, Shire

Invoice for Samwise Gamgee
Cost per night: $110
Shire Taxes per night: @Rate of 6.75% is $7.42
City of Bree Taxes per night: @Rate of 1.25% is $1.37
Total Cost per night: $118.79
Number of nights Stayed: 4
Total Cost: $475.16

(Bonus: Change the program so that the name of the Person and the two Tax Districts can also be entered by the user. These names should be allowed to contain spaces)

Solutions

Expert Solution

import java.util.Scanner;
public class HelloWorld{

public static void main(String []args){

Scanner sc = new Scanner(System.in);
System.out.print("the number of nights Sam stayed at the Inn: ");
int nights = sc.nextInt();
System.out.print("the tax rate for the taxes collected by the Shire: ");
float shireTaxRate = sc.nextFloat();
System.out.print("the tax rate for the taxes collected by the City of Bree: ");
float breeTaxRate = sc.nextFloat();
System.out.print("cost per night of staying at the Inn: ");
int costPerNight = sc.nextInt();

float ShireTaxPerNight = (costPerNight*shireTaxRate)/100;
float breeTaxPerNight = (costPerNight*breeTaxRate)/100;
float totalCostPerNight = costPerNight + ShireTaxPerNight + breeTaxPerNight;

float totalCost = totalCostPerNight*nights;
System.out.println("The Prancing Pony Inn, City of Bree, Shire");
System.out.println("Invoice for Samwise Gamgee");
System.out.printf("Cost per night: $%d\n", costPerNight);
System.out.printf("Shire Taxes per night: @Rate of %.2f%% is $%.2f\n", shireTaxRate, ShireTaxPerNight);
System.out.printf("City of Bree Taxes per night: @Rate of %.2f%% is $%.2f\n", breeTaxRate, breeTaxPerNight);
System.out.printf("Total Cost per night: $%.2f\n", totalCostPerNight);
System.out.printf("Number of nights Stayed: %d\n", nights);
System.out.printf("Total Cost: $%.2f\n", totalCost);
}
}


Related Solutions

• Using the Scanner class to obtain input from the user. • Using printf to output...
• Using the Scanner class to obtain input from the user. • Using printf to output information to the user. • Using arithmetic operators to perform calculations. • Using if statements to make decisions based on the truth or falsity of a condition. • Using relational operators to compare variable values. Project 1: Write an application that asks the user to enter two integers, obtains them from the user and prints their sum, product, difference and quotient (division). Sample Output:...
Write a Java program that uses printf, and takes user input to find the name of...
Write a Java program that uses printf, and takes user input to find the name of the File. FileCompare (20) Write a program that compares to files line by line, and counts the number of lines that are different. You can use file1.txt and file2.txt when testing your program, but you must ask for the file names in the input. main Interactively requests the names of the two files, after creating the Scanner for the keyboard. Creates a Scanner for...
Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
User the Scanner class for your input Write a java program to calculate the area of...
User the Scanner class for your input Write a java program to calculate the area of a rectangle. Rectangle Area is calculated by multiplying the length by the width   display the output as follow: Length =   Width = Area = Load: 1. Design (Pseudocode ) 2. Source file (Java file, make sure to include comments) 3. Output file (word or pdf or jpig file)
Write a C++ or Java program that reads an input graph data from a user. Then,...
Write a C++ or Java program that reads an input graph data from a user. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number ofvertices in the input graph is less than or equal to 20. Input format: This is a sample input from a user. 4 12 0 1 2 0 3 7 0 2 5 1 0 2 1 2 8 1 3 3 2...
Write a Java program that reads an input graph data from a user. Then, it should...
Write a Java program that reads an input graph data from a user. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number of vertices in the input graph is less than or equal to 20. Input format: This is a sample input from a user. 4 12 0 1 2 0 3 7 0 2 5 1 0 2 1 2 8 1 3 3 2 0...
Write java program that will ask for the user for 2 input lines and print out...
Write java program that will ask for the user for 2 input lines and print out all words that occur 1 or more times on both lines (case sensitive). Write this without arrays and method. Here is a sample run: <Output> Enter two lines to process. The quick brown fox jumps over a lazy dog The fox hound outruns the lazy dog The words that occur on both lines are: The fox lazy dog
Write a program in C, that uses standard input and output to ask the user to...
Write a program in C, that uses standard input and output to ask the user to enter a sentence of up to 50 characters, the ask the user for a number between 1 & 10. Count the number of characters in the sentence and multiple the number of characters by the input number and print out the answer. Code so far: char sentence[50]; int count = 0; int c; printf("\nEnter a sentence: "); fgets(sentence, 50, stdin); sscanf(sentence, %s;    for(c=0;...
Ask the user to input a series of numbers, write a C# program to output the...
Ask the user to input a series of numbers, write a C# program to output the sum, max, and min. Be sure to do error checking if the user input is not a number.
This program must be in java, use printf, and request user input for a file name....
This program must be in java, use printf, and request user input for a file name. NameSubsLast3 (30 points) Write a program that does the following. Write a program that does the following. Requests the name of an input file and uses it to create an input file Scanner. Requests the name of an output file and uses it to create a PrintWriter. I have posted firstNames.txt and outNames.txt in the homework for Lesson 6. Calls createArray to create a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT