This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Method Type

 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I write the markCorrectPathAndCountStars method(last method) recursively using the markTheCorrectPath method(second to last method) when markCorrectPathAndCountStars is type boolean but it shouldn't it return an int because it is marking only the correct path, and display the count of STEP?
 
Marshal
Posts: 66237
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are all your methods static?
 
Ana Smith
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My teacher gave us all the method headers.
 
Sheriff
Posts: 6374
172
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have to return two or more things from a method that are different in type, you can do one of two things:

1) Create a Record class (or any other name) that has a boolean and an int as fields.

2) Make one of the values a field, that is, declared outside of any method in this class.
 
Ana Smith
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I realized that should print it out in the method but I don't know how to recursively write the last method. Can someone look over my program and help me write the last method?
 
Ana Smith
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this is the instructions attached, I am having so much trouble with the last method and would really appreciate someone's help. Thanks.
This is my code with given method headers, please run and help me with last method:
maze.PNG
[Thumbnail for maze.PNG]
 
Ana Smith
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I figured out the method logic but I don't know how to put it into code. The logic for the last method is to figure out the point where counts is at its max to print out because it will backtrack to 0. Please help!
 
Bartender
Posts: 3611
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ana,

you invoke the method 'markCorrectPathAndCountStars(startrow, startcol, 0)'. Now, suppose that you do the recursion with 'markCorrectPathAndCountStars(r, c - 1, count + 1)' (and likewise for the other neighbors), then the argument 'count' will be 1 for that neighbor, and 2 for the neighbor's neighbor, et cetera. Then, when EXIT is reached, the value of count is the number of STEPS. You can print that value out if you so whish.

So, if you have this maze: "S . . E", then after three recursions, the value of count will be three.

Only problem is how to report the value of 'count' when EXIT is reached. The method returns a boolean and not an int. Besides, you invoke 'markCorrectPath' and that doesn't have any notion of count.

One way to solve that is to give the method 'markCorrectPathAndCountStars' the exact same body as 'markCorrectPath' (taking care of that extra parameter), printing out the value of count when EXIT is reached. But that is repeating code.

I did it by giving the method 'markCorrectPath' also a count variable, Why not? But I guess you are not allowed to change method signatures.

A third way is to create a private method

and the methods

and



Edit: forgot to mention that in all your code sofar you have still an error at line 75!
 
Rancher
Posts: 3524
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are other threads on this program:  https://coderanch.com/t/720341/java/Logic-error
 
Ana Smith
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:hi Ana,

you invoke the method 'markCorrectPathAndCountStars(startrow, startcol, 0)'. Now, suppose that you do the recursion with 'markCorrectPathAndCountStars(r, c - 1, count + 1)' (and likewise for the other neighbors), then the argument 'count' will be 1 for that neighbor, and 2 for the neighbor's neighbor, et cetera. Then, when EXIT is reached, the value of count is the number of STEPS. You can print that value out if you so whish.

So, if you have this maze: "S . . E", then after three recursions, the value of count will be three.

Only problem is how to report the value of 'count' when EXIT is reached. The method returns a boolean and not an int. Besides, you invoke 'markCorrectPath' and that doesn't have any notion of count.

One way to solve that is to give the method 'markCorrectPathAndCountStars' the exact same body as 'markCorrectPath' (taking care of that extra parameter), printing out the value of count when EXIT is reached. But that is repeating code.

I did it by giving the method 'markCorrectPath' also a count variable, Why not? But I guess you are not allowed to change method signatures.

A third way is to create a private method




and the methods

and



Edit: forgot to mention that in all your code sofar you have still an error at line 75!



Thank you for your response. I realized in order to print the count(number of steps) I have to system.out.print(count). But how would I do the recursion? Are you suggesting in the first part of your message, I should do
               
                 count++;

 
Piet Souris
Bartender
Posts: 3611
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I meant this:


So I am not changing the value of count, just passing as argument 'count + 1'.
If I arrive at position (r, c) with count = 10, then I invoke all neighbours with 'count + 1' = 11. In your code snippet, the first neighbor is invoked with count = 11, the second one with count = 12, et cetera.
 
Ana Smith
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that mean I need to change this line
else if(n==2)
     {
        markCorrectPathAndCountStars(startRow, startCol, 0);
         
     }
in solve() to
else if(n==2)
     {
        markCorrectPathAndCountStars(startRow, startCol, 0, true);
         
     }
 
Piet Souris
Bartender
Posts: 3611
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be sure: I use a Helper method here, that contins the full bode of what you have in your current markCorrectPath' method, adjusted to take care of the two extra parameters: count and a boolean theat indicates whether count will be printed when EXIT is reached.
The two methods that ou must implement look therefore like this:

and


so that you have the complicated body only once.
 
Ana Smith
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Our teacher didn't want us to use helper methods, unfortunately. He said just to add the code in each method.
 
Ana Smith
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I do that?
 
Piet Souris
Bartender
Posts: 3611
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ana Smith wrote:Does that mean I need to change this line
else if(n==2)
     {
        markCorrectPathAndCountStars(startRow, startCol, 0);
         
     }
in solve() to
else if(n==2)
     {
        markCorrectPathAndCountStars(startRow, startCol, 0, true);
         
     }


See my previous reply. The method 'markCorrectPathAndCountStars' does not need the boolean parameter, since it invokes that Helper class with that parameter. In fact, you do not even need the '0' argument, dince that is also set by that helper method.


Edit: so in fact you can leave that routine (if (n == 1) ...) as it is.
 
Ana Smith
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I do the last method without the helper method? Our teacher doesn't want us to add helper methods.
 
Ana Smith
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Our teacher gave us a shell and we had to fill out the last two method. Everything compiles but I have a logic error in the last method. After I run my program and select option 2, the number of steps to get through the maze is wrong. The last method is supposed to do that. Something is wrong with my last method.
This is the output:(it shouldn't diplay 0, it should display 13).


Enter the maze's filename: maze1.txt
WWWWWWWW
W....W.W
WW.WW..W
W....W.W
W.W.WW.E
S.W.WW.W
WW.....W
WWWWWWWW

1: Mark only the correct path. If no path exists, display 'No path exists'.
2: Mark only the correct path, and display the count of STEPs.

Please make a selection: 2
0
WWWWWWWW
W....W.W
WW.WW..W
W***.W.W
W*W*WW*E
S*W*WW*W
WW.****W
WWWWWWWW


 
Norm Radder
Rancher
Posts: 3524
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this the same program as here: https://coderanch.com/t/720326/java/Method-Type
 
Ana Smith
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Is this the same program as here: https://coderanch.com/t/720326/java/Method-Type


Yes
 
Norm Radder
Rancher
Posts: 3524
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same here also:  https://coderanch.com/t/720248/java/Unreachable-statement-error

Why start new threads for the same program and problem?  It makes it hard for anyone to follow.


Something is wrong with my last method.


Please give the name of the method when discussing it to avoid confusion.

 
Ana Smith
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Same here also:  https://coderanch.com/t/720248/java/Unreachable-statement-error

Why start new threads for the same program and problem?  It makes it hard for anyone to follow.


I thought it would be easier because there are too many questions under 1 topic so I made it easier so it's a different question.
 
Norm Radder
Rancher
Posts: 3524
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

number of steps to get through the maze is wrong.


Can you show the current number of steps and the correct number of steps?

Can you post the input file so the program can be executed for testing?



It returns a boolean value.  When should it return true and when should it return false?
Please add comments to the code describing the values it returns.
 
Ana Smith
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

number of steps to get through the maze is wrong.


Can you show the current number of steps and the correct number of steps?

Can you post the input file so the program can be executed for testing?


Yes I showed it in the output above. It was 0. But it should be 13.
 
Norm Radder
Rancher
Posts: 3524
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Please make a selection: 2
0 <<<< Is this the 0 you are talking about that should be 13?


What is the name of the variable that is printed there?  On what line in the program is it printed?

It would be clearer if the name of the variable was also shown:
 
Ana Smith
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

Please make a selection: 2
0 <<<< Is this the 0 you are talking about that should be 13?


What is the name of the variable that is printed there?  On what line in the program is it printed?



That is count and it is in the last method: markCorrectPathAndCountStars
 
Norm Radder
Rancher
Posts: 3524
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the input?
 
Ana Smith
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Enter the maze's filename: maze1.txt
WWWWWWWW
W....W.W
WW.WW..W
W....W.W
W.W.WW.E
S.W.WW.W
WW.....W
WWWWWWWW


1: Mark only the correct path. If no path exists, display 'No path exists'.
2: Mark only the correct path, and display the count of STEPs.

Please make a selection: 2
0WWWWWWWW
W....W.W
WW.WW..W
W***.W.W
W*W*WW*E
S*W*WW*W
WW.****W
WWWWWWWW
 
Norm Radder
Rancher
Posts: 3524
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It returns a boolean value.  When should it return true and when should it return false?
Please add comments to the code describing the values it returns and when it returns them.
 
Norm Radder
Rancher
Posts: 3524
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That input file does not work.  The code expects int values not char.
 
Ana Smith
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:That input file does not work.  The code expects int values not char.



It worked on my laptop. Also, everything works but it just has a logic error in the last method.
 
Ana Smith
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:
It returns a boolean value.  When should it return true and when should it return false?
Please add comments to the code describing the values it returns and when it returns them.


It returns true once it reaches the destination.
 
Norm Radder
Rancher
Posts: 3524
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So it never returns false?

Please post the input file.
 
Ana Smith
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maze1.txt aka input file
WWWWWWWW
W....W.W
WW.WW..W
W....W.W
W.W.WW.E
S.W.WW.W
WW.....W
WWWWWWWW
 
Ana Smith
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maze1.txt aka input file
WWWWWWWW
W....W.W
WW.WW..W
W....W.W
W.W.WW.E
S.W.WW.W
WW.....W
WWWWWWWW
 
Norm Radder
Rancher
Posts: 3524
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code tries to read some int values on line 32.  Where are they?
 
Ana Smith
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:The code tries to read some int values on line 32.  Where are they?

Sorry

8 8
WWWWWWWW
W....W.W
WW.WW..W
W....W.W
W.W.WW.E
S.W.WW.W
WW.....W
WWWWWWWW
 
Norm Radder
Rancher
Posts: 3524
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Please add comments to the code describing the values in the variables: "r, c and count".  What are they used for?
 
Ana Smith
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:
Please add comments to the code describing the values in the variables: "r, c and count".  What are they used for?



"r" is the row "c" is the column and count is supposed to be the number of steps to get to exit. This is what I'm having trouble on.
 
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!