Java enhanced for loop

Hello experts!

This is my first foray into the enhanced for loop in Java. I have everything working except for one some problem. This line: "System.out.println("For the " + anim + ": ");" produces some awkward results. The results are:

For the Dog@42e816:
For the Fish@9304b1:

Why is the code producing the @xxxxxx? Also, is there a way to safely remove the @xxxxxx from printing to the screen? Thanks!
//Begin Animal.java

public abstract class Animal {
	String type, describe, sound, sleep, move;
	
	public Animal(String name) {
		this.type = name;
	}
	
	abstract void describe();
	abstract void sound();
	abstract void sleep();
	abstract void move();
}
//end Animal.java

//begin Dog.java

public class Dog extends Animal{
	
	String name;
	
	//default constructor
	public Dog() {
		super("Dog");
	}
	
	//constructor with one parameter
	public Dog(String name) {
		super("Dog");
		this.name = name;
	}
	
	//override abstract methods from Animal.java
	public void describe() {
		System.out.println(this.name + " is a Dog that can be very fun to play with!");
	}
	
	public void sound() {
		System.out.println("Bark Bark!");
	}
	
	public void sleep() {
		System.out.println("Some dogs kick their legs while they sleep.");
	}
	
	public void move() {
		System.out.println("As long as " + this.name + " isn't lazy, Dogs move very fast.");
	}
}
//end Dog.java

//begin Fish.java
public class Fish extends Animal{
	
	String name;
	
	//default constructor
	public Fish() {
		super("Fish");
	}
	
	//constructor with one parameter
	public Fish(String name) {
		super("Fish");
		this.name = name;
	}
	//override abstract methods from Animal.java
	public void describe() {
		System.out.println(this.name + " is a Fish that likes to swim!");
	}
	
	public void sound() {
		System.out.println("What sounds does a fish make?");
	}
	
	public void sleep() {
		System.out.println("Fish sleep with their eyes open!");
	}
	
	public void move() {
		System.out.println("Fish swim with their tails.");
	}
}
//end Fish.java

//begin LoopTest.java

import java.util.ArrayList;

public class LoopTest {
	public static void main(String[] args) {
		
		//create new ArrayList
		ArrayList<Animal> myAnimals = new ArrayList<Animal>();
		
		//create the objects
		Dog dog = new Dog("Ruff Ruff");
		Fish fish = new Fish("Bubbles");
		
		//add objects to ArrayList
		myAnimals.add(dog);
		myAnimals.add(fish);
		
		for(Object anim : myAnimals) {
			System.out.println("For the " + anim + ": ");
			System.out.print("This is:  "); ((Animal) anim).describe();
			System.out.print("Sound:    "); ((Animal) anim).sound();
			System.out.print("Sleeping: "); ((Animal) anim).sleep();
			System.out.print("Moving:   "); ((Animal) anim).move();
			System.out.print("\n");
		}
		
		System.out.println("End of program.");
		
	}
}

//end LoopTest.java

Open in new window

spogglesAsked:
Who is Participating?
 
ozlevanonCommented:
When java has to print an object it calls the class's toString() method (except for null objects, in which case it generates a "null" String). The toString() implementation in a class should print the data you want to see. Usually it's just a list of all the members you have and their values, something like the code below.
Naturally, you can print anything you want in your toString implementation. If you want to format a value before printing it - feel free.
When a class doesn't explicitly implement toString() it uses the implementation it inherits from java.lang.Ojbect which contains the class name and some pointer like identifier of it, which is what you see in your code.

See more at http://www.java-samples.com/showtutorial.php?tutorialid=218
0
 
CEHJCommented:
You need to implement toString in your classes
0
 
spogglesAuthor Commented:
Can you give me an example of how I would implement the toString?
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
ozlevanonCommented:
Sample code:
public class ClassWithToString
{
	private String name;
	private String phone;
	private int zipCode;

	@Override
	public String toString()
	{
		return "ClassWithToString{" +
				"name='" + name + '\'' +
				", phone='" + phone + '\'' +
				", zipCode=" + zipCode +
				'}';
	}
}

Open in new window

0
 
spogglesAuthor Commented:
That link explained everything in more detail. I had no idea of toString()
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.