Write a class Bug that models a bug moving along a horizontal line. The bug move
ID: 3827356 • Letter: W
Question
Write a class Bug that models a bug moving along a horizontal line. The bug moves either to the right or left. Initially, the bug moves to the right but at random times it will turn and change its direction. In each move, its position changes by one unit in the current direction. Provide a constructor: public Bug(int initialPosition) Provide methods: void turn (), void move (), int getPosition(). The main program should construct three bugs, have them all start at position 5 (use the constructor!), make them move and turn them approximately 1/4 of the time but not necessarily at the same time. Use a loop to make the bugs move 10 times with an occasional turn. Have a race between the bugs and print the position of each bug after each move and declare a winner at the end of 10 moves. Sample output On your mark ... get set ... Position of bugs 6 6 6 7 7 7 8 8 8 9 9 7 10 10 6 11 11 7 12 12 6 13 13 5 14 12 4 15 11 5 The winner is bug 1Explanation / Answer
Below is the Class Bug which has the functions move() , turn() and getPosition()
package BugSolution;
public class Bug {
int position ;
public Bug(int initialPosition){
this.position = initialPosition;
}
public void move(){
position++;
}
public void turn(){
position--;
}
public int getPosition(){
return position;
}
}
Below is the main Class which does the main activity of competing
package BugSolution;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
//contains the maon function which initiates the competition
public class Compete {
// initialise three bugs with initail position 5;
Bug bugA = new Bug(5);
Bug bugB = new Bug(5);
Bug bugC = new Bug(5);
List<Bug> bugList = new ArrayList();
public Compete(){
bugList.add(bugA);
bugList.add(bugB);
bugList.add(bugC);
initCompetion();
}
public static void main(String[] args){
new Compete();
}
private void initCompetion() {
System.out.println("On your mark .. get set ...");
System.out.println("Position of Bugs");
Random rand = new Random();
for(int i = 0 ; i<10 ; i++){
for(Bug bug : bugList){
//generate the random number among 1 and 2
// if 1 call move()
// if 2 call turn()
int n = rand.nextInt(2) + 1;
switch(n){
case 1:
bug.move();
break;
case 2:
bug.turn();
break;
}
System.out.print(bug.getPosition()+" ");
}
// print a new line.
System.out.println();
}
int winner = 0;
int tempPosition = -9999;
int count = 0;
//loop to decide the winner among the three bugs
//find the Bug having the maximum position
for(Bug bug : bugList){
count++;
if(count == 1){
tempPosition = bug.getPosition();
winner++ ;
}else {
//compare the position with the previous maximum position
if(tempPosition < bug.getPosition()){
tempPosition = bug.getPosition();
winner = count;
}
}
}
System.out.println("The winner is bug " + winner);
}
}
One of the outputs is as follows :
On your mark .. get set ...
Position of Bugs
6 6 4
5 7 5
4 6 4
3 7 5
2 8 4
3 9 3
2 10 4
3 9 5
4 10 6
3 9 7
The winner is bug 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.