Find Us On:

# Simple Java program 3

use file Drunk.java A student has decided to celebrate the start of Thanksgiving break a tad early by getting a bit smashed. Her home is at the corner of 1st street and Main Street, and the Jail is at corner of 11th street and Main Street. The student starts at corner of 6th street, and wanders one corner to the left or to the right with probability 0.5 for each; she repeats the process until arriving safely at home or landing in jail. That is, at each corner, the drunk student has a 50-50 probability of staggering left or right, to the next higher-numbered or next lower-numbered corner. Write a method named drunkWalk() using a while or a do-while loop that simulates the drunk student’s walk; your method should return with a single integer that indicates how many steps were taken. You should not print out each step taken in your final version of the method, though you might want to do this while you are debugging. Once you have your method working, have your main program call upon your drunkWalk() method N times (where N is a final variable). Finally, have it calculate and print the average number of steps that the student took for one trip. Here’s what your program might look like in action, with N equal to 5: Here we go again... time for a walk! Took 37 steps, and Landed at HOME Here we go again... time for a walk! Took 19 steps, and Landed in JAIL Here we go again... time for a walk! Took 13 steps, and Landed in JAIL Here we go again... time for a walk! Took 25 steps, and Landed in JAIL Here we go again... time for a walk! Took 15 steps, and Landed at HOME Average # of steps equals 21.8 Attachments
Tags Clarifications

## Solution for Drunk java program

mariobeka

Below the result screen shot and the implementation of the Drunk java class. Attachments
Screen Shot 2016-11-11 at 19.22.48.png (62 K) Preview

Drunk.java (1 K) Preview
} else { System.out.println("Landed at " + LOCATIONS[1]); } } double avg = (double) total / N; System.out.printf("Average # of steps equals %.1f\n", avg); } /** * Perform a drunk walk. * * @return Return the number of steps that were need until Home or Jail. */ static int

Screenshots