Results 1 to 6 of 6
Thread: Recursive Method
 11282007, 04:54 AM #1Member
 Join Date
 Nov 2007
 Posts
 12
 Rep Power
 0
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:
Java 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; } }
 11282007, 07:23 AM #2
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 21
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?
 11282007, 04:52 PM #3
If I understand the problem correctly, I think this should do it:
Java 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 % 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); } }
 11282007, 10:17 PM #4Member
 Join Date
 Nov 2007
 Posts
 12
 Rep Power
 0
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.
 11282007, 10:28 PM #5
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.
Java 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 % 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); } }
 11292007, 05:45 AM #6
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 21
Ya, you have miss the object for Recursive4 class. Should use for calling methods.
Similar Threads

exercise of recursive method
By amexudo in forum New To JavaReplies: 2Last Post: 03092008, 06:55 PM 
Recursive Method ==> find minimum value from array
By NatNat in forum New To JavaReplies: 1Last Post: 02162008, 10:10 PM 
Recursive Method ==> find how many times a value is repeated in an array
By NatNat in forum New To JavaReplies: 2Last Post: 02162008, 09:52 PM 
Recursive Anagram
By zoe in forum Advanced JavaReplies: 1Last Post: 08072007, 06:15 AM 
Help with recursive implementation
By toby in forum Advanced JavaReplies: 1Last Post: 08072007, 05:57 AM
Bookmarks