Define a class constant for the maximum number

Assignment Help JAVA Programming
Reference no: EM131585836

This assignment will give you practice with while loops and pseudorandom numbers. You are going to write a program that allows the user to play a simple guessing game in which your program thinks up an integer and allows the user to make guesses until the user gets it right. For each incorrect guess you will tell the user whether the right answer is higher or lower. Your program is required to exactly reproduce the format and behavior of the log of execution at the end of this write-up.

At a minimum, your program should have the following static methods in addition to method main:
- a method that introduces the game to the user
- a method to play one game with the user (just one game, not multiple games)
- a method to report overall results to the user

You may define more methods than this if you find it helpful, although you will find that the limitation that methods can return only one value will tend to limit how much you can decompose this problem.

You are to define a class constant for the maximum number used in the guessing game. The sample log shows the user making guesses from 1 to 100, but the choice of 100 is arbitrary. By introducing a constant for 100, you should be able to change just the value of the constant to make the program play the game with a range of 1 to 50 or a range of 1 to 250 or some other range starting with 1.

When you ask the user whether or not to play again, you should use the "next()" method of the Scanner class to read a one-word answer from the user. You should continue playing if this answer begins with the letter "y" or the letter "Y". Notice that the user is allowed to type words like "yes". You are to look just at the first letter of the user's response and see whether it begins with a "y" or "n" (either capitalized or not) to determine whether to play again.

Assume that the user always types an integer when guessing, that the integer is always in an appropriate range and that the user gives you a one-word answer beginning with "y", "Y", "n" or "N" when asked whether to play again. You may assume that no game involves more than 9,999 guesses.

You will notice at the end of the log that you are to report various statistics about the series of games played by the user. You are to report the total number of games played, the total number of guesses made (all games included), the average number of guesses per game, and the best (fewest) number of guesses used in any single game. The average number of guesses per games should be rounded to one decimal place (you can use either the round1 method or a printf).
Because this program uses pseudorandom numbers, you won't be able to recreate the sample log. We will provide sample logs where the answer is always 42. Obviously you won't want your program to always pick 42 as the number to be guessed. You should modify your program to set the answer to 42 and check its behavior against the sample logs. Then you should put it back to the normal behavior of picking a different number for every game before you turn it in.

Here are a few helpful hints to keep in mind.
- To deal with the yes/no response from the user, you will want to use some of the String class methods described in section 3.3 and 4.1 of the book or the lecture slides for the lecture on Strings. You should use the next() method of the Scanner class to read a word from the console.
- It's a good idea to change the value of your class constant and run the program to make sure that everything works correctly with the new value of the constant. For example, turn it into a guessing game for numbers between 1 and 5.
- While you are developing your program, you might want to have it print out the answer before the user begins guessing. Obviously you don't want this in the final version of the program, but it can be helpful for you while you are developing the code.
- The chapter 5 case study is a particularly relevant example for this assignment.
You should handle the case where the user guesses the correct number on the first try. Print the following message:
You got it right in 1 guess
In the last program we asked you to write very short methods that were no longer than 15 lines long and to have a very short main. This program is more difficult to decompose into methods, so you may end up having methods that are longer than 15 lines. You can also include more code in your main method than we allowed in the last program. In particular, you are required to have a while loop in main that plays multiple games and prompts the user for whether or not to play another game. You shouldn't have all of the code in main because you are required to have the methods described at the beginning of this write-up.
We will once again expect you to use good programming style and to include useful comments throughout your program. We will expect you to make appropriate choices about when to store values as int versus double, which if/else constructs to use, what parameters to pass, and so on.
For this assignment you are limited to the language features in Chapters 1-5 shown in lecture or the textbook. You are not allowed to use the break statement or to have a return statement in a void method because these often lead to bad style.
Use whitespace and indentation properly. Limit lines to 100 characters. Give meaningful names to methods and variables, and follow Java's naming standards. Localize variables whenever possible. Include a comment at the beginning of your program with basic description information and a comment at the start of each method. Some students try to achieve repetition without properly using while loops, by writing a method that calls itself; this is not appropriate on this assignment and will result in a deduction in points
Your program should be stored in a file called Guess.java.

Log of execution (user input bold and underlined)
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 between 1 and 100... Your guess? 50
It's lower. Your guess? 25 It's lower.
Your guess? 12
It's lower. Your guess? 6
You got it right in 4 guesses Do you want to play again? y

I'm thinking of a number between 1 and 100... Your guess? 50
It's lower. Your guess? 25 It's lower.
Your guess? 12
It's higher. Your guess? 18
You got it right in 4 guesses Do you want to play again? YES

I'm thinking of a number between 1 and 100... Your guess? 50
It's higher. Your guess? 75 It's lower.
Your guess? 62
It's higher. Your guess? 68 It's lower.
Your guess? 65
It's higher. Your guess? 66 It's higher.
Your guess? 67
You got it right in 7 guesses Do you want to play again? nope

Overall results: total games = 3
total guesses = 15 guesses/game = 5.0 best game = 4

Attachment:- Guess19.rar

Reference no: EM131585836

Questions Cloud

An anxiety disorder and a substance use disorder : hat are the unique challenges in treating someone with an anxiety disorder and a substance use disorder?
The china challenge-narrated presentation assignment : You have been hired as a management consultant by CEO Mia Foster. She is travelling to China to meet with Louis Chen, and wants your advice.
What special considerations should you make : Identify the DSM-5 diagnostic criteria that you notice in the case studies below and explain how the client meets the criteria.
What is the difference between prejudice and discrimination : What is the difference between prejudice and discrimination, and how does each of these reinforce white privilege
Define a class constant for the maximum number : Your program is required to exactly reproduce the format and behavior of the log of execution at the end of this write-up - practice with while loops
Firm has debt to asset ratio-calculate return on equity : A firm has a debt to asset ratio of 75%, $216,000 in debt, and net income of $43,200. Calculate return on equity.
What will be the price of the product in the long run : The following are cost data for four different size plants available to a competitive producer: What will be the price of the product in the long run
Discuss dealing with people with mental disorders : In your opinion, is psychodynamic-psychoanalytic psychotherapy an effective strategy in dealing with people with mental disorders
What is covered in the given standards : Using the AICPA Code of Professional Conduct as your reference, what is covered in these standards that relates to the preparation of financial statements?

Reviews

Write a Review

JAVA Programming Questions & Answers

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd