Programming Project #2 – Guessing Game Part 1 ✓ Solved
Programming Project #2 – Guessing Game Part 1 This is the
Write a command-line Java program called GuessingGame that implements a guessing game where the computer randomly generates a number between 1 and 32. The user must guess the number, receiving hints if their guess is too high or too low. The game continues until the user guesses correctly or enters -1 to give up. Upon exiting the loop, display feedback including the total number of guesses, how many were too low, and how many were too high. Asterisks should represent the number of smaller and larger guesses graphically. You must include specific loops and conditionals as described, and keep the names of classes and variables descriptive. Further, for extra credit, create a GUI version called GuessingGameGUI using dialog boxes for input and output without altering the original game code.
Design Requirements:
- List of necessary variables, like
int totalGuesses; - Flow chart or pseudo code for decision statements regarding high/low guesses.
- Flow chart or pseudo code for the loop including -1 check and variable updates.
- Do not modify part 1 when working on part 2; retain both programs.
Lastly, consider the algorithm behind minimizing wrong guesses to discover how random numbers are generated in programming.
Paper For Above Instructions
The Guessing Game Project aims to develop a command-line Java program that allows users to engage interactively by guessing a number generated by the computer. This project is divided into two parts, with the first emphasizing the fundamental concepts of programming in Java, and the second encouraging enhanced user interface design. Here, we present the design, implementation, and considerations necessary to complete the assignment successfully.
Game Design
The game begins with a computer generating a random number between 1 and 32. The Java Random class is ideal for this, providing a straightforward way to achieve the desired outcome using Random rand = new Random(); to instantiate the random number generator and int value = rand.nextInt(32)+1; to obtain the random number. These steps are crucial as they set the foundational logic for the game's mechanics.
The user interaction is initiated by prompting the player with a message to guess the randomly generated number. User guesses will be captured through a continuous loop, allowing them to input values until they either guess correctly or opt to give up by entering -1. The program will then respond with hints indicating whether the user's guess was too high or too low.
Implementing the Game Logic
To ensure the program operates correctly, several key components must be included:
- A
whileloop to manage continuous user interaction until a correct guess or a termination signal (-1) is received. - Conditional statements to assess whether the guess made is higher, lower, or equal to the generated number.
- Tracking of the total number of guesses, as well as how many of those guesses were too low or too high.
System.out.printf("*");will be used to graphically represent the results with asterisks following the game's conclusion, reflecting statistical data such as the number of smaller or larger guesses.
After every guess, the system will provide feedback, for instance, “Your guess is larger than the random value. Next guess:,” so that the user can adjust accordingly. If a player opts to end the game prematurely by entering -1, the program will culminate with a summary of their performance—total guesses and graphical representations using asterisks.
Pseudocode Flow Example
declare variables totalGuesses, smallerGuesses, largerGuesses
initialize totalGuesses, smallerGuesses, largerGuesses to 0
generate randomNumber
while true
prompt user for a guess
if guess is -1
break
increment totalGuesses
if guess
increment smallerGuesses
print "Your guess is too low."
else if guess > randomNumber
increment largerGuesses
print "Your guess is too high."
else
print "You've guessed correct!"
break
print summary of totalGuesses, smallerGuesses, largerGuesses
print asterisks for smaller and larger guesses
Part 2 - Extra Credit
For the GUI component, Java Swing can be utilized for creating dialog boxes. The main aspects of your logic and calculations can be retained, but user input/output will transition from the command line to a GUI setup. The first step is to create a new class named GuessingGameGUI within the same project, thereby maintaining a clear division between both implementations.
Utilize methods such as JOptionPane.showInputDialog() for input and JOptionPane.showMessageDialog() for output to handle user interactions in the GUI version. Ensure you validate user inputs and maintain the same functionality as in part one.
Conclusion
The development of the Guessing Game not only reinforces the understanding of basic programming concepts such as loops, conditionals, and user input mechanisms but also presents an opportunity to learn about GUI design. By employing systematic methods to break down the project requirements, aspiring developers can incrementally build their skills in Java programming and software development.
References
- Lee, J. (2020). Java Programming Basics. New York: O'Reilly Media.
- Bloch, J. (2018). Effective Java. Addison-Wesley.
- Schildt, H. (2019). Java: The Complete Reference (11th ed.). McGraw-Hill.
- Deitel, P. J., & Deitel, H. M. (2020). Java: How to Program (11th ed.). Pearson.
- Java Platform, Standard Edition. (2021). Oracle. Available at: https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
- GeeksforGeeks. (2023). Java Random Number Generation. Available at: https://www.geeksforgeeks.org/java-random-number-generation/
- Oracle. (2022). Java Documentation: Random Class. Available at: https://docs.oracle.com/javase/8/docs/api/java/util/Random.html
- Oracle. (2022). Java GUI Programming with Swing. Available at: https://docs.oracle.com/javase/tutorial/uiswing/index.html
- Subramanian, K. (2021). Understanding the Basics of Java GUI Programming. Springer.
- Martin, R. C. (2018). Clean Code: A Handbook of Agile Software Craftsmanship. Prentice Hall.