Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hints TimeTravel class 1. The ticket code should be read in all at once and stor

ID: 3882325 • Letter: H

Question

Hints TimeTravel class 1. The ticket code should be read in all at once and stored in a variable of type String, after which the individual values should be extracted using the substring method. The String value for price should be converted to type double (using Double.parseDouble) so that it can be used to calculate cost. When printing the values for price and cost, they should be formatted properly by creating an appropriate DecimalFormat object and calling its format method. Since all items other than the price will not be used in arithmetic expressions, they can be left as String values (or char value in the case of category). 2. Since char values are primitive types, == and != can be used to compare two values for equality and inequality respectively. Thus, if the category is extracted from the input using the String method charAt() which returns a char, then == and != can be used to compare char values. Otherwise, if category is a String, you should not use == and != to compare two String values. String values should be compared for equality using the String equals method which has a boolean return type. For example, if s1 and s2 are String objects, to check to see if their respective character strings are equal you should use s1.equals(s2) rather than s1 == s2 which is only true if s1 and s2 are aliases for the same String object. The time and date should have leading zeros as appropriate. Therefore, these can be printed as String values by concatenating their components with ":" and "/" as needed. Hints TimeTravel class 1. The ticket code should be read in all at once and stored in a variable of type String, after which the individual values should be extracted using the substring method. The String value for price should be converted to type double (using Double.parseDouble) so that it can be used to calculate cost. When printing the values for price and cost, they should be formatted properly by creating an appropriate DecimalFormat object and calling its format method. Since all items other than the price will not be used in arithmetic expressions, they can be left as String values (or char value in the case of category). 2. Since char values are primitive types, == and != can be used to compare two values for equality and inequality respectively. Thus, if the category is extracted from the input using the String method charAt() which returns a char, then == and != can be used to compare char values. Otherwise, if category is a String, you should not use == and != to compare two String values. String values should be compared for equality using the String equals method which has a boolean return type. For example, if s1 and s2 are String objects, to check to see if their respective character strings are equal you should use s1.equals(s2) rather than s1 == s2 which is only true if s1 and s2 are aliases for the same String object. The time and date should have leading zeros as appropriate. Therefore, these can be printed as String values by concatenating their components with ":" and "/" as needed. TimeTravel.java Requirements: The purpose of this program is to accept coded ticket information to another time and place (and back) as input that includes the date, time, category, price, and seat, followed by the description of the travel. Note that the nine digits for price have an implied decimal point. The program should then print the ticket information including the actual cost, which is the price with discount applied as appropriate: 50% for a student ticket (s), 25% for employee ticket(e), and none for regular tickets (i.e., anything other than s or e). The last line of the ticket should contain a "prize number" between I and 9999 inclusive that should always be printed as 4 digits (e.g., 7 should be printed as 0007). The coded input is formatted as follows: 153012152090s02498990018BTime Machine 7 to NYC 12/31/1880 time date ticket description (goes through last character in the code) category scat category [s, e, any other character is a regular ticket with no discount; store this as type char] "price [012479900 has an implied decimal point; the double value should be 124799.00] Whitespace (e.g., spaces or tabs) before or after the coded information should be disregarded. Hint: After the ticket code has been read in as a String, it should be trimmed using the trimO method.] Your program will need to print the date and time, seat, the itinerary (i.e, ticket description), the ticket price, the ticket category, the actual cost, and a random prize number in the range to 9999. If the user enters a code that does not have at least 26 characters, then an error message should be printed and the program should end. [The 26 character of the code is part of the ticket description.] Design: Several examples of input/output for the program are shown below Line # Enter ticket code: 123456789 Invalid ticket code Ticket code must have at least 26 characters Note that the ticket code below results in the indicated output except for the prize number which is random. When more than one item is shown on the same line (e.g., time, date, and seat on line 3), there are three spaces between them (do not use the tab escape sequence lt) Line # 1 Pro Enter ticket code: 153012152090s02498990018BTime Machine 7 to NYC 12/31/1880 Time: 15:30 Date: 12/15/2090 Seat: 18B Itinerary: Time Machine 7 to NYC 12/31/1880 Price: $249,899.00 Cost: $124,949.50 Category: s Prize Number: 9019

Explanation / Answer

/*package whatever //do not write package name here */

import java.io.*;

import java.util.Scanner;

import java.text.DecimalFormat;

import java.util.Random;

public class TimeTravel {

  

public static void main (String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter ticket code: ");

String c= sc.nextLine();

String code = c.trim();

if(code.length() < 26 )

{

System.out.print("********Invalid Ticket code*********" );

return;

}

System.out.println(code);

System.out.print("Time: ");

System.out.print(code.substring(0, 2) + ":" + code.substring(2, 4) );

System.out.print(" Date: ");

System.out.print(code.substring(4, 6) + "/" + code.substring(6, 8) + "/" + code.substring(8, 12));

System.out.print(" Seat: ");

System.out.println(code.substring(22, 25) );

  

System.out.print("itinerary: ");

System.out.println(code.substring(25,code.length()));

double result =Double.parseDouble(code.substring(14, 22));

System.out.print("Price: $");

String category = code.substring(12, 13);

DecimalFormat format = new DecimalFormat("#,###.00");

System.out.print(format.format(result));

System.out.print(" Cost: $");

if(category.equals("s")) {

System.out.print(format.format(result/2));

}

else if(category.equals('e')) {

System.out.print(format.format(result/4));

}else {

System.out.print(format.format(result));

}

System.out.println(" Category: " + category);

System.out.print("Prize Number: ");

Random rand = new Random();

System.out.printf("%04d%n", rand.nextInt(10000));

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote