spoggles
asked on
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!
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 +
'}';
}
}
ASKER
That link explained everything in more detail. I had no idea of toString()
:-)
ASKER