Results 1 to 6 of 6
  1. #1
    bluegreen7hi is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

    Default 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;
    		}
    }
    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!!

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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?

  3. #3
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    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);
    	
    	}
    	
    
    
    }

  4. #4
    bluegreen7hi is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

    Default

    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.

  5. #5
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    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 &#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);
        
        }
        
    
    
    }

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. exercise of recursive method
    By amexudo in forum New To Java
    Replies: 2
    Last Post: 03-09-2008, 05:55 PM
  2. Replies: 1
    Last Post: 02-16-2008, 09:10 PM
  3. Replies: 2
    Last Post: 02-16-2008, 08:52 PM
  4. Recursive Anagram
    By zoe in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 06:15 AM
  5. Help with recursive implementation
    By toby in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 05:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •