To create a number Guessing Game using principles of Abstraction/

Dear Gurus:
I have  the following assignment.

Purpose: To use software development principles of abstraction in problem solving and structured problem solving techniques to create an application that uses various control structures and the SecureRandom Java class to create a number guessing game.

Details:
Create a class called GuessTheNumber that generates a random magic number between 1 and 10 inclusive. It will then ask the user to guess the magic number, and display one of the following messages:
Your guess is too high
Your guess is too low
You guessed correctly in nn guesses!
If the user guesses incorrectly, indicate if the guess is too high or too low, and continue asking for a guess until the user guesses the magic number. The class should count the number of guesses it took the user, and display that value for nn.

The class should have at least the following two methods:
•      public void play() – Which plays the game. The method should create the magic number the user is trying to guess. It should call theaskForGuessmethod to obtain the user’s guess and then check if the user guessed the magic number. If the user didn’t guess the magic number this method should display to the user if the guess was too high or too low, and then ask for a new guess. When the user finally guess the magic number, it should display to the user that they guessed the correct number and indicate how many guesses it took.
•      privateintaskForGuess() – Which asks the user for their guess. This method should check that the user’s guess is between 1 and 10 inclusive, and if it is, return the guess. If the user’s guess is not within range, the method should indicate the user’s guess was out of range and continue asking for a guess.

Create a second class called GuessTheNumberTest that contains the main method. The method should create a GuessTheNumber object and call the play method. The class should play the game at least once, and then ask the user if they would like to play the game again. Keep playing the game until the user decides they want to stop.

Upload both source files to Blackboard.

Note: Ensure that your program is properly formatted and it follows all Java naming conventions.


The issue is that I am unable to call the "GuessTheNumberTest"  from  GuessTheNumber.

Please find my code attcahed
GuesstheNumber-_code.docx
Flex TronDeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gskTechnology consultantCommented:
Hi Flex Tron,

I have corrected the code
=======================================

import java.util.Random;
public class GuessTheNumber {

    private int randomNumber;
    private int tries = 0;
    private int range;

    public void play(int range) {
        this.range = range;
        generateRandomNumber();
    }

    private void generateRandomNumber() {
        Random rand = new Random();
        this.randomNumber = rand.nextInt(range) + 1;
    }

    private int askForGuess(int guess) {
        tries++;
        if (guess == randomNumber) {
            if (tries > 1) {
                System.out.println("You got the number in " + tries + " tries.");
                return true;
            } else {
                System.out.println("You got the number in " + tries + " try.");
                return true;
            }
        } else if (guess > randomNumber) {
            System.out.println("Too high try again");
            return false;
        } else if (guess < randomNumber) {
            System.out.println("Too low try again ");
            return false;
        }
        return false;

    }

    public void reset() {
        this.tries = 0;
        generateRandomNumber();

    }

}
====================================================================================
package game;

             import java.util.Random;

             import java.util.Scanner;
        import game.GuessTheNumber;
             public class GuessTheNumberTest{

                 public static void main(String[] args) {
                     Scanner scan = new Scanner(System.in);
                     boolean win = false;
                     boolean running = true;
                     System.out.println("Please enter the upper limit: ");
                     GuessTheNumber game = new GuessTheNumber();

                     int num=scan.nextInt();
                     while (running) {

                         System.out.println("Please enter a guess ");

                         while (!win) {
                             if (game.askForGuess(num)){
                                 win = true;
                               
                             }
                         }
                         System.out.println("Would you like to play again y/n?");
                         String playAgain = scan.next();
                         if (playAgain.equalsIgnoreCase("y")) {
                             game.reset();
                             running = true;
                             win = false;
                         } else {
                             running = false;
                             System.out.println("Game over");
                         }
                     }
                 }
             }

=============================================================================
please type these two classes code in your eclipse project folder or whatever IDE you are using and it will work,any more help required
please comment
0
CEHJCommented:
What is your question?
0
Flex TronDeveloperAuthor Commented:
Hi gsk / Cehj
This is the output I am getting

run:
Please enter the upper limit:
2
Please enter a guess
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types: int cannot be converted to boolean
      at game.GuessTheNumberTest.main(GuessTheNumberTest.java:26)
C:\Users\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 4 seconds)
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

David Johnson, CD, MVPOwnerCommented:
not knowing java my question is .. where is the input? I see the output lines but not the input.

private int askForGuess(int guess) {
should probably be
private bool askForGuess(int guess) {
0
Flex TronDeveloperAuthor Commented:
@David ..Input is thru the scan function

    System.out.println("Please enter the upper limit: ");
                     GuessTheNumber game = new GuessTheNumber();
0
gskTechnology consultantCommented:
Hi Flex,

I changed your two classes two work.there were errors in your classes. and I think you wanted to  call GuesstheNumber  from GuesstheNumberTest.I will look more into your code make it work.
0
Flex TronDeveloperAuthor Commented:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package game;
import java.util.Random;
public class GuessTheNumber {

    private int randomNumber;
    private int tries = 0;
    private int range;

    public void play(int range) {
        this.range = range;
        generateRandomNumber();
    }

    private void generateRandomNumber() {
        Random rand = new Random();
        this.randomNumber = rand.nextInt(range) + 1;
    }

    public boolean guess(int guess) {
    tries++;
    if (guess == randomNumber) {
        System.out.println("You got the number in " + tries + ((tries > 1) ? " tries." : " try."));
        return true;
    } //implicit else because return would end the function

    if (guess > randomNumber) {
        System.out.println("Little high try again");
    } else {
        System.out.println("Bit low try again ");
    }
    return false;
}
    /*return false;*/

    }

    /*public void reset() {
        this.tries = 0;
        generateRandomNumber();

    }*/

Open in new window


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package game;

import java.util.Random;
import java.util.Scanner;

public class GuessTheNumberTest {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        boolean win = false;
        boolean running = true;
        System.out.println("Please enter the upper limit: ");
        int upperLimit = scan.nextInt();
        
        GuessTheNumber game = new GuessTheNumber();
        game.play(upperLimit);
        while (running) {

            System.out.println("Please enter a guess ");
            int guessedInt = scan.nextInt();

                if (game.guess(guessedInt)) {
                    win = true;

                }
            System.out.println("Play again y/n?");
            String playAgain = scan.next();
            if (playAgain.equalsIgnoreCase("y")) {
                //game.reset();
                running = true;
                win = false;
            } else {
                running = false;
                System.out.println("Game over");
            }
        }
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Flex TronDeveloperAuthor Commented:
Java Gurus are the most precious assets in this planet. Danke !
0
gskTechnology consultantCommented:
herzlich willkommen, Flex Tron :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Software

From novice to tech pro — start learning today.