Question

Project: Using Java API Classes Page 3 of 5 SpaceTicket.java Requirements: The purpose of this program is accept coded space

Project: Using Java API Classes Page 4 of 5 Line Program output Enter ticket code: 12579500s15300701209817DSpaceX-001 Earth t

Project: Using Java API Classes Page 3 of 5 SpaceTicket.java Requirements: The purpose of this program is accept coded space ticket information as input that includes the ticket price, category, time, date, and seat, followed by the description of the travel. Note that the eight 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: 25% for a student ticket (s), 35% for child ticket (c), and none for regular tickets (i.e., anything but s or c). The last line of the ticket should contain a "prize number" between 1 and 999999 inclusive that should always be printed as 6 digits (e.g., 1 should be printed as 000001). The coded input is formatted as follows: 12579500s15300701209817DSpaceX-001 Earth to Mars date ticket description (goes through last character in the code) seat time category [s, c, or anything else is a regular ticket with no discount; store this as typee char] price 12579500 has an implied decimal point for 125795.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.) Your program will need to print the ticket description, the date and time, seat, the ticket price, the ticket category, the actual cost, and a random prize number in the range 1 to 999999. If the user enters a code that does not have at least 25 characters, then an error message should be printed. [The 25th character of the code is part of the ticket description.] Design: Several examples of input/output for the program are shown below. Line Program output Enter ticket code: 123456789 ** Inval1id ticket code ** 3 Ticket code must have at least 25 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., date, time, and seat on line 4), there are three spaces between them (do not use the tab escape sequence \t). Line Program output Enter ticket code: 12579500x15300701209817DSpaceX-001 Earth to Mars Space Ticket: SpaceX-001 Earth to Mars Date: 07/01/2098 Time: 15:30 Seat: 17D Price: $125,795.00 Prize Number: 174889 Cost: $125,795.00 Category: x 6 Page 3 of 5
Project: Using Java API Classes Page 4 of 5 Line Program output Enter ticket code: 12579500s15300701209817DSpaceX-001 Earth to Mars 1 Space Ticket: SpaceX-001 Earth to Mars Date: 07/01/2098 Time: 15:30 Seat: 17D Cost: $94,346.25 Category: s ize Number: 004907 Note that the ticket code below has five leading spaces (be sure you are trimming the input code). Line Program output Enter ticket code: 12579500c15300701209817DSpacex-001 Earth to Mars Ppa 013098 mime 15.30 Seat: 17D Price: $125,795.00 Prize Number: 887295 Cost: $81, 766.75 Category: c 6 Code: In order to receive full credit for this assignment, you must use the appropriate Java API classes and methods to trim the input string, to do the extraction of the category character, extraction of the substrings, conversion of substrings of digits to numeric values as appropriate, and formatting. These include the String methods trim, charAt, and substring, as well as wrapper class method Double.parseDouble which can be used to convert a String of digits into a numeric value. The dollar amounts should be formatted so that both small and large amounts are displayed properly, and the prize number should be formatted so that six digits are displayed including leading zeroes, if needed, as shown in the examples above. It is recommended as a practice that you not modify input values once they are stored. While not a requirement, you should consider making the student discount and child discount constants. For example, the following statements could be placed above the main method, static final double STUDENT DISCOUNT = .25; static final double CHILD DISCOUNT = .35; Test: You are responsible for testing your program, and it is important to not rely only on the examples above. Remember, when entering standard input in the Run I'O window, you can use the up-arrow on the keyboard to get the previous values you have entered. This will avoid having to retype the ticket info data each time you run your program, Grading Web-CAT Submission: You must submit both "completed" programs to Web-CAT at the same time. Prior to submitting, be sure that your programs are working correctly and that they have passed Checkstyle. If you do not submit both programs at once, Web-CAT will not be able to compile and run its test files with your programs which means the submission will receive zero points for correctness. I recommend that you create a jGRASP project and add the two files. Then you will be able to submit the project to Web-CAT from jGRASP. Activity 1 (pages 5 and 6) describes how to create a jGRASP project containing both of your files Page 4 of 5
0 0
Add a comment Improve this question Transcribed image text
Answer #1

SpaceTicket.java

import java.util.Random;
import java.util.Scanner;

public class SpaceTicket {
  
private static final double STUDENT_DISCOUNT = 0.25;
private static final double CHILD_DISCOUNT = 0.35;
  
public static void main(String[]args)
{
System.out.print("Enter ticket code: ");
Scanner sc = new Scanner(System.in);
String code = sc.nextLine().trim();
  
// checking whether the ticket code has at least 25 characters
if(code.length() < 25)
{
System.out.println("\n*** Invalid ticket code ***\nTicket code must have at least 25 characters.\n");
System.exit(0);
}
  
// assuming price always has 8 digits; 9 because substring excludes the end index
String price = code.substring(0, 8);
double ticketPrice = Double.parseDouble(price.substring(0, 6) + "." + price.substring(6));
  
// now extracting the category and applying the discount
char category = code.charAt(8);
double cost = 0;
switch(category)
{
case 's':
cost = ticketPrice - (STUDENT_DISCOUNT * ticketPrice);
break;
  
case 'c':
cost = ticketPrice - (CHILD_DISCOUNT * ticketPrice);
break;
  
default:
cost = ticketPrice;
break;
}
  
// extracting the time
String time = code.substring(9, 11) + ":" + code.substring(11, 13);
  
// extracting the date
String date = code.substring(13, 15) + "/" + code.substring(15, 17) + "/" + code.substring(17, 21);
  
// extracting the seat number
String seatNumber = code.substring(21, 24);
  
// extracting the ticket description
String ticketDesc = code.substring(24);
  
// generating the prize number
Random rand = new Random();
int prize = rand.nextInt(999999) + 1;
String prizeNum = String.valueOf(prize);
int padding = 0;
String zeros = "";
if(prizeNum.length() < 6)
padding = 6 - prizeNum.length();
for(int i = 0; i < padding; i++)
{
zeros += "0";
}
prizeNum = zeros + prizeNum;
  
/* DISPLAYING ALL THE DETAILS */
  
System.out.println("\nSpace Ticket: " + ticketDesc);
System.out.println("Date: " + date + "\tTime: " + time + "\tSeat: " + seatNumber);
System.out.println("Price: $" + String.format("%,.2f", ticketPrice) + "\tCategory: " + category + "\tCost: $"
+ String.format("%,.2f", cost));
System.out.println("Prize Number: " + prizeNum + "\n");
}
}

******************************************************************* SCREENSHOT ********************************************************

DD DD run: Enter ticket code: 123456789 Invalid ticket code * Ticket code must have at least 25 characters. BUILD SUCCESSFUL

run: Enter ticket code: 12579500x15300701209817DSpaceX-001 Earth to Mars Space Ticket: SpaceX-001 Earth to Mars Seat: 17D Dat

run Enter ticket code: 12579500s15300701209817DSpaceX-001 Earth to Mars Space Ticket: SpaceX-001 Earth to Mars Date: 07/01/20

run: DD Enter ticket code: 12579500c15300701209817DSpaceX-001 Earth to Mars Space Ticket: SpaceX-001 Earth to Mars Date: 07/0

Add a comment
Know the answer?
Add Answer to:
Project: Using Java API Classes Page 3 of 5 SpaceTicket.java Requirements: The purpose of this program is accept coded...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For this program, you will be working with data from the NASA website which lists Near...

    For this program, you will be working with data from the NASA website which lists Near Earth Objects detected by the JPL Sentry System. You are given a text file listing the designation and impact probability (with Earth, generally within the next 100 years) of 585 Near Earth Objects. Your job will be to sort these objects by their impact probabilities. Input File Format The input file contains 585 records. Each record is on a separate line. Each line contains...

  • Modify When executing on the command line having only this program name, the program will accept...

    Modify When executing on the command line having only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with your new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the command line to be tested...

  • GENERAL INSTRUCTIONS              All requirements specified on page 64-67 of the course packet “2.5 Programming Assignment...

    GENERAL INSTRUCTIONS              All requirements specified on page 64-67 of the course packet “2.5 Programming Assignment Submission Requirements” and “2.6 Flow Chart Symbols” should be followed.              Plan the programs on paper before you attempt to write any programs (it will take less time to complete the assignment overall).              Electronic version of your programs (the .m files you create) must be uploaded to Canvas. Attach multiple files to one submission. All files must be received by the beginning of...

  • Help Please on JAVA Project: Validating the input is where I need help. Thank you Requirements...

    Help Please on JAVA Project: Validating the input is where I need help. Thank you Requirements description: Assume you work part-time at a sandwich store. As the only employee who knows java programming, you help to write a sandwich ordering application for the store. The following is a brief requirement description with some sample output. 1. Selecting bread When the program starts, it first shows a list/menu of sandwich breads and their prices, then asks a user to select a...

  • Assignment You will be developing a speeding ticket fee calculator. This program will ask for a t...

    Assignment You will be developing a speeding ticket fee calculator. This program will ask for a ticket file, which is produced by a central police database, and your program will output to a file or to the console depending on the command line arguments. Furthermore, your program will restrict the output to the starting and ending dates given by the user. The ticket fee is calculated using four multipliers, depending on the type of road the ticket was issued: Interstate...

  • Need help with java programming. Here is what I need to do: Write a Java program...

    Need help with java programming. Here is what I need to do: Write a Java program that could help test programs that use text files. Your program will copy an input file to standard output, but whenever it sees a “$integer”, will replace that variable by a corresponding value in a 2ndfile, which will be called the “variable value file”. The requirements for the assignment: 1.     The input and variable value file are both text files that will be specified in...

  • Please use Java to write the program The purpose of this lab is to practice using...

    Please use Java to write the program The purpose of this lab is to practice using inheritance and polymorphism. We need a set of classes for an Insurance company. Insurance companies offer many different types of policies: car, homeowners, flood, earthquake, health, etc. Insurance policies have a lot of data and behavior in common. But they also have differences. You want to build each insurance policy class so that you can reuse as much code as possible. Avoid duplicating code....

  • Create a program that performs the following operations: 1. Prompt for and accept a string of...

    Create a program that performs the following operations: 1. Prompt for and accept a string of up to 80 characters from the user. • The memory buffer for this string is created by: buffer: .space 80 #create space for string input The syscall to place input into the buffer looks like: li $v0,8 # code for syscall read_string la $a0, buffer #tell syscall where the buffer is li $a1, 80 # tell syscall how big the buffer is syscall 2....

  • Project 3 Objective: The purpose of this lab project is to exposes you to using menus,...

    Project 3 Objective: The purpose of this lab project is to exposes you to using menus, selection writing precise functions and working with project leaders. Problem Specification: The PCCC Palace Hotel needs a program to compute and prints a statement of charges for customers. The software designer provided you with the included C++ source and you are asked to complete the code to make a working program using the given logic. The function main () should not change at all....

  • Writing Unix Utilities in C (not C++ or C#) my-cat The program my-cat is a simple...

    Writing Unix Utilities in C (not C++ or C#) my-cat The program my-cat is a simple program. Generally, it reads a file as specified by the user and prints its contents. A typical usage is as follows, in which the user wants to see the contents of my-cat.c, and thus types: prompt> ./my-cat my-cat.c #include <stdio.h> ... As shown, my-cat reads the file my-cat.c and prints out its contents. The "./" before the my-cat above is a UNIX thing; it...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT