file_6
file_6
Purpose:
The "Number Guessing Game" task challenges you to create a simple Java program where the computer generates a random number,
and the user (simulated here) tries to guess it. The program uses loops and conditionals to provide feedback (e.g., "too high" or "too low")
until the correct guess is made. Assigned on Day 2 and due on Day 5, this reinforces control flow from Day 2 while encouraging logical
thinking—key for backend development tasks like user interaction or game logic.
Introduction:
Write a program that:
Sets a fixed "secret" number (e.g., 42) for simplicity (randomization is optional).
Uses a loop to simulate guesses.
Provides feedback with conditionals ("Too high", "Too low", "Correct!").
Tracks and displays the number of attempts.
Add randomization with Math.random() or limit the number of guesses.
Real-Life Context:
Imagine you’re building a mini-game feature for a mobile app’s backend. The server picks a number, and the client (user) guesses it, with
the server responding until the guess is correct. This mimics interactive logic in gaming or educational apps.
Duration:
Assigned: Day 2.
Due: Day 5.
Tools Needed:
IDE: IntelliJ IDEA, Eclipse, or VS Code with Java extension.
Java Installed: JDK 17 or later.
Submission: Upload a .java file (e.g., via a learning management system or email).
Requirements:
Create a Main class with a main method.
Use variables to store the secret number and guess count.
Apply a loop (while, for, or do-while) to handle guesses.
Use if-else statements for feedback ("Too high", "Too low", "Correct!").
Print the number of attempts when the game ends.
Code must compile and run without errors.
Problem Statement
Task:
Write a Java program named NumberGuessingGame that simulates a number guessing game. The program picks a secret number
between 1 and 100 (e.g., 42—hardcode it for now). Simulate user guesses (you can increment a guess variable or use a fixed sequence)
within a loop. For each guess, print feedback: "Too high" if the guess is above the secret number, "Too low" if below, or "Correct!" if it
matches. When the correct guess is made, display the total attempts and end the game.
Submission:
Submit NumberGuessingGame.java by Day 5. Test with at least one guess sequence (e.g., starting at 50 and adjusting).
Tip: Adjust guess by 10 each time for simplicity—real users would vary this.
Run it. For secretNumber = 42 and guess = 50, expect a sequence like:
Guess 50: Too high → 40
Guess 40: Too low → 50 (loop repeats, needs refinement—see bonus).
Refine guess logic if needed (e.g., smaller steps near the target).
Limit guesses:
Example Solutions
Basic Solution:
Output:
Troubleshooting Tips
Common Errors:
Infinite Loop: Guess doesn’t adjust toward secretNumber. Fix: Ensure guess changes logically.
Off-by-One: attempts miscounted. Fix: Increment at the right spot (e.g., include winning guess).
No Break: Loop runs forever if guess oscillates. Fix: Refine adjustment (e.g., smaller steps).
Debugging:
Print guess and secretNumber each iteration to trace logic.
Test with secretNumber = guess to ensure instant win works.
Real-Life Extension
In a game server: