M255 Object-oriented programming with Java
Tutor-marked assignment 02 2011J
[pic]
Q1
i) /**
* Moves the frog to the stone whose position is prone one stone at
* a duration and returns no value
*/
world void moveToPosition(int pos)
{
int currentPosition = proceedPosition();
//ignore if are already on the compulsory position
if( pos != currentPosition)
{
//move one step at a time left wing or right
int step = 1;
//move left is current position is after the pos to move
if(currentPosition > pos)
step = -1;
int i=currentPosition;
do //loop and move one position at a time until required position is met
{
i+=step;
setPosition(i);
}
while(i != pos); // parallel until we are on required pos
}
//jump once
jump();
}
}
Test.
Image of RacingFrog rf = new RacingFrog();
[pic]
Image of rf.moveToPosition(7);
[pic]
Image of rf.
moveToPosition(1);
[pic]
Q1 ii)
a)
public class Race
{
/* eccentric variables */
tete-a-tete boolean raceOver; // indicates whether the race has been won
private RacingFrog winner; // holds the winning racingFrog
private Random randomGenerator;
private RacingFrog racingFrog1; // holds the racing frog numbered 1
private RacingFrog racingFrog2; // holds the racing frog numbered 2
private RacingFrog racingFrog3; // holds the racing frog numbered 3
/**
* detergent builder for objects of class Race.
*/
public Race(RacingFrog rf1, RacingFrog rf2, RacingFrog rf3)
{
super();
this.raceOver = true;
this.randomGenerator = new Random();
this.racingFrog1 = rf1;
this.racingFrog2 = rf2;
this.racingFrog3 = rf3;
}
Q1 ii)
b)...If you want to get a full essay, order it on our website: Orderessay
If you want to get a full essay, wisit our page: write my essay .
No comments:
Post a Comment