[SOLVED] COMP1210 Project 3: Using Java API Classes

35.00 $

Category:

Description

Rate this product

Deliverables

Your project files should be submitted to Web-CAT by the due date and time specified.  In order to avoid a late penalty for the project, you must submit your completed code files to Web-CAT by 11:59

p.m. on the due date. You may submit your project up to 24 hours after the due date, but there is a late penalty of 15 points.  No projects will be accepted after the one-day late period. If you are unable to submit via Web-CAT, you should e-mail your project Java files in a zip file to your TA before the deadline.

 

Files to submit to Web-CAT:

  • java
  • 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 ticket to a destination in space and then interpret and print the formatted ticket information.

 

  • ExpressionEvaluator.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 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 ExpressionEvaluator program are shown below.

 

Line number Program output
1

2

3

4

5

Enter a value for x: 1

Result: 0.2

# digits to left of decimal point: 1

# digits to right of decimal point: 1 Formatted Result: 0.2

 

 

Line number Program output
1

2

3

4

5

Enter a value for x: -3.9

Result: 14264.157114937527

# digits to left of decimal point: 5

# digits to right of decimal point: 12 Formatted Result: 14,264.15711

 

Line number Program output
1

2

3

4

5

Enter a value for x: 10.5

Result: 9651967.171179585

# digits to left of decimal point: 7

# digits to right of decimal point: 9 Formatted Result: 9,651,967.17118

 

Line number Program output
1

2

3

4

5

Enter a value for x: 12345.6789

Result: 8.851764802663216E26

# digits to left of decimal point: 1

# digits to right of decimal point: 18

Formatted Result: 885,176,480,266,321,600,000,000,000.0

 

When the digits to the right of the decimal in the unformatted result end with E followed by one or more digits (e.g., E26 indicates an exponent of 26), the ‘E’ should be included in the count of the digits 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.

  • SpaceTicket.java

 

Requirements: The purpose of this program is to 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 type char]

price  [12511500 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
1

2

3

4

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

Line # Program output
1

2

3

4

5

6

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   Category: x   Cost: $125,795.00 Prize Number: 174889

 

 

Line # Program output
1

2

3

4

5

6

Enter ticket code: 12579500s15300701209817DSpaceX-001 Earth to Mars

Space Ticket: SpaceX-001 Earth to Mars

Date: 07/01/2098   Time: 15:30   Seat: 17D

Price: $125,795.00   Category: s   Cost: $94,346.25 Prize Number: 004907

 

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

 

Line # Program output
1

2

3

4

5

6

Enter ticket code:      12579500c15300701209817DSpaceX-001 Earth to Mars

Space Ticket: SpaceX-001 Earth to Mars

Date: 07/01/2098   Time: 15:30   Seat: 17D

Price: $125,795.00   Category: c   Cost: $81,766.75 Prize Number: 887295

 

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 

 

 

Hints

SpaceTicket class

 

  1. The ticket code should be read in as a single string, after which the 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).

 

  1. 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.

 

  1. 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.