Lab 4

Purpose

This lab introduces you to conditional statements and loops. These control structures allow for more powerful programs. It is important for you to become comfortable with each type of conditional statement and loop, as each has its own use.

Much of this lab can be done without loops and conditional statements. However, you should use loops as often as possible. They will make your code easier to read and debug and will prepare you for future assignments.

For extra credit, you will have the chance to create your own first java GUI. These tools will be helpful throughout the semester.

Key Reading

  • 3.9-3.11
  • 4.6-5.10

Introduction

Setting

BYU Credit Union is one of the fastest growing businesses in the Northwest. You have an interview for a summer internship. A few days before the interview, a human resource representative from the company calls to give you a brain teaser to test your ability to think creatively. Your success in solving this problem will have a significant impact on the outcome of your interview:

Number Prediction

Design, implement, and test a Java program which prompts the user to select (but not disclose) a number between 0 and 31, and then poses exactly five questions to the user to determine the number the user has selected. Each of the five questions is posed by presenting four lines containing a program-generated group of sixteen numbers, asking if the user's selected number is in the group. The program then determines and announces the user's number. Lastly, the program asks the user if he or she wants to do the activity again. If so, the process is repeated. If not, the program terminates.

Requirements

Part 1 - Five Sets of Fun (8 points if generated using loops, 4 if hard-coded)

  • Display the correct five sets of sixteen numbers (one at a time)
  • Display the numbers in rows of four
  • See class use below

Part 2 - It's Magic (7 points)

  • Correctly determine the user-selected number
  • See class use below

Part 3 - Mix It Up (8 points if generated using loops, 4 if hard-coded)

  • As you display the number sets from part 1, randomly display (on a set by set basis) the complement of some sets of numbers instead of the original sets of numbers. Your program must still be able to determine the user-selected number.
  • See class use below

Part 4 - Forever and Ever (7 points)

  • Allow the user to "play the game" again and again or to terminate

Extra Credit - Let's See It (10 points)

  • Design, implement, and test a JFrame based GUI that performs the functions described in parts 1-4 above.

Requirement Notes

General

  • For this lab you will be making one program. As you complete each part of the lab, you will be adding different features to this game.

Class Use

  • To receive credit for this lab, your implementation must make use of at least 2 classes. One of them will be the driver class, and another to help with the number sets.

    Driver class
    • Contains your main method.

    Number set class
    • Must have variables to store the user's number and/or their responses to whether their number is contained in the displayed sets
    • Must have a method or methods for displaying the number sets and their complements
    • Must have a method for returning the user's number
    You only need to have methods for the part of the program that you implement. For example, if your program only displays the sets and not the complement sets, you do not need to have methods to display the complement sets to receive credit for the lab. However, if you want to receive credit for all parts, you must have the things listed above.

Number Sets

  • The key to this assignment is the secret of binary. The sets of numbers are generated by fixing the appropriate binary digit at 1.
    • The 2^4 set of numbers is the following:
      16 17 18 19
      20 21 22 23
      24 25 26 27
      28 29 30 31
    • The 2^0 set of numbers can be generated by starting with 1, then adding 2 up to 31
    • The other sets of numbers can each be generated using a set pattern for each set. Using these patterns will simplify your loops used to generate your sets of numbers.

Determining Number

  • Each user answer is either yes or no (indicating whether or not the secret number is in the set). Think how this might relate to the binary number sets

Complement

  • The complements can be thought of it two ways.
    • The sets are generated by fixing a binary digit at 1, the complements are generated by fixing a binary digit at 0.
    • The complement of a subset of numbers is the complete set of numbers (0 to 31) less the set. The complement of the example above is the set of numbers from 0 to 15.
    Both ways yeild the same numbers in each set and complement set.

GUI

  • Your program may print the number sets on the command line, but all user input, components for resetting the game, and components for displaying the final value must be in the GUI
Designed by Andy Griffin