Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 25th, 2009, 08:53 AM
ds462228 ds462228 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 7 ds462228 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 27 m 8 sec
Reputation Power: 0
Homework - Error I just can't beat

Hey guys, I'm being a little bit stupid. My professor wanted me to write a basic recursive java program that raises a double to the power of an int, and returns a double. I decided, in my foolishness, to try to make it really fancy, and know DrJava keeps giving me this error that I can't work out. Here's the code...
//Let's start by grabbing the necessary packages...
import java.io.*;
import java.util.*;

//Names the class
public class Recurs1 {

//Fields
private double base;
private int power;

//Constructors
public Recurs1() {}


//Here's the recursive method...
public double recursPower(double, int){
if(this.power==0){
return 0.0;
}//if
else if(this.power==1){
return this.base;
}//else if
else{
this.power = this.power - 1;
return this.base * this.recursPower(this.base, this.power);
}//else
}//recursPower

//Now we need a main to get the input and run recursPower
public static void main() {
Recurs1 thingy = new Recurs1();
//Here's for the double
System.out.println("floating point number:");
Scanner s1 = new Scanner(System.in);
thingy.base = s1.nextDouble();
//Here's for the int
System.out.println("integer:");
Scanner s2 = new Scanner(System.in);
thingy.power = s2.nextInt();
//And, finally, our method call, which will return our answer.
thingy.recursPower(thingy.base, thingy.power);
}//main
}//class

The error is at the line where it says "public double recursPower(double, int){". It keeps telling me for both the double an the int this:<identifier> expected. Any ideas?

Reply With Quote
  #2  
Old June 25th, 2009, 09:03 AM
FluffieBird FluffieBird is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2009
Location: Denmark
Posts: 41 FluffieBird User rank is Sergeant Major (2000 - 5000 Reputation Level)FluffieBird User rank is Sergeant Major (2000 - 5000 Reputation Level)FluffieBird User rank is Sergeant Major (2000 - 5000 Reputation Level)FluffieBird User rank is Sergeant Major (2000 - 5000 Reputation Level)FluffieBird User rank is Sergeant Major (2000 - 5000 Reputation Level)FluffieBird User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 20 h 38 m 58 sec
Reputation Power: 30
You're forgetting names for the parameters in the method recursPower. It should be something like this:

Code:
public double recursPower(double x, int y)
{
  ...
}
Comments on this post
ishnid agrees!

Reply With Quote
  #3  
Old June 25th, 2009, 09:26 AM
ds462228 ds462228 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 7 ds462228 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 27 m 8 sec
Reputation Power: 0
Thank you much , I pulled the fields out so there is nothing between the (), having anything there was overkill. It runs just fine now. The only issue is getting it to spit the number back out. I don't exactly remember how to use the System.out.println(); to have it print out the returned value. Final question and everything should function properly.

Reply With Quote
  #4  
Old June 25th, 2009, 09:36 AM
Yawmark's Avatar
Yawmark Yawmark is offline
Feelin' Groovy
Click here for more information.
 
Join Date: Aug 2001
Location: WDSMIA
Posts: 9,073 Yawmark User rank is General 30th Grade (Above 100000 Reputation Level)Yawmark User rank is General 30th Grade (Above 100000 Reputation Level)Yawmark User rank is General 30th Grade (Above 100000 Reputation Level)Yawmark User rank is General 30th Grade (Above 100000 Reputation Level)Yawmark User rank is General 30th Grade (Above 100000 Reputation Level)Yawmark User rank is General 30th Grade (Above 100000 Reputation Level)Yawmark User rank is General 30th Grade (Above 100000 Reputation Level)Yawmark User rank is General 30th Grade (Above 100000 Reputation Level)Yawmark User rank is General 30th Grade (Above 100000 Reputation Level)Yawmark User rank is General 30th Grade (Above 100000 Reputation Level)Yawmark User rank is General 30th Grade (Above 100000 Reputation Level)Yawmark User rank is General 30th Grade (Above 100000 Reputation Level)Yawmark User rank is General 30th Grade (Above 100000 Reputation Level)Yawmark User rank is General 30th Grade (Above 100000 Reputation Level)Yawmark User rank is General 30th Grade (Above 100000 Reputation Level)Yawmark User rank is General 30th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 4 Weeks 1 Day 11 h 8 m 22 sec
Reputation Power: 2975
Send a message via ICQ to Yawmark Send a message via MSN to Yawmark
Quote:
Originally Posted by ds462228
I don't exactly remember how to use the System.out.println(); to have it print out the returned value. Final question and everything should function properly.

http://java.sun.com/docs/books/tuto...tion/index.html

~
__________________
Yawmark
class Sig{public static void main(String...args){\u0066or(int
\u0020$:"vÌÈÊ\"¤¾Àʲ¬Æ\"v¤Î¤\"²¤¨¸¬Æ".to\u0043h\u0061rArray()
)System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
+(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}

Reply With Quote
  #5  
Old June 25th, 2009, 11:28 AM
ds462228 ds462228 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 7 ds462228 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 27 m 8 sec
Reputation Power: 0
Thank you all for your help... I solved the issue by putting my method call of recursPower on the thingy object inside the parens of System.out.println(); and i also added the forgotten String[] args within the parens on the main method. Program functions beautifully now. Here's a glance at my final code for anyone who is interested:
//Let's start by grabbing the necessary packages...
import java.io.*;
import java.util.*;

//Names the class
public class Recurs1 {

//Fields
private double base;
private int power;

//Constructors
public Recurs1() {}


//Here's the recursive method...
public double recursPower(){
if(this.power==0){
return 0.0;
}//if
else if(this.power==1){
return this.base;
}//else if
else{
this.power = this.power - 1;
return this.base * this.recursPower();
}//else
}//recursPower

//Now we need a main to get the input and run recursPower
public static void main(String[] args) {
Recurs1 thingy = new Recurs1();
//Here's for the double
System.out.println("floating point number:");
Scanner s1 = new Scanner(System.in);
thingy.base = s1.nextDouble();
//Here's for the int
System.out.println("integer:");
Scanner s2 = new Scanner(System.in);
thingy.power = s2.nextInt();
//And, finally, our method call, which will return our answer.
System.out.println(thingy.recursPower());
}//main
}//class

Once again, thanks for your help. My prof will be most excited

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - Error I just can't beat


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
Stay green...Green IT