Description
You will write two programs this week. The first will compute the value generated by a
specified formula and the second will calculate the total for a food order from a set menu.
• PolyFormula.java
Requirements: Calculate the following expression for any value of the variable x, and save the
result 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
somewhat large expression, or you may break the formula into appropriate multiple assignment
statements.
6 5 4 3 2 2x − 4x + (3x − 5x )
Next, determine the number of digits to the left and to the right of the decimal point in the result.
[Hint: You can convert the double variable into a String variable using the static method
Double.toString(result). Then, on this String variable use the indexOf() method from the String
class to find the position of the period and the length() method to find the length. 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 printed using the class java.text.DecimalFormat so that to the right of
the decimal there are at most three 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).
Design: Several examples of input/output for the PolyFormula program are shown below.
Line number Program output
1
2
3
4
5
Enter a value for x: 2
Result: 8.0
# digits to left of decimal point: 1
# digits to right of decimal point: 1
Formatted Result: 8.0
Line number Program output
1
2
3
4
5
Enter a value for x: -5.6
Result: 3839.346077995761
# digits to left of decimal point: 4
# digits to right of decimal point: 12
Formatted Result: 3,839.346
Line number Program output
1
2
3
4
5
Enter a value for x: 30
Result: 2295296.4514415124
# digits to left of decimal point: 7
# digits to right of decimal point: 10
Formatted Result: 2,295,296.451
Line number Program output
1
2
3
4
5
Enter a value for x: 1000
Result: 2.995000333222018E12
# digits to left of decimal point: 1
# digits to right of decimal point: 18
Formatted Result: 2,995,000,333,222.018
Note that in the example above the digits to the right of the decimal in the unformatted result
are followed by E12 (i.e., an exponent of 12). For this program, the “E12” 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 input values once they are 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.
• TakeOutOrder.java
Requirements: The purpose of this program is to accept coded information as input that includes
the number of adult meals, price of adult meal, number of child meals, price of child meal, and
name of the customer. The program should then print the user’s order information including the
total, as well as a “lucky number” between 1 and 9999 inclusive. The coded information is
formatted as follows:
091295040395Jo Jones
first and last name
price per child meal
number of child meals
price per adult meal
number of adult meals
Whitespace before or after the coded information should be disregarded (e.g., if the user enters
spaces or tabs before the coded information then the spaces should be disregarded). Your
program will need to print the customer’s name, number of adult meals, price per adult meal,
number of child meals, price per child meal, total, and a random “lucky” number in the range 1 to
9999. If the user enters a code that does not have at least 12 characters, then an error message
should be printed. [For this assignment, the first 12 characters, if present, will need to be digits;
otherwise a runtime exception will occur.]
Design: Several examples of input/output for the program are shown below.
Line number Program output
1
2 (blank)
3
4
Enter your order code: 123456789
Invalid Order Code.
Order code must have at least 12 characters.
Line number Program output
1
2
3
4
5
Name: Jo Jones
Adult meals: 2 at $10.50
Child meals: 4 at $5.25
Total: $42.00
Lucky Number: 3272
Line number Program output
1
2
3
4
5
Name: Pat Smith
Adult meals: 24 at $14.95
Child meals: 0 at $0.00
Total: $358.80
Lucky Number: 6937
Line number Program output
1
2
3
4
5
Name: Samantha’s Party
Adult meals: 2 at $15.00
Child meals: 20 at $5.00
Total: $130.00
Lucky Number: 1032
Line number Program output
1
2
3
4
5
Name: Sam’s Big Party
Adult meals: 80 at $25.00
Child meals: 40 at $10.00
Total: $2,400.00
Lucky Number: 9733
Code: In order to receive full credit for this assignment, you must use the appropriate Java API
classes and methods to do the extraction of the substrings, calculation, and formatting. The dollar
amounts should be formatted so that both small and large amounts are displayed properly, and the
lucky number should be formatted so that three digits are displayed including leading zeros if
needed. It is recommended as a practice that you do 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. You should use a calculator or jGRASP interactions to check your answers.
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 have passed
Checkstyle.
If you do not submit both programs at the same time, the submission will receive zero
points for correctness because Web-CAT will attempt to compile both programs with the
JUnit test cases but will fail. Activity 1 describes how to create a jGRASP project containing
both of your files.