Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Starting Out With Java: Early Objects 5th Ed. Drunk Walker I used to ask students to write a Java program that generates a random (drunk)

Starting Out With Java: Early Objects 5th Ed. Drunk Walker

I used to ask students to write a Java program that generates a random (drunk) walk across a 10 X 10 array. Initially each cell of the array should contain a period (.) It is a good exercise but I found that many students were not thinking for them selves. So I have amended the assignment. I broke my program and am attaching it to the assignment so that you may fix it and turn in the correct program. Additionally please describe the processing that occurs in the getRand() method. Also please indicate any examples of defensive programming. The directions that students were given are below.

The program must randomly walk from element to element always going up, down, right, or left (no diagonal) by one element.

The elements visited will be labeled by the letters A through Z in the order visited.

Ask the user to enter the initial row and column on which to start.

To determine your direction to move, use the random() method.

If the number generated is a:

0 move up (north)

1 move right (east)

2 move down (south)

3 move left (west)

If the spot you are trying to move to is already occupied by a letter (has been visited), try the next spot. In other words

Random number is a 0 check N, E, S, W

1 check E, S, W, N

2 check S, W, N, E

3 check W, N, E, S

Keep within the array (check array bounds before moving)

If you hit a point where you cannot move, stop or if you get to the letter Z, stop. Output if you stopped because you are blocked, use a heading of Stuck in traffic. If you make it to the letter Z, use a heading of Made it home safely. Then print the array

Please read the directions in the attached document and use the BrokenDrunkWalker.java file as a starting point.

// include the Math import import java.lang.Math.*; import java.util.Scanner; class BrokenDrunkWalker { /*********************************** * data items ************************************/ private static char walk[][] = new char[10][10]; private static int randNSEW; private static int stopCode = 0; private static int row = 0; private static int col = 0; private static int alphaIndex ; private static final char alpha[] = {'A','B','C','D','E','F','G','H', 'I','J','K','L','M','N','O','P', 'Q','R','S','T','U','V','W','X','Y','Z'}; private static boolean goodMove = false; private static int chkCount = 0; public static void main(String args[]) { //row = 4; //col = 6; loadArray(); printArray(); row = initRow(); col = initColumn(); while(stopCode != 1) { if(alphaIndex == 0) walk[row][col] = alpha[alphaIndex++]; else { getRand(); goodMove = false; while((chkCount < 4) && (!goodMove)) { switch(randNSEW) { case 0: if(row -1 >= 0) if((walk[row-1][col] == '.')) { row = row - 1; goodMove = true; } else { randNSEW = 1; chkCount++; } break; case 1: if((col +1 <= 9) && (col +1 >= 0)) if((walk[row][col + 1] == '.')) { col = col + 1; goodMove = true; } else { randNSEW = 2; chkCount++; } break; case 2: if((row + 1 <= 9 ) && (row + 1 >= 0)) if((walk[row+1][col] == '.')) { row = row + 1 ; goodMove = true; } else { randNSEW = 3; chkCount++; } break; case 3: if(col - 1 > 0) if((walk[row][col - 1] == '.')) { col = col - 1; goodMove = true; } else { randNSEW = 0; chkCount++; } break; default: break; } // end switch } // End while if(! goodMove || (alphaIndex == 26)) { stopCode = 1; } else { walk[row][col] = alpha[alphaIndex++]; } } } // end while loop StopCode if (alphaIndex < 26) System.out.println("Stuck in traffic "); else System.out.println("Made it home safely"); printArray(); } // end main public static void loadArray() { int row; int col; for(row=0; row<10; row++) { for(col=0; col<10; col++) { walk[row][col] = '.'; } } }// end loadArray public static void printArray() { int row; int col; for(row=0; row<10; row++) { System.out.println(); for(col=0; col<10; col++) { System.out.print(walk[row][col]); } } System.out.println(); }// end printArray public static void getRand() { int x100 = 0; double randomNum = 0.0; randomNum = Math.random(); x100= (int)(randomNum * 100); randNSEW = x100 % 4; } public static int initRow() { Scanner scanIn = new Scanner(System.in); System.out.println("Please enter the row where you'd like to start: "); row = scanIn.nextInt(); while(row < 0 || row > 9) { System.out.println("Sorry, that is not a valid row number."); System.out.println("Please enter a row between 0-9: "); row = scanIn.nextInt(); } return row; } public static int initColumn() { Scanner scanIn = new Scanner(System.in); System.out.println("Please enter the column where you'd like to start: "); col = scanIn.nextInt(); while(col < 0 || col > 9) { System.out.println("Sorry, that is not a valid column number."); System.out.println("Please enter a column between 0-9: "); col = scanIn.nextInt(); } return col; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database Design With Microcomputer Applications

Authors: Glenn A. Jackson

1st Edition

0137718411, 978-0137718412

More Books

Students also viewed these Databases questions

Question

=+ what roles should government play in them ?

Answered: 1 week ago

Question

=+ Why do we have markets and, according to economists,

Answered: 1 week ago