Results 1 to 20 of 22
Thread: Need help, beginner here
- 10-01-2011, 09:37 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 11
- Rep Power
- 0
Need help, beginner here
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:
Java 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; } }And the test packages they need to correspond to:Java Code: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; } }
Java Code: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()); } }The errors I notice in the test packages are:Java Code: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)); } }
"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.Last edited by Fubarable; 10-01-2011 at 09:41 PM. Reason: quote tags changed to code tags
-
Re: Need help, beginner here
Moderator Edit: [quote] / [/quote] tags changed to [code] / [/code] tags so that the code would be readable.
Regarding your error, we can help you better if you actually show us the complete error message.
- 10-01-2011, 09:52 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 11
- Rep Power
- 0
-
Re: Need help, beginner here
Please show the error text here as text, and again tell us which line of which class is causing the error.
-
Re: Need help, beginner here
OK, your Player class has only two available constructors, can you find them? Once you find them, please list what parameters are necessary to call each constructor, and list the types for both constructors here just as the error message is listing them.
- 10-01-2011, 10:00 PM #6
Member
- Join Date
- Oct 2011
- Posts
- 11
- Rep Power
- 0
Re: Need help, beginner here
The lines causing errors are in GameTest package:
line 51 and line 181-183.
In the PlayerTest package:
line 44, 172 and line 191-193.
Errors in text after I run it? I made a picture of it in the last response because they are long to right out.
But basically it's saying a TestConvenienceConstructor, a TestToString, and TestEquals failed for the player.
In the game test it's saying the errors are the TestConvenienceConstructor and TestSetPlayer.
-
Re: Need help, beginner here
See my comment in post #5 of this thread.
- 10-01-2011, 10:02 PM #8
Member
- Join Date
- Oct 2011
- Posts
- 11
- Rep Power
- 0
Re: Need help, beginner here
My two constructors for the player class I made previously:
Again, I'm just starting in Java and not very experienced so I was unaware of what proper constructors in this class I was to use.Java Code: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; }
- 10-01-2011, 10:04 PM #9
Member
- Join Date
- Oct 2011
- Posts
- 11
- Rep Power
- 0
Re: Need help, beginner here
The errors are saying in the player class it's the testconvenienceconstructor that's failing. Is that the second or first one? I thought the first is the default.
-
Re: Need help, beginner here
The excericse I want you to do is to list the parameters types for each constructor -- and you're not doing this :(.
I guess I have to do this part for you:
constructor 1: no parameters
constructor 2: String, int, int, int int
The reason this is important is that when you call a Player constructor it must pass either no parameters or five parameters, one String and 4 ints in that order. You're trying to call a constructor that takes one String and one int, and that just doesn't exist -- you simply can't do this. Instead chose one of the two constructors that you can use.
- 10-01-2011, 10:12 PM #11
Member
- Join Date
- Oct 2011
- Posts
- 11
- Rep Power
- 0
Re: Need help, beginner here
Okay so you're saying for the first constructor do the one string four ints and in the next constructor it's the no argument one?
-
Re: Need help, beginner here
No, I'm not saying anything of the sort. You are only allowed to use one or the other constructor and can't make up a constructor. I'm saying use the one constructor of the two that is most appropriate for your need at the time. You'll have to choose which one to use, but if you use the one that takes parameters, be sure to include all parameters.
- 10-01-2011, 10:17 PM #13
Member
- Join Date
- Oct 2011
- Posts
- 11
- Rep Power
- 0
Re: Need help, beginner here
All parameters as in the declared variables?
In the stock class the teacher showed us and did all parameters in both constructors, so I'm a little confused.
- 10-01-2011, 10:21 PM #14
Member
- Join Date
- Oct 2011
- Posts
- 11
- Rep Power
- 0
Re: Need help, beginner here
It's something wrong with the second constructor in the player class.
If we take out the first one, the test goes down to 0 percent so the first constructor is right. I need help on the second one which probably involves the newPlayer and their names and holdings.
-
Re: Need help, beginner here
- 10-01-2011, 10:26 PM #16
Member
- Join Date
- Oct 2011
- Posts
- 11
- Rep Power
- 0
Re: Need help, beginner here
I just want this thing cleared of errors and need help on it.
You've seemed to have narrowed down the second constructor in the player class being the issue, and I can't seem to think of a good constructor that will take out errors such as the New Player and their holdings.
-
Re: Need help, beginner here
Or are you allowed to change the Player class, to add a new constructor if necessary?
- 10-01-2011, 10:31 PM #18
Member
- Join Date
- Oct 2011
- Posts
- 11
- Rep Power
- 0
Re: Need help, beginner here
Yes you can change the player class and add a constructor or any code you want.
-
Re: Need help, beginner here
Ah, then that's another possible solution -- create a constructor that takes a String and an int parameter, and then you're in business!
- 10-02-2011, 12:33 AM #20
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Similar Threads
-
beginner
By Eiolvit in forum New To JavaReplies: 4Last Post: 07-10-2011, 05:32 PM -
Beginner
By gozuhair in forum New To JavaReplies: 12Last Post: 07-10-2011, 03:14 PM -
Need Help - Beginner
By ooooohmaul in forum New To JavaReplies: 4Last Post: 08-08-2010, 04:22 AM -
beginner here...help please
By shroomiin in forum New To JavaReplies: 6Last Post: 09-15-2009, 11:06 PM -
almost done...beginner needs help plz..
By shongo in forum New To JavaReplies: 15Last Post: 11-10-2008, 08:14 AM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks