toString错误返回错误的结果

时间:2013-10-04 03:59:36

标签: java

我的任务是创建4个java类来启动一个关于cribbage游戏的项目。我已完成其中3个,并且最后一个出现了toString错误。

以下是我的游戏类

的代码
public class Game implements CribbageConstants {

    private Player player1, player2;
    private boolean player1Deals;
    private Game game;

    /**
     * No argument constructor - set default values for Game
     */
    public Game() {
        player1 = new Player("?");
        player2 = new Player("?");
        player1Deals = true;



    }

    // convenience constructor
    public Game(Player player1, Player player2) {
        this.player1 = player1;
        this.player2 = player2;
        this.player1Deals = true;

    }

    //mutator
    public void setPlayer1(Player player1) {
        this.player1 = player1;

    }

    public void setPlayer2(Player player2) {
        this.player2 = player2;
    }

    public void setPlayer1Deals(boolean player1Deals) {
        this.player1Deals = player1Deals;
    }

    public void setGame(Game game) {
        this.game = game;
    }

    //utility methods
    public Player getPlayer1() {
        return player1;
    }

    public Player getPlayer2() {
        return player2;
    }

    /**
     * @return the player1Deals
     */
    public boolean isPlayer1Deals() {
        return player1Deals;
    }

    /**
     * @return the game
     */
    public Game getGame() {
        return game;
    }

//-----------utility methods------------

    public String toString() {
        return "Game between " + player1 + " and " + player2;
    }

}

这是我的testToString代码

public void testToString() {
        System.out.println("toString");
        Game instance = new Game();
        Player p1 = new Player("Jim");
        p1.setHand(hand1);
        Player p2 = new Player("George");
        p2.setHand(hand2);
        assertEquals("Game between ? (0) and ? (0)", instance.toString());
        instance.setPlayer1(p1);
        assertEquals("Game between Jim (0) and ? (0)", instance.toString());
        instance.setPlayer2(p2);
        assertEquals("Game between Jim (0) and George (0)", instance.toString());
    }

我得到的错误是:

Failed: expected <Game between ?([0) and ? (0)]> 
but was: Game between ? ([AC, AC, AC, AC, AC, AC) @0 and ? (AC, AC, AC, AC, AC, AC) @ 0]>

注意 - AC AC AC AC AC AC是上一课程中指定的默认指针。我很困惑为什么player1和player2返回默认牌而不是名字

以下是我的播放器类的代码

/*
 * player class
 */
package model;

public class Player implements CribbageConstants {

    //-----fields------
    private String name;
    private Hand hand;
    private int position;

    //---------- Constructors ---------   delete constructors for 33.3%
    /**
     * No argument constructor - set default values for card
     */
    public Player() {
        name = "?";
        hand = new Hand();
        position = 0;
    }

    // convenience constructor
    public Player(String name) {
        this.name = name;
        hand = new Hand();    
    }

    //------mutator-----
    public void setName(String name) {
        this.name = name;
    }

    public void setHand(Hand hand) {
        this.hand = hand;
    }

    public void setPosition(int position) {
        this.position = position;
    }
    //-------------- Utility methods --------------

    /**
     * Provide a text representation of a hand.
     *
     * @return the hand's cards
     */
    public String getName() {
        return name;
    }

    public Hand getHand() {
        return hand;
    }

    public int getPosition() {
        return position;
    }

//-----------utility methods------------
    public String toString() {
        return name + " (" + hand + ")" + " @ " + position;
    }
}

以下是我的toString for Player Class的代码

public void testToString() {
        System.out.println("toString");
        Player instance = new Player();
        assertEquals("? (AC, AC, AC, AC, AC, AC) @ 0", instance.toString());
        instance.setName("Alexander");
        instance.setHand(hand2);
        instance.setPosition(50);
        assertEquals("Alexander (7H, 8C, QS, AH, 4D, 4S) @ 50", instance.toString());
    }

1 个答案:

答案 0 :(得分:0)

  1. 您不需要Game类中Game类型的属性。
  2. 您需要先将新创建的玩家分配到游戏中,然后才能使用toString()它们。