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]

public double getPairs(); {
return rabbitPairs;
}
Can you see your error now? Also in your default constructor you are re-declaring the rabbitPairs variable.
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?
The java file that tests your code is usually called the Driver.It has a main method and you can google: java main method
Object a = new Object();a.method1();System.ouot.println(a.method2());
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?
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!
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).
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;
^
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?
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?
What is incrementation? Like, setting up multiple variables inside the loop to make sure they are what I want them to be?
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
Incrementation just means increase a value. I'm just guessing but should it be += in your code?
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
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.
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 :-(
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 :-(
Yeah last minute is bad but can't...resist...too...much...time!!!