Lab 06 Solution
Version 1

 


Problem 01

  Though for loops are the preferred way to write loops when you know in advance how many times you want to loop, such loops can also be written with while loops. Write a program using a while loop which will print out the numbers from 1 to 50 and, when done, print out the sum of those numbers as well.

Click Here To Run This Program On Its Own Page
// Program: LAB06PR01
// Author:  Joshua S. Hodas
// Date:    October 22, 1996
// Purpose: Prints the numbers from 1 to 50 and their sum
//          using a while loop.
 
import HMC.HMCSupport;
 
class LAB06PR01 {
 
  public static void main(String args[]) {
 
    int sum = 0, count;
 
    HMCSupport.out.println("This program prints the numbers 1-50"
                         + " and their sum.");
    HMCSupport.out.println();
 
    count = 1;
    while ( count <= 50 ) {
 
      HMCSupport.out.println(count);
      sum = sum + count;
      count++;
    }
 
    HMCSupport.out.println();
    HMCSupport.out.println("The sum is " + sum);
  }
 
}


Problem 02

  Write a program which generates a random number between 1 and 20 and asks the user to guess it. If they guess right, they are complimented and the program ends. If they guess wrong, tell them whether they are too high or too low and go back and get another guess.

Click Here To Run This Program On Its Own Page
// Program: LAB06PR02
// Author:  Joshua S. Hodas
// Date:    October 22, 1998
// Purpose: Prompt for number, say if it's od or even (reprompt if not positive)
 
import HMC.HMCSupport;
 
class LAB06PR02 {
 
  public static void main(String args[]) {
 
    int input;
 
    HMCSupport.out.print("Please enter a positive number: ");
    input = HMCSupport.in.nextInt();
 
    while ( input <= 0 ) {
 
      HMCSupport.out.println("That's not positive.");
      HMCSupport.out.print("Please enter a positive number: ");
      input = HMCSupport.in.nextInt();
    }
 
    if (input % 2 == 0) {
 
      HMCSupport.out.println("That number is even.");
    }
    else {
 
      HMCSupport.out.println("That number is odd.");
    }
  }
 
}


Problem 03

  Write a program which generates a random number between 1 and 20 and asks the user to guess it. If they guess right, they are complimented and the program ends. If they guess wrong, tell them whether they are too high or too low and go back and get another guess.

Click Here To Run This Program On Its Own Page
// Program: LAB06PR03
// Author:  Joshua S. Hodas
// Date:    October 22, 1996
// Purpose: Plays a guessing game with the user.
 
import HMC.HMCSupport;
 
class LAB06PR03 {
 
  public static void main(String args[]) {
 
    int myValue, theirGuess;
 
    HMCSupport.out.println("This program picks a number from 1 to"
                           + " 20 and challenges you to guess it.");
    HMCSupport.out.println();
 
    myValue = (int) Math.ceil(20 * Math.random());
 
    do {
 
      HMCSupport.out.print("Please enter a guess: ");
      theirGuess = HMCSupport.in.nextInt();
 
      if (theirGuess < myValue) {
 
        HMCSupport.out.println("That was too low.");
      }
      else if (theirGuess > myValue) {
 
        HMCSupport.out.println("That was too high.");
      }
    } while (theirGuess != myValue);
 
    HMCSupport.out.println("That's right!!!!");
 
  }
 
}


Problem 04

  Write a program based on the last one, but in which the user is limited to at most 5 guesses. (It may help you to first modify the program so that it simply keeps track of how many guesses they have made and tells them at each guess what guess number they are on.)

Click Here To Run This Program On Its Own Page
// Program: LAB06PR04
// Author:  Joshua S. Hodas
// Date:    October 22, 1998
// Purpose: Plays a guessing game with the user,
//          limiting them to 5 guesses.
 
import HMC.HMCSupport;
 
class LAB06PR04 {
 
    public static void main(String args[]) {
 
        int myValue, theirGuess, guessNum = 0;
 
        HMCSupport.out.println("This program picks a number from 1 to"
                               + " 20 and challenges you to guess it.");
        HMCSupport.out.println("You only get five guesses.");
        HMCSupport.out.println();
 
        myValue = (int) Math.ceil(20 * Math.random());
 
        do {
 
            guessNum++;
            HMCSupport.out.print("Please enter guess number " +
                                 guessNum + ": ");
            theirGuess = HMCSupport.in.nextInt();
 
            if (theirGuess < myValue) {
 
                HMCSupport.out.println("That was too low.");
            }
            else if (theirGuess > myValue) {
 
                HMCSupport.out.println("That was too high.");
            }
        } while (theirGuess != myValue && guessNum < 5);
 
        if (theirGuess == myValue) {
 
            HMCSupport.out.println("That's right!!!!");
        }
        else {
 
            HMCSupport.out.println("Sorry! You're out of guesses.");
        }
    }
 
}


Problem 05

  Write a program that plays the guessing side of the guessing game. Tell the user to think of a number from one to twenty. After the program guesses a random number in that range, ask the user if it is correct. Allow for three responses: "yes","higher", or "lower". If you get one of the latter two responses, have the program guess again, but modifying the range of the guess appropriately. Repeat until the program guesses correctly, or loses for using up five guesses.

Click Here To Run This Program On Its Own Page
// Program: LAB06PR05
// Author:  Joshua S. Hodas
// Date:    October 22, 1998
// Purpose: Plays a guessing game with the user,
//          with the computer making the guesses.
 
import HMC.HMCSupport;
 
class LAB06PR05 {
 
    public static void main(String args[]) {
 
        int myGuess, guessNum = 0, low, high;
        String howDidIDo;
 
        HMCSupport.out.println("This program guesses a number from "
                               + " 1 to 20. Please think of one.");
        HMCSupport.out.println();
 
        low = 1;
        high = 20;
 
        do {
 
            guessNum++;
 
            // Generate a guess between low and high inclusive:
            myGuess = (int) Math.floor((high - low + 1) * Math.random())
                      + low;
 
            HMCSupport.out.println("My guess #" + guessNum + " is: "
                                   + myGuess);
 
            HMCSupport.out.print("Was I right? ");
            howDidIDo = HMCSupport.in.nextWord();
 
            if (howDidIDo.equals("higher")) {
 
                low = myGuess+1;
            }
            else if (howDidIDo.equals("lower")) {
 
                high = myGuess-1;
            }
        } while (!howDidIDo.equals("yes") && guessNum < 5);
 
        if (howDidIDo.equals("yes")) {
 
            HMCSupport.out.println("Cool!");
        }
        else {
 
            HMCSupport.out.println("Drats!");
        }
    }
 
}

Last modified August 28 for Fall 99 cs5 by fleck@cs.hmc.edu


This page copyright ©1998 by Joshua S. Hodas. It was built with Frontier on a Macintosh . Last rebuilt on Thu, Oct 22, 1998 at 10:46:38 PM.
http://www.cs.hmc.edu/~hodas/courses/cs5/week_06/labsolutions.html