package class3_1_Polymorphism; import java.text.DecimalFormat; import java.util.ArrayList; public abstract class Animal { private String name; private static ArrayList names = new ArrayList(); protected DecimalFormat formatter = new DecimalFormat("#.#"); /** * The distance forward from some point * in meters */ private double location; /** * Create a new animal. * @param name display name of the animal */ public Animal(String name) { names.add(name); if(name.length() >0) { this.name = name; } else { this.name = "UNDEFINED"; } } /** * Say something */ public abstract void speak(); /** * Simulate animal running * @param dist distance to run forward (meters) */ public void runForwardMeters(double dist) { location += dist; } public static void printAllNames() { for(String name: names) { System.out.println("Name: "+name); } } public boolean equals(Object o) { boolean result; if(o instanceof Animal) { Animal animal = (Animal) o; result = this.location == animal.location; } else { result = false; } return result; } // public boolean equals(Object o) { // Animal a = (Animal)o; // return this.name == a.name // && this.location == a.location; // } @Override public String toString() { return name+ "\t" + formatter.format(location) + " meters from start"; } }