X
    Categories: Java

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 highNum) {
highNum = num2;

} else {
if (num2 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:

coltrinit:
Related Post