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

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

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

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.
Valid HTML 4.01 Transitional