java programming 2. Write a Random Walker class that randomly walks in the lefi
ID: 3716802 • Letter: J
Question
java programming
2. Write a Random Walker class that randomly walks in the lefi or rit omnWalkobserve communicates its movement to an observer that implements the Random interface direction and WalkObserver The Random Walker has the following properties Takes one step per second. You can use the Thread.sleep(1000) to r thread sleep for 1000 milliseconds or make it an event-driven class using a Has a 50/50 probability of taking a step in the left or right direction Has the facility for adding and deleting Random WalkObserver objects make the curr Timer object. ill communicate its movement to the observers using the methods in the Random WalkerObserver interface interface Random WalkObserver ( void moved(Random WalkEvent); The RandomEvent object contains the direction of movement and the reference to the Random Walker object. Write an observer class that implements Random WalkObserver interface and monitors the movement of the Random Walker. The observer should print how many steps it takes for the Random Walker to move 5, 10, 15, 20, and 25 spaces in the left or right direction from its initial starting point. (You can plot the path of the Random Walker if you like).Explanation / Answer
Here is the code you asked. Implemented all the classes RandomWalker, RandomWalkEvent, Observer etc exactly as needed and created a main method in Observer class to create a walker, register the observer and start walking until he crosses 25 spaces mark. Please note that as the probability of moving right/left is 50-50, it will take a long long time for the walker to reach 25 spaces. To test it, change the time delay of walker to 1 millisecond. Currently it is 1000 (1 second.). Thanks.
// RandomWalkerObserver.java
public interface RandomWalkerObserver {
void moved(RandomWalkEvent event);
}
// RandomWalkEvent.java
public class RandomWalkEvent {
Direction direction;
RandomWalker walker;
//constructor which takes a direction and a walker to initialize fields
public RandomWalkEvent(Direction direction,RandomWalker walker) {
this.direction=direction;
this.walker=walker;
}
}
/**
* an enumeration to represent left and right directions
*/
enum Direction {
LEFT, RIGHT;
}
// RandomWalker.java
import java.util.ArrayList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class RandomWalker {
int initialPosition;
int currentPosition;
Random random;
// list of all observers
ArrayList<RandomWalkerObserver> observersList;
Timer timer;
long TIME_INTERVAL = 1000;// 1 second time delay
/**
* constructor to initialize a walker
*/
public RandomWalker() {
random = new Random();
initialPosition = 0;// starting point
currentPosition = initialPosition;// current point
// defining empty observers list
observersList = new ArrayList<RandomWalkerObserver>();
// timer to execute the walking process
timer = new Timer();
}
/**
* method to add an observer to the list
*/
public void addObserver(RandomWalkerObserver ob) {
if (!observersList.contains(ob)) {
observersList.add(ob);
}
}
/**
* method to remove an observer from the list
*/
public void removeObserver(RandomWalkerObserver ob) {
if (observersList.contains(ob)) {
observersList.remove(ob);
}
}
/**
* method to start walking
*/
public void startWalking() {
/**
* creating a timer task with fixed delay between each runs
*/
TimerTask task = new TimerTask() {
@Override
public void run() {
Direction dir;
/**
* generating a random direction, using 50-50 probability, if
* the random object draws a true value, walker moves right,
* else left
*/
if (random.nextBoolean()) {
dir = Direction.RIGHT;
currentPosition++;// updating current position
} else {
dir = Direction.LEFT;
currentPosition--;
}
/**
* creating an event and informing all the observers about the
* event
*/
RandomWalkEvent event = new RandomWalkEvent(dir,
RandomWalker.this);
for (RandomWalkerObserver ob : observersList) {
ob.moved(event);
}
}
};
/**
* timer starts executing until stopWalking() method is called
*/
timer.schedule(task, 0, TIME_INTERVAL);
}
/**
* method to stop walking
*/
public void stopWalking() {
timer.cancel();
}
}
// Observer.java
public class Observer implements RandomWalkerObserver {
int steps;// number of steps taken by the walker
/**
* boolean flags to denote if the walker crossed 5, 10,15,20 or 25 spaces
* marks
*/
boolean hasCrossed5spaces;
boolean hasCrossed10spaces;
boolean hasCrossed15spaces;
boolean hasCrossed20spaces;
boolean hasCrossed25spaces;
public Observer() {
/**
* initializing everything
*/
steps = 0;
hasCrossed5spaces = false;
hasCrossed10spaces = false;
hasCrossed15spaces = false;
hasCrossed20spaces = false;
hasCrossed25spaces = false;
}
public void moved(RandomWalkEvent event) {
//incrementing the steps
steps++;
/**
* displaying the stats
*/
System.out.println("The walker moved " + event.direction.name());
System.out.println("Current position: " + event.walker.currentPosition);
int spacesMoved = Math.abs(event.walker.currentPosition
- event.walker.initialPosition);
if (spacesMoved == 5 && !hasCrossed5spaces) {
hasCrossed5spaces = true;
System.out.println("It took " + steps + " steps to move 5 spaces");
}
if (spacesMoved == 10 && !hasCrossed10spaces) {
hasCrossed10spaces = true;
System.out.println("It took " + steps + " steps to move 10 spaces");
}
if (spacesMoved == 15 && !hasCrossed15spaces) {
hasCrossed15spaces = true;
System.out.println("It took " + steps + " steps to move 15 spaces");
}
if (spacesMoved == 20 && !hasCrossed20spaces) {
hasCrossed20spaces = true;
System.out.println("It took " + steps + " steps to move 20 spaces");
}
if (spacesMoved == 25 && !hasCrossed25spaces) {
hasCrossed25spaces = true;
System.out.println("It took " + steps + " steps to move 25 spaces");
/**
* walker moved 25 spaces, so stopping the walk.
*/
event.walker.stopWalking();
}
}
public static void main(String[] args) {
/**
* Creating a walker
*/
RandomWalker walker = new RandomWalker();
/**
* Creating an observer
*/
Observer observer = new Observer();
/**
* adding observer to the walker
*/
walker.addObserver(observer);
/**
* start walking
*/
walker.startWalking();
}
}
/*OUTPUT (partial)*/
The walker moved LEFT
Current position: -1
The walker moved RIGHT
Current position: 0
The walker moved LEFT
Current position: -1
The walker moved RIGHT
Current position: 0
The walker moved LEFT
Current position: -1
The walker moved RIGHT
Current position: 0
The walker moved LEFT
Current position: -1
The walker moved RIGHT
Current position: 0
The walker moved RIGHT
Current position: 1
The walker moved LEFT
Current position: 0
The walker moved RIGHT
Current position: 1
The walker moved RIGHT
Current position: 2
The walker moved RIGHT
Current position: 3
..
..
Current position: -5
It took 43 steps to move 5 spaces
..
..
..
The walker moved RIGHT
Current position: 24
The walker moved RIGHT
Current position: 25
It took 951 steps to move 25 spaces
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.