import tester.*; interface IAnimal { // Determines whether the animal is less than n lbs boolean isLighter(double n); IAnimal feed(); } class Snake implements IAnimal { String name; int weight; String food; Snake(String name, int weight, String food) { this.name = name; this.weight = weight; this.food = food; } // Determines whether the name is < n lbs public boolean isLighter(double n) { return this.weight < n; } // To feed the snake 5 lbs of food public Snake feed() { return new Snake(this.name, this.weight + 5, this.food); } } class Dillo implements IAnimal { double weight; boolean isAlive; Dillo(double weight, boolean isAlive) { this.weight = weight; this.isAlive = isAlive; } // To determine whether the dillo is < n lbs public boolean isLighter(double n) { return this.weight < n; } // To feed the dillo 2 lbs of food if it's alive public Dillo feed() { if (this.isAlive) return new Dillo(this.weight + 2, true); else return this; } } class Posn { double x; double y; Posn(double x, double 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; } // To determine whether the ant is < n lbs public boolean isLighter(double n) { return this.weight < n; } public Ant feed() { return new Ant(this.weight + 0.001, this.location); } } interface IGrade { } class NoGrade implements IGrade { NoGrade() { } } class NumGrade implements IGrade { int n; NumGrade(int n) { this.n = n; } } // A list-of-num is either // - empty-list-of-num // - cons-list-of-num interface IIntList { int length(); } // A empty-list-of-num is // - (make-empty-list-of-num) class EmptyIntList implements IIntList { EmptyIntList() { } public int length() { return 0; } } // A cons-list-of-num is // - (make-cons-list-of-num num list-of-num) class ConsIntList implements IIntList { int first; IIntList rest; ConsIntList(int first, IIntList rest) { this.first = first; this.rest = rest; } public int length() { return 1 + this.rest.length(); } } class Examples{ Snake slinky = new Snake("Slinky", 12, "rats"); Dillo dillo1 = new Dillo(7, true); Dillo dillo2 = new Dillo(2, false); Ant ant1 = new Ant(0.001, new Posn(5, 2)); Posn home = new Posn(0, 0); Ant ant2 = new Ant(0.0005, home); IAnimal suzy = new Snake("Suzy", 8, "bananas"); IGrade notTaken = new NoGrade(); IGrade barelyPassed = new NumGrade(75); void tests(Tester t) { t.checkExpect(slinky.isLighter(10), false); t.checkExpect(new Snake("Slimey", 5, "apples").isLighter(10), true); t.checkExpect(dillo1.isLighter(10), true); t.checkExpect(dillo1.isLighter(5), false); t.checkExpect(ant1.isLighter(0.002), true); t.checkExpect(ant2.isLighter(0.0002), false); t.checkExpect(suzy.isLighter(10), true); t.checkExpect(slinky.feed(), new Snake("Slinky", 17, "rats")); t.checkExpect(dillo1.feed(), new Dillo(9, true)); t.checkExpect(ant1.feed(), new Ant(0.002, new Posn(5, 2))); } }