Help with toString method and an addObject method?
I have a Room class and an Animal class. A Room can hold 10 Animals. Both rooms and animals have names. I'm having trouble with the Room class's toString method. I want it to return its [the room's] name and the names of all the animals in the room. My addAnimal method in the Room isn't coded well, I can't figure out the correct way to go about it. Originally I was trying to cycle through the array 'animals' with a for loop to put an animal in each slot of the array but my method was giving me errors. The code for that was something like:
public void addAnimal(Animal a){
for(int x=0; x < animals.length; x++){
animals[x] = a;
}
}
That was giving me the incorrect input I wanted, so I tried manually inserting a different Animal object into each slot of the array, 'animals'.
My main class has comments on what should be outputting...
public class Main {
public static void main(String[] args) {
Room myroom = new Room("Bathroom");
myroom.addAnimal(new Animal("Dog"));
myroom.addAnimal(new Animal("Cat"));
myroom.addAnimal(new Animal("Bird"));
String temp = myroom.toString();
//should print nothing on the screen by this point
System.out.println(temp);
//should print on the screen the following:
//Room Snygg 107 populated by:
//Animal Dog
//Animal Cat
//Animal Bird
}
}
Here are my Animal and Room Classes:
public class Animal {
private Room room;
private String name;
String toString;
public Animal(String animalName){
name = animalName;
}
public String getName(){
return name;
}
public String toString(){
return name;
}
}
------------------
public class Room {
private String name;
private Animal [] animals;
public Room(String roomName)
{
name = roomName;
animals = new Animal[10];
}
public void addAnimal(Animal a){
Animal b = new Animal("");
Animal c = new Animal("");
Animal d = new Animal("");
Animal e = new Animal("");
Animal f = new Animal("");
Animal g = new Animal("");
Animal h = new Animal("");
Animal i = new Animal("");
Animal j = new Animal("");
animals[0] = a;
animals[1] = b;
animals[2] = c;
animals[3] = d;
animals[4] = e;
animals[5] = f;
animals[6] = g;
animals[7] = h;
animals[8] = i;
animals[9] = j;
}
public String toString(){
String names = "";
for(Animal animal : animals){
names = animal.toString;
}
return "Room: " + name + ", populated by... \n" + names;
}
}
My current output with this code is:
"Room: Snygg 107, populated by...
null"
Re: Help with toString method and an addObject method?
Please put your code in [code] tags.
Re: Help with toString method and an addObject method?
Try reviewing your code to see what it's doing. For example, this method populates the entire array with the same Animal class. You'll need to write a few more lines of code to get the first index in the array that does not have an Animal and put the input Animal into that one slot.
Code :
public void addAnimal(Animal a){ // Your method above
for(int x=0; x < animals.length; x++){
animals[x] = a;
}
}
For your toString method, try something like this:
Code :
public String toString() {
String output = "";
int i = 0;
while ( i < animals.length ... ) { // loop through the animals array and check if this index has an animal
// Add the name to the output value
// don't forget your line breaks
}
System.out.println("Room " + this.name + " contains: " + output);
}