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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.