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

2018/3/5

Testing Act() Method

tonym840 tonym840

2018/3/5

#
Hey guys, I have an ant that moves throughout a Lawn world. The ant always starts facing east. It moves by moving one space forward then taking a random turn using: public void randomTurn() { boolean andy = Random.generator().nextBoolean(); if (andy) { this.turn(90); } else { this.turn(-90) } } Now when I go to test it, I have: public void testAct() { beach = new Beach(1); beach.addTurtle(); Turtle.act(); } I'm not sure which assertions to put. I want to test if my ant turned right or left, but since it's random, I won't know which happened. Thoughts?
danpost danpost

2018/3/5

#
Are you having one world create a test world called Beach for the Turtle object? and where is the ant you mentioned? Definitely need more background info. Also, are you wanting to make the assertion in your code or just for your information?
danpost danpost

2018/3/5

#
What is this:
boolean andy = Random.generator().nextBoolean();
The java.util.Random class does not have any method with the name 'generator' ('static' or otherwise). It does have a 'nextBoolean' method; but, you would need to call that on a Random object -- not on the class:
Random rand = new Random(); // creating an instance of the Random class (a Random object)
// ****************
if (rand.nextBoolean())
// etc.
However, you can just as well do something like this:
turn(180*Greenfoot.getRandomNumber(2)-90);
for a random turn.
You need to login to post a reply.