$25
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 meal order and then interpret and print the formatted meal order information.
• EvalExpression.java
Requirements: Evaluate the expression in the formula below for a value x of type double which is read in from the keyboard, and save the result in a variable of the type double. You must use the pow(), sqrt(), and abs()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.
(𝟏𝟐. 𝟒𝒙𝟔 − 𝟏. 𝟐𝒙𝟑) + 4|𝟐. 𝟔𝒙𝟓 − 𝟔. 𝟖𝒙 + 𝟕|
𝒓𝒆𝒔𝒖𝒍𝒕 = (𝒙𝟒 + 𝟗)
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 invoke 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 four 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###" when you create your DecimalFormat object. 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 EvalExpression program are shown below.
Line #
Program output
1
2
3
4
5
Enter a value for x: 0
Result: 0.2939723678960656
# of characters to left of decimal point: 1
# of characters to right of decimal point: 16 Formatted Result: 0.294
Line #
Program output
1
2
3
4
5
Enter a value for x: 1.0
Result: 1.2873320053068151
# of characters to left of decimal point: 1
# of characters to right of decimal point: 16 Formatted Result: 1.2873
Line #
Program output
1
2
3
4
5
Enter a value for x: 100
Result: 123999.97845245346
# of characters to left of decimal point: 6
# of characters to right of decimal point: 11 Formatted Result: 123,999.9785
Line #
Program output
1
2
3
4
5
Enter a value for x: 98765.4321
Result: 1.2095717116595642E11
# of characters to left of decimal point: 1
# of characters to right of decimal point: 19 Formatted Result: 120,957,171,165.9564
When the characters to the right of the decimal in the unformatted result end with E followed by one or more digits (e.g., E11 indicates an exponent of 11), 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.
• TicketDecoder.java
Requirements: The purpose of this program is to accept coded football ticket information as input that includes the ticket price, discount, time, date, section, row, seat, followed by the description of the game. Note that the five digits for price and two digits for discount have an implied decimal point. The program should then print the ticket information including the actual cost, which is the price with discount applied. 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:
0500020180009142019043121Auburn vs Kent State
date seat game description row (goes through last character on line) section time
discount [20 has an implied decimal point for 0.20 representing a 20% discount]
price [05000 has an implied decimal point before the last two digits for 50.00]
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 discount, 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 26 characters, then an error message should be printed. [The 26th 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
1
2
3
4
Enter your ticket code: 987654321
Invalid Ticket Code.
Ticket code must have at least 26 characters.
Note that the ticket code entered 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
1
2
3
4
5
6
Enter your ticket code: 0500000180009142019043121Auburn vs Kent State
Game: Auburn vs Kent State Date: 09/14/2019 Time: 18:00
Section: 04 Row: 31 Seat: 21
Price: $50.00 Discount: 0% Cost: $50.00
Prize Number: 4997613
Note that the ticket code entered below includes a 20% discount.
Line #
Program output
1
2
3
4
5
6
Enter your ticket code: 0500020180009142019043121Auburn vs Kent State
Game: Auburn vs Kent State Date: 09/14/2019 Time: 18:00
Section: 04 Row: 31 Seat: 21
Price: $50.00 Discount: 20% Cost: $40.00
Prize Number: 6934865
Note that the ticket code below has four leading spaces (be sure you are trimming the input code). It also includes a 70% discount.
Line #
Program output
1
2
3
4
5
6
Enter your ticket code: 0500070180009142019043121Auburn vs Kent State
Game: Auburn vs Kent State Date: 09/14/2019 Time: 18:00
Section: 04 Row: 31 Seat: 21
Price: $50.00 Discount: 70% Cost: $15.00
Prize Number: 2429840
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 extract the substrings, conversion of substrings of digits to numeric values as appropriate, and formatting. These include the String methods trim, and substring, as well as wrapper class methods such Double.parseDouble which can be used to convert a String of digits into a numeric value for price and discount. 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.
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