Lab 5 Comp 170-201 -- Dordal/Nabicht -- Wed, Sept 25, 2002 Final version due Fri Oct 4 The exam is Monday, Sept 30 Animals Take the Animal class and answer the following questions and make the indicated changes: 1. The field _kind is defined as a member field, but never used. Add it to: the existing constructor, so it will have form Animal(String name, String kind); the driver program (eg horse = new Animal("Flash", "horse");) toString(), so we get (Misty the horse gray legs=4 age=5) 2. Add a *second* constructor, that takes and sets parameters name, kind, age, numberLegs, color Use it to create a new animal instance. 3. Add a new field altogether, with appropriate field accessor and mutator. The new field is any animal attribute of your choice: it could be _eyecount, or _hasTail (boolean), or _tailCount (a bit odd, but...), or _hornCount (Triceratops, anyone?). Make toString() print out something sensible about your attribute. The constructors should all sensibly initialize your attribute (eg to 0), but you do *not* have to add it as a parameter. The following are to be done in the Farm class, not in Animal: 4. Write a function void setAge(Animal a, int n) **in the Farm class, *NOT* in Animal**, that attempts to set the age of the Animal a to n. This can be done as long as a.age() <=n; if a.age()>n, print a warning message and leave a unchanged. 5. Write a function void moveLeg(Animal donor, Animal recipient) that makes donor lose a leg, if it has one, and recipient gains a leg. If donor has no legs to start with, nothing at all is done. 6. Run your revised class with a few new animals. Questions: 1. Which member functions (methods) are accessors? Which are mutators? I guess that's all the questions. //////////////////////////////////////////////////////// // class Animal //////////////////////////////////////////////////////// import java.awt.*; /** * Represents an animal * peter dordal, 9/25/02 */ public class Animal { // instance variables - replace the example below with your own private String _kind; private String _name; private int _numberLegs; private Color _color; // list of colors is on p 114 (or below) private int _age; /** * Constructor for objects of class Animal */ public Animal(String name) { // initialise instance variables _name = name; _numberLegs = 0; _color = Color.green; // why this type? _age = 0; } // "copy ctor": special kind of ctor for making copies public Animal(Animal a) { _name = a._name; _kind = a._kind; _numberLegs = a._numberLegs; _color = a._color; _age = a._age; } public void setName (String name) {_name = name;} public String name() {return _name;} public void setColor(Color c) {_color = c;} public Color color() {return _color;} public int legs() {return _numberLegs;} public void growLeg() {_numberLegs++;} public void loseLeg() { if (_numberLegs > 0) _numberLegs--; } public void increaseAge() {_age++;} public int age() { return _age;} // why toString?? public String toString() { String result = "("; result += _name; result += ' '; result += color2string(_color); result+= " legs="; result += _numberLegs; result += " age="; result += _age; result += ")"; return result; } // end of toString private String color2string(Color c) { if (c == Color.black) return "black"; else if (c == Color.blue) return "blue"; else if (c == Color.cyan) return "cyan"; else if (c == Color.gray) return "gray"; else if (c == Color.darkGray)return "darkGray"; else if (c == Color.lightGray)return "lightGray"; else if (c == Color.green) return "green"; else if (c == Color.magenta)return "magenta"; else if (c == Color.orange) return "orange"; else if (c == Color.pink) return "pink"; else if (c == Color.red) return "red"; else if (c == Color.white) return "white"; else if (c == Color.yellow) return "yellow"; else return c.toString(); } // color2string } // end of class Animal //////////////////////////////////////////////////////// // class Farm //////////////////////////////////////////////////////// import java.awt.Color; // this is all we need from java.awt /** * Write a description of class Farm here. * * @author (your name) * @version (a version number or a date) */ public class Farm { public static void main (String[] s) { Animal cow, duck, horse, cat, chicken, fish; duck = new Animal("Rev Graham"); horse = new Animal("Misty"); cat = new Animal ("Blackie"); chicken = new Animal("Red"); fish = new Animal("Jaws"); duck.growLeg(); duck.growLeg(); duck.increaseAge(); System.out.println(duck.toString()); horse.growLeg(); horse.growLeg(); horse.growLeg(); horse.growLeg(); horse.setColor(Color.gray); for (int i=0; i<10; i++) horse.increaseAge(); System.out.println(horse.toString()); Animal mandy = new Animal(horse); mandy.setName("Mandy"); System.out.println(horse.toString()); cat.growLeg(); cat.growLeg(); cat.growLeg(); cat.growLeg(); cat.setColor(Color.black); System.out.println(cat.toString()); chicken.growLeg(); chicken.growLeg(); chicken.setColor(Color.red); chicken.increaseAge(); System.out.println(chicken.toString()); fish.setColor(Color.yellow); //fish.setColor(Color.yellow()); // oops! System.out.println(fish.toString()); } } // end of Farm