Stanford cs106a, Assignment 2; Problem 3. – FindRange – Part 1

The instructions are to write a ConsoleProgram that reads in a list of integers, one per line, until a sentinel
value of 0 is entered. When the sentinel is read, your program should display the smallest and largest values in the
list, as illustrated in this sample run:
SampleFindRange

The program should handle the following special cases:
• If the user enters only one value before the sentinel, the program should report
that value as both the largest and smallest.
• If the user enters the sentinel on the very first input line, then no values have been
entered, and your program should display a message to that effect.

I found that programming this assignment got me familiar with using the debugger. I place breakpoints along the lines of code I want to watch. I start the program in debugging mode and then hit the resume (F8) button to run the program until the next break-point, error, or endless loop :-p. I can then watch as variables are assigned values, and at what point a “while” iteration loops. I can see if certain conditions are met and if so, what happens next.

Also, I found that it’s not over til it’s over and the project is not complete until the program has been tested extensively. Just when you think it’s complete and you run a test one more time, the program doesn’t behave as expected. This is just the case below. The program is mostly complete except for when multiple values are supplied; the program loses track of which number is the lowest. But for today I’m happy with the progress I made.

Besides the debugger I learned about the “break;” command, ‘and’ (&&) and ‘or’ (||) operators, and appropriate use of methods(). Although I did not create a method in this program, I understand I can take code that has been repeated somewhere, turn it into a method, and clean up the code in general.

Here’s where I am so far. First the code, then the condition cases fulfillment, a successful run, and then the last problematic condition that still needs to be resolved.


/*
* File: FindRange.java
* Name: Jason Coltrin
* Section Leader: n/a
* --------------------
* This file is the starter file for the FindRange problem.
* write a ConsoleProgram that reads in a list of integers, one per line, until a sentinel value of 0
* is entered. When the sentinel is read, your program should display the smallest and largest values
* in the list.
* The program should handle the following special cases:
* • If the user enters only one value before the sentinel, the program should report
* that value as both the largest and smallest.
* • If the user enters the sentinel on the very first input line, then no values have been
* entered, and your program should display a message to that effect.
*/

import acm.program.*;

public class FindRange extends ConsoleProgram {

public void run() {

println("This program finds the largest and smallest numbers.");

int getNum = readInt("?:"); // gets initial input from user
int sentinel = getNum; // sets first input to value sentinel
int highNum = 0;
int lowNum = 0;
highNum = getNum; // allow the first input highNum to be set as the
// largest number

if ((getNum == 0) && (highNum == 0)) { // check for first input zero
// constraint
println("No values have been entered. Please start over.");

} else { // if not 0, set getNum to be equal to highNum
if (getNum > highNum) {
highNum = getNum;
}
getNum = readInt("?:"); // ask for the 2nd getNum
if (getNum < highNum) { lowNum = getNum; // assign new getNum to lowNum if less than // highNum } if (getNum == 0) { // if second getNum is equal to 0, meet // constraint to set high/low to same number println("Smallest:" + highNum); println("Largest:" + highNum); lowNum = highNum; } } while ((sentinel != 0) && (lowNum != highNum)) { getNum = readInt("?:"); if (getNum == 0) { println("Smallest:" + lowNum); println("Largest:" + highNum); break; } if (getNum > highNum) {
highNum = getNum;

} else {

if (getNum < highNum) { lowNum = getNum; } } } } }
Condition 1 met:
NoValuesException
Condition 2 met:
OneNumberException
Success!...
success
But wait, not so fast...
BugDoh