need help to make this prog work

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2009
Posts: 3
Reputation: karan_the is an unknown quantity at this point 
Solved Threads: 0
karan_the karan_the is offline Offline
Newbie Poster

need help to make this prog work

 
0
  #1
Apr 9th, 2009
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;
}
}
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,832
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: need help to make this prog work

 
0
  #2
Apr 9th, 2009
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?

  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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 3
Reputation: karan_the is an unknown quantity at this point 
Solved Threads: 0
karan_the karan_the is offline Offline
Newbie Poster

Re: need help to make this prog work

 
0
  #3
Apr 9th, 2009
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
...............
................
...............
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,832
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: need help to make this prog work

 
0
  #4
Apr 9th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: need help to make this prog work

 
0
  #5
Apr 10th, 2009
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.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 3
Reputation: karan_the is an unknown quantity at this point 
Solved Threads: 0
karan_the karan_the is offline Offline
Newbie Poster

Re: need help to make this prog work

 
0
  #6
Apr 12th, 2009
i need to know how to start not the coding program description is all there explain Array
eew
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,620
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 205
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: need help to make this prog work

 
0
  #7
Apr 12th, 2009
What you just said makes no sense. It looks like 3 sentences jumbled into one. In English, please?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: need help to make this prog work

 
0
  #8
Apr 13th, 2009
> explain Array

Which array you want us to explain ?
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 21
Reputation: amarjeetsingh has a little shameless behaviour in the past 
Solved Threads: 1
amarjeetsingh amarjeetsingh is offline Offline
Newbie Poster

Re: need help to make this prog work

 
-2
  #9
Apr 13th, 2009
Originally Posted by amarjeetsingh View Post
sfdstrg
Originally Posted by amarjeetsingh View Post
hhytrujytjttrtry
Originally Posted by verruckt24 View Post
> explain Array

Which array you want us to explain ?
how to explain this Array
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,692
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 227
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: need help to make this prog work

 
0
  #10
Apr 13th, 2009
  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:
sfdstrg
hhytrujytjttrtry
If you don't understand something, repost with better explanation
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC