SE-1011 Software Development 1
Lab 4: Temperature Converter Revisited
Outcomes
- Continue to develop proficiency with the Java language
- Create Java mathematical expressions and
formulas using various primitive datatypes
- Incorporate conditional control
statements, such as if, else if, and else into a Java program to affect
program control flow.
Assignment
In this lab, you will write a program that uses a dialog
box to prompt the user for a temperature in degrees Fahrenheit, computes the
equivalent temperature in degrees Celsius, and outputs the result in a dialog
box, along with an additional note indicating whether the temperature is frigid,
cold, ideal, warm, or hot.
Detailed requirements
Study these requirements before implementing your program.
This assignment requires you to construct a flowchart before you begin coding.
- After prompting the user for input, your program must
detect (using if, else if, and else statements) whether the user:
- entered a value (which may or may not be valid,
but the input String contains at least 1 character)
- pressed OK with no input (the input String
is empty in this case)
- pressed Cancel (the input String is
null, regardless of whether the user actually typed something or
not)
For the last two conditions, your program must
print an error message to the user and terminate (end/exit). You can
explicitly terminate by invoking the exit() method of the
System class.
For the first condition, the user may have entered
an invalid temperature (e.g. "a", or "-10_3" ). Handling this error
condition is beyond what you learned so far in this course, so you don't
have to worry about it for now. It is OK if your program crashes for
this case.
- (This point is repeated from last week) The mathematical formula for converting from degrees F
to degrees C is given by
where Tc is
the computed temperature in degrees Celsius
TF is the input temperature in degrees Fahrenheit
- You must translate the math formula above
into an equivalent Java formulation, using the correct operators for
grouping, multiplication, and division.
- You must create your own Java variables
with meaningful names (and appropriate datatypes) to represent the values
for the temperatures.
- When the user input a valid temperature in F,
your program - in addition to outputting the equivalent temperature in
C -, must (using if, else if, and else statements):
- output the word "frigid" if the temperature is
less than or equal to 0 C.
- output the word "cold" if the temperature is
above 0 but less than 20 C.
- output the word "ideal" if the temperature is
20-25 C.
- output the word "warm" if the temperature is
above 25 but less than 35 C.
- output the word "hot" if the temperature is
above 35 C.
-
Begin by drawing a (neat) flowchart - including
decision conditions - that illustrates the algorithm your program will
implement.
- Every conditional test implemented in Java must be fully
commented to let the reader know what you are doing in the case where the
if condition is satisfied, as well as the case where the else or
else if condition is satisfied instead.
Hints:
Test your program and make certain that it works
correctly, both for valid and invalid inputs. As before, add comments to the end
of your program that describe these tests and their results. Be sure your test
values force your program through every condition that your program is
designed to handle. Think about this carefully - there are quite a few
different conditions.
Remember: While you may
discuss the concepts with your classmates, you must write your own program. You
may not copy or borrow code from your classmates. If you do, you will be
given a grade of 0 and reported.
Lab Submission (due by end of lab)
Submit your assignment following these instructions:
- Hand in your flowchart to your instructor.
- Upload your .java file through
WebCT
(assignment "Lab 4: Revised Temperature Converter").
Be sure to keep copies of all your java files, in case something gets lost.
Your grade will be based on the following criteria:
- Meeting requirements; your program must compile and
run. If you have trouble, you must come to see me. If you don't and you
submit a program that does not compile or produce meaningful results will
receive a grade of no higher than 50, even after reworking your program.
- Technical quality of your program. Technical quality consists of formatting, commenting, and
following coding style guidelines we discussed in lectures.
- Spelling and grammar (Eclipse even has a built-in spell
checker - make use of it!)
- Timeliness of submission as stated in the
course policies.