如何访问另一个类中的方法

时间:2014-09-29 18:58:29

标签: java

我有3个文件

Guesser.java
GuessWhoGame.java
GuesserTest.java

在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) 
        {
         }
    }

在GuessWhoGame.java我有

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;
    }
}

在GuesserTest.java中我有

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();
    }
}

我的问题是在Guesser中我需要实例化GuessWhoGame类的主要方法,访问play方法,如果猜测是否正确则打印,并打印GuessWhoGame.class中的分数 在GuesserTest.java中我需要测试Guesser.play方法。 我该怎么办?我完全被难倒了

2 个答案:

答案 0 :(得分:1)

初始化一个GuessWhoGame对象并调用它的播放方法。

非常基本:

    @Test
    public void testGuesser() {
        GuessWhoGame guessWhoGame = new GuessWhoGame();
        guessWhoGame.play();
        // Do your test checks
    }

答案 1 :(得分:0)

其中一些问题可以从各种java站点访问,但首先要让你开始:

要实例化另一个类,必须执行以下操作

GuessWhoGame gwg = new GuessWhoGame();

如果您想要其他构造函数,请执行以下操作

GuessWhoGame gwg = new GuessWhoGame(1);

所以在main方法中做这个部分,然后你可以从主要方法中调用GuessWhoGame的方法

还要玩

Guesser.play(gwg);

因为你是从静态主体那样做的,所以你不能使用&#34;这个&#34;但必须使用名称Guesser。

获得分数:

 int s = gwg.score();

要打印行:

System.out.println("STUFF GOES HERE");
System.out.println("Score:"+s);