Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-28-2007, 04:54 AM
Member
 
Join Date: Nov 2007
Posts: 12
bluegreen7hi is on a distinguished road
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!!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-28-2007, 07:23 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,510
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to 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?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-28-2007, 04:52 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to 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 % 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); } }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-28-2007, 10:17 PM
Member
 
Join Date: Nov 2007
Posts: 12
bluegreen7hi is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-28-2007, 10:28 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to 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 % 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); } }
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-29-2007, 05:45 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,510
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Ya, you have miss the object for Recursive4 class. Should use for calling methods.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
exercise of recursive method amexudo New To Java 2 03-09-2008 06:55 PM
Recursive Method ==> find minimum value from array NatNat New To Java 1 02-16-2008 10:10 PM
Recursive Method ==> find how many times a value is repeated in an array NatNat New To Java 2 02-16-2008 09:52 PM
Recursive Anagram zoe Advanced Java 1 08-07-2007 07:15 AM
Help with recursive implementation toby Advanced Java 1 08-07-2007 06:57 AM


All times are GMT +3. The time now is 11:31 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org