In this assignment you will implement the following recursive function in a ZFunction class. You must implement a main that will gather the parameters for the recursive function and display the result. Use JOptionPlane for the input and output.

Given the function Z(x,y) where x and y are (small positive) integers:

Base case: if(x=0) Z(x,y) returns the value y+1
1st recursive case: if(x>0 and y=0) Z(x,y) returns the value Z(x-1,1)
2nd recursive case: else Z(x,y) returns the value Z(x-1,Z(x,y-1)
Notice that in the last case, the "inner" Z function will be computed first to provide the "y" for the "outer" Z function

So if this was to all go correct if I was to enter 3 for x and 3 for y the answer should be 61, or if I was to enter 4 for x and 0 for y the answer should be 13.

My first question is how do I make the inputs that I give run the z function with the x and y as variables

``````public class ZFunction {
private int x;
private int y;

public int z(int x, int y){
if (x==0)
return y+1;
else if (x>0 && y==0)
return z(x-1,1);
else
return z(x-1,z(x,y-1));
}``````

I don't believe z is suppose to be a variable at all, just a method.

here is my main:

``````public static void main(String[] args) {
String x = JOptionPane.showInputDialog("Enter the x value");
int z = Integer.parseInt(x);
String y = JOptionPane.showInputDialog("Enter the y value");
int z = Integer.parseInt(y);

JOptionPane.showMessageDialog(null, "Your x and y values are" + x  +  y);
}
}``````

The rest of the assignment can't be done until this part is done. But since I've been wasting 3-5 hours on the top part I think I might as well try for the last part.

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 good news I got my orignal problem solved. Now all I have to do is this part:

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

Here is my solved code:

``````import javax.swing.*;

public class ZFunction {

public static int z(int x, int y){
if (x==0)
return y+1;
else if (x>0 && y==0)
return z(x-1,1);
else
return z(x-1,z(x,y-1));

}

public static void main(String[] args) {
String a = JOptionPane.showInputDialog("Enter the x value");
int x = Integer.parseInt(a);

String b = JOptionPane.showInputDialog("Enter the y value");
int y = Integer.parseInt(b);

JOptionPane.showMessageDialog(null, "The value of z is" + z(x,y));
}
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.