CS 201 - Computer Science I - Spring 2009
Lab 8 - Selection and Testing
Loyola College >
Department of Computer Science >
Dr. James Glenn >
CS 201 >
Labs >
Lab 8
Due
Monday, March 23rd at 11:59pm.
Labs submitted one day late will be assessed a
20% penalty. Labs will not be accepted more than one day late.
Objectives
- to use ifs for selection of which statements to execute
- to perform white-box testing
- to perform black-box testing
Reading
Anderson and Franceschi, Chapter 5
Assignment
Fix the line equation calculator so that it is robust (does not produce
any incorrect or even strange results regardless of the values the user
enters) and so that its output conforms to mathematical conventions:
write "x" or "-x" instead of "1.0x" or "-1.0x"; omit the x term
for horizontal lines; omit the y-intercept if it is zero (unless the
line is horizontal); and if the intercept is negative write
"2.0x - 1.0" instead of "2.0x + -1.0".
Your program should not crash for any set of values the user enters, nor
should it produce strange results like "y = Infinityx + Infinity",
except that you need not worry about overflow, truncation,
or the number of significant digits displayed (but see the
extra credit below).
Exercises
- Draw a flow chart illustrating your program's execution paths
(it may be useful to do this before you write your code).
- For each possible execution path in your program, come up with an
input that follows that path. Run your program on these inputs
to make sure your program operates correctly. (This is white-box
testing.)
- Come up with any additional inputs you would use to test someone
else's program. Think about what kinds of conditions other people
might use, and come up with inputs that test those conditions.
(This is black-box testing.)
Files
Start with LineCalculator.java, which
is the code from Lab 3 corrected
and modified to accept user input.
Suggestions
You can use a test-first approach to design your code. First, write down
all of the different output formats your program should be able to
generate. To come up with all of the output formats, think about when the
code
System.out.println("y = " + slope + "x + " + intercept);
would be wrong or would not look right.
Then come up with a condition that determines when a subset
of those formats should be used. Repeat the process on the resulting subsets
until each sequence of conditions distinguishes a single output format.
For example,
For example, if we call the 6 formats A, B, C, D, E, and F,
then you might come up with a condition that says "if (condition) then the
output format is either A or E; otherwise is is B, C, D, or F". You then
need another condition to distinguish when you should use format A versus
when you should use format E, and more conditions to narrow your selection
among B, C, D, and F.
In order to reduce the number of cases you have,
you might want to consider outputting the slope
and outputting the intercept to be two separate steps (output the
slope with System.out.print and then output the intercept
with System.out.println). For example, for a line that should
be displayed as
y = -x + 5.0
and one that should be displayed as
y = 2.0x + 5.0
you will have to execute different blocks to output the slope,
but you can use the same block of code to output the intercept.
Extra Credit
- Add code that will output a warning message if any of the input values
or the calculated values are infinite (see the appropriate class
(static) method in the Double class).
- Add code that will output a warning message if truncation causes
the results to be too far off (and come up with a reasonable
definition of "too far off").
- Use a DecimalFormat object to display doubles without
digits after the decimal point where appropriate (that is,
display "2x" instead of "2.0x").
Submissions
Submit the source code (.java file) for your edited
LineCalculator class. Attach a text file containing your
white-box and black-box tests (or include them in the body of your e-mail).
Submit your flow chart on paper.