// from Lecture Notes 14 // http://silo.cs.indiana.edu:8346/c212/sum2016/exam02/0713a.phps public class Drunkard { private int x, y; public Drunkard() { // places the Drunkard in the origin this(0, 0); // just another way to place the Drunkard in the origin } public Drunkard(int x, int y) { super(); this.x = x; this.y = y; } public void move() { int direction = (int) (Math.random() * 4); // 0, 1, 2, 3 if (direction == 0) { // go north (up) this.y -= 1; } else if (direction == 1) { // go east (right) this.x += 1; } else if (direction == 2) { // go south (down) this.y += 1; } else if (direction == 3) { // go west (right) this.x -= 1; } else { System.out.println( "Whoa, can't be!" ); // can we do something more spectacular here } } public String toString() { return "Hiccup: (" + this.x + ", " + this.y + ")"; } public static void main(String[] args) { Drunkard a = new Drunkard(4, 5); for (int i = 0; i < 100; i++) { a.move(); System.out.println( a ); } } }