Starting from:

$25

COMP1210- Project 3: Using Java API Classes Solved

Files to submit to Web-CAT:

•       FormulaEval.java

•       FootballTicket.java

 

Specifications
Overview: You will write two programs this week.  The first will compute the value generated by a specified expression and the second will read data for a football ticket and then interpret and print the formatted ticket information.

 

        •    FormulaEval.java

 

Requirements:  Calculate the following expression for a value x of type double which is read in from the keyboard, and save the result of the expression in a variable of the type double.  You must use the sqrt(), abs() and pow() methods of the Math class to perform the calculation.  You may use a single assignment statement with a single expression, or you may break the expression into appropriate multiple assignment statements.  The latter may easier to debug if you are not getting the correct result.  

 

 𝟏𝟎𝟎𝟎𝒙 + %|𝟑. 𝟗𝒙𝟓 − 𝒙𝟑 + 𝟏|

                                                                                                𝟐 + 𝟐. 𝟕𝒙 + 𝟑. 𝟖)       

(𝟏. 𝟔𝒙

 

Next, determine the number of characters (mostly digits) to the left and to the right of the decimal point in the unformatted result.  [Hint: You should consider converting the type double result into a String using the static method Double.toString(result) and storing it into a String variable. Then, on this String variable use the indexOf(".") method from the String class to find the position of the period (i.e., decimal point) and the length() method to find the length of the String.  Knowing the location of the decimal point and the length, you should be able to determine the number of digits on each side of the decimal point.]

 

Finally, the result should be printed using the class java.text.DecimalFormat so that to the right of the decimal there are at most five digits and to the left of the decimal each group of three digits is separated by a comma in the traditional way.  Also, there should also be at least one digit on each side of the decimal (e.g., 0 should be printed as 0.0).  Hint: Use the pattern "#,##0.0####" in your DecimalFormat constructor.  However, make sure you know what this pattern means and how to modify and use it in the future.

 

Design: Several examples of input/output for the FormulaEval program are shown below.

 

Line number
Program output









Enter a value for x: 0 

Result: 0.2631578947368421 

# of characters to left of decimal point: 1 

# of characters to right of decimal point: 16 Formatted Result: 0.26316  
       

Line number
Program output









Enter a value for x: 15.3 

Result: 40.76517723751596 

# of characters to left of decimal point: 2 

# of characters to right of decimal point: 14 Formatted Result: 40.76518  
 

Line number
Program output









Enter a value for x: -15.3 

Result: -40.03373803500063 

# of characters to left of decimal point: 3 

# of characters to right of decimal point: 14 Formatted Result: -40.03374  
 

Line number
Program output









Enter a value for x: 9876543210987654321 

Result: 3.878955567931786E9 

# of characters to left of decimal point: 1 

# of characters to right of decimal point: 17 Formatted Result: 3,878,955,567.93179  
When the characters to the right of the decimal in the unformatted result end with E followed by one or more digits (e.g., E9 indicates an exponent of 9), the ‘E’ should be included in the count of the characters to the right of the decimal point.

 

Code: In order to receive full credit for this assignment, you must use the appropriate Java API classes and method to do the calculation and formatting.  It is recommended as a practice that you do not modify the input value once it is stored.

 

Test: You will be responsible for testing your program, and it is important to not rely only on the examples above. Assume that the amount entered can be any positive or negative floating-point number.   

 

        •    FootballTicket.java 

 

Requirements: The purpose of this program is to accept coded football ticket information as input that includes the ticket price, category, time, date, seat, row, section, followed by the description of the game.  Note that the five 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 (none for regular tickets (anything but s or f), 67% for student tickets (s), and 20% for faculty/staff tickets (f).  The last line of the ticket should contain a random "prize number" between 1 and 9999999 inclusive that should always be printed as seven digits (e.g., 1 should be printed as 0000001).  The coded input is formatted as follows:

 

  r12500143009152018213105Auburn vs LSU 
  

                                 date                 section             game description                                                       row           (goes through last character in the code)                                              seat

                time

      price  [12500 has an implied decimal point for 125.00]

category  [s,  f, or anything else is a regular ticket with no discount]

Whitespace before or after the coded information should be disregarded (e.g., if the user enters spaces or tabs before or after the coded information, these should be disregarded).  Your program will need to print the game description, the date and time, the section, row, and seat number, the ticket price, the ticket category, the actual cost, and a random prize number in the range 1 to 9999999.  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 game description.]  

 

Design: Several examples of input/output for the program are shown below.

 

Line #
Program output


2  




Enter your ticket code: 123456789 

 

Invalid Ticket Code.  

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., game, date, and time on line 3), there are three spaces between them (do not use the tab escape sequence \t).

Line #
Program output
 


2  








Enter your ticket code: r12500143009152018213105Auburn vs LSU  

Game: Auburn vs LSU   Date: 09/15/2018   Time: 14:30 

Section: 5   Row: 31   Seat: 21 

Price: $125.00   Category: r   Cost: $125.00 

Prize Number: 3354928 

 
 
Line #
Program output


2  








Enter your ticket code: s12500143009152018213105Auburn vs LSU  

Game: Auburn vs LSU   Date: 09/15/2018   Time: 14:30 

Section: 5   Row: 31   Seat: 21 

Price: $125.00   Category: s   Cost: $41.25 

Prize Number: 4247895 

 
 

Note that the ticket code below has five leading spaces (be sure you are trimming the input code).

Line #
Program output


2  








Enter your ticket code:      f12500143009152018213105Auburn vs LSU  

Game: Auburn vs LSU   Date: 09/15/2018   Time: 14:30 

Section: 5   Row: 31   Seat: 21 

Price: $125.00   Category: f   Cost: $100.00 

Prize Number: 0241193 

 

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 methods such as Integer.parseInt and 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 seven 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 faculty/staff discount constants.  For example, the following statements could be placed above the main method. static final double STUDENT_DISCOUNT = 0.67; static final double FACULTY_STAFF_DISCOUNT = 0.20; 
 

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.   

 

Hints
FootballTicket 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.


             

More products