943,865 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 868
  • Java RSS
Apr 9th, 2009
0

need help to make this prog work

Expand Post »
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;
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
karan_the is offline Offline
3 posts
since Apr 2009
Apr 9th, 2009
0

Re: need help to make this prog work

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?

JAVA Syntax (Toggle Plain Text)
  1. import java.lang.Math.*;
  2.  
  3. class DrunkWalker
  4. {
  5.  
  6. /*****************
  7. * data items
  8. ******************/
  9. private static char ch[][] = new char[10][10];
  10. private static int randNSEW;
  11. private static int stopCode = 0;
  12. private static int row = 0;
  13. private static int col = 0;
  14. private static int alphaIndex = 0;
  15.  
  16.  
  17.  
  18. public static void main(String args[])
  19. {
  20. row = 4;
  21. col = 6;
  22.  
  23. loadArray();
  24. printArray();
  25.  
  26. while(stopCode != 1)
  27. {
  28. getRand();
  29. switch(randNSEW)
  30. {
  31. case 0:
  32. break;
  33. case 1:
  34. break;
  35. case 2:
  36. break;
  37. case 3:
  38. break;
  39. default: break;
  40. } // end switch
  41.  
  42. if(alphaIndex == 26)
  43. {
  44. stopCode = 1;
  45. }
  46. } // end while loop
  47.  
  48. } // end main
  49. public static void loadArray()
  50. {
  51. int row;
  52. int col;
  53. for(row=0; row<10; row++)
  54. {
  55. for(col=0; col<10; col++)
  56. {
  57. ch[row][col] = '.';
  58. }
  59. }
  60. }// end loadArray
  61.  
  62. public static void printArray()
  63. {
  64. int row;
  65. int col;
  66. for(row=0; row<10; row++)
  67. {
  68. System.out.println();
  69. for(col=0; col<10; col++)
  70. {
  71. System.out.print(ch[row][col]);
  72. }
  73. }
  74. System.out.println();
  75. }// end printArray
  76. public static void getRand()
  77. {
  78. int x100 = 0;
  79. double randomNum = 0.0;
  80. randomNum = Math.random();
  81. x100= (int)(randomNum * 100);
  82. randNSEW = x100 % 4;
  83. }
  84. }
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Apr 9th, 2009
0

Re: need help to make this prog work

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
...............
................
...............
Reputation Points: 10
Solved Threads: 0
Newbie Poster
karan_the is offline Offline
3 posts
since Apr 2009
Apr 9th, 2009
0

Re: need help to make this prog work

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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Apr 10th, 2009
0

Re: need help to make this prog work

Quote ...
plz help me to complete this program if any of you know how to do it
We would not complete the program for you irrelevant of the fact whether we know it or not and as VD has said tell us what point in the program your stuck at, ask specific questions to which we might be willing to answer.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Apr 12th, 2009
0

Re: need help to make this prog work

i need to know how to start not the coding program description is all there explain Array
Reputation Points: 10
Solved Threads: 0
Newbie Poster
karan_the is offline Offline
3 posts
since Apr 2009
Apr 12th, 2009
0

Re: need help to make this prog work

What you just said makes no sense. It looks like 3 sentences jumbled into one. In English, please?
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Apr 13th, 2009
0

Re: need help to make this prog work

> explain Array

Which array you want us to explain ?
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Apr 13th, 2009
-2

Re: need help to make this prog work

sfdstrg
hhytrujytjttrtry
Click to Expand / Collapse  Quote originally posted by verruckt24 ...
> explain Array

Which array you want us to explain ?
how to explain this Array
Reputation Points: 2
Solved Threads: 1
Newbie Poster
amarjeetsingh is offline Offline
21 posts
since Feb 2009
Apr 13th, 2009
0

Re: need help to make this prog work

Java Syntax (Toggle Plain Text)
  1. String [] array = new String[5]; // array is not null but array[0], ... are null
  2. //array variable is an array and should be treated like one.
  3.  
  4. // instatiating the elements of the array:
  5. array[0] = "aa";
  6. array[1] = "bb";
  7.  
  8. System.out.println("Array: "+array);
  9. for (int i=0;i<array.length;i++) {
  10. System.out.println("Elements: "+array[i]);
  11. }
  12.  
  13. //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:
Quote ...
sfdstrg
hhytrujytjttrtry
If you don't understand something, repost with better explanation
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help! Help! Help!
Next Thread in Java Forum Timeline: easy question...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC