Description
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. 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:
• TwoVariableExpression.java
• TimeInSeconds.java
Specifications
Overview: You will write two programs this week. The first will find the result of a specified
expression after reading input values for x and y, and the other will determine the number of days,
hours, minutes, and seconds for an input value representing a raw time in seconds.
• TwoVariableExpression.java
Requirements: A program is needed that inputs values of type double for x and y and solves for
the result of the indicated expression when xy is not equal to zero. If xy is equal to zero, then the
result should be reported as undefined rather than infinity (see examples below).
Design: The result should be calculated as follows (except for the special case):
������ = (4.5� + 12.5) (3� − 9.0)
��
for �� ≠ 0
Note: if xy is 0, then result is undefined.
Four examples of program output for the indicated input values are shown below. Note that lines
2 and 3 for the input values begin with tab which is equivalent to three spaces in jGRASP (i.e.,
your program should use the \t escape sequence for a tab).
Example #1
Line # Program output
1
2
3
4
5
—-jGRASP exec: java TwoVariableExpression
result = (4.5x + 12.5) (3y – 9.0) / xy
x = 1.0
y = 1.0
result = -102.0
—-jGRASP: operation complete.
Project: Variables and Expressions Page 2 of 3
Page 2 of 3
Example #2
Line # Program output
1
2
3
4
5
—-jGRASP exec: java TwoVariableExpression
result = (4.5x + 12.5) (3y – 9.0) / xy
x = 0.0
y = 67.0
result is “undefined”
—-jGRASP: operation complete.
Example #3
Line # Program output
1
2
3
4
5
—-jGRASP exec: java TwoVariableExpression
result = (4.5x + 12.5) (3y – 9.0) / xy
x = 17
y = 0
result is “undefined”
—-jGRASP: operation complete.
Example #4
Line # Program output
1
2
3
4
5
—-jGRASP exec: java TwoVariableExpression
result = (4.5x + 12.5) (3y – 9.0) / xy
x = 62.4
y = 43.8
result = 13.135142255005269
—-jGRASP: operation complete.
Code: Your numeric variables should be of type double. Use an if-else statement to determine if
the divisor in the expressions is zero. Note that in the example output above, one of the variables
is zero in Example #2 and #3, which means the divisor, xy, is zero, and thus, result is undefined in
each case. Hint: your if statement should check to see if (x * y == 0).
Test: You are responsible for testing your program, and it is important to not rely only on the
examples above. Remember that the input values are doubles, so be sure to test both positive and
negative values (with and without a decimal point) for x and y. You should use a calculator or
jGRASP interactions to check your answers.
• TimeInSeconds.java
Requirements: A digital timer manufacturer would like a program that accepts a raw time
measurement in seconds (of type int) and then then displays the time as a combination of days,
hours, minutes, and seconds. When a negative raw time measurement is entered, an appropriate
message is printed as shown in the first of the two examples below.
Project: Variables and Expressions Page 3 of 3
Page 3 of 3
Design: The digital timer manufacturer would like the output to look as shown below when the
test values -321 is entered as the raw time for one run and 654321 is entered for another run. Note
that in the second run, the program converts all the seconds to days first, and then converts the
remainder to hours, then converts the remainder of that to minutes, and so on.
Line # Program output
1
2
3
—-jGRASP exec: java TimeInSeconds
Enter the raw time in seconds: -321
*** Time must be non-negative. ***
—-jGRASP: operation complete.
Line # Program output
1
2
3
4
5
6
7
8
9
10
—-jGRASP exec: java TimeInSeconds
Enter the raw time in seconds: 654321
Time by combined days, hours, minutes, seconds:
days: 7
hours: 13
minutes: 45
seconds: 21
654321 seconds = 7 days, 13 hours, 45 minutes, 21 seconds
—-jGRASP: operation complete.
Your program must follow the above format with respect to the output. In the run with
amount 1234567, note that lines 4 through 7 begin with tab (i.e., your output should use the
escape sequence for a tab).
Code: Your numeric variables should be of type int. You should calculate the number of days,
hours, minutes, and seconds and store each of the values in separate variables of type int. Create
a Scanner object on System.in to read in the value for the raw time using the nextInt() method. It
is recommended as a practice that you do not modify input values once they are read in and
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 integer less than or equal to 2,147,483,647
(the maximum value for a 32 bit int) and greater than or equal to -2,147,483,648 (the minimum value for
a 32 bit int).
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, the submission will receive
zero points for correctness. Activity 1 describes how to create a jGRASP project containing
both of your files, which is recommended.