0% found this document useful (0 votes)
7 views

file_6

The document outlines a coding task to create a 'Number Guessing Game' in Java, where the computer generates a secret number and the user guesses it, receiving feedback until the correct guess is made. It includes step-by-step guidance for setting up the program, defining variables, creating a guessing loop, and handling correct guesses, with optional features for randomization and limiting attempts. The task is assigned on Day 2 and due on Day 5, requiring submission of a .java file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

file_6

The document outlines a coding task to create a 'Number Guessing Game' in Java, where the computer generates a secret number and the user guesses it, receiving feedback until the correct guess is made. It includes step-by-step guidance for setting up the program, defining variables, creating a guessing loop, and handling correct guesses, with optional features for randomization and limiting attempts. The task is assigned on Day 2 and due on Day 5, requiring submission of a .java file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Coding Task 2 (Guided): "Number Guessing Game"

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.

Advanced Additional Features:


Use Math.random() to generate a random secret number.
Limit guesses to 10 attempts, ending with a "Game Over" message if exceeded.

Submission:

Submit NumberGuessingGame.java by Day 5. Test with at least one guess sequence (e.g., starting at 50 and adjusting).

Step-by-Step Guidance for Trainees


Step 1: Setup Your Program

Create a new project named NumberGuessingGame in your IDE.


Add a Main class:

1 public class Main {


2 public static void main(String[] args) {
3 // Your code goes here
4 }
5 }

Step 2: Define the Secret Number and Variables

Set a secret number and initialize counters:

1 int secretNumber = 42; // Hardcoded for simplicity


2 int guess = 50; // Starting guess
3 int attempts = 0; // Track guesses

Tip: Start with a guess like 50 to simulate a binary search-like approach.

Step 3: Create the Guessing Loop

Use a while loop to compare guesses:

1 while (guess != secretNumber) {


2 attempts++;
3 if (guess > secretNumber) {
4 System.out.println("Guess " + guess + ": Too high");
5 guess -= 10; // Adjust downward
6 } else {
7 System.out.println("Guess " + guess + ": Too low");
8 guess += 10; // Adjust upward
9 }
10 }

Tip: Adjust guess by 10 each time for simplicity—real users would vary this.

Step 4: Handle the Correct Guess

Exit the loop and print results:

1 attempts++; // Count the final guess


2 System.out.println("Guess " + guess + ": Correct!");
3 System.out.println("You won in " + attempts + " attempts!");
Tip: Increment attempts one last time for the winning guess.

Step 5: Test Your Code

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).

Bonus Steps (Optional):

Randomize the secret number:

1 int secretNumber = (int) (Math.random() * 100) + 1; // 1-100

Limit guesses:

1 int maxAttempts = 10;


2 while (guess != secretNumber && attempts < maxAttempts) {
3 // Guess logic
4 }
5 if (attempts >= maxAttempts) {
6 System.out.println("Game Over! Secret was " + secretNumber);
7 }

Example Solutions
Basic Solution:

1 public class Main {


2 public static void main(String[] args) {
3 int secretNumber = 42;
4 int guess = 50;
5 int attempts = 0;
6
7 while (guess != secretNumber) {
8 attempts++;
9 if (guess > secretNumber) {
10 System.out.println("Guess " + guess + ": Too high");
11 guess -= 5; // Smaller steps for demo
12 } else {
13 System.out.println("Guess " + guess + ": Too low");
14 guess += 5;
15 }
16 }
17 attempts++;
18 System.out.println("Guess " + guess + ": Correct!");
19 System.out.println("You won in " + attempts + " attempts!");
20 }
21 }

Output:

1 Guess 50: Too high


2 Guess 45: Too high
3 Guess 40: Too low
4 Guess 45: Too high
5 Guess 42: Correct!
6 You won in 5 attempts!

Bonus Solution (Random + Limit):

1 public class Main {


2 public static void main(String[] args) {
3 int secretNumber = (int) (Math.random() * 100) + 1;
4 int guess = 50;
5 int attempts = 0;
6 int maxAttempts = 10;
7
8 while (guess != secretNumber && attempts < maxAttempts) {
9 attempts++;
10 if (guess > secretNumber) {
11 System.out.println("Guess " + guess + ": Too high");
12 guess -= 10;
13 } else {
14 System.out.println("Guess " + guess + ": Too low");
15 guess += 10;
16 }
17 }
18 if (attempts < maxAttempts) {
19 attempts++;
20 System.out.println("Guess " + secretNumber + ": Correct!");
21 System.out.println("You won in " + attempts + " attempts!");
22 } else {
23 System.out.println("Game Over! Secret was " + secretNumber);
24 }
25 }
26 }

Sample Output (e.g., secret = 73):

1 Guess 50: Too low


2 Guess 60: Too low
3 Guess 70: Too low
4 Guess 80: Too high
5 Guess 70: Too low
6 Guess 80: Too high
7 Guess 70: Too low
8 Guess 80: Too high
9 Guess 70: Too low
10 Guess 80: Too high
11 Game Over! Secret was 73

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:

Random number from a pool.


User guesses via API calls.
Server tracks attempts and ends the game—scalable interactive logic!

You might also like