• Post Reply Bookmark Topic Watch Topic
  • New Topic

Returning a reference to a specific barrier.  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to write a getter getBarrier that returns a reference to a specific barrier(circles) in an array. For example, a call to getBarrier(2) should return a reference to the third barrier (since the first barrier has the index 0). There are 4 classes all up that include the GameObject class, GameBarriors class, a GameEngine class and the main class. I also have a test class which keeps telling me that I can't invoke setX(int) and getX() on the primitive type int. (using Eclipse).

Should the array in the barriers class be private GameObject[] barriers? Or int[] like I have it at the moment.

So confused!

Below is the JUnit test method for the getBarrier method.


Below is the barriers class and the getBarrier method I have so far.



 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, from the design point of view you have to store the actual barriers in your code somewhere, don't you? So far you don't have any GameBarrier objects anywhere as far as I can see.

And also your test code contains this call:



You'll notice that the getBarrier(2) method is supposed to return some object with a getX() method. An int isn't an object and it doesn't have a getX() method, so getBarrier(2) shouldn't return an int.

Does that help?
 
Veronica Love
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul,

If I may ask, when you say store the actual GameObject barriers and if I'm correct, is this not already done from the main class? The below code shows you what I mean.


I'm confused because this main class has an instance variable public static final int NUM_BARRIERS = 5; in it so why do I need a constructor in my GameBarriors class initializing more GameObjects? Isn't the 'size' in the below code referring to how many GameObjects I want stored in the array? With the for loop in the constructor adding the objects into the array?



Sorry all this probably doesn't even make sense
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!