I just need help on one little thing!

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

Join Date: Sep 2007
Posts: 58
Reputation: Noliving is an unknown quantity at this point 
Solved Threads: 0
Noliving Noliving is offline Offline
Junior Poster in Training

I just need help on one little thing!

 
0
  #1
Apr 17th, 2008
I basically have the entire program done except for one little problem my maxDepth output is wrong!

But the rest of the assignment is after your program gets the correct answers, add static variables calls, depth, and maxDepth to your class and add code to your implementation of Z to keep track of total number of calls to the Z method and the maximum depth of the activation stack. To do the latter you must increment depth, whenever you enter Z and decrement it when you leave. maxDepth is the largest value that depth achieves during your run. Your main should display calls and maxDepth when it displays the value computed for the Z function.

Ok now that you know what I'm trying to do here is my input and what the answers are suppose to be input is 2 for x, 3 for y

Outs puts are suppose to be z value is 9, calls = 44 and maxDepth = 10

I get all of those except maxDepth = 22 not 10

  1. import javax.swing.*;
  2.  
  3. public class ZFunction {
  4. public static int calls = 0;
  5. public static int depth = 0;
  6. public static int z1;
  7. public static int z2;
  8. public static int maxDepth = 0;
  9.  
  10.  
  11.  
  12.  
  13.  
  14. public static int z(int x, int y){
  15. calls++;
  16. depth++;
  17.  
  18.  
  19. if (x==0){
  20. if (depth > maxDepth)
  21. maxDepth = depth;
  22. return y+1;}
  23. else if (x>0 && y==0){
  24. z1 = z(x-1,1);
  25. depth = depth -1;
  26. return z1;}
  27.  
  28.  
  29. else{
  30. z2 = z(x-1,z(x,y-1));
  31. depth = depth -1;
  32. return z2;
  33. }
  34.  
  35.  
  36. }
  37.  
  38.  
  39.  
  40. public static void main(String[] args) {
  41. String a = JOptionPane.showInputDialog("Enter the x value");
  42. int x = Integer.parseInt(a);
  43.  
  44. String b = JOptionPane.showInputDialog("Enter the y value");
  45. int y = Integer.parseInt(b);
  46.  
  47.  
  48. JOptionPane.showMessageDialog(null, "The value of z is" + z(x,y));
  49. JOptionPane.showMessageDialog(null, "Z method was called" + calls);
  50. JOptionPane.showMessageDialog(null, "Z method was max depth" + maxDepth);
  51. }
  52. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,267
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 493
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: I just need help on one little thing!

 
0
  #2
Apr 17th, 2008
As the post is marked as solved I guess you found the problem. Would you mind to share your solution?
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 58
Reputation: Noliving is an unknown quantity at this point 
Solved Threads: 0
Noliving Noliving is offline Offline
Junior Poster in Training

Re: I just need help on one little thing!

 
0
  #3
Apr 17th, 2008
Ya it was a one line solution, after line 21 you have to add:

depth = depth -1;

So lines 14 too 36 or more like 37 from the orignal/first post shoud look like this:

  1. public static int z(int x, int y){
  2. calls++;
  3. depth++;
  4.  
  5.  
  6. if (x==0){
  7. if (depth > maxDepth)
  8. maxDepth = depth;
  9. depth = depth -1;
  10. return y+1;}
  11. else if (x>0 && y==0){
  12. z1 = z(x-1,1);
  13. depth = depth -1;
  14. return z1;}
  15.  
  16.  
  17. else{
  18. z2 = z(x-1,z(x,y-1));
  19. depth = depth -1;
  20. return z2;
  21. }
  22.  
  23.  
  24. }
Last edited by Noliving; Apr 17th, 2008 at 5:18 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 460 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC