Write a complete Java program in a source file to be named Assignment2.java. The
ID: 3628348 • Letter: W
Question
Write a complete Java program in a source file to be named Assignment2.java.The program prints out the following menu to a user, using ' ' (tabs) and ' ' (newline), and print or println methods of System.out:
Welcome to the Movie Theatre!
---------------------------------------------
The cost of movie tickets:
Matinee $5.00
Normal $7.50
Rush Hour $3.50
Then prompt to a user:
How many Matinee tickets would you like to buy?
and read in the number.
Then prompt to a user:
How many Normal tickets would you like to buy?
and read in the number.
Then prompt to a user:
How many Rush Hour tickets would you like to buy?
and read in the number.
Then the program computes and prints the following:
The total cost for the Matinee tickets
The total cost for the Normal tickets
The total cost for the Rush Hour tickets
The total cost for all tickets
The total number of tickets
The average cost per ticket for this purchase
Use the NumberFormat class to format dollar amounts to be printed. The NumberFormat class is defined in the java.text package.
Output Sample: the user input is in bold
Welcome to the Movie Theatre!
---------------------------------------------
The cost of movie tickets:
Matinee $5.00
Normal $7.50
Rush Hour $3.50
How many Matinee tickets would you like to buy?
12
How many Normal tickets would you like to buy?
20
How many Rush Hour tickets would you like to buy?
32
The total cost for the Matinee tickets: $60.00
The total cost for the Normal tickets: $150.00
The total cost for the Rush Hour tickets: $112.00
The total cost for all tickets: $322.00
The total number of tickets: 64
The average cost per ticket for this purchase: $5.03
Explanation / Answer
import java.text.NumberFormat; import java.util.Currency; import java.util.Scanner; public class Assignment2 { public static void main(String[] args) { /* // Declaring variable to use for exercises. String myString = "Now is the time for the birthday party"; // Statement to print the number of characters in the string. // Use .length and declare an int to store the number in. int myStringLength = myString.length(); System.out.println("Number of characters in the string: " + myStringLength); // Statement that outputs the character at index 16. // Declaring char to hold character in and getting value. char sixteen = myString.charAt(16); System.out.println("The character at index 16 is: " + sixteen); // Statement that outputs the position of the word "for" in the sentence. int position = myString.lastIndexOf("for"); System.out.println("The position of the word 'for' in the sentence is: " + position); // Statement that outputs the sentence in uppercase letters. System.out.println(myString.toUpperCase()); // Extract the new string "birthday party" and print it to screen. String extraction = myString.substring(24); System.out.println("The new string is: " + extraction); */ // Declaring new scanner to read input. Scanner scan = new Scanner(System.in); // Printing out the menu: System.out.println("Welcome to the Movie Theatre!"); System.out.println("-----------------------------"); System.out.println("The cost of movie tickets:"); System.out.println("Matinee: $5.00"); System.out.println("Normal: $7.50"); System.out.println("Rush Hour: $3.50"); // Declaring the three ints for storage from input: int matinee, normal, rushHour; // Prompting how many Matinee tickets user wants to buy: System.out.print(" How many Matinee tickets would you like to buy? "); matinee = scan.nextInt(); // Prompting for Normal tickets: System.out.print(" How many Normal tickets would you like to buy? "); normal = scan.nextInt(); // Prompting for Rush Hour: System.out.print(" How many Rush Hour tickets would you like to buy? "); rushHour = scan.nextInt(); // Calculations: // Initializing doubles for totalCostMatinee, etc. double totalCostMatinee, totalCostNormal, totalCostRushHour, totalCostAll, averageCost; // Declaring int for total number of tickets. int totalTickets; // set up numberformat: NumberFormat nf = NumberFormat.getCurrencyInstance(); // Total costs: totalCostMatinee = matinee * 5.0; System.out.println(" The total cost for the Matinee tickets: " + nf.format(totalCostMatinee)); totalCostNormal = normal * 7.5; System.out.println("The total cost for the Normal tickets: " + nf.format(totalCostNormal)); totalCostRushHour = rushHour * 3.5; System.out.println("The total cost for the Rush Hour tickets: " + nf.format(totalCostRushHour)); totalCostAll = totalCostMatinee + totalCostNormal + totalCostRushHour; System.out.println("The total cost for all tickets: " + nf.format(totalCostAll)); totalTickets = matinee + normal + rushHour; System.out.println("The total number of tickets: " + totalTickets); averageCost = totalCostAll / totalTickets; System.out.println("The average cost per ticket for this purchase: " + nf.format(averageCost)); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.