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

After re-reading the instructions for these sets of problems, I found that I should replace multiple statements of code used for one action with a method().
Additionally, the best practice is comment your preconditions and post conditions before every method in your program.
This practice helps in writing the code without getting off track.
It took a little while to learn that if I initialize an “Int” variable in the class before my methods, and if I use “Int” with the same variable again inside the method, then I am re-declaring the variable as a local variable, and the class variable will not retain it’s values throughout the different methods. If you want a variable’s values to be available to that variable throughout your methods, only declare the type one time in the class.

A few “firsts” for me in this app was 1. The use of the break; command to exit a loop. and 2. The use of if(){} else {if{}} and 3. Methods() to replace multiple statements.

So here is the much-cleaned up and easier to read/use (and working) solution to Problem 3:


/*
* 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 {

int num1; // declare num1 as int
int num2; // declare num2 as int
int getNum = 1; // declare getNum as int and give initial value of 1. Later,
// if getNum = 0, then it will break the getuntilzero loop
int highNum; // declare the largest number as int
int lowNum; // declare the lowest number as int

public void run() {

// Print game description
println("This program finds the largest and smallest numbers.");

/*
* Precondition is high/low variables are initialized as int.Post
* condition is if getNum1 = 0 then message "Please try again.". If
* number is not = 0, set both highNum and lowNum to getNum1
*/
getNum1();

/*
* Precondition is highNum == lowNum. Get a new number, num2, and if
* num2 = 0 then print "Highest: highNum" and "Lowest: lowNum" and end
* the method/program. If num2 is not = 0 determine if num2 is greater
* than highNum or less than lowNum. If num2 > highNum, highNum is now
* equal to num2. If num2 < lowNum, then lowNum is now equal num2. If * num2 is equal to highNum and num2 is equal to lowNum, then getNum2 * again. Postcondition is num2 is now set to set to either lowNum or * highNum. lowNum is the lower number. highNum is the higher number. * lowNum is not equal to highNum. */ getNum2(); /* * Precondition is lowNum is not equal to highNum. get a new number * getNum. while getNum is not equal to 0, if getNum < lowNum, then * lowNum is now equal to getNum. if getNum is higher than highNum, then * highNum is now equal to highNum. get a new number again. if getNum is * equal to zero, display the highest and lowest numbers and end the * method/program Postcondition is largest and smallest numbers are * displayed. */ getUntilZero(); } public void getNum1() { num1 = readInt("?:"); if (num1 == 0) { println("Please try again without 0."); getNum1(); } highNum = num1; lowNum = num1; } public void getNum2() { if ((highNum != 0) && (lowNum != 0)) { num2 = readInt("?:"); if (num2 == 0) { println("Biggest number:" + highNum); println("Smallest number:" + lowNum); getUntilZero(); } else { if (num2 > highNum) {
highNum = num2;

} else {
if (num2 < lowNum) { lowNum = num2; } else { if ((num2 == highNum) && (num2 == lowNum)) getNum2(); } } } } } public void getUntilZero() { while ((getNum != 0) && (highNum != lowNum)) { getNum = readInt("?:"); if (getNum == 0) { displayHighLow(); endApp(); break; } else { if (getNum < lowNum) { lowNum = getNum; } else { if (getNum > highNum)
highNum = getNum;
}
}
}
}

// precondition is sentinel has been entered
// postcondition is smallest and largest numbers are displayed
private void displayHighLow() {
println("Smallest:" + lowNum);
println("Largest:" + highNum);
}

// :-)
private void endApp() {
println("Thanks for playing");

}
}

And here is the output:
Working