I am very new to programming, and Java is my first real language. At this point in class we are touching on recursion, and have been asked to make a method that fits certain parameters:
The method is as such: public void power(int p) where we cannot change the formal parameters of the method. I've looked into this elsewhere and it seems that a lot of solutions involve adding the int n (for the number to have the power performed upon) as a parameter.
When writing similar methods we were encouraged to use "this" as our representation of what we are trying to change. Normally "this" is used in instance methods i.e.: this.increment().
When I write pseudocode for it I find I want to do the following:
public void power(int p) {
assert p >= 0 : "Violation of: p >= 0";
int x = 0;
if (p == 0) {
return 0;
}
if (p % 2 == 0) {
x = this.power(p / 2);
return x * x;
} else {
return this * this.power(p - 1);
}
I just don't know how to write it so that it can actually run with eclipse and java. Any insight would be appreciated!