Project_08 - COMP 1210 Project 8: Enhanced Number Guessing...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: COMP 1210 Project 8: Enhanced Number Guessing Page 1 of 4 Due: Monday, March 19, 2012 by 11:59 PM Deliverables The following project files must be submitted to the “graded” assignment in Web-CAT by the due date and time specified above (see the Lab Guidelines for information on submitting project files). You should plan to start on the project not later than Wednesday in lab, and you should make sure that your files are successfully submitted to the “ungraded” assignment in Web-CAT during lab on Wednesday. Projects sent via e-mail past the deadline at 11:59 PM will not be accepted without a universityapproved excuse. Files to submit to Web-CAT: • • • Number.java Game.java EnhancedNumberGuessingApp.java Specifications Overview: Design an application that plays a guessing game with numbers. The program should allow the user to enter the number of digits (1 to 9) that the computer’s number will have, generate the number so that the digits consists of different random numbers between 0 and 9 (inclusive) in a certain combination, and then repeatedly prompt the user to guess the combination of these digits. The user’s guess input must be a integer with the same number of digits as the computer’s number (the digits are not separated by spaces). For each guess, report to the user how many of the guessed digits were totally right (i.e., both the number and position were correct) and how many of the guessed digits were correct but in the wrong position. Continue accepting guesses until the user guesses correctly or until the user enters “key” or “end” to quit and see the answer. Count the number of guesses and report that value as part of the output that prefaces the guess as shown in the example below. For this assignment, do not use any ArrayList objects; instead use arrays. • Number.java Requirements: The Number class will represent the n digits of the computer generated “number” which has random digits between 0 and 9 inclusive that do not repeat. Design: The Number class has fields, a constructor, and methods as outlined below. (1) Field: a single field of type int array that holds the n randomly generated digits: int digits; COMP 1210 Project 8: Enhanced Number Guessing Page 2 of 4 (2) Constructor: Accepts an int n parameter representing the number of digits entered by the user and generates n different random numbers between 0 and 9 (inclusive) and assigns them to the elements of the int array. Note that “Different” means no repeating numbers. (3) Methods: Usually a class provides methods to access (or read) and modify each of its instance variables (i.e., get and set methods) along with any other required methods. The methods for Number are described below. o getNumberOfDigits: returns an int, the length of the digits array. o setDigit: accepts two int parameters: the index of the digit to set and its value. o getDigit: accepts a int parameter and returns the indicated digit. o toString: returns a String representing of the number represented by the digits array. Code and Test: The most complicated method in the Numbers class is the constructor. See the Java API documentation (Math class or Random class) for details on how to generate a random number. Make sure that the integers generated are random numbers between 0 and 9 (inclusive) and that they do not repeat (i.e., they are different from each other). One approach would be to have an outer while loop which contains a for loop to generate the random digits followed by nested for loops to check for duplicates. The outer while should loop until an acceptable set of random digits has been generated. • Game.java Requirements: The Game class will provide a constructor that creates a Game (based on an instance of the Number class) as well method to start and play the game. Design: The Number class has fields, a constructor, and methods as outlined below. (1) Fields: two instance variables 1) the Number passed to the constructor and 2) a boolean variable to indicate the game is over (initialized to false). (2) Constructor: Accepts a Number parameter representing the computer’s number and assigns it the instance variable. (3) Methods: Usually a class provides methods to access (or read) and modify each of its instance variables (i.e., get and set methods) along with any other required methods. The methods for Number are described below. o start: returns a String that announces the game; see sample output below. o play: accepts a String representing the user’s guess and returns a Sting representing the appropriate response based on: whether the user entered “end” or “key” (if this is true, gameOver should be set to true); whether the user entered the correct number of digits; whether the user entered the correct number (if this is true, gameOver should be set to true); or otherwise the number correct and correct position and number correct wrong position. COMP 1210 o o Project 8: Enhanced Number Guessing Page 3 of 4 isGameOver: returns boolean value of gameOver field. end: returns the String "Thanks for playing.\n". Code and Test: In order to facilitate comparison of the digits in the computer’s number and the user’s guess, you may want to use a combination of int arrays, char arrays, and/or the charAt method of the String class. However, you need to make sure that you are comparing compatible types. Then use nested for statements to compare these two combinations and count how many digits are correct and in the correct position, and print how many of the digits are correct but in the wrong position. Remember, you are not to use ArrayLists in the project. • EnhancedNumberGuessingApp.java Requirements: Creates and controls an enhanced number guessing game using the Number and Game classes described above. Design: NumberGuessingApp.java contains only a main method that meets the requirements above as follows. Welcomes the user and indicates the how the game is played; gets number of digits from user; creates a Number object based on the number of digits; creates a new game based on the Number object; starts the game; gets user’s guess; plays the game with the guess; prints the results of play; continues as long as the game is not over. Code and Test: You only need one Scanner for user input since the user’s guess does not have spaces between the digits. After you read in the user’s guess as a String, you can call the Game object’s play method with the guess. Your program will need to accept all values as type string. The sample output below indicates the format of the input and output as well as the content and format of the various Strings returned by the Game object’s play method. COMP 1210 Project 8: Enhanced Number Guessing Page 4 of 4 Program output Welcome to COMP 1210 Enhanced Number Guessing Game. The enhanced game allows you to enter the number of digits for the computer's number. Increasing the number of digits makes the game more difficult to win. Start new game?(Y/N): y Enter number of digits (1 to 9): 3 Enter a number consisting of 3 different digits with no spaces or enter "key" or "end" anytime to see key and end the game. Guess #1: 123 Correct number, Correct position: 0 Correct number, Wrong position: 1 Guess #2: 12 Your guess should have 3 digits. Guess #3: end The key: 962 Start Enter Enter Enter new game?(Y/N): y number of digits (1 to 9): 21 number of digits (1 to 9): 0 number of digits (1 to 9): 7 Enter a number consisting of 7 different digits with no spaces or enter "key" or "end" anytime to see key and end the game. Guess #1: 5689310 Correct number, Correct position: 4 Correct number, Wrong position: 0 Guess #2: 5687240 Congratulations! Smart kid! Your answer is correct. Start new game?(Y/N): n Thanks for playing. ...
View Full Document

This note was uploaded on 03/02/2012 for the course COMP 1210 taught by Professor Cross during the Winter '07 term at Auburn University.

Ask a homework question - tutors are online