| | |
need help to make this prog work
![]() |
•
•
Join Date: Apr 2009
Posts: 3
Reputation:
Solved Threads: 0
Write a JAVA program that generates a ‘random” (drunk) walk across a 10 X 10 array. Initially the array will contain all periods(‘.’)
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.
To determine you direction to move, use the random() method to generate a random integer
double rand = random();
Convert the random integer to a number 0~3 (hint: use the mod %)
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 and print the array
If you get to the letter ‘Z’, stop and print the array
Ask for the user to enter the initial row and column on which to start.
Validate that row and column entered are valid (within the array)
i have have of the Program
_____________________________________________
import java.lang.Math.*;
class DrunkWalker
{
/*****************
* data items
******************/
private static char ch[][] = 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 = 0;
public static void main(String args[])
{
row = 4;
col = 6;
loadArray();
printArray();
while(stopCode != 1)
{
getRand();
switch(randNSEW)
{
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
default: break;
} // end switch
if(alphaIndex == 26)
{
stopCode = 1;
}
} // end while loop
} // end main
public static void loadArray()
{
int row;
int col;
for(row=0; row<10; row++)
{
for(col=0; col<10; col++)
{
ch[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(ch[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;
}
}
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.
To determine you direction to move, use the random() method to generate a random integer
double rand = random();
Convert the random integer to a number 0~3 (hint: use the mod %)
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 and print the array
If you get to the letter ‘Z’, stop and print the array
Ask for the user to enter the initial row and column on which to start.
Validate that row and column entered are valid (within the array)
i have have of the Program
_____________________________________________
import java.lang.Math.*;
class DrunkWalker
{
/*****************
* data items
******************/
private static char ch[][] = 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 = 0;
public static void main(String args[])
{
row = 4;
col = 6;
loadArray();
printArray();
while(stopCode != 1)
{
getRand();
switch(randNSEW)
{
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
default: break;
} // end switch
if(alphaIndex == 26)
{
stopCode = 1;
}
} // end while loop
} // end main
public static void loadArray()
{
int row;
int col;
for(row=0; row<10; row++)
{
for(col=0; col<10; col++)
{
ch[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(ch[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;
}
}
•
•
Join Date: Jan 2008
Posts: 3,811
Reputation:
Solved Threads: 501
Code tags and formatting/indentation please.
[code=JAVA]
// paste code here
[/code]
Your code is below, but it's still hard to read since there is no indentation. Second, what is the question?
[code=JAVA]
// paste code here
[/code]
Your code is below, but it's still hard to read since there is no indentation. Second, what is the question?
JAVA Syntax (Toggle Plain Text)
import java.lang.Math.*; class DrunkWalker { /***************** * data items ******************/ private static char ch[][] = 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 = 0; public static void main(String args[]) { row = 4; col = 6; loadArray(); printArray(); while(stopCode != 1) { getRand(); switch(randNSEW) { case 0: break; case 1: break; case 2: break; case 3: break; default: break; } // end switch if(alphaIndex == 26) { stopCode = 1; } } // end while loop } // end main public static void loadArray() { int row; int col; for(row=0; row<10; row++) { for(col=0; col<10; col++) { ch[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(ch[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; } }
•
•
Join Date: Apr 2009
Posts: 3
Reputation:
Solved Threads: 0
VernonDozier well i have post description first and after _________dis line i have code of program
......plz help me to complete this program if any of you know how to do it output should look like dis when i compile
walk completed
A............
BCD.........
.FG..........
HG..........
I.............
J.........Z..
K..RSTUVY
LMPQ...WX..
.NO.............
..................
walk blocked
...................
...................
.............A...
..........BC...
............DKJ
...........ELI
..........FGH
...............
................
...............
......plz help me to complete this program if any of you know how to do it output should look like dis when i compile
walk completed
A............
BCD.........
.FG..........
HG..........
I.............
J.........Z..
K..RSTUVY
LMPQ...WX..
.NO.............
..................
walk blocked
...................
...................
.............A...
..........BC...
............DKJ
...........ELI
..........FGH
...............
................
...............
•
•
Join Date: Jan 2008
Posts: 3,811
Reputation:
Solved Threads: 501
You have a project specification, but not a question. Did you write the code or was it part of the specification? You need to repost please using indentation and code tags, and explain where exactly you are stuck (i.e. a certain line doesn't compile, it has a run-time error on a certain line, you're confused about certain aspects of the program, etc.). Otherwise we have no idea what the problem is.
•
•
•
•
plz help me to complete this program if any of you know how to do it
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Java Syntax (Toggle Plain Text)
String [] array = new String[5]; // array is not null but array[0], ... are null //array variable is an array and should be treated like one. // instatiating the elements of the array: array[0] = "aa"; array[1] = "bb"; System.out.println("Array: "+array); for (int i=0;i<array.length;i++) { System.out.println("Elements: "+array[i]); } //array[0] is a String and should be treated like a String
When you are creating any array this is one of the ways:
SomeObject [] array = new SomeObject[N];
With this
array[0] = new SomeObject(); you are creating a SomeObject instance. Remember array[0] IS a SomeObject.
array is an array.
Also if you still don't understand run my example.
And these things that you wrote will get you in trouble:
•
•
•
•
sfdstrg
hhytrujytjttrtry
Check out my New Bike at my Public Profile at the "About Me" tab
![]() |
Similar Threads
- TXT File Loading Prog (C++)
- Out of 9 Lives - The Dreaded Inventory Prog (Java)
- Any recommendations on good Flash books? (Graphics and Multimedia)
- run error (C++)
- help me in my password checking prog (C++)
- SQL Server vs MYSQL vs MSQL (i'm stopping now) (MS SQL)
- XP Boot problems due to virus? (Viruses, Spyware and other Nasties)
- Program runs in an infinite loop (C++)
- Adding data to print job (Visual Basic 4 / 5 / 6)
Other Threads in the Java Forum
- Previous Thread: Help! Help! Help!
- Next Thread: easy question...
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game gameprogramming givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






