Having some trouble

I need to create a class with two methods, getPairs and waitAMonth, that will determine rabbit population. I have this so far.

publicclass RabbitPopulation

{

public RabbitPopulation()

{

double rabbitPairs=0;

}

public RabbitPopulation(double initialRabbitPairs)

{

rabbitPairs = initialRabbitPairs;

}

publicvoid waitAMonth(double month)

{

double newrabbitPairs = month * (rabbitPairs - 1) + month * (rabbitPairs - 2);

rabbitPairs = newrabbitPairs;

}

publicdouble getPairs();

{

return rabbitPairs;

}

double rabbitPairs;

}

However, it won't compile. I'm getting two error messages.

RabbitPopulation.java:26: missing method body, or declare abstract

public double getPairs();

^

RabbitPopulation.java:28: return outside method

return rabbitPairs;

^

Any suggestions?

[1668 byte] By [sjmclean7a] at [2007-11-26 20:02:29]
# 1

public double getPairs(); {

return rabbitPairs;

}

Can you see your error now? Also in your default constructor you are re-declaring the rabbitPairs variable.

floundera at 2007-7-9 23:01:43 > top of Java-index,Java Essentials,New To Java...
# 2
You're my hero :-D
sjmclean7a at 2007-7-9 23:01:43 > top of Java-index,Java Essentials,New To Java...
# 3
can i say some?
fastmikea at 2007-7-9 23:01:43 > top of Java-index,Java Essentials,New To Java...
# 4
I also have to write a test program to make sure it works...which I have no idea how. We're just starting to touch on objects and classes, I don't know how to write another class that will call on the objects/methods I've just made. Can anyone point me in the right direction?
sjmclean7a at 2007-7-9 23:01:43 > top of Java-index,Java Essentials,New To Java...
# 5
The java file that tests your code is usually called the Driver.It has a main method and you can google: java main method
lethalwirea at 2007-7-9 23:01:44 > top of Java-index,Java Essentials,New To Java...
# 6
Object a = new Object();a.method1();System.ouot.println(a.method2());
floundera at 2007-7-9 23:01:44 > top of Java-index,Java Essentials,New To Java...
# 7

Ok, so this is what I have at this point.

public class RabbitPopulation

{

public RabbitPopulation(double initialRabbitPairs)

{

rabbitPairs = initialRabbitPairs;

}

public void waitAMonth(double month)

{

double newrabbitPairs = month * (rabbitPairs - 1) + month * (rabbitPairs - 2);

rabbitPairs = newrabbitPairs;

}

public double getPairs()

{

return rabbitPairs;

}

double rabbitPairs;

public static void main(String [] args){

RabbitPopulation a = new RabbitPopulation();

a.waitAMonth(10);

System.out.println(a.getPairs());

}

}

However, it's giving me the error

RabbitPopulation.java:30: cannot find symbol

symbol : constructor RabbitPopulation()

location: class RabbitPopulation

RabbitPopulation a = new RabbitPopulation();

^

What did I do wrong?

sjmclean7a at 2007-7-9 23:01:44 > top of Java-index,Java Essentials,New To Java...
# 8
Wow I forgot to give the parameters for my RabbitPopulation(1), ok I'm sorry. I see it. Now it works, I just get to fix the logic errors. Thanks guys!
sjmclean7a at 2007-7-9 23:01:44 > top of Java-index,Java Essentials,New To Java...
# 9
month * (rabbitPairs - 1) + month * (rabbitPairs - 2);Are you sure this is the correct formula. If you initial population is 1 then this will be (10 * 0) + (10 * -1).
floundera at 2007-7-9 23:01:44 > top of Java-index,Java Essentials,New To Java...
# 10

That's the logic error I was talking about...the formula is actually

F * (N-1) + F * (N-2)

its a function, I'm working on a loop right now. thus far I've got

public void waitAMonth(double month)

{

double f = 1;

while(f<=month){

double newrabbitPairs = (f * (rabbitPairs - 1)) + (f * (rabbitPairs - 2));

f++;}

rabbitPairs = newrabbitPairs;

}

but it's giving me the error message

RabbitPopulation.java:23: cannot find symbol

symbol : variable newrabbitPairs

location: class RabbitPopulation

rabbitPairs = newrabbitPairs;

^

sjmclean7a at 2007-7-9 23:01:44 > top of Java-index,Java Essentials,New To Java...
# 11

Fixed the error message by declaring/initializing newrabbitsPairs

public void waitAMonth(double month)

{

double f = 1;

double newrabbitPairs = 0;

while(f<=month){

newrabbitPairs = (f * (rabbitPairs - 1)) + (f * (rabbitPairs - 2));

f++;}

rabbitPairs = newrabbitPairs;

}

but my answers are still far off. Is my loop wrong?

sjmclean7a at 2007-7-9 23:01:44 > top of Java-index,Java Essentials,New To Java...
# 12
Each time around the loop you are assigning a new value to newrabbitPairs, overwriting the last value stored there. Essentially the only value newrabbitPairs will hold will be the last one. Do you need to do some incrementation as well?
floundera at 2007-7-9 23:01:44 > top of Java-index,Java Essentials,New To Java...
# 13
What is incrementation? Like, setting up multiple variables inside the loop to make sure they are what I want them to be?
sjmclean7a at 2007-7-9 23:01:44 > top of Java-index,Java Essentials,New To Java...
# 14
Incrementing means to add values to your number.For instance myVal = myVal = 1; or myVal ++;If you never had increments or decrements in your loops, they would run forever, or not at all.Message was edited by: lethalwire
lethalwirea at 2007-7-9 23:01:44 > top of Java-index,Java Essentials,New To Java...
# 15
Incrementation just means increase a value. I'm just guessing but should it be += in your code?
floundera at 2007-7-21 17:49:33 > top of Java-index,Java Essentials,New To Java...
# 16

Right now this is what I've got

public class RabbitPopulation

{

public RabbitPopulation(double initialRabbitPairs)

{

rabbitPairs = initialRabbitPairs;

}

public void waitAMonth(double month)

{

double f = 1;

double newrabbitPairs = 0;

double x = 0;

double y = 0;

while(f<=month){

rabbitPairs = (f * (rabbitPairs - 1)) + (f * (rabbitPairs - 2));

if(rabbitPairs <= 0){

rabbitPairs=1;}

f++;}

}

public double getPairs()

{

return rabbitPairs;

}

double rabbitPairs;

public static void main(String [] args){

RabbitPopulation a = new RabbitPopulation(1);

a.waitAMonth(10);

System.out.println(a.getPairs());

}

}

It's returning a value of 1 every time. the Rabbit Population was becoming less than one, so I put in the if statement, but now it just stays at one

sjmclean7a at 2007-7-21 17:49:33 > top of Java-index,Java Essentials,New To Java...
# 17

rabbitpairs = 1

f = 1

(f * (rabbitPairs - 1)) + (f * (rabbitPairs - 2))

= (1 * (1-1)) + (1 * (1 - 2))

= (1* 0) + (1 * -1)

= 0 + -1

= -1

So rabbitPairs is now -1 which is < 0 so you change it back to one and go around the loop.

Did you read reply #15.

I think you need to go back to the lecturer and clarify how the formula is supposed to work.

floundera at 2007-7-21 17:49:33 > top of Java-index,Java Essentials,New To Java...
# 18

The formula I have to implement is found at

http://en.wikipedia.org/wiki/Fibonacci_number

It's pretty simple, but how to make java do it is escaping me. I've changed my code to

public void waitAMonth(double month)

{

double f = 1;

double newrabbitPairs = 0;

double rabbitPairs2 = 0;

double y = 0;

while(f<=month){

if(f<1){newrabbitPairs = 0;}

if(f==1){newrabbitPairs = 1;}

if(f>1){newrabbitPairs = f * (rabbitPairs - 1) + f * (rabbitPairs - 2);}

f++;}

}

and still returning 1 :-(

sjmclean7a at 2007-7-21 17:49:33 > top of Java-index,Java Essentials,New To Java...
# 19
Wow...looking back on that formula, I was pretty dumb. I figured it out/finished my project, thanks for all the help. I guess that's what happens when you work 8 hours on a project you've had for three weeks, but is due tomorrow :-(
sjmclean7a at 2007-7-21 17:49:33 > top of Java-index,Java Essentials,New To Java...
# 20
Yeah last minute is bad but can't...resist...too...much...time!!!
Icycoola at 2007-7-21 17:49:33 > top of Java-index,Java Essentials,New To Java...