2014-09-29 76 views
-2

訪問方法,我有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中的分數。我需要測試Guesser.play方法。 我該怎麼做?我完全難住了

+0

我知道我並不想讀的代碼牆。提供公共調用方法,您可以輕鬆完成此操作。 – duffymo 2014-09-29 19:02:19

+0

請不要只是把你的作業倒在這裏,並要求我們爲你做。你在問什麼是基本的Java,如果你不知道如何開始,那麼你需要回去學習你的課程材料。如果您遇到困難,請提出問題併發布您嘗試的代碼 - 您的_own_代碼,而不是「pritchey」編寫的代碼。 – ajb 2014-09-29 19:06:27

+0

Guesser是我自己的代碼,我只是不知道如何訪問不在同一個文件中的方法。我全面搜索了它。通過查看你的代碼,其他代碼被給予我 – Joe 2014-09-29 19:20:38

回答

0

有些問題可以從不同的Java站點進行訪問,但讓你先開始第一件事:

實例化另一個類必須做到以下

GuessWhoGame gwg = new GuessWhoGame(); 

如果您需要其他構造函數,請執行以下操作

GuessWhoGame gwg = new GuessWhoGame(1); 

所以做這一部分的主要方法,那麼你可以調用,在主

同樣在GuessWhoGame的方法來發揮

Guesser.play(gwg); 

因爲你從你不能用靜態的主這樣做「this」但是必須使用名字Guesser來代替。

要獲得得分:

int s = gwg.score(); 

執行打印線:

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

@Joe看起來你不得不說玩(gwg)。觀察Guesser中的播放方法。通過查看代碼,沒有經常播放(),只有一個播放(GuessWhoGame) – Rika 2014-09-29 19:14:35

+0

假設我想訪問GuessWhoGame中的猜測方法和Score方法猜猜誰是遊戲並打印結果,我該怎麼做?我需要存儲然後執行它?比如int score = score(gwg); ? – Joe 2014-09-29 19:19:22

+0

@Joe,好吧,首先我想你不瞭解發生了什麼。所以讓我們先看看播放方法。去Guesser類並尋找play方法並查看它的參數(或括號中的內容),它有什麼內容?所以現在進入GuessWhoGame並尋找評分方法,參數(或括號中的內容)對它們有什麼影響? – Rika 2014-09-29 19:42:19

1

初始化一個GuessWhoGame對象並將其稱爲play方法。

非常基本: