This site requires JavaScript, please enable it in your browser!
Greenfoot back
rockon411
rockon411 wrote ...

2017/3/21

Returning a List of Actor objects

rockon411 rockon411

2017/3/21

#
I have a code that has a actor which eats other actors. I am supposed to create a method that returns a list of all the food objects that have been eaten and add a field that holds a list of actor objects. I don't even know where to begin so any help would be greatly appreciated.
Nosson1459 Nosson1459

2017/3/22

#
It could be that some people will be able to help you on this but I think that you should read this.
Super_Hippo Super_Hippo

2017/3/22

#
I am not sure why you want to save the food which was eaten, but it could be done like this:
//outside class
import java.util.ArrayList;

//in class, outside methods
private ArrayList<Food> eatenFood = new ArrayList<Food>(0);

//in act method
Food food = (Food) getOneIntersectingObject(Food.class);
if (food != null)
{
    eatenFood.add(food); //add the food object to the list
    getWorld().removeObject(food); //remove the food from the world
}

//method which returns the list
public ArrayList<Food> getEatenFood() {return eatenFood;}
rockon411 rockon411

2017/3/22

#
Thanks.
public void eat(Minnow nemo)
    {
        Minnow food = (Minnow) getOneIntersectingObject(Minnow.class);
        if (getOneIntersectingObject(Minnow.class) != null)
        {
            eatenFood.add(food);
            getWorld().remove(food);
        }
    }
    
    public ArrayList<Minnow> getStomachContents()
    {
        return eatenFood;
    }
I wrote it all out changing the classes and it compiles, but my tests currently do not pass. It seems to be really small but I can't seem to fix it
public void testGetStomachContents()
    {
        Minnow nemo = new Minnow();
        ocean.add(nemo, 50, 50);
        ocean.add(shark, 50, 50);
        
        shark.eat(nemo);

        assertEquals(0, ocean.getObjects(Minnow.class).size());
        assertEquals((nemo), shark.getStomachContents()); 
    }
It expected: <Minnow at (50, 50)> but was: the same thing with brackets. I've tried adding the brackets to the expected but then it won't compile.
Super_Hippo Super_Hippo

2017/3/22

#
The 'testGetStomachContents' method: - from where is it called? - what do you want it to do? Also, your eat method shouldn't have a parameter. It is not used anyways.
rockon411 rockon411

2017/3/22

#
That method is in SharkTest, a TestCase that I created. I need the testGetStomachContents to test that getStomachContents returns a list of actors. It seems to return the list fine in a Minnow at (x, y), Minnow at (x, y) format, but the method returns the list with brackets around it so it does not pass. And thanks I changed the eat method.
Super_Hippo Super_Hippo

2017/3/22

#
The methods returns the list. The question is what you are trying to do with it. Not sure what you are doing with the assertEquals method. So the method is in SharkTest (actor?) and you add a shark and a nemo to ocean? I am bit confused.
rockon411 rockon411

2017/3/22

#
I am just trying to test the getStomachContents method that was created by having a shark eat the minnow and seeing if there is one in the "stomach."
Super_Hippo Super_Hippo

2017/3/22

#
But what is the issue with the brackets now?
rockon411 rockon411

2017/3/22

#
When I run my test, it fails and I receive a message that says
expected: <Minnow at (50, 50)> but was: <[Minnow at (50, 50)]>
Super_Hippo Super_Hippo

2017/3/22

#
Where do you get that error?
rockon411 rockon411

2017/3/22

#
It's at line 195 of the SharkTest code which is the assertEquals(nemo... line of this code
public void testGetStomachContents()
    {
        Minnow nemo = new Minnow();
        ocean.add(nemo, 50, 50);
        ocean.add(shark, 50, 50);
        
        shark.eat(nemo);

        assertEquals(0, ocean.getObjects(Minnow.class).size());
        assertEquals(nemo, shark.getStomachContents()); 
    }
Super_Hippo Super_Hippo

2017/3/23

#
Show the assertEquals method. Oh and it doesn't look like you changed the eat method.
Nosson1459 Nosson1459

2017/3/23

#
Does the second parameter for the assertEquals method match shark.getStomachContents()?
rockon411 rockon411

2017/3/23

#
Nevermind! It seems to work now. Thank you so much for your help!
You need to login to post a reply.