import java.util.Scanner; /** * Animal.java * CS230 Lab on creating java user-defined classes. * * @author Sohie and Stella * * */ public class Animal { // instance variables private String type; //like dog private String name; //like Spot private String voice; //like woof private boolean canFly; private int numberLegs; private boolean goodHealth; /** * Constructor for Animal objects * * @param t the type of the animal * @param n the name of the animal * @param v the voice of the animal * @param fly boolean, to indicate wether the animal can fly or not * @param numLegs the number of legs of this animals * @param health boolean indicates the health state of this animal */ public Animal(String t, String n, String v, boolean fly, int numlegs) { this.type = t; this.name = n; voice = v; this.canFly = fly; this. numberLegs = numlegs; } /** * It constructs an animal with the given type and name. * By default the animal is silent (no voice), has 4 legs, * and cannot fly * * @param t the type of the animal * @param n the name of the animal */ public Animal(String t, String n) { this(t, n, "", false, 4); } /** * sets instance variable canFly to be true */ public void tookFlyingLessons() { canFly = true; } /** * sets instance variable voice to be the empty string */ public void caughtLaryngitis() { this. voice = ""; //or just voice = "'; } /** * sets instance variable voice to be "meow" */ public void likesToImitateCats() { voice = "meow"; } /** * Returns the voice of this animal * * @return The voice of this animal */ public String getVoice() { return voice; } /** * Returns the type of the animal * * @return The type of this animal */ public String getType() { return type; } /** * Returns the name of the animal * * @return The name of this animal */ public String getName() { return name; } /** * Returns whether the animal can fly or not * * @return true if this animal can fly, false otherwise */ public boolean getCanFly() { return canFly; } /** * Returns the number of legs of this animal * * @return the number of legs of this animal */ public int getNumberLegs() { return numberLegs; } /** * Returns the health status of this animal * * @return true if this animal is in good health, * false otherwise */ public boolean isHealthGood() { return goodHealth; } /** * Sets the voice of this animal to the input value * * @param the new voice of this animal */ public void setVoice(String v) { voice = v; } /** * Sets the health status of this animal to the input value * * @param boolean the true for healthy, false for unhealthy */ public void setHealth(boolean h) { this.goodHealth = h; } /** * Sets the name of this animal to the input value * * @param the new name of this animal */ public void setName(String n) { name = n; } /** * Sets the type of this animal to the input value * * @param the new type of this animal */ public void setType(String t) { type = t; } /** * Sets the flying status of this animal to the input value * * @param the new flying of this animal */ public void setCanFly(boolean fly){ canFly = fly; } /** * Prints the value of the voice of this animal 3 times */ public void speak() { System.out.println(voice + " " + voice + " " + voice); } /** * Sets the number of legs of this animal to the input value * * @param the new number of legs for this animal */ public void setNumberLegs(int legs){ numberLegs = legs; } /** * Returns a string representation of this animal * * @return A string containing all atributes of this animal */ public String toString() { String s = ""; String doesItFly = canFly ? "" : "not"; String isItHealth = ""; if (!goodHealth) { isItHealth = " not "; } s+= name + " is a " + type + " with " +numberLegs + " legs" + " that can" + doesItFly + " fly" + " that says "+ voice + ". This animal is " + isItHealth + " in good health"; return s; } /** * Returns a string representation of conversation between two animals. * (the invoker and the input one) * * @param The animal this one is conversing with * @return The conversation between the two animals */ public String converse(Animal other) { String header = "A conversation between " + this.name + " and " + other.name + ":" + "\n"; String space = " "; String conversation = space + this.voice + "\n"; conversation += space + other.voice + "\n"; conversation += space + this.voice+ "\n"; conversation += space + other.voice + "\n"; return header + conversation; } /** * Returns true if this animal and the input one have * the same type and the same name, false othrwise * * @param The animal to be compared for equality to this one * @return true if the two animals are the same, false otherwise */ public boolean equals(Animal other) { return (this.type).equals(other.type) && (this.name).equals(other.name); } /** * Uses a scanner to read information about an animal from the user. * Then it creates the animal, and at the end returns it. * * @return Animal instance, created according * to the input collected from the user * */ public static Animal readAndCreateAnimal() { Scanner s = new Scanner(System.in); //ask for and read input from the user System.out.println("What kind of animal is this?"); String kind = s.nextLine(); System.out.println("What is the name of it?"); String name = s.nextLine(); System.out.println("What sound does it make?"); String sound = s.nextLine(); System.out.println("Can it fly (true/false)?"); boolean fly = s.nextBoolean(); System.out.println("How many legs does it have?"); int legs = s.nextInt(); // Create the animal and return it return new Animal(kind, name, sound, fly, legs); } /** * main() method for testing * */ public static void main (String [] args ) { System.out.println("-------------- Starting Testing --------------"); Animal pet1 = new Animal("spider","Charlotte", " ",false,8); Animal pet2 = new Animal("dog","Lassie"); Animal pet3 = new Animal("frog","Bill","ribbit",false,4); Animal a; System.out.println(pet1); System.out.println(pet2); System.out.println(pet3); System.out.println("Testing getters"); System.out.println("getName(). Expect Charlotte. Got: " + pet1.getName()); System.out.println("getvVoice(). Expect (nothing). Got: " + pet1.getVoice()); System.out.println("canFly(). Expect false. Got: : " + pet1.getCanFly()); System.out.println("getNumberLegs(). Expect 4. Got: " + pet1.getNumberLegs()); System.out.println("\nTesting setters"); pet3.setType("goat"); System.out.println("setType(). Expect goat " + pet3.getType()); pet3.setNumberLegs(2); System.out.println("setNumberLegs(). Expect 2 " + pet3); } }