Question

(Intro to Java help?) Define a class named RandomWalker. A RandomWalker object should keep track of...

(Intro to Java help?)

Define a class named RandomWalker. A RandomWalker object should keep track of its (x, y) location. All walkers start at the coordinates (0, 0). When a walker is asked to move, it randomly moves either left, right, up or down. Each of these four moves should occur with equal probability. The resulting behavior is known as a "random walk." (A 2-dimensional random walk example is pictured at right.)

Each RandomWalker object should have the following public methods. You may add whatever fields or methods you feel are necessary to implement these methods:

move()
Instructs this random walker to randomly make one of the 4 possible moves (up, down, left, or right).

getX()
Returns this random walker's current x-coordinate.

getY()
Returns this random walker's current y-coordinate.

getSteps()
Returns the number of steps this random walker has taken.

Random walks have interesting mathematical properties. For example, given infinitely many steps, a random walker approaches 100% chance of reaching a particular (x, y) coordinate. To learn more about random walks, visit http://mathworld.wolfram.com/RandomWalk.html .

Test your RandomWalker by running it with the TestRandomWalker test class, found on the Labs section of the course web site. The TestRandomWalker will run your random walker in a loop and animate its position as it moves.

import java.awt.*;

public class TestRandomWalker {
    public static final int STEPS = 500;
    
    public static void main(String[] args) {
        RandomWalker walker = new RandomWalker();                       // instantiate Walker object
        
        DrawingPanel panel = new DrawingPanel(500, 500);
        Graphics g = panel.getGraphics();
        
        // advanced features -- center and zoom in the image
        panel.getGraphics().translate(250, 250);
        panel.getGraphics().scale(4, 4);
        
        // make the walker walk, and draw its movement
        int prevX = walker.getX();                                      // initialize Walker display
        int prevY = walker.getY();
        for (int i = 1; i <= STEPS; i++) {
            g.setColor(Color.BLACK);
            g.drawLine(prevX, prevY, walker.getX(), walker.getY());     // update Walker display

            walker.move();                                              // move Walker 1 step
            prevX = walker.getX();                                      // update x value
            prevY = walker.getY();                                      // update y value
            
            g.setColor(Color.RED);                                      
            g.drawLine(prevX, prevY, walker.getX(), walker.getY());     // update Walker display

            int steps = walker.getSteps();                              // record Walker steps
            if (steps % 10 == 0) {
                System.out.println(steps + " steps");
            }
            
            panel.sleep(100);
        }
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is your class

/*

* The class RandomWalker that contains

* methods to set x and y values of random

* walker object.

*

* */

//RandomWalker.java

public class RandomWalker {

// declare instance variables

private int x;

private int y;

private int steps;

// default constructor

public RandomWalker() {

x = 0;

y = 0;

}

// Parameter constructor to set x and y values

public RandomWalker(int x, int y) {

this.x = x;

this.y = y;

}

// Method move that generates a random number

// value in a range of 0 to 1

public void move() {

// increment the steps by 1

steps++;

// generate a random value in a range of 0-1

double rand = Math.random();

if (rand < 0.25)

++x; // move to right

else if (rand < 0.5)

--y; // move to up

else if (rand < 0.75)

--x; // move to left

else if (rand < 1.0)

++y;// move to down

}

// Returns x value

public int getX() {

return x;

}

// Returns y value

public int getY() {

return y;

}

// Returns steps

public int getSteps() {

return steps;

}

}// end of RandomWalker class

Add a comment
Know the answer?
Add Answer to:
(Intro to Java help?) Define a class named RandomWalker. A RandomWalker object should keep track of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • a derived class from Organism. You must complete the constructors, and the method definitions that were...

    a derived class from Organism. You must complete the constructors, and the method definitions that were abstract methods in Organism. /** Organism.java * Definition for the Organism base class. * Each organism has a reference back to the World object so it can move * itself about in the world. */ public abstract class Organism {    protected int x, y;       // Position in the world    protected boolean moved;   // boolean to indicate if moved this turn   ...

  • Complete the following coding in java Create a class Circle that represents a round moving dot....

    Complete the following coding in java Create a class Circle that represents a round moving dot. A circle object needs to contain double variables to store the: Current X location of the circle's center Current Y location of the circle's center Radius of the circle Direction of the circle's movement in radians Speed of the circle's movement . . The Circle class must contain a constructor Circle double x, double y, double radius) that initializes the circles center x, y...

  • Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles....

    Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles. You will need to define a Triangle class containing a single instance variable, representing the length of one of the triangle’s sides. Have the program create circles, rectangles, and triangles with equal probability. Circle and Rectangle is done, please comment on your methods so i can understand */ package NervousShapes; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class...

  • (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */...

    (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */ import java.awt.Graphics; public abstract class Animal { private int x; // x position private int y; // y position private String ID; // animal ID /** default constructor * Sets ID to empty String */ public Animal( ) { ID = ""; } /** Constructor * @param rID Animal ID * @param rX x position * @param rY y position */ public Animal( String...

  • For this question you must write a java class called Rectangle and a client class called...

    For this question you must write a java class called Rectangle and a client class called RectangleClient. The partial Rectangle class is given below. (For this assignment, you will have to submit 2 .java files: one for the Rectangle class and the other one for the RectangleClient class and 2 .class files associated with these .java files. So in total you will be submitting 4 files for part b of this assignment.) // A Rectangle stores an (x, y) coordinate...

  • Simulate how many steps its take a random walker starting at the center of an 10x10...

    Simulate how many steps its take a random walker starting at the center of an 10x10 grid to visit every cell of the grid. If the walker tries to go outside of the grid then it doesn't move in that step. Write a java program which simulates th steps of the random walker, and keeps hold about the grid with 2D boolean array and write out the steps when the random walker have walked all the cells. ----------------------------------------------------------------------------------------------- I have...

  • Java Help 2. Task: Create a client for the Point class. Be very thorough with your...

    Java Help 2. Task: Create a client for the Point class. Be very thorough with your testing (including invalid input) and have output similar to the sample output below: ---After declaration, constructors invoked--- Using toString(): First point is (0, 0) Second point is (7, 13) Third point is (7, 15) Second point (7, 13) lines up vertically with third point (7, 15) Second point (7, 13) doesn't line up horizontally with third point (7, 15) Enter the x-coordinate for first...

  • Making a rectangle class in java write a simple class that will represent a rectangle. Later...

    Making a rectangle class in java write a simple class that will represent a rectangle. Later on, we will see how to do this with graphics, but for now, we will just have to use our imaginations for the visual representations ofWthe rectangles. Instance Variables Recall that an object is constructed using a class as its blueprint. So, think about an rectangle on your monitor screen. To actually draw a rectangle on your monitor screen, we need a number of...

  • Hello! This is C++. Q3. Write a program Define a Super class named Point containing: An...

    Hello! This is C++. Q3. Write a program Define a Super class named Point containing: An instance variable named x of type int. An instance variable named y of type int. Declare a method named toString() Returns a string representation of the point. Constructor that accepts values of all data members as arguments. Define a Sub class named Circle. A Circle object stores a radius (double) and inherit the (x, y) coordinates of its center from its super class Point....

  • In java language here is my code so far! i only need help with the extra...

    In java language here is my code so far! i only need help with the extra credit part For this project, you will create a Rock, Paper, Scissors game. Write a GUI program that allows a user to play Rock, Paper, Scissors against the computer. If you’re not familiar with Rock, Paper, Scissors, check out the Wikipedia page: http://en.wikipedia.org/wiki/Rock-paper-scissors This is how the program works: The user clicks a button to make their move (rock, paper, or scissors). The program...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT