简体   繁体   中英

Convert two mutually recursive methods into single recursive method in java?

I need to take a program with two mutually recursive methods and modify the program so that it contains a single recursive method. From what i understand, I need to combine the two recursive methods by placing the recursive calls in a single method in the order in which they are called. The problem is there are 4 integers passed through the methods and the first method calls the second method twice and the second method calls the first method twice.

This is the original code:

public void drawHorizontal(Graphics graphics, double xMid, double yMid, double length )
{
    // find left endpoint
    double x1 = xMid - (length / 2);
    double y1 = yMid;

    // find right endpoint
    double x2 = xMid + (length / 2);
    double y2 = yMid;

   if (length > 5)
    {
    // draw a line from (x1,y1) to (x2,y2)
    graphics.drawLine((int) x1, (int) y1, (int) x2, (int) y2);

    // draw a vertical line with left end of horizontal as  midpoint of new line
    drawVertical(graphics, x1, y1, (length) );

    // draw a vertical line with right endof horizontal as  midpoint of new line
    drawVertical(graphics, x2, y2, (length) );
    }     

} // end drawHorizontal()

public void drawVertical(Graphics graphics, double xMid, double yMid, double length )
{
    // find upper endpoint
    double x1 = xMid; 
    double y1 = yMid - (length / 2);

    // right lower endpoint
    double x2 = xMid;
    double y2 = yMid + (length / 2);


   if (length > 5)
    {
    // draw a line from (x1,y1) to (x2,y2)
    graphics.drawLine((int) x1, (int) y1, (int) x2, (int) y2);

    // draw a 1/2 size horizontal line with top end of vertical as  midpoint of new line
    drawHorizontal(graphics, x1, y1, (length/2) );

    // draw a 1/2 horizontal line with bottom end of vertical as  midpoint of new line
    drawHorizontal(graphics, x2, y2, (length/2) );
    }

} // end drawVertical()

Below is my most recent modified code. Its ugly I know but I just cant figure out how to make adjustments to the x and y coordinates independently of each other. I tried to fix this by creating more variables but I can't help but feel that I'm just making more work. The closest stack question I could find was this . I've been at it since 11 and it's 4:15 now. A nudge in the right direction would be greatly appreciated, thank you for your time.

Edit * Thank you for the quick replies, I appreciate it. I know it seems counter intuitive to break mutually recursive methods down in this fashion, but I'm new to programing and java so I'm exploring different ways of breaking problems down. This is what I eventually broke it down to and it runs fine. Thank you for your time.

modified code:

    public void Total(Graphics graphics, boolean type, double xMid, double yMid, double length) {

    double x1;
    double y1;

    // find right endpoint
    double x2;
    double y2;

    if (type == false) {
        // find left endpoint
        x1 = xMid - (length / 2);
        y1 = yMid;

        // find right endpoint
        x2 = xMid + (length / 2);
        y2 = yMid;

        if (length > 5) {
            // draw a line from (x1,y1) to (x2,y2)
            graphics.drawLine((int) x1, (int) y1, (int) x2, (int) y2);

            // draw a vertical line with left end of horizontal as  midpoint of new line
            Total(graphics, true, x1, y1, (length));

            // draw a vertical line with right endof horizontal as  midpoint of new line
            Total(graphics, true, x2, y2, (length));
        }
    } else {

        // find upper endpoint
        x1 = xMid;
        y1 = yMid - (length / 2);

        // right lower endpoint
        x2 = xMid;
        y2 = yMid + (length / 2);

        if (length > 5) {
            // draw a line from (x1,y1) to (x2,y2)
            graphics.drawLine((int) x1, (int) y1, (int) x2, (int) y2);

            // draw a 1/2 size horizontal line with top end of vertical as  midpoint of new line
            Total(graphics, false, x1, y1, (length / 2));

            // draw a 1/2 horizontal line with bottom end of vertical as  midpoint of new line
            Total(graphics, false, x2, y2, (length / 2));
        }
    }
} 

Your request is strange, mutual recursion generally helps reading... But ok, there is (at least) one general method for this. If you have:

f(f_params) {
  …
  g(g_params…);
  …
}
g(g_params) {
  …
  f(f_params…);
  …
}

that could be transformed like this:

fg(what,f_params,g_params) {
  if (what==FUNC_F) {
    …
    fg(FUNC_G,f_params,g_params);
    …
  } else if (what==FUNC_G) {
    …
    fg(FUNG_F,f_params,g_params);
    …
  }
}

It is a little more complex if you need to manage different return types: just add some out parameters corresponding to the return values/types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM