Recursive Method

• 11-28-2007, 04:54 AM
bluegreen7hi
Recursive Method
Hello again, I am yet again confused by one of my assignments for my Java class. The assignment is as follows:

Write a very simple class using the recursive method on page 99. Create the class with the name Recursive4. In the class put the method exactly like it is in the book. Place a main method in the class that has a int variable 'num' set equal to 7. Call the power3 method with the num variable as its parameter. Then output the return value from the method. You will need to have a variable capture the return value:
int returnValue = power3(num);

Submit the working program.

The recursive method from the book that it is talking about:

Code:

```        // Precondition: n >= 0         public int power3(int n)         {                 if (n == 0)  //if n equals to 0                   return 1;                 else                 {                         int p = power3(n/2);                                 // when n is odd, n/2 is truncated to an integer                                 // for example, 7/2 gives 3 and 1/2 gives 0                                                         p *= p;  // multiply by itself                                                 if (n % 2 == 1)  //if n is odd,                           p *= 3;        // multiply p by 3                                                   return p;                 } }```
I am pretty confused overall as to what the assignment is asking of me. If anyone could point me in the right direction it would be greatly appreciated. Thanks!!
• 11-28-2007, 07:23 AM
Eranga
Seems to me simply return a value. May that number can be an kind of..(prime, etc..)

Can you tell more about num variable. How it is change?
• 11-28-2007, 04:52 PM
ShoeNinja
If I understand the problem correctly, I think this should do it:

Code:

```public Class Recursive4{         //method from book                 // Precondition: n >= 0         public int power3(int n)         {                 if (n == 0)  //if n equals to 0                   return 1;                 else                 {                         int p = power3(n/2);                                 // when n is odd, n/2 is truncated to an integer                                 // for example, 7/2 gives 3 and 1/2 gives 0                                                         p *= p;  // multiply by itself                                                 if (n &#37; 2 == 1)  //if n is odd,                           p *= 3;        // multiply p by 3                                                   return p;                 }         }                 //now all you need is a main method         public static void main(String[] args){                 //required num variable                 int num = 7;                                 //required return value                 int returnValue = power3(num);                                 //print returnValue                 System.out.println(returnValue);                 }         }```
• 11-28-2007, 10:17 PM
bluegreen7hi
I tried compiling that but now I'm getting a bunch of errors:

--------------------Configuration: Recursive4 - JDK version 1.6.0_03 <Default> - <Default>--------------------
C:\JavaPrograms\Recursive4\src\Recursive4.java:1: class, interface, or enum expected
public Class Recursive4{
^
C:\JavaPrograms\Recursive4\src\Recursive4.java:5: class, interface, or enum expected
public int power3(int n)
^
C:\JavaPrograms\Recursive4\src\Recursive4.java:9: class, interface, or enum expected
else
^
C:\JavaPrograms\Recursive4\src\Recursive4.java:15: class, interface, or enum expected
p *= p; // multiply by itself
^
C:\JavaPrograms\Recursive4\src\Recursive4.java:17: class, interface, or enum expected
if (n % 2 == 1) //if n is odd,
^
C:\JavaPrograms\Recursive4\src\Recursive4.java:20: class, interface, or enum expected
return p;
^
C:\JavaPrograms\Recursive4\src\Recursive4.java:21: class, interface, or enum expected
}
^
C:\JavaPrograms\Recursive4\src\Recursive4.java:25: class, interface, or enum expected
public static void main(String[] args){
^
C:\JavaPrograms\Recursive4\src\Recursive4.java:30: class, interface, or enum expected
int returnValue = power3(num);
^
C:\JavaPrograms\Recursive4\src\Recursive4.java:33: class, interface, or enum expected
System.out.println(returnValue);
^
C:\JavaPrograms\Recursive4\src\Recursive4.java:35: class, interface, or enum expected
}
^
11 errors

Process completed.
• 11-28-2007, 10:28 PM
ShoeNinja
Sorry. I just wrote it in a text editor and didn't bother compiling. I was just trying to get the general idea across. I thought you could at least do some of your homework. Here's a working version.

Code:

```public class Recursive4{     public Recursive4(){     }     //method from book         // Precondition: n >= 0     public int power3(int n)     {         if (n == 0)  //if n equals to 0           return 1;         else         {             int p = power3(n/2);                 // when n is odd, n/2 is truncated to an integer                 // for example, 7/2 gives 3 and 1/2 gives 0                             p *= p;  // multiply by itself                         if (n &#37; 2 == 1)  //if n is odd,               p *= 3;        // multiply p by 3                           return p;         }     }         //now all you need is a main method     public static void main(String[] args){       Recursive4 foo = new Recursive4();         //required num variable         int num = 7;                 //required return value         int returnValue = foo.power3(num);                 //print returnValue         System.out.println(returnValue);         }     }```
• 11-29-2007, 05:45 AM
Eranga
Ya, you have miss the object for Recursive4 class. Should use for calling methods.