text: CPSC 124, Winter 1998
  Sample Answers to Lab 2
  This page contains sample answers to some of the exercises from Lab #2 in CPSC 124: Introductory Programming, Winter 1998. See the information page for that course for more information.
  Exercise 1: The exercise was to write a certain program, following certain rules of programming style. The program must have a main comment saying what it does and including the author's name. The declaration of each variable must be commented. Blank lines and indentation must be used to display the structure of the program. Here is a sample solution:public class ConsoleApplication { // This program will let the user enter two real numbers. The user // can then select one of the four operations addition, subtraction, // multiplication, and division to perform on the two numbers. // The program will compute and display the answer. The user then // has the option of continuing with another set of numbers. The program // uses a non-standard console window class for doing input/output. // // Written by: David J. Eck // January 22, 1998 public static void main(String[] args) { Console console = new Console(); // open a console window for I/O double x,y; // The two numbers entered by the user. double ans; // The answer computed by the program. int opCode; // A number input by the user to indicate which // operation to perform on x and y. boolean goAgain = true; // Used to test whether the user wants to // repeat the process with new numbers. // This is set to true so that the while // loop will execute at least once. while (goAgain) { console.putln(); // read user's numbers console.put("Enter your first number: "); x = console.getlnDouble(); console.put("Enter your second number: "); y = console.getlnDouble(); console.putln(); // display menu console.putln("Choose an operation:"); console.putln(" 1. Add"); console.putln(" 2. Subtract"); console.putln(" 3. Multiply"); console.putln(" 4. Divide"); console.put("Enter the number of your choice: "); opCode = console.getlnInt(); // read user's selection if (opCode == 1) // compute the answer ans = x + y; else if (opCode == 2) ans = x - y; else if (opCode == 3) ans = x * y; else ans = x / y; console.putln(); // display the answer console.putln("The answer is " + ans); console.putln(); // see if user wants to continue console.put("Do you want to go again? "); goAgain = console.getlnBoolean(); } // end of while console.putln(); console.putln("Thanks for using this program."); console.putln("I hope I have been of service."); console.close(); } // end of main() } // end of class ConsoleApplication
  Exercise 2: Here is a sample program for the guessing game:public class ConsoleApplication { // In this program, the computer selects a random integer in // the range 0 to 100. The user tries to guess the number. // The computer tells the user whether the answer is too high, // too low, or exactly right. This continues until the user // guesses the number. // // Written by: David J. Eck // January 22, 1998 public static void main(String[] args) { Console console = new Console(); // open a console window for I/O int computersNumber; // Random number in the range 0 to 100, // selected by the computer int usersGuess; // Guessed value input from the user. randomNumber = (int)(Math.random() * 100) + 1; console.putln("I have selected a number between 1 and 100."); console.putln("Try to guess it!"); console.putln(); console.put("What is your first guess? "); usersGuess = console.getlnInt(); while (usersGuess != randomNumber) { if (usersGuess > randomNumber) console.put("Too high! Guess again: "); else console.put("Too low! Guess again: "); usersGuess = console.getlnInt(); } console.putln("You got it!"); console.putln("Thanks for playing."); console.close(); } // end of main() } // end of class ConsoleApplication
  Exercise 3: was canceled.
  Exercise 4: The assignment is to develop the guessing game program from exercise 2, using pseudocode and stepwise refinement. The development process should have at least five stages, and there should be explanatory comments between the stages. Here is a sample answer:
  A very basic outline of what the computer has to do in this program is:Select a random number. Let the user try to guess it.
  However, the user gets to keeps guessing until the correct number is selected, so we can expand the second step:Select a random number. repeat get and process the user's guess as long as the user's guess is incorrect
  Now, in the step inside the while loop, the computer has to read the user's guess and give the user some feedback:Select a random number repeat get the user's guess tell the user if it is too high, too low, or correct as long as the user's guess is incorrect
  The first step in the loop involves asking the user a question and getting a response. The second uses an if statement to distinguish among the three possible cases:Select a random number repeat ask the user to enter a number read the users response if the answer is too big Tell the user "Too high" else if the answer is too small Tell the user "Too low" else Tell the user "Correct" as long as the user's guess is incorrect
  As the final step before the final Java code, give names to the variables and fix up the syntax to make it look more like Java. Also, add some extra ouput to tell the user what is going on.randomNumber = random number between 1 and 100 Tell the user about the game do Ask the user to enter a number. usersGuess = the user's response if usersGuess > randomNumber Tell user "Too high" else if usersGuess < randomNumber Tell user "Too low" else Tell user "Correct" while usersGuess != randomNumber
  And here it is as a Java program segment. (Note it's not quite the same program as the one I gave above as an answer to exercise 2.)int usersGuess; int randomNumber = (int)(100 * Math.random()) + 1; console.putln("I've chosen a number between 1 and 100"); console.putln("Try to guess it."); do { console.putln(); console.put("Enter your guess: "); usersGuess = console.getlnInt(); if (usersGuess > randomNumber) console.putln("Too high! Try again."); else if (usersGuess < randomNumber) console.putln("Too low! Try again."); else console.putln("You got it!"); } while (usersGuess != randomNumber); console.putln(); console.putln("Thanks for playing!);
  David Eck, 22 January 1998