Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this project you will develop a class that models a random walk and write a c

ID: 3762834 • Letter: I

Question

In this project you will develop a class that models a random walk and write a client program that use the class. Several core concepts we introduced in Chapter 7, will be utilized, such as large method decomposition, and method overload. A random walk is basically a sequence of steps in some enclosed space where the direction of each step is random. The walk terminates either when a maximum number of steps has been taken or a step goes outside of the boundary of the space. Random walks are used to model physical phenomena such as the motion of molecules and economic phenomena such as stock prices.

We will assume that the random walk takes place on a square grid with the point (0,0) at the center. The boundary of the square will be a single integer that represents the maximum x and y coordinate for the current position on the square (so for a boundary value of 10, both the x and y coordinates can vary from -10 to 10, inclusive). Each step will be one unit up, one unit down, one unit to the left, or one unit to the right. (No diagonal movement.)

The RandomWalk class will have the following instance data (all type int):

the x coordinate of the current position

the y coordinate of the current position

the maximum number of steps in the walk

the number of steps taken so far in the walk

the boundary of the square (a positive integer -- the x and y coordinates of the position can vary between plus and minus this value)

Create a new file RandomWalk.java. You’ll define the RandomWalk class incrementally testing each part as you go.

First declare the instance data (as described above) and add the following two constructors and toString method.

RandomWalk (int max, int edge) - Initializes the RandomWalk object. The maximum number of steps and the boundary are given by the parameters. The x and y coordinates and the number of steps taken should be set to 0.

RandomWalk (int max, int edge, int startX, int startY) -- Initializes the maximum number of steps, the boundary, and the starting position to those given by the parameters.

String toString() - returns a String containing the number of steps taken so far and the current position -- The string should look something like: Steps: 12; Position: (-3,5)

Compile what you have so far then open the file TestWalk.java. This file will be used to test your RandomWalk methods. So far it prompts the user to enter a boundary, a maximum number of steps, and the x and y coordinates of a position. Add the following in the driver program:

HERE IS TESTWALK.JAVA

Declare and instantiate two RandomWalk objects -- one with boundary 5, maximum steps 10, and centered at the origin (use the two parameter constructor) and the other with the values entered by the user.

Print out each object. Note that you won’t get any information about the boundary or maximum number of steps (think about what your toString method does), but that’s ok.

Compile and run the program to make sure everything is correct so far before you move to next step.

Next add the following method to the RandomWalk class: void takeStep(). This method simulates taking a single step either up, down, left, or right. To “take a step” generate a random number with 4 values (say 0, 1,2, 3) then use a switch statement to change the position (one random value will represent going right, one left, and so on). Your method should also increment the number of steps taken.

Add a for-loop to TestWalk.java to have each of your RandomWalk objects take 5 steps. Print out each object after each step so you can see what is going on. Compile and run the program to make sure it is correct so far before you move to next step.

Now add to RandomWalk.java the following two methods. Each should be a single return statement that returns the value of a boolean expression.

boolean moreSteps() - returns true if the number of steps taken is less than the maximum number; returns false otherwise

boolean inBounds() - returns true if the current position is on the square (include the boundary as part of the square); returns false otherwise.

Add a method named walk, to the RandomWalk class. The method has no parameters and returns nothing. Its job is to simulate a complete random walk. That is, it should generate a sequence of steps as long the maximum number of steps has not been taken and it is still in bounds (inside the square). This should be a very simple loop (while or do-while). You will need to call the methods takeStep, moreSteps, and inBounds.

Add to TestWalk.java a statement to instantiate a RandomWalk object with a boundary of 10 and 200 as the maximum number of steps. Then add a statement to have the object walk. Print the object after the walk. Compile and run the program. Run it more than once – you should be able to tell by the value printed whether the object went out of bounds or whether it stopped because it reached the maximum number of steps.

Now write a client program in a file named DrunkenWalk.java. The program should simulate a drunk staggering randomly on some sort of platform (imagine a square dock in the middle of a lake). The goal of the program is to have the program simulate the walk many times (because of randomness each walk is different) and count the number of times the drunk falls off the platform (goes out of bounds). Your program should read in the boundary, the maximum number of steps, and the number of drunks to simulate. It should then have a loop (a for loop would be a good idea) that on each iteration instantiates a new RandomWalk object to represent a drunk, has the object walk, then determines whether or not the drunk fell off the platform (and updates a counter if it did). After the loop print out the number of times the drunk fell off. Compile and run your program. To see the “randomness” you should run it several times. Try input of 10 for the boundary and 200 for the number of steps first (sometimes the drunk falls off, sometimes not); try 10 for the boundary and 500 for the steps (you should see different behavior); try 50 for the boundary and 200 for the steps (again different behavior).

Explanation / Answer

public class RandomWalk {

   int x_current,y_current;
   int max_steps,current_steps;
   int boundary;
    public RandomWalk()
    {
    }
    public RandomWalk (int max, int edge)
    {
       max_steps=max;
       boundary=edge;
       x_current=y_current=0;
    }
    public RandomWalk (int max, int edge, int startX, int startY)
    {
       max_steps=max;
       boundary=edge;
       x_current=startX;
       y_current=startY;
    }
    public String toString()
    {
       String str="Steps: ";
       str=str+Integer.toString(current_steps)+"; Position: ("+Integer.toString(x_current)+","+Integer.toString(y_current)+")";
       return str;
    }
    public void takeStep()
    {
       int randomNum = 0+ (int)(Math.random()*4);
       switch(randomNum)
       {
           case 0:
               y_current++;
           break;
           case 1:
               y_current--;
           break;
           case 2:
               x_current--;
           break;
           case 3:
               x_current++;
           break;
           default:
           break;  
       }
       current_steps++;  
    }
    public boolean moreSteps()
    {
       return (this.current_steps<this.max_steps);
    }
    public boolean inBounds()
    {
       return ((this.x_current>=-this.boundary) && (this.x_current<=this.boundary) && (this.y_current>=-this.boundary) && (this.y_current<=this.boundary));
   }
   public void walk()
   {
       boolean exit_flag=false;
       do
       {
           if(this.moreSteps() && this.inBounds())
               this.takeStep();
           else
               exit_flag=true;
       }while(exit_flag==false);
       System.out.println(this.toString());  
   }
}

------------------------------------------------------------------------------------------------------------------------------

import java.util.Scanner;
public class TestWalk
{
        public static void main (String[] args)
        {
                int maxSteps; // maximum number of steps in a walk
                int maxCoord; // the maximum x and y coordinate
                int x, y; // starting x and y coordinates for a walk
                RandomWalk w=new RandomWalk(100,5);
               System.out.println("First Object: "+w.toString());
               Scanner scan = new Scanner(System.in);
                System.out.println (" Random Walk Test Program");
                System.out.println ();
                System.out.print ("Enter the boundary for the square: ");
                maxCoord = scan.nextInt();
                System.out.print ("Enter the maximum number of steps: ");
                maxSteps = scan.nextInt();
                System.out.print ("Enter the starting x and y coordinates with a space between: ");
                x = scan.nextInt();
                y = scan.nextInt();
                RandomWalk w1=new RandomWalk(maxSteps,maxCoord,x,y);
               System.out.println("Second Object: "+w1.toString());
               for (int i=0;i<5;i++)
               {
                   w.takeStep();
                   w1.takeStep();
               }
               System.out.println("First Object: "+w.toString());
               System.out.println("Second Object: "+w1.toString());
               w.walk();
       }
}

------------------------------------------------------------------------------------------------------------------------------------


import java.util.Scanner;

public class DrunkenWalk {
  
    public DrunkenWalk() {
    }
    public static void main(String args[])
    {
       Scanner scan = new Scanner(System.in);
        System.out.print ("Enter the boundary for the square: ");
        int maxCoord = scan.nextInt();
        System.out.print ("Enter the maximum number of steps: ");
        int maxSteps = scan.nextInt();
        System.out.print ("Enter the maximum number of drunks: ");
        int maxDrunks = scan.nextInt();      
        int fall_off=0;
        for(int i=0;i<maxDrunks;i++)
        {
           RandomWalk w=new RandomWalk(maxSteps,maxCoord);
           w.walk();
           if(w.current_steps>=w.max_steps)
           {
               System.out.println("Drunken "+i+" Not Fell Off");
           }
           else
           {
               System.out.println("Drunken "+i+" Fell Off");
               fall_off++;
           }
        }
        System.out.println("Drunken Fell OFF "+fall_off+" Times");
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote