/* AUTHOR: Sandy Bartlett COURSE: SI 543 PROJECT: OO programming assignment DUE DATE: Feb. 11, 2002 SUBMISSION DATE: Feb. 11, 2002 SUMMARY A class that represents a tree. INPUT none OUTPUT The draw method draws a representation of the tree on a Graphics object. CLASS HIERARCHY Object - Plant - Tree ASSUMPTIONS The specified land area will be greater than or equal to the minimum room size; sizes will be in square meters; huts are square. Room sizes will not be negative. */ import java.awt.*; class Tree extends Plant { public static final String HEIGHT_UNITS = "meters"; private static final Color BROWN = new Color(139, 69, 19); private static final Color DARK_GREEN = new Color(0, 100, 0); private boolean deciduous; private double height; public Tree() { super("Tree"); } // default constructor // tree seed public Tree(String name, boolean deciduous) { super(name); this.deciduous = deciduous; } // constructor - tree seed // baby tree public Tree(String name, boolean deciduous, double height) { super(name); this.deciduous = deciduous; if (height >= 0) this.height = height; else this.height = .2; } // constructor - baby tree public Tree(String name, int age, double height, boolean deciduous) { this(name, age, height, deciduous, false, false); } // constructor - name age height deciduous // everything constructor public Tree(String n, int a, double h, boolean d, boolean p, boolean e) { super(n, a, p, e); deciduous = d; if (h >= 0) height = h; else height = .2 * getAge(); } // constructor - everything public void draw(Graphics g) { Color oldColor = g.getColor(); g.setColor(BROWN); if (height == 0) // just a seed g.fillOval(20, 300, 10, 5); else { int trunkHeight = (int)(height * 2); trunkHeight = trunkHeight > 150 ? 150 : trunkHeight < 10 ? 10 : trunkHeight; int trunkWidth = getAge() > 30 ? 30 : getAge() < 2 ? 2 : getAge(); g.fillRect(100, 300 - trunkHeight, trunkWidth, trunkHeight); g.setColor(DARK_GREEN); g.fillOval(100 - trunkHeight / 2 - trunkWidth / 2, 300 - trunkHeight * 2, trunkHeight * 2, trunkHeight); } // else g.setColor(oldColor); } // draw public double getHeight() { return height; } // getHeight public void setHeight(double newHeight) { height = newHeight; // may get shorter if electric company tops it } // setHeight public boolean isDeciduous() { return deciduous; } // isDeciduous } // Tree