Okay so I have read the guidelines and don't believe this post is against the rules or against the rules for asking for help because I have put in an effort and I'm around 70 percent in testing the files. I just need help with this basic Java code to see what I'm doing wrong.

We just have to make the three classes test at 100 percent and correspond to the test packages.

Here is my code:

package model;

public class Player {
    private String name;
    private int cash;
    private int Holding1;
    private int Holding2;
    private int Holding3;
    
  
    /**
     * @return the name
     */
    
    public Player() {
        name="?";
                cash=5000;
                Holding1=0;
                        Holding2=0;
                        Holding3=0;
    }
    
    public Player(String MyName, int MyCash, int MyHolding1, int MyHolding2, int MyHolding3){
    name = MyName;
            cash = MyCash;
            Holding1=MyHolding1;
                    Holding2=MyHolding2;
                            Holding3=MyHolding3;
            
                    
    }
    
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the cash
     */
    public int getCash() {
        return cash;
    }

    /**
     * @param cash the cash to set
     */
    public void setCash(int cash) {
        this.cash = cash;
    }

    /**
     * @return the Holding1
     */
    public int getHolding1() {
        return Holding1;
    }

    /**
     * @param Holding1 the Holding1 to set
     */
    public void setHolding1(int Holding1) {
        this.Holding1 = Holding1;
    }

    /**
     * @return the Holding2
     */
    public int getHolding2() {
        return Holding2;
    }

    /**
     * @param Holding2 the Holding2 to set
     */
    public void setHolding2(int Holding2) {
        this.Holding2 = Holding2;
    }

    /**
     * @return the Holding3
     */
    public int getHolding3() {
        return Holding3;
    }

    /**
     * @param Holding3 the Holding3 to set
     */
    public void setHolding3(int Holding3) {
        this.Holding3 = Holding3;
    }
    
    
}
package model;

public class Game {
    private boolean Started;
    private boolean Over;
    private Player player;
    private Stock stock1;
    private Stock stock2;
    private Stock stock3;
    

    /**
     * @return the Started
     */
    
    public Game(){    
        Started = false;
     Over = false;
     player=null;
    stock1=null;
            stock2=null;
            stock3=null;
            }
   
   public Game(Stock gold, Stock oil, Stock grain, Player george){       
       setStock1(gold);       
       setStock2(oil);       
       setStock3(grain);              
   setPlayer(george);}

    /**
     * @return the Started
     */
    public boolean isStarted() {
        return Started;
    }

    /**
     * @param Started the Started to set
     */
    public void setStarted(boolean Started) {
        this.Started = Started;
    }

    /**
     * @return the Over
     */
    public boolean isOver() {
        return Over;
    }

    /**
     * @param Over the Over to set
     */
    public void setOver(boolean Over) {
        this.Over = Over;
    }

    /**
     * @return the player
     */
    public Player getPlayer() {
        return player;
    }

    /**
     * @param player the player to set
     */
    public void setPlayer(Player player) {
        this.player = player;
    }

    /**
     * @return the stock1
     */
    public Stock getStock1() {
        return stock1;
    }

    /**
     * @param stock1 the stock1 to set
     */
    public void setStock1(Stock stock1) {
        this.stock1 = stock1;
    }

    /**
     * @return the stock2
     */
    public Stock getStock2() {
        return stock2;
    }

    /**
     * @param stock2 the stock2 to set
     */
    public void setStock2(Stock stock2) {
        this.stock2 = stock2;
    }

    /**
     * @return the stock3
     */
    public Stock getStock3() {
        return stock3;
    }

    /**
     * @param stock3 the stock3 to set
     */
    public void setStock3(Stock stock3) {
        this.stock3 = stock3;
    }

}

and the test packages they need to correspond to

package model;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

public class GameTest {

    public GameTest() {
    }

public void testDefaultConstructor() {
        System.out.println("Constructor");
        Game instance = new Game();
        // everything has better be false or null!
        assertEquals("Default setting - started", false, instance.isStarted());
        assertEquals("Default setting - over", false, instance.isOver());
        assertNull("Default setting - player", instance.getPlayer());
        assertNull("Default setting - stock1", instance.getStock1());
        assertNull("Default setting - stock2", instance.getStock2());
        assertNull("Default setting - stock3", instance.getStock3());
    }

    /**
     * Test the convenience Stock constructor
     */
    @Test
    public void testConvenienceConstructor() {
        System.out.println("Constructor (convenience)");

        // Setup some dummy data
        Stock gold = new Stock("GOLD", "Mining", 100);
        Stock oil = new Stock("OIL", "Resources", 100);
        Stock grain = new Stock("GRAN", "Farming", 100);
        Player george = new Player("George", 5000);

        // And make a game out of these
        Game game = new Game(gold, oil, grain, george);

        assertEquals("Default setting - started", false, game.isStarted());
        assertEquals("Default setting - over", false, game.isOver());
        assertEquals("Specified setting - player", george, game.getPlayer());
        assertEquals("Specified setting - stock1", gold, game.getStock1());
        assertEquals("Specified setting - stock2", oil, game.getStock2());
        assertEquals("Specified setting - stock3", grain, game.getStock3());
    }

    /**
     * Test of isStarted method, of class Game.
     */
    @Test
    public void testStarted() {
        System.out.println("setStarted");
        Game instance = new Game();
        boolean expResult = false;
        instance.setStarted(expResult);
        assertEquals("Setting started flag off", expResult, instance.isStarted());
        expResult = true;
        instance.setStarted(expResult);
        assertEquals("Setting started flag on", expResult, instance.isStarted());
    }

    /**
     * Test of isOver method, of class Game.
     */
    @Test
    public void testOver() {
        System.out.println("setOver");
        Game instance = new Game();
        boolean expResult = false;
        instance.setOver(expResult);
        assertEquals("Setting Over flag off", expResult, instance.isOver());
        expResult = true;
        instance.setOver(expResult);
        assertEquals("Setting Over flag on", expResult, instance.isOver());
    }

    /**
     * Test of setStock1 method, of class Game.
     */
    @Test
    public void testSetStock1() {
        System.out.println("setStock1");
        Stock gold = new Stock("GOLD", "Mining", 100);
        Stock oil = new Stock("OIL", "Resources", 100);
        Stock grain = new Stock("GRAN", "Farming", 100);

        // test what should be good values
        Stock[] good = {gold, oil, grain};
        for (Stock expResult : good) {
            Game instance = new Game();
            instance.setStock1(expResult);
            assertEquals("Good stock1 rejected", expResult, instance.getStock1());
        }
        // test what will be bad values, but they're ok at this point        
        Stock[] bad = {null};
        for (Stock expResult : bad) {
            Game instance = new Game();
            instance.setStock1(expResult);
            assertEquals("Bad stock1 prematurely rejected", expResult, instance.getStock1());
        }
    }

    /**
     * Test of setStock2 method, of class Game.
     */
    @Test
    public void testSetStock2() {
        System.out.println("setStock2");

        Stock gold = new Stock("GOLD", "Mining", 100);
        Stock oil = new Stock("OIL", "Resources", 100);
        Stock grain = new Stock("GRAN", "Farming", 100);

        // test what should be good values
        Stock[] good = {gold, oil, grain};
        for (Stock expResult : good) {
            Game instance = new Game();
            instance.setStock2(expResult);
            assertEquals("Good stock2 rejected", expResult, instance.getStock2());
        }
        // test what will be bad values, but they're ok at this point
        Stock[] bad = {null};
        for (Stock expResult : bad) {
            Game instance = new Game();
            instance.setStock2(expResult);
            assertEquals("Bad stock1 prematurely rejected", expResult, instance.getStock2());
        }
    }

    /**
     * Test of setStock3 method, of class Game.
     */
    @Test
    public void testSetStock3() {
        System.out.println("setStock3");

        Stock gold = new Stock("GOLD", "Mining", 100);
        Stock oil = new Stock("OIL", "Resources", 100);
        Stock grain = new Stock("GRAN", "Farming", 100);

        // test what should be good values
        Stock[] good = {gold, oil, grain};
        for (Stock expResult : good) {
            Game instance = new Game();
            instance.setStock3(expResult);
            assertEquals("Good stock3 rejected", expResult, instance.getStock3());
        }
        // test what will be bad values, but they're ok at this point
        Stock[] bad = {null};
        for (Stock expResult : bad) {
            Game instance = new Game();
            instance.setStock3(expResult);
            assertEquals("Bad stock3 prematurely rejected", expResult, instance.getStock3());
        }
    }

    /**
     * Test of setPlayer method, of class Game.
     */
    @Test
    public void testSetPlayer() {
        System.out.println("setPlayer");

        Player tom = new Player("Tom", 1000);
        Player dick = new Player("Dick", 50000);
        Player harry = new Player("Harry", 5000);
        Player joe = new Player();

        // test what should be good values
        Player[] good = {tom, dick, harry};
        for (Player expResult : good) {
            Game instance = new Game();
            instance.setPlayer(expResult);
            assertEquals("Good player rejected", expResult, instance.getPlayer());
        }
        // test what will be bad values, but they're ok at this point
        Player[] bad = {null, joe};
        for (Player expResult : bad) {
            Game instance = new Game();
            instance.setPlayer(expResult);
            assertEquals("Bad player prematurely rejected", expResult, instance.getPlayer());
        }
    }
package model;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

public class PlayerTest {

    public PlayerTest() {
    }

    /**
     * Test the default Player constructor
     */
    @Test
    public void testDefaultConstructor() {
        System.out.println("Constructor");
        Player instance = new Player();
        assertEquals("Default setting - name", "?", instance.getName());
        assertEquals("Default setting - cash", 5000, instance.getCash());
        assertEquals("Default setting - holding1", 0, instance.getHolding1());
        assertEquals("Default setting - holding2", 0, instance.getHolding2());
        assertEquals("Default setting - holding3", 0, instance.getHolding3());
    }

    /**
     * Test the convenience Player constructor
     */
    @Test
    public void testConvenienceConstructor() {
        System.out.println("Constructor (convenience)");
        Player instance = new Player("Henri", 2000);
        assertEquals("Default setting - name", "Henri", instance.getName());
        assertEquals("Default setting - cash", 2000, instance.getCash());
        assertEquals("Default setting - holding1", 0, instance.getHolding1());
        assertEquals("Default setting - holding2", 0, instance.getHolding2());
        assertEquals("Default setting - holding3", 0, instance.getHolding3());
    }

    /**
     * Test of setName method, of class Player.
     */
    @Test
    public void testSetName() {
        System.out.println("setName");
        // test what should be good values
        String[] good = {"George", "Henri", "StRaNgE_bUt_lEgAl"};
        for (String expResult : good) {
            Player instance = new Player();
            instance.setName(expResult);
            assertEquals("Good name rejected", expResult, instance.getName());
        }
        // test what will be bad values, but they're ok at this point
        String[] bad = {"", null, "James L", "Hmm - I wonder if we should have a restriction on the length of a name, or should we allow ridiculously long stock names?"};
        for (String expResult : bad) {
            Player instance = new Player();
            instance.setName(expResult);
            assertEquals("Bad name prematurely rejected", expResult, instance.getName());
        }
    }

    /**
     * Test of setCash method, of class Player.
     */
    @Test
    public void testSetCash() {
        System.out.println("setCash");
        // test what should be good values
        int[] good = {50, 500, 1000, 5000, 100000};
        for (int expResult : good) {
            Player instance = new Player();
            instance.setCash(expResult);
            assertEquals("Good value rejected", expResult, instance.getCash());
        }
        // test what will be bad values, but they're ok at this point
        int[] bad = {-5, 0, 201, 1, 2, 77};
        for (int expResult : bad) {
            Player instance = new Player();
            instance.setCash(expResult);
            assertEquals("Bad value prematurely rejected", expResult, instance.getCash());
        }
    }

    /**
     * Test of setHolding1 method, of class Player.
     */
    @Test
    public void testSetHolding1() {
        System.out.println("setHolding1");
        // test what should be good values
        int[] good = {0, 50, 500, 1000, 5000, 100000};
        for (int expResult : good) {
            Player instance = new Player();
            instance.setHolding1(expResult);
            assertEquals("Good value rejected", expResult, instance.getHolding1());
        }
        // test what will be bad values, but they're ok at this point
        int[] bad = {-5, 201, 1, 2, 77};
        for (int expResult : bad) {
            Player instance = new Player();
            instance.setHolding1(expResult);
            assertEquals("Bad value prematurely rejected", expResult, instance.getHolding1());
        }
    }

    /**
     * Test of setHolding2 method, of class Player.
     */
    @Test
    public void testSetHolding2() {
        System.out.println("setHolding2");
        // test what should be good values
        int[] good = {0, 50, 500, 1000, 5000, 100000};
        for (int expResult : good) {
            Player instance = new Player();
            instance.setHolding2(expResult);
            assertEquals("Good value rejected", expResult, instance.getHolding2());
        }
        // test what will be bad values, but they're ok at this point
        int[] bad = {-5, 201, 1, 2, 77};
        for (int expResult : bad) {
            Player instance = new Player();
            instance.setHolding2(expResult);
            assertEquals("Bad value prematurely rejected", expResult, instance.getHolding2());
        }
    }

    /**
     * Test of setHolding3 method, of class Player.
     */
    @Test
    public void testSetHolding3() {
        System.out.println("setHolding3");
        // test what should be good values
        int[] good = {0, 50, 500, 1000, 5000, 100000};
        for (int expResult : good) {
            Player instance = new Player();
            instance.setHolding3(expResult);
            assertEquals("Good value rejected", expResult, instance.getHolding3());
        }
        // test what will be bad values, but they're ok at this point
        int[] bad = {-5, 201, 1, 2, 77};
        for (int expResult : bad) {
            Player instance = new Player();
            instance.setHolding3(expResult);
            assertEquals("Bad value prematurely rejected", expResult, instance.getHolding3());
        }
    }

    /**
     * Test of toString method, of class Player.
     */
    @Test
    public void testToString() {
        System.out.println("toString");
        Player instance = new Player();
        String expResult = "?... $5000 cash and 0/0/0 holdings";
        String result = instance.toString();
        assertEquals("Text representation from default constructor", expResult, result);
        instance = new Player("John", 111);
        expResult = "John... $111 cash and 0/0/0 holdings";
        result = instance.toString();
        assertEquals("Text representation from convenience constructor", expResult, result);
        instance.setHolding1(500);
        instance.setHolding2(1000);
        instance.setHolding3(500);
        expResult = "John... $111 cash and 500/1000/500 holdings";
        result = instance.toString();
        assertEquals("Text representation from convenience constructor", expResult, result);
    }

    /**
     * Test of equals method, of class Player.
     */
    @Test
    public void testEquals() {
        System.out.println("equals");

        Player tom = new Player("Tom", 1000);
        Player harry = new Player("Harry", 5000);
        Player henri = new Player("Harry", 100);

        // identical objects
        assertTrue("Tom is Tom", tom.equals(tom));

        // same objects
        assertTrue("Harry and Henri are the same", harry.equals(henri));

        // not same objects
        assertFalse("Tom and Harry are different", tom.equals(harry));

    }
}

The errors I notice in the test packages are:

"Player tom = new Player("Tom", 1000);
Player harry = new Player("Harry", 5000);
Player henri = new Player("Harry", 100);"

"instance = new Player("John", 111);"

"Player george = new Player("George", 5000);"

So it's something to do with that and I'm stuck. There is no advanced code needed, just wonder if it's something to do with the player class or game class and what code is needed to take it out.

Recommended Answers

All 2 Replies

The errors I notice in the test packages are:

Can you explain what the errors are?
What happens when you compile and execute the code?

Look at your line 15 and 23 which are the constructor declaration. Your have only Player() and Player(String, int, int, int, int) but not Player(String, int) which you are trying to initiate. That's why you get the error. You need to implement another constructor as...

public Player(String MyName, int MyCash) {
  ...
}

That should be what you are looking for.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.