|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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? |
|
#2
|
|||
|
|||
|
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)
{
...
}
|
|
#3
|
|||
|
|||
|
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. |
|
#4
|
||||
|
||||
|
Quote:
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)));}} |
|
#5
|
|||
|
|||
|
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 ![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > Homework - Error I just can't beat |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|