Recursively printing equations

I did this program and I need to make sure I did it right. The answers come out to be right but I want to make sure nothing is wrong with the coding.

class RecursiveMethods
{
RecursiveMethods()          //default constructor
{
}

public int fOf(int x)
{
if (x <= 10)                        //the base case
{
System.out.println(x + " <= 10, therefore ... f(" + x + ") = -5");
return -5;
}
else
{
System.out.println(x + " > 10, therefore ... f(" + x + ") = f(" + x + " - 3) + 2 = f(" + (x -3) + ") + 2");
return fOf(x-3) + 2;
}
}
}

public class RecursionMethodTester
{
public static void main(String[] args)
{
int x;
RecursiveMethods rMethods = new RecursiveMethods();

System.out.println("---------------------------------");
System.out.println("       f(x - 3) + 2    if x >  10");
System.out.println("f(x) = ");
System.out.println("       -5              if x <= 10");
System.out.println("---------------------------------");
System.out.println();

x = 20;
System.out.println("Example 1:  x = " + x);
System.out.println("f(" + x + ") = " + rMethods.fOf(x));
System.out.println();

x = 19;
System.out.println("Example 2:  x = " + x);
System.out.println("f(" + x + ") = " + rMethods.fOf(x));
System.out.println();

x = 18;
System.out.println("Example 3:  x = " + x);
System.out.println("f(" + x + ") = " + rMethods.fOf(x));
System.out.println();
}
}

• Could you add an explanation of what is the code supposed to do? Feb 5, 2013 at 20:18

As mentioned by @Cyrille fOf is not a good method name.

I disagree with the use of more comments, if you name your function well enough, you shouldn't need comments. Comments should be used to explain hard to understand or confusing code, not describe what is going on.

In java the opening { is usually placed at the end of the line where the code block starts. i.e.

if (somethingIsTrue) {


You can get rid of the else in your fOf function. It is implied with the return in the if.

You don't need to specify the default constructor. It does nothing, so it is just clutter.

I'm also not happy with having the System.out.println in your class. Off of the top of my head, I would create an reporting structure, which would include an interface and at least one class. Something like

interface Reporter {

void reportLine(string message);
}


Then you could implement it like

public class ConsoleReporter implements Reporter {

void reportLine(string message) {
System.out.println(message);
}

}


and your implementation. Notice the constructor now does something, so my point about removing it is not valid in this case.

class RecursiveMethods {

private Reporter reporter;

RecursiveMethods(Reporter reporter)          //default constructor
{
this.reporter = reporter;
}

public int fOf(int x)
{
if (x <= 10)                        //the base case
{
reporter.reportLine(x + " <= 10, therefore ... f(" + x + ") = -5");
return -5;
}

reporter.reportLine(x + " > 10, therefore ... f(" + x + ") = f(" + x + " - 3) + 2 = f(" + (x -3) + ") + 2");

return fOf(x-3) + 2;
}
}

public static void main(String[] args)
{
Reporter reporter = new ConsoleReporter();
int x;
RecursiveMethods rMethods = new RecursiveMethods(reporter);

// Change all System.out.writeLn to reporter.reportLine
}


Doing this will allow you to output to different mediums, for instance, you could create FileReporter, which would output to a file, or StreamingReporter, which would output to a stream of some sort. It just makes your code more versatile. You could also add a method like reportString, which would write an entire formatted string.

It is fine. The code could use the following improvements:

• some additional comments, to describe in particular what the recursive function does. Recursive functions are by nature harder to read.
• "fOf" is not a particularly good method name. Try to use longer, explicit names.