// Name: Anita Blake Partner: Ted Forrester // ID: 014916 ID: 642708 // Sec: Yan T 2:30 Sec: Artemov R 1:25 // Date: March 9, 2000 // Project 4: Better Late Than Never (dice, snowmen, Parthons) import java.awt.*; // run Parts 2, 3a, 3b -- bonus version (Part 1 omitted -- it has no bonuses) public class Project4bonus { public static void main(String[] args) { Project4_2.main(args); Project4_3a.main(args); Project4_3b.main(args); } } // Drawing window: draw 3 snowmen, as specified in Part 2 -- roughly, // . . . // \o/ -o- /o\ // O O O // red green blue class Drawing extends Frame { // draw a snowman with the requested color and armPosition // translated horizontally by the specified offset void snowman(Graphics g, Color c, int armPosition, int offset) { g.setColor(c); g.fillOval(offset+40-10, 10, 20, 20); g.fillOval(offset+40-20, 30, 40, 40); g.fillOval(offset+40-30, 70, 60, 60); g.drawLine(offset+20, 50, offset+20-20, 50+armPosition); g.drawLine(offset+60, 50, offset+60+20, 50+armPosition); } // draw 3 snowmen as specified in Part 2 public void paint(Graphics g) { // drop below window title bar g.translate(getInsets().left, getInsets().top); snowman(g, Color.red, -20, 0); snowman(g, Color.green, 0, 80); snowman(g, Color.blue, +20, 2*80); } } // create the Drawing window for Part 2 class Project4_2 { static void main(String[] args) { Drawing d = new Drawing(); d.setTitle("Drawing Window"); // set title of Drawing window d.setSize(400,200); // set size of Drawing window d.show(); // display/show window (starts off hidden) } } // the parthogenic alien race Parthon class Parthon { String name; // name int age; // age Parthon parent; // parent, if any Parthon olderSibling; // closest older sibling, if any Parthon youngestChild; // youngest child, if any // create a new youngest child of rent with name n, age a, no children // (rent may be null) Parthon(String n, int a, Parthon rent) { name = n; age = a; parent = rent; if (rent == null) olderSibling = null; else { olderSibling = rent.youngestChild; rent.youngestChild = this; } youngestChild = null; } // return label'd relative or "no $label$" (if relative does not exist) private String helpdescribe(String label, Parthon relative) { return relative == null ? "no " + label : label + " " + relative.name; } // print a description of the Parthon: name, age, names of // parent, grandparent, older sibling, youngest child, oldest child. // use labels P, GP, OS, YC, OC, respectively. // if a relative "R" does not exist, indicate by printing "no R". void describe() { System.out.println(name + ", " + age + ", " + helpdescribe("P", parent) + ", " + helpdescribe("GP", grandparent()) + ", " + helpdescribe("OS", olderSibling) + ", " + helpdescribe("YC", youngestChild) + ", " + helpdescribe("OC", oldestChild())); } // return grandparent, if any Parthon grandparent() { return parent == null ? null : parent.parent; } /* the methods describeAll and oldestSibling call themselves -- yes, if done properly, this works! this powerful technique of *recursion* is usually a cs211/cs212/cs312 topic. */ // describe entire family tree below and including self void describeAll() { describe(); if (youngestChild != null) youngestChild.describeAll(); if (olderSibling != null) olderSibling.describeAll(); } // return oldest sibling or self (if have no oldest sibling) Parthon oldestSibling() { return olderSibling == null ? this : olderSibling.oldestSibling(); } // return oldest child, if any Parthon oldestChild() { return youngestChild == null ? null : youngestChild.oldestSibling(); } } // create and describe the family tree for Part 3a: // // Ani // age 60 // | // | // Naj // age 40 // / | \ // / | \ // Sil Ur Wil // age 20 age 18 age 17 // class Project4_3a { static void main(String[] args) { Parthon ani = new Parthon("Ani", 60, null); Parthon naj = new Parthon("Naj", 40, ani); Parthon sil = new Parthon("Sil", 20, naj); Parthon ur = new Parthon("Ur", 18, naj); Parthon wil = new Parthon("Wil", 17, naj); System.out.println("\n4.3a\n"); ani.describeAll(); } } // create and describe the family tree for Part 3b: // // Tori // age 117 // | // | // | // Dana // age 95 // / \ // / \ // / \ // Jack Lee // age 74 age 60 // / / \ // / / \ // / / \ // Jamie Robin Shannon // age 37 age 30 age 28 // / | \ | // / | \ | // / | \ | // Leslie Chris Pat Kim // age 11 age 7 age 3 age 5 // class Project4_3b { static void main(String[] args) { Parthon tori = new Parthon("Tori", 117, null); Parthon dana = new Parthon("Dana", 95, tori); Parthon jack = new Parthon("Jack", 74, dana); Parthon jamie = new Parthon("Jamie", 37, jack); Parthon leslie = new Parthon("Leslie", 11, jamie); Parthon chris = new Parthon("Chris", 7, jamie); Parthon pat = new Parthon("Pat", 3, jamie); Parthon lee = new Parthon("Lee", 60, dana); Parthon robin = new Parthon("Robin", 30, lee); Parthon shannon = new Parthon("Shannon", 28, lee); Parthon kim = new Parthon("Kim", 5, shannon); System.out.println("\n4.3b\n"); tori.describeAll(); } }