Sie sind auf Seite 1von 4

TCSS 142 Object-Oriented Programming Winter 2012 Assignment #5

Due 16 February 2012 Value: 15% of Homework Assignments Grade This assignment includes one problem and an extra credit problem. Each problem is an interactive application using indefinite loops to allow the program to be run repeatedly until the user decides to quit. Place your files into a folder called Assignment5. Compress the folder and turn it in electronically using the link on the Assignment 5 page of the course web site. For each program make sure that your code conforms to the conventions covered in class: Indent your code consistently; be consistent with the placement of curly braces. Give meaningful names to methods and variables in your code. Follow Java's naming conventions about the format of ClassNames, methodAndVariableNames, and CONSTANT_NAMES. Localize variables whenever possible -- that is, declare them in the smallest scope in which they are needed. Use whitespace to separate methods and to generally improve the readability of the code. Avoid line length greater than 80 characters. Include a multiline comment at the top of each program, as in:
/* * File Name * * TCSS 142 - Autumn 2010 * Assignment 5 */

After the multiline comment include a line of whitespace followed by any required import statements, followed by a line of whitespace, followed by a javadoc class comment such as:
/** * This program ...(Describe the purpose of the program here) * * @author Your Name * @version A date or version number */ public class YourClassName {

Include a javadoc comment at the start of each method to explain the purpose of the method. include @param and @return tags where needed:
/** * Explain the methods purpose. (Prompts for..., Calculates..., etc) * * @param myParameterName (Explain the purpose of this parameter here) * //you will need an @param tag for each parameter passed to a method * @return (Explain what the method returns here) */ public static <returnType> myMethodName(<parameterType> <parameterName>) {

Include a Javadoc comment for any class constants used:

Problem 1 (100%): This problem will give you practice with indefinite loops and pseudorandom numbers. Write an interactive program named GuessingGame.java that generates a random number between 1 and some max value (maybe 100) and prompts the user repeatedly to guess the number. When the user guesses incorrectly, the game should give the user a hint about whether the correct answer is higher or lower than the guess. Once the user guesses correctly, the program should ask the user if they would like to play again. The game should repeat until the user chooses to quit. When the user chooses to quit, the program should display a summary of statistics including the number of games played, the total number of guesses made, the average number of guesses per game rounded to 2 decimal places (by using printf), the minimum number of guesses (best game), and the maximum number of guesses (worst game). Your program is required to exactly reproduce the format and behavior as shown in the sample output. Your numbers will be different, but the format and the behavior should be the same. At a minimum, your program should have the following static methods in addition to the main method: a method to give an introduction and instructions to the user a method to play one round of the game with the user (just one game, not multiple games) (Use an indefinite loop in main to repeatedly call the method to play rounds of the game until the user chooses to quit.) NOTE: This method can return a value to main if you choose (it might be very useful to do so). a method to ask the user if they would like to play another game this method should return a boolean a method to report overall results to the user when the user decides to quit. The main() method should not contain any print statements. You may define more methods than this if you find it helpful. Define a class constant for the upper limit used in the guessing game. The sample output shows the user making guesses from 1 to 100, but the choice of 100 is arbitrary. By changing the value of the class constant you should be able to change the upper limit of the range of values the program uses when it generates the random number for the user to guess. For instance, setting the constant to 50 would cause the program to use a range of 1 to 50. Remember to document the class constant with a javadoc comment. For this assignment, do not assume that the user will input reasonable values when prompted. You must check the user input and prompt again if necessary until valid input is obtained. The case study from chapter 5 provides a good example of how to do this. For example, when the user enters a guess, the number entered should be an integer. If the user enters anything other than an integer you should give an error message and prompt again until valid input is entered. On page 355 of the textbook there is an example that may provide ideas about how to do this efficiently. When you ask the user whether or not to play again, you should use the next() method of the Scanner class to read the users response as a String. The program should continue to play if the user enters y or Y or "yes" or "Yes". The program should end the game and display the final results if the user enters n or N or "no" or "No". If the user types anything else, give an error message and prompt again until valid input is entered. Here are a few helpful hints to keep in mind. This program needs to generate pseudorandom numbers. This is described in section 5.1 Its a good idea to change the value of your class constant and run the program to make sure that everything works right with the new value of the constant. Test your program with invalid user input to make sure that the program is robust. Extra Credit: Notice that in this program there are multiple sections of code that must be controlled by indefinite loops. In chapter 5 there are three different variations of indefinite loops described. They are the while loop, the do/while loop, and the infinite loop containing a break statement. For 5% extra credit, use each of these forms of indefinite loop in your program rather than reusing the same type of loop in every place where an indefinite loop is needed.

Sample Output: (User input in bold)


This program allows you to play a guessing game. I will think of a number between 1 and 100 and will allow you to guess until you get it. For each guess, I will tell you whether the right answer is higher or lower than your guess. I'm thinking of a number . . . Your guess? 50 higher Your guess? 75 higher Your guess? 87 lower Your guess? 81 higher Your guess? 84 You got it right in 5 guesses Do you want to play again? Y/N: y I'm thinking of a number . . . Your guess? 50 higher Your guess? 75 lower Your guess? 62 higher Your guess? 68 You got it right in 4 guesses Do you want to play again? Y/N: yes I'm thinking of a number . . . Your guess? 50 lower Your guess? Hello Not an integer; try again. Your guess? 4.4 Not an integer; try again. Your guess? 25 higher Your guess? 37 lower Your guess? 31 You got it right in 4 guesses Do you want to play again? Y/N: maybe I did not understand your answer. Do you want to play again? Y/N: N Overall results: total games played total guesses average guesses/game minimum guesses maximum guesses = = = = = 3 13 4.33 4 5

Extra Credit (5%): Rewrite any one of the homework program from assignments, to use indefinite loops to control program execution. That is you may rewrite any one of the following: ChessKing.java Fibonacci.java Circle.java Tax.java ISBN.java prompt for the size as an int prompt for the number of Fibonacci values to display as an int prompt for the radius as a double prompt for the income as a double prompt for the ISBN as a String

For the program you choose, modify it to be an interactive program such that the user can run it multiple times (with different input values) until the user decides to quit. The user should be able to quit by entering a sentinel value. In the comments in your code explain why you chose the sentinel value that you are using. The format of the code and comments must meet the general requirements for this assignment and must have the same general structure as specified for GuessingGame.java. This means you may have to change several things about your old program to meet the current requirements. The goal here is to provide the user with a good user experience. The introduction message should give a good orientation to the program. The prompts for user input should be clearly understandable. For this assignment, do not assume that the user will input reasonable values when prompted. You must check the user input and prompt again if necessary until valid input is obtained. Provide an appropriate end of program message when the user decides to quit.

Das könnte Ihnen auch gefallen