As part of my project, I have to make a loop to repeatedly add objects to my queue
Here is the code
for(int rabbitcounter=0; rabbitcounter<30;rabbitcounter++) {
yettoracequeue.add(new Rabbits("Rabbit" + rabbitcounter, Math.random()*100));
System.out.println(yettoracequeue);
System.out.println(rabbitcounter);
I always use System.out.println to check if things are going as expected.
However when the System.out.println executes above, it gives me
[queuepart.Rabbits@7852e922]
Instead of Rabbit 1.
Using the above, I tried to call the getName() method from my Rabbits class on it with the following line
System.out.println(queuepart.Rabbits@7852e922.getName());
but it gives at error. From what I understand it is because the object has not been initialized.
Here is my Rabbits class
package queuepart;
public class Rabbits {
// properties of rabbits
private double speed;
private String name;
//private int counter = 1;
//Constructor, must be name of object
public Rabbits() {
}
public Rabbits(String name, double speed) {
this.name = name;
this.speed = speed;
//counter++;
}
//Speedgetter
public double getSpeed() {
return speed;
}
//Namegetter
public String getName() {
return name;
}
//Speedsetter
public void setSpeed (double speed) {
this.speed = speed;
}
//Namesetter
public void setName(String name) {
this.name = name;
}
}
I think I am still able to proceed to the next step of my project with the wrongly provided names but the conclusion of my project requires me to have the correct rabbit names e.g Rabbit 1, Rabbit 2 etc etc
Thank you and sorry for the long post. :)