COMP 1210 Project: Using Java API Classes solved

$35.00

Category: You will Instantly receive a download link for .zip solution file upon Payment || To Order Original Work Click Custom Order?

Description

5/5 - (5 votes)

Specifications
Overview: You will write two programs this week. The first will solve for the result of a specified
expression, and the second will read data for a ticket to the Moon and then interpret and print the
formatted ticket information.
• UsingMathMethods.java
Requirements: Solve for the result of 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.
������ = ��� + ,|��� + ��� + �� + �|
(� + |�|)
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
Project: Using Java API Classes Page 2 of 5
Page 2 of 5
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 UsingMathMethods program are shown below.
Line # Program output
1
2
3
4
5
6
—-jGRASP exec: java UsingMathMethods
Enter a value for x: 0
Result: 0.5
# of characters to left of decimal point: 1
# of characters to right of decimal point: 1
Formatted Result: 0.5
—-jGRASP: operation complete.
Line # Program output
1
2
3
4
5
6
—-jGRASP exec: java UsingMathMethods
Enter a value for x: 1.0
Result: 2.4
# of characters to left of decimal point: 1
# of characters to right of decimal point: 1
Formatted Result: 2.4
—-jGRASP: operation complete.
Line # Program output
1
2
3
4
5
6
—-jGRASP exec: java UsingMathMethods
Enter a value for x: 1234.5
Result: 1.500231759672595E10
# of characters to left of decimal point: 1
# of characters to right of decimal point: 18
Formatted Result: 15,002,317,596.726
—-jGRASP: operation complete.
Line # Program output
1
2
3
4
5
6
—-jGRASP exec: java UsingMathMethods
Enter a value for x: -987654321.0
Result: 7.707346598333211E27
# of characters to left of decimal point: 1
# of characters to right of decimal point: 18
Formatted Result: 7,707,346,598,333,211,000,000,000,000.0
—-jGRASP: operation complete.
When the characters to the right of the decimal in the unformatted result end with E followed
by one or more digits (e.g., E27 indicates an exponent of 27), the ‘E’ should be included in
the count of the characters to the right of the decimal point.
Project: Using Java API Classes Page 3 of 5
Page 3 of 5
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.
• MoonTrip.java
Requirements: The purpose of this program is to accept coded ticket information as input that
includes the date, time, price, discount, seat, followed by the description of the ticket. Note that
the nine 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 information should contain a random “prize number” between 1
and 99999 inclusive that should always be printed as five digits (e.g., 1 should be printed as
00001). The coded input is formatted as follows:
0915203118303250000005013CBlue Origin 111 Earth to Moon
date time price* seat ticket description
discount** (goes through last character in the code)
*price [325000000 has an implied decimal point for 3250000.00]
**discount [50 has an implied decimal point for 0.50 (or 50%)]
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 ticket description, date, time, seat number, price, discount, and cost, and a
random prize number in the range 1 to 99999 as shown in the examples below. If the user enters a
code that does not have at least 27 characters, then an error message should be printed. [The 27th
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
1
2
3
4
5
—-jGRASP exec: java MoonTrip
Enter ticket code: Let’s go to the Moon!
*** Invalid Ticket Code ***
Ticket code must have at least 27 characters.
—-jGRASP: operation complete.
Project: Using Java API Classes Page 4 of 5
Page 4 of 5
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., ticket, 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
—-jGRASP exec: java MoonTrip
Enter ticket code: 0915203118303250000005013CBlue Origin 111 Earth to Moon
Ticket: Blue Origin 111 Earth to Moon Date: 09/15/2031 Time: 18:30
Seat: 13C Price: $3,250,000.00 Discount: 50% Cost: $1,625,000.00
Prize Number: 83898
—-jGRASP: operation complete.
Note that the ticket code entered below includes a 75% discount.
Line # Program output
1
2
3
4
5
6
—-jGRASP exec: java MoonTrip
Enter ticket code: 0915203118303250000007513CBlue Origin 111 Earth to Moon
Ticket: Blue Origin 111 Earth to Moon Date: 09/15/2031 Time: 18:30
Seat: 13C Price: $3,250,000.00 Discount: 75% Cost: $812,500.00
Prize Number: 31265
—-jGRASP: operation complete.
Note that the ticket code below has 5 leading spaces (be sure you are trimming the input code). It
also includes a 75% discount.
Line # Program output
1
2
3
4
5
6
—-jGRASP exec: java MoonTrip
Enter ticket code: 0915203118303250000007513CBlue Origin 111 Earth to Moon
Ticket: Blue Origin 111 Earth to Moon Date: 09/15/2031 Time: 18:30
Seat: 13C Price: $3,250,000.00 Discount: 75% Cost: $812,500.00
Prize Number: 09803
—-jGRASP: operation complete.
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 five 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.
Project: Using Java API Classes Page 5 of 5
Page 5 of 5
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 code each time you run your program.
Hints:
1. The ticket code should be read in all at once using the Scanner’s nextLine method and stored
in a variable of type String. Then the individual values should be extracted using the
substring method. The String value for price and discount should be converted to type double
(using Double.parseDouble) so that it can be used to calculate cost. When printing the values
for price, discount, cost, and prize number, they should be formatted properly by creating an
appropriate DecimalFormat object (see patterns below) and calling its format method.
To format price and cost, use the pattern “$#,##0.00” when you create your
DecimalFormat object.
To format discount, use the pattern “0%” when you create your DecimalFormat object.
For prize number, use the pattern “00000” when you create your DecimalFormat object.
2. Since all items in the ticket code other than the price and discount will not be used in
arithmetic expressions, they can and should be left as type String.
3. The time and date should have leading zeros as appropriate. Therefore, these can be printed
as String values by concatenating their components with “:” in the time and “/” in the date
as needed.
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.