import tester.*; interface IAnimal { IAnimal feed(double n); } class Snake implements IAnimal { String name; double weight; String food; Snake(String name, double weight, String food) { this.name = name; this.weight = weight; this.food = food; } // To check whether this snake is lighter than // n pounds boolean isLighter(double n) { return this.weight < n; } // To feed the snake amt pounds of food public IAnimal feed(double amt) { return new Snake(this.name, this.weight + amt, this.food); } } class Dillo implements IAnimal { double weight; boolean isAlive; Dillo(double weight, boolean isAlive) { this.weight = weight; this.isAlive = isAlive; } public IAnimal feed(double n) { if (this.weight >= 10) return this; else if (this.isAlive) return new Dillo(this.weight + n, true); else return this; } } class Posn { int x; int y; Posn(int x, int y) { this.x = x; this.y = y; } } class Ant implements IAnimal { double weight; Posn location; Ant(double weight, Posn location) { this.weight = weight; this.location = location; } public IAnimal feed(double n) { return new Ant(this.weight + n, this.location); } } interface IGrade { } class NoGrade implements IGrade { NoGrade() { } } class NumberGrade implements IGrade { int grade; NumberGrade(int grade) { this.grade = grade; } } class Examples{ Dillo rolly = new Dillo(2, true); Ant suzy = new Ant(0.0001, new Posn(3, 4)); Snake fred = new Snake("Fred", 10, "cereal"); IAnimal friend = suzy; // Ant yourFriend = friend; //-- not ok IGrade g1 = new NoGrade(); IGrade g2 = new NumberGrade(100); void tests(Tester t) { t.checkExpect(new Snake("Slinky", 12, "grass").isLighter(10), false); t.checkExpect(fred.isLighter(13), true); t.checkExpect(new Snake("Slinky", 12, "grass").feed(2), new Snake("Slinky", 14, "grass")); t.checkExpect(fred.feed(3), new Snake("Fred", 13, "cereal")); t.checkExpect(rolly.feed(1), new Dillo(3, true)); t.checkExpect(new Dillo(3, false).feed(1), new Dillo(3, false)); t.checkExpect(new Dillo(13, true).feed(1), new Dillo(13, true)); t.checkExpect(friend.feed(0.002), new Ant(0.0021, new Posn(3, 4))); } }