CSE 201 - Lab 4 - Flow of Control


Note: This lab assumes that you have completed lab 1, and that you know how to login to your account, and how to open, edit, save your files, as well as compile and run your programs. If you have forgotten any of these techniques, refer back to the lab 1 handout. Be sure to follow the directions very carefully. If you have any problems or questions, be sure to ask your instructor as soon as possible. Points will be deducted if the submitted programs do not have the appropriate comments included.


Table of Contents


Objective

The objective for this lab is to write a Java program to practice the use of Java control structures. Note that you can get started with this assignment as soon as you know what a while loop is.


Overview

This lab assignment has two parts. For the first part, you will be writing a Java program from scratch. The program will allow the user to play a single guess-a-number game. For the second part, you will extend the program you wrote in the first part so that the user can play multiple games.


Materials Provided

For this lab no other files are necessary.


Part 1 - Set up

Start Eclipse, create a new project, Lab4, and create a new Lab4Part1 class (the corresponding file name will be Lab4Part1.java) in the project. Complete the Lab4Part1 program according to the description below. If you don't remember how to create a project or a class in Eclipse, see Lab 1 for instructions.


Part 1 - Description

Here is a sample interaction between a user and the program you will write in the Lab4Part1 class (user inputs are in bold):
    I am thinking of a number between 1 and 100. Try to guess it.

    What's your guess? 50
    50 is too small
    What's your guess? 75
    75 is too big
    What's your guess? 62
    62 is too small
    What's your guess? 69
    69 is too big
    What's your guess? 65
    65 is too big
    What's your guess? 63
    63 is too small
    What's your guess? 64
    You've got it in 7 guesses. That was ok!

    Goodbye!

The program performs the following actions:

  1. Generate a secret random number between 1 and 100 (see below how to do that), and print a message to communicate that to the user;
  2. 
    
  3. Repeatedly ask the user for a guess, and tell the user whether the guess is too big or too small compared to the secret number;
  4. 
    
  5. When the user finally guesses the correct number, print a message stating how many guesses were needed, and a message assessing the user's performance according to the following table:
  6. 
    
    Number of guessesMessage
    1That was lucky!
    2-4That was amazing!
    5-6That was really good!
    7That was ok!
    8-9That was pretty bad!
    10 or moreThis is not your game!

Generating random numbers

To generate a random number between 1 and 100 and assign the value to a variable x, you can use the following Java statement:
    int x = (int)(100 * Math.random ()) + 1;
Math.random is a function that returns a pseudo-random double number greater than or equal to 0.0 and less than 1.0. The (int) notation is called a cast and truncates to an integer the value of the expression following it.


Part 2 - Set up

Once you are satisfied that Part 1 is done and working, make a copy of the Lab4Part1 class in Eclipse and call it Lab4Part2 (and the corresponding file name will be Lab4Part2.java). The easiest way to do this in Eclipse is to select Lab4Part1.java in the Lab4 project in the Package Explorer panel at the left; then select Copy from the Edit menu, and finally select Paste from the Edit menu. A dialog box will appear and will give you a chance to rename the copy to Lab4Part2. Click OK.


Part 2 - Description

Here is a sample interaction between a user and the program you will write in the Lab4Part2 class (user inputs are in bold):
    Do you want to play a game? y

    I am thinking of a number between 1 and 100. Try to guess it.

    What's your guess? 50
    50 is too small
    What's your guess? 75
    75 is too big
    What's your guess? 62
    62 is too small
    What's your guess? 69
    69 is too big
    What's your guess? 65
    65 is too big
    What's your guess? 63
    63 is too small
    What's your guess? 64
    You've got it in 7 guesses. That was ok!

    Do you want to play another game? y

    I am thinking of a number between 1 and 100. Try to guess it.

    What's your guess? 50
    50 is too big
    What's your guess? 25
    25 is too small
    What's your guess? 37
    37 is too small
    What's your guess? 43
    You've got it in 4 guesses. That was amazing!

    Do you want to play another game? n
    Goodbye!

The difference between the programs in Part 1 and Part 2 is that now the program repeatedly asks the user if he/she wants to play a game; if the user enters anything but 'y', the program terminates; otherwise the program allows the user to play a game before asking again if the user wants to play another game.


Lab Submission

Make sure your programs compile and run correctly before submitting. To submit, use the Submit tool available on the Widows desktop to submit the Lab4Part1.java and Lab4Part2.java files from the Lab4 project (the location of the project in the file system will be z:\eclipse\workspace\Lab4). Make sure that you select lab4 from the list of Assignments. If you don't remember how to use the Submit tool, see Lab 1 for instructions.