Question: Write a class named RandomWalker following the good programming practices seen in class (test, document, conventional formatting, etc). I will give you a main program
Write a class named RandomWalker following the good programming practices seen in class (test, document, conventional formatting, etc). I will give you a main program that creates and manipulates RandomWalkerobjects, but it won't work correctly unless you have written RandomWalker.
A RandomWalker object should keep track of its (x, y) location. All walkers should start at the coordinates (0, 0) when created. When a walker is asked to move, it should randomly move exactly one unit either left, right, up or down (ie, adds +/- 1 to either its x or y value, but not both). Each of these four possible moves should occur with equal probability. When done repeatedly, the resulting behavior is known as a "random walk".
Each RandomWalker object should have the following public methods. You may add whatever private fields or methods you feel are necessary to implement these methods:
move() : Instructs this random walker to update its coordinates by randomly making 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 (ie, the number of time move was called).
You don't write any loops in your RandomWalker code. The program I give you will call your methods repeatedly to get its walk done.
Random walks have interesting mathematical properties. For example, given infinitely many steps, a random walker approaches 100% chance of reaching any particular (x, y) coordinate.
- Write each method.
- Write brief javadoc documentation for each public method and for the class as a whole.
- Write a main program in a class java that exercises all of the methods in RandomWalker and demonstrates that they are working.
- copy the following 2 files into same directory as RandomWalker.java and run testWalker.java
TestRandomWalker.java
import java.awt.*;
public class TestRandomWalker { public static final int STEPS = 500;
public static void main(String[] args) { RandomWalker walker = new RandomWalker();
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();
int prevY = walker.getY();
for (int i = 1; i <= STEPS; i++) {
g.setColor(Color.BLACK);
g.drawLine(prevX, prevY, walker.getX(), walker.getY());
walker.move();
prevX = walker.getX();
prevY = walker.getY();
g.setColor(Color.RED);
g.drawLine(prevX, prevY, walker.getX(), walker.getY());
int steps = walker.getSteps();
if (steps % 10 == 0) { System.out.println(steps + " steps");
}
panel.sleep(100);
}
}
}
DrawingPanel.java
/* Stuart Reges and Marty Stepp 07/01/2005 The DrawingPanel class provides a simple interface for drawing persistent images using a Graphics object. An internal BufferedImage object is used to keep track of what has been drawn. A client of the class simply constructs a DrawingPanel of a particular size and then draws on it with the Graphics object, setting the background color if they so choose. To ensure that the image is always displayed, a timer calls repaint at regular intervals. */ import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class DrawingPanel implements ActionListener { public static final int DELAY = 50; // delay between repaints in millis private static final String DUMP_IMAGE_PROPERTY_NAME = "drawingpanel.save"; private static String TARGET_IMAGE_FILE_NAME = null; private static final boolean PRETTY = true; // true to anti-alias private static boolean DUMP_IMAGE = true; // true to write DrawingPanel to file private int width, height; // dimensions of window frame private JFrame frame; // overall window frame private JPanel panel; // overall drawing surface private BufferedImage image; // remembers drawing commands private Graphics2D g2; // graphics context for painting private JLabel statusBar; // status bar showing mouse position private long createTime; static { TARGET_IMAGE_FILE_NAME = System.getProperty(DUMP_IMAGE_PROPERTY_NAME); DUMP_IMAGE = (TARGET_IMAGE_FILE_NAME != null); } // construct a drawing panel of given width and height enclosed in a window public DrawingPanel(int width, int height) { this.width = width; this.height = height; this.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); this.statusBar = new JLabel(" "); this.statusBar.setBorder(BorderFactory.createLineBorder(Color.BLACK)); this.panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0)); this.panel.setBackground(Color.WHITE); this.panel.setPreferredSize(new Dimension(width, height)); this.panel.add(new JLabel(new ImageIcon(image))); // listen to mouse movement MouseInputAdapter listener = new MouseInputAdapter() { public void mouseMoved(MouseEvent e) { DrawingPanel.this.statusBar.setText("(" + e.getX() + ", " + e.getY() + ")"); } public void mouseExited(MouseEvent e) { DrawingPanel.this.statusBar.setText(" "); } }; this.panel.addMouseListener(listener); this.panel.addMouseMotionListener(listener); this.g2 = (Graphics2D)image.getGraphics(); this.g2.setColor(Color.BLACK); if (PRETTY) { this.g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); this.g2.setStroke(new BasicStroke(1.1f)); } this.frame = new JFrame("Building Java Programs - Drawing Panel"); this.frame.setResizable(false); this.frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { if (DUMP_IMAGE) { DrawingPanel.this.save(TARGET_IMAGE_FILE_NAME); } System.exit(0); } }); this.frame.getContentPane().add(panel); this.frame.getContentPane().add(statusBar, "South"); this.frame.pack(); this.frame.setVisible(true); if (DUMP_IMAGE) { createTime = System.currentTimeMillis(); this.frame.toBack(); } else { this.toFront(); } // repaint timer so that the screen will update new Timer(DELAY, this).start(); } // used for an internal timer that keeps repainting public void actionPerformed(ActionEvent e) { this.panel.repaint(); if (DUMP_IMAGE && System.currentTimeMillis() > createTime + 4 * DELAY) { this.frame.setVisible(false); this.frame.dispose(); this.save(TARGET_IMAGE_FILE_NAME); System.exit(0); } } // obtain the Graphics object to draw on the panel public Graphics2D getGraphics() { return this.g2; } // set the background color of the drawing panel public void setBackground(Color c) { this.panel.setBackground(c); } // show or hide the drawing panel on the screen public void setVisible(boolean visible) { this.frame.setVisible(visible); } // makes the program pause for the given amount of time, // allowing for animation public void sleep(int millis) { try { Thread.sleep(millis); } catch (InterruptedException e) {} } // take the current contents of the panel and write them to a file public void save(String filename) { String extension = filename.substring(filename.lastIndexOf(".") + 1); // create second image so we get the background color BufferedImage image2 = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB); Graphics g = image2.getGraphics(); g.setColor(panel.getBackground()); g.fillRect(0, 0, this.width, this.height); g.drawImage(this.image, 0, 0, panel); // write file try { ImageIO.write(image2, extension, new java.io.File(filename)); } catch (java.io.IOException e) { System.err.println("Unable to save image: " + e); } } // makes drawing panel become the frontmost window on the screen public void toFront() { this.frame.toFront(); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
