Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Looking for a second opinion

1. ## Looking for a second opinion

Hello Everyone,
I have the following assignment to do.
"
(Recursive power Method) Write a recursive method power(base, exponent) that, when called, returns
base exponent

For Example, power(3,4) = 3*3*3*3. Assume that exponent is an integer greater than or equal to 1.
Hint: The recursion step should use the relationship
base exponent = base * base exponent-1

and the terminating condition occurs when exponent is equal to 1, because
base exponent 1 = base

Incorporate this method into a program that enables the user to enter the base and exponent.
"

Here is the code I wrote,
```package recursionpower;

import java.util.Scanner;

public class RecursionPower {
int baseNumber;
int exponent;
int count;
int base;

public static void main(String[] args) {
RecursionPower begin = new RecursionPower();
begin.UserEntry();
}

public void UserEntry(){
Scanner input = new Scanner(System.in);
System.out.print("Enter base number -> ");
base = input.nextInt();
System.out.print("Enter the exponent number -> ");
exponent = input.nextInt();

baseNumber = base;
Power(base, exponent);

}

public int Power(int base, int count){
if(count==1){
System.out.println("1 - The Answer is: " + base);
}
else{
System.out.println(count + " - " + base + " * " + baseNumber + " = " + (baseNumber * base));
base = baseNumber * base;
count--;
Power(base, count);
}
return base;

}
}```

The above code works and the correct answer is produced every time,

```
run:
Enter base number -> 9
Enter the exponent number -> 5
5 - 9 * 9 = 81
4 - 81 * 9 = 729
3 - 729 * 9 = 6561
2 - 6561 * 9 = 59049
1 - The Answer is: 59049
BUILD SUCCESSFUL (total time: 6 seconds)
```

I am just looking for a more experience java programmer who can critique this and let me know what or how to make it better, if any.

Thanks,

2. ## Re: Looking for a second opinion

also it is possible to store temporary results to array
and print it in separated method or in main()
but this is more demonstrative

```import java.util.Scanner;

public class RecursionPower {
public static void main(String[] args) {
int[] input = RecursionPower.UserEntry();
int base = input[0], exponent = input[1];

power(base, exponent));
}

public static int[] UserEntry() {
Scanner input = new Scanner(System.in);
System.out.println("Enter base number:");
int base = input.nextInt();
System.out.println("Enter the exponent number: ");
int exponent = input.nextInt();
System.out.println();

return new int[] {base,  exponent};
}

public static int power(int base, int count) {
String format = "(%d) %,9d * %d = %,9d\n";

if (count == 1) {
System.out.printf(format, count, 1, base, 1 * base);
return base;
} else {
//return power(base, count-1) * base;

int before = power(base, count - 1),
result = before * base;
System.out.printf(format, count, before, base, result);
return result;
}
}
}```

3. ## Re: Looking for a second opinion

Thanks Zemiak, I tried your code and it looks a lot cleaner and the output was a lot nicer as well.. Is there an advantage of using an array to store the two variables as pose to how I did it. IE. Does it speed up the processing time, etc. This project was so small it was hard to tell, but I am looking long range, when we get into larger projects.

4. ## Re: Looking for a second opinion

. maintaining a large projects,
it is easier to rewrite or change part of code if not direct depending on others parts.
change probably does not create hidden errors In other parts
. easy testing changes
. preventing to unwishing rewrite from other code with the same variable name used