-2

I have 3 files

Guesser.java
GuessWhoGame.java
GuesserTest.java

In Guesser.java

public class Guesser {



       /**
         * Guesses which character you picked
         */
        public static String play(GuessWhoGame g) {
    if (g.hairIsColor(Color.BROWN) && g.shirtIsColor(Color.GREEN)) 
    {
        if (g.eyeIsColor(Color.BLUE))
            return "Alice";
        else if (g.eyeIsColor(Color.GREEN))
            return "Frank";
        else if (g.eyeIsColor(Color.BROWN) && g.isWearingGlasses())
            return "Bob";
        else if (g.eyeIsColor(Color.BROWN))
            return "Dave";
        else
            return "Isabelle";
    }

    if (g.hairIsColor(Color.BROWN) && g.shirtIsColor(Color.RED)) 
    {
        if (g.eyeIsColor(Color.GREEN))
            return "Philip";
        else if (g.eyeIsColor(Color.BLUE) && !g.isSmiling())
            return "Wendy";
        else if (g.eyeIsColor(Color.BLUE) && g.isWearingGlasses()) 
            return "Mallie";
        else if (g.eyeIsColor(Color.BLUE))
            return "Nick";
        else if (g.eyeIsColor(Color.BROWN) && g.isWearingHat())
            return "Robert";
        else if (g.eyeIsColor(Color.BROWN) && g.isSmiling())
            return "Quinn";
        else if(g.eyeIsColor(Color.HAZEL))
            return "Emily";
    }



    else if (g.hairIsColor(Color.BLACK) && g.shirtIsColor(Color.BLUE)) {
        if (!g.isSmiling())
            return "Carol";
        else if (g.isWearingHat())
            return "Gertrude";
        else
            return "Olivia";
    }


    if (g.hairIsColor(Color.BROWN))
    {
        if (g.eyeIsColor(Color.BLUE))
        return "Tucker";
      else if (g.eyeIsColor(Color.BROWN))
        return "Zander";
    }
      if (g.hairIsColor(Color.BLOND))
    {  
       if(g.shirtIsColor(Color.RED))
        return "Henry";
      else if(g.shirtIsColor(Color.BLUE))
        return "Jack";
    }

     if (g.hairIsColor(Color.BLACK))
    { 
      if(g.eyeIsColor(Color.HAZEL))
        return "Karen";
    else if(g.isWearingHat())
        return "Xavier";
    else if(g.eyeIsColor(Color.BROWN))
        return "Ursula";
    }

    if (g.hairIsColor(Color.RED))
    {
     if(g.shirtIsColor(Color.GREEN))
        return "Yasmine";
    else if (g.eyeIsColor(Color.BLUE))
        return "Larry";
    else if (g.isWearingHat())
        return "Sarah";
    else if (g.isSmiling())
        return "Victor";
    }     
            return null;
        }

        /**
         * TODO documentation for the main method
         */
        public static void main(String[] args) 
        {
         }
    }

In GuessWhoGame.java I have

import java.util.Random;

/**
 * Implements the Guess Who game.
 * Picks a secret character and counts the number of questions asked.
 * 
 * @author pritchey
 * @version 2014-07-17
 */

public class GuessWhoGame {
    /**
     * secret character
     */
    private Character secret;

    /**
     * number of questions asked
     */
    private int numQuestions;

    /**
     * array of secret characters
     */
    private static final Character[] characters = new Character[] {
        new Character("Alice",    Color.BROWN, Color.BLUE,  Color.GREEN, true,  true,  false),
        new Character("Bob",      Color.BROWN, Color.BROWN, Color.GREEN, true,  false, true),
        new Character("Carol",    Color.BLACK, Color.BLUE,  Color.BLUE,  true,  false, false),
        new Character("Dave",     Color.BROWN, Color.BROWN, Color.GREEN, false, true,  true),
        new Character("Emily",    Color.BROWN, Color.HAZEL, Color.RED,   true,  true,  true),
        new Character("Frank",    Color.BROWN, Color.GREEN, Color.GREEN, true,  true,  false),
        new Character("Gertrude", Color.BLACK, Color.BLUE,  Color.BLUE,  true,  true,  true),
        new Character("Henry",    Color.BLOND, Color.BROWN, Color.RED,   true,  true,  false),
        new Character("Isabelle", Color.BROWN, Color.HAZEL, Color.GREEN, true,  true,  false),
        new Character("Jack",     Color.BLOND, Color.BROWN, Color.BLUE,  false, true,  false),
        new Character("Karen",    Color.BLACK, Color.HAZEL, Color.GREEN, false, true,  false),
        new Character("Larry",    Color.RED,   Color.BLUE,  Color.BLUE,  true,  false, false),
        new Character("Mallie",   Color.BROWN, Color.BLUE,  Color.RED,   true,  true,  false),
        new Character("Nick",     Color.BROWN, Color.BLUE,  Color.RED,   false, true,  false),
        new Character("Olivia",   Color.BLACK, Color.BROWN, Color.BLUE,  false, true,  false),
        new Character("Philip",   Color.BROWN, Color.GREEN, Color.RED,   false, true,  false),
        new Character("Quinn",    Color.BROWN, Color.BROWN, Color.RED,   false, true,  false),
        new Character("Robert",   Color.BROWN, Color.BROWN, Color.RED,   false, true,  true),
        new Character("Sarah",    Color.RED,   Color.BROWN, Color.BLUE,  true,  true,  true),
        new Character("Tucker",   Color.BROWN, Color.BLUE,  Color.BLUE,  false, true,  false),
        new Character("Ursula",   Color.BLACK, Color.BROWN, Color.GREEN, false, true,  false),
        new Character("Victor",   Color.RED,   Color.BROWN, Color.BLUE,  true,  true,  false),
        new Character("Wendy",    Color.BROWN, Color.BLUE,  Color.RED,   true,  false, false),
        new Character("Xavier",   Color.BLACK, Color.BROWN, Color.GREEN, true,  true,  true),
        new Character("Yasmine",  Color.RED,   Color.BLUE,  Color.GREEN, true,  true,  false),
        new Character("Zander",   Color.BROWN, Color.BROWN, Color.BLUE,  false, true,  false)
    };

    /**
     * Class to represent a Guess Who character
     * @author pritchey
     * @version 2014-07-17
     */
    private static class Character {
        /**
         * hair color
         */
        private Color hair;
        /**
         * eye color
         */
        private Color eyes;
        /**
         * shirt color
         */
        private Color shirt;
        /**
         * wears glasses?
         */
        private boolean glasses;
        /**
         * is smiling?
         */
        private boolean smiling;
        /**
         * wearing a hat?
         */
        private boolean hat;
        /**
         * character's name
         */
        private String name;

        /**
         * construct a new character with the specified attributes
         * @param name
         * @param hair
         * @param eyes
         * @param shirt
         * @param glasses
         * @param smiling
         * @param hat
         */
        public Character(String name, Color hair, Color eyes, Color shirt,
                         boolean glasses, boolean smiling, boolean hat) {
            this.hair = hair;
            this.eyes = eyes;
            this.shirt = shirt;
            this.glasses = glasses;
            this.smiling = smiling;
            this.hat = hat;
            this.name = name;
        }

        /**
         * 
         * @return the hair color of the character
         */
        public Color hair() { return this.hair; }
        /**
         * 
         * @return eye color of the character
         */
        public Color eyes() { return this.eyes; }
        /**
         * 
         * @return shirt color of the character
         */
        public Color shirt() { return this.shirt; }
        /**
         * 
         * @return true if character wears glasses
         */
        public boolean glasses() { return this.glasses; }
        /**
         * 
         * @return true if character is smiling
         */
        public boolean smiling() { return this.smiling; }
        /**
         * 
         * @return true if character is wearing a hat
         */
        public boolean hat() { return this.hat; }
        /**
         * 
         * @return the character's name
         */
        public String name() { return this.name; }
    }

    /**
     * select the secret character at random
     */
    public GuessWhoGame() {
        Random random = new Random(System.currentTimeMillis());
        secret = characters[random.nextInt(characters.length)];
        numQuestions = 0;
    }

    /**
     * select the i-th secret character<br>
     * use for JUnit testing
     */
    public GuessWhoGame(int i) {
        secret = characters[i % characters.length];
        numQuestions = 0;
    }

    /**
     * 
     * @param c - Color of hair to ask about
     * @return true if secret chartacter's hair is the specified color
     */
    public boolean hairIsColor(Color c) {
        numQuestions++;
        return secret.hair().equals(c);
    }

    /**
     * 
     * @param c - Color of etrue to ask about
     * @return true if secret character's etrue are the specified color
     */
    public boolean eyeIsColor(Color c) {
        numQuestions++;
        return secret.eyes().equals(c);
    }

    /**
     * 
     * @param c - Color of shirt to ask about
     * @return true if secret character's shirt is the specified color
     */
    public boolean shirtIsColor(Color c) {
        numQuestions++;
        return secret.shirt().equals(c);
    }

    /**
     * 
     * @return true if secret character is wearing glasses
     */
    public boolean isWearingGlasses() {
        numQuestions++;
        return secret.glasses();
    }

    /**
     * 
     * @return true if secret character is smiling
     */
    public boolean isSmiling() {
        numQuestions++;
        return secret.smiling();
    }

    /**
     * 
     * @return true if secret character is wearing a hat
     */
    public boolean isWearingHat() {
        numQuestions++;
        return secret.hat();
    }

    /**
     * method to guess the identity of the secret character
     * @param name - name of character as a String
     * @return true if secret character's name is correct
     */
    public boolean guess(String name) {
        if (secret.name().equalsIgnoreCase(name)) {
            numQuestions++;
            return true;
        } else {
            numQuestions += 11; // penalty for incorrect guess
            System.out.println("it was " + secret.name());
            return false;
        }
    }

    /**
     * 
     * @return the number of questions asked
     */
    public int score() {
        return this.numQuestions;
    }
}

in GuesserTest.java I have

import static org.junit.Assert.*;

import org.junit.Test;


public class GuesserTest {

    @Test
    public void testGuesser() {

    }
    @Test
    public void testMain() {
        Guesser.main(new String[0]);
    }

    @Test
    public void testConstructor() {
        new Guesser();
    }
}

My problem is in Guesser in the main method I need to instantiate the GuessWhoGame class, access the play method, print if the guess if correct or not, and print the score which is in the GuessWhoGame.class and in GuesserTest.java I need test the Guesser.play method. How would I do that? I am totally stumped

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Joe
  • 23
  • 1
  • I know I don't want to read that wall of code. Provide public methods to call and you'll be able to do this easily. – duffymo Sep 29 '14 at 19:02
  • Please do not just dump your homework here and ask us to do it for you. What you're asking about is basic Java, and if you don't know enough to get started then you need to go back and study your course material. And if you get stuck, please ask a question and post the code you've tried--your _own_ code, not the code "pritchey" wrote. – ajb Sep 29 '14 at 19:06
  • The Guesser is my own code I just had no idea how to access a method that wasn't in the same file. I searched all over for it. The other code was given to me – Joe Sep 29 '14 at 19:20

2 Answers2

1

Initialize a GuessWhoGame object and call it's play method.

Very basically:

    @Test
    public void testGuesser() {
        GuessWhoGame guessWhoGame = new GuessWhoGame();
        guessWhoGame.play();
        // Do your test checks
    }
proulxs
  • 498
  • 2
  • 13
0

Some of these questions can be accessed from various java sites, but to get you started first things first:

To instantiate another class one must do the following

GuessWhoGame gwg = new GuessWhoGame();

If you want the other constructor then do the following

GuessWhoGame gwg = new GuessWhoGame(1);

So do this part in the main method, then you can call GuessWhoGame's methods from within the main

Also to play

Guesser.play(gwg);

Because you doing this from static main you can't use "this" but must use the name Guesser instead.

To get score:

 int s = gwg.score();

To do print lines:

System.out.println("STUFF GOES HERE");
System.out.println("Score:"+s);
Rika
  • 768
  • 1
  • 5
  • 16
  • @Joe by looking at your code it looks like you have to say play(gwg). Observe the play method in Guesser. By looking at the code there is no regular play(), only a play(GuessWhoGame) – Rika Sep 29 '14 at 19:14
  • Say I want to access the Guess method in GuessWhoGame and the Score method in guess who game and print the results how would I do that? I need to store and then do it? like int score = score(gwg); ? – Joe Sep 29 '14 at 19:19
  • @Joe, well, first I think you not understanding what is going on. So lets look at the play method first. Go to the Guesser class and look for play method and look at its parameters(or the things in parenthesis), what does it have in it? So now go to the GuessWhoGame and look for the score method, what does the parameters(or things in parenthesis) have in them? – Rika Sep 29 '14 at 19:42
  • play Wants GuessWhoGame g so a g instance of GuessWhoGame? and score has no parameters and returns an int – Joe Sep 29 '14 at 19:52
  • @Joe Correct. So for to get play in the class Guesser in the main method, we just had to do this.play(g) because it took an instance of GuessWhoGame. Also to note, "this" keyword means this class. So for class Guesser, it had the main method and play method in the same class, so all you had to do was this.play(g). But for score it is in a different class(GuessWhoGame), so you will do g.score(). So to store it will be int s = g.score(); – Rika Sep 29 '14 at 20:12
  • So we do GuessWhoGame g = new GuessWhoGame(); this.play(g); int s = g.score(); ? – Joe Sep 29 '14 at 20:30
  • Also how do I access play in GuesserTest I tried Guesser guessWhoGame = new Guesser(); guessWhoGame.play(); but it gives me an error – Joe Sep 29 '14 at 20:31
  • Also using this.play(g) says a nonstatic variable this can't be referenced from a static content – Joe Sep 29 '14 at 20:35