// 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.*; /* Output: 4.1 -- saw 162 sevens 4.3a Ani, 60, no P, no GP, no OS, YC Naj, OC Naj Naj, 40, P Ani, no GP, no OS, YC Wil, OC Sil Wil, 17, P Naj, GP Ani, OS Ur, no YC, no OC Ur, 18, P Naj, GP Ani, OS Sil, no YC, no OC Sil, 20, P Naj, GP Ani, no OS, no YC, no OC 4.3b Tori, 117, no P, no GP, no OS, YC Dana, OC Dana Dana, 95, P Tori, no GP, no OS, YC Lee, OC Jack Lee, 60, P Dana, GP Tori, OS Jack, YC Shannon, OC Robin Shannon, 28, P Lee, GP Dana, OS Robin, YC Kim, OC Kim Kim, 5, P Shannon, GP Lee, no OS, no YC, no OC Robin, 30, P Lee, GP Dana, no OS, no YC, no OC Jack, 74, P Dana, GP Tori, no OS, YC Jamie, OC Jamie Jamie, 37, P Jack, GP Dana, no OS, YC Pat, OC Leslie Pat, 3, P Jamie, GP Jack, OS Chris, no YC, no OC Chris, 7, P Jamie, GP Jack, OS Leslie, no YC, no OC Leslie, 11, P Jamie, GP Jack, no OS, no YC, no OC */ // run Parts 1, 2, 3a, 3b -- core version, with no bonuses public class Project4core { public static void main(String[] args) { Project4_1.main(args); Project4_2.main(args); Project4_3a.main(args); Project4_3b.main(args); } } // simulate 1000 rolls of a pair of dice; print number of sevens class Project4_1 { static void main(String[] args) { int count7 = 0; // # of 7s seen so far int ROLLS = 1000; // # of times to roll the group of dice int GROUPSIZE = 2; // # dice in group for (int i = 0; i<ROLLS; i++) { int sum = 0; // sum of GROUPSIZE dice for (int j = 0; j < GROUPSIZE; j++) sum += 1 + (int)(Math.random()*6); if (sum == 7) count7++; } System.out.println("4.1 -- saw " + count7 + " sevens"); } } // Drawing window: draw 3 snowmen, as specified in Part 2 -- roughly, // . . . // \o/ -o- /o\ // O O O // red green blue class Drawing extends Frame { public void paint(Graphics g) { // drop below window title bar g.translate(getInsets().left, getInsets().top); // draw leftmost snowman g.setColor(Color.red); g.fillOval(40-10, 10, 20, 20); g.fillOval(40-20, 30, 40, 40); g.fillOval(40-30, 70, 60, 60); g.drawLine(20, 50, 20-20, 50-20); g.drawLine(60, 50, 60+20, 50-20); // draw middle snowman g.setColor(Color.green); g.fillOval(80+40-10, 10, 20, 20); g.fillOval(80+40-20, 30, 40, 40); g.fillOval(80+40-30, 70, 60, 60); g.drawLine(80+20, 50, 80+20-20, 50+0); g.drawLine(80+60, 50, 80+60+20, 50+0); // draw rightmost snowman g.setColor(Color.blue); g.fillOval(2*80+40-10, 10, 20, 20); g.fillOval(2*80+40-20, 30, 40, 40); g.fillOval(2*80+40-30, 70, 60, 60); g.drawLine(2*80+20, 50, 2*80+20-20, 50+20); g.drawLine(2*80+60, 50, 2*80+60+20, 50+20); } } // 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 // 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.print(name + ", " + age); if (parent == null) System.out.print(", no P"); else System.out.print(", P " + parent.name); if (grandparent() == null) System.out.print(", no GP"); else System.out.print(", GP " + grandparent().name); if (olderSibling == null) System.out.print(", no OS"); else System.out.print(", OS " + olderSibling.name); if ( youngestChild== null) System.out.print(", no YC"); else System.out.print(", YC " + youngestChild.name); if (oldestChild() == null) System.out.print(", no OC"); else System.out.print(", OC " + oldestChild().name); System.out.println(); } // return grandparent, if any Parthon grandparent() { if (parent == null) return null; return parent.parent; } // return oldest child, if any Parthon oldestChild() { Parthon oldest = youngestChild; if (oldest != null) while (oldest.olderSibling != null) oldest = oldest.olderSibling; return oldest; } } // 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) { // create family tree Parthon ani = new Parthon(); ani.name = "Ani"; ani.age = 60; Parthon naj = new Parthon(); naj.name = "Naj"; naj.age = 40; Parthon sil = new Parthon(); sil.name = "Sil"; sil.age = 20; Parthon ur = new Parthon(); ur.name = "Ur"; ur.age = 18; Parthon wil = new Parthon(); wil.name = "Wil"; wil.age = 17; ani.youngestChild = naj; naj.parent = ani; sil.parent = naj; ur.olderSibling = sil; ur.parent = naj; wil.olderSibling = ur; wil.parent = naj; naj.youngestChild = wil; // describe family tree System.out.println("\n4.3a\n"); ani.describe(); naj.describe(); wil.describe(); ur.describe(); sil.describe(); } } // 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) { // create family tree Parthon tori= new Parthon(); tori.name="Tori"; tori.age=117; Parthon dana=new Parthon(); dana.name="Dana"; dana.age=95; dana.parent=tori; Parthon jack=new Parthon(); jack.name="Jack"; jack.age=74; jack.parent=dana; Parthon jamie=new Parthon(); jamie.name="Jamie"; jamie.age=37; jamie.parent=jack; Parthon leslie=new Parthon(); leslie.name="Leslie"; leslie.age=11; leslie.parent=jamie; Parthon chris=new Parthon(); chris.name="Chris"; chris.age=7; chris.parent=jamie; Parthon pat=new Parthon(); pat.name="Pat"; pat.age=3; pat.parent=jamie; Parthon lee=new Parthon(); lee.name="Lee"; lee.age=60; lee.parent=dana; Parthon robin=new Parthon(); robin.name="Robin"; robin.age=30; robin.parent=lee; Parthon shannon=new Parthon(); shannon.name="Shannon"; shannon.age=28; shannon.parent=lee; Parthon kim=new Parthon(); kim.name="Kim"; kim.age=5; kim.parent=shannon; tori.youngestChild = dana; lee.olderSibling = jack; dana.youngestChild = lee; jack.youngestChild = jamie; chris.olderSibling = leslie; pat.olderSibling = chris; jamie.youngestChild = pat; shannon.olderSibling = robin; lee.youngestChild = shannon; shannon.youngestChild = kim; // describe family tree System.out.println("\n4.3b\n"); tori.describe(); dana.describe(); lee.describe(); shannon.describe(); kim.describe(); robin.describe(); jack.describe(); jamie.describe(); pat.describe(); chris.describe(); leslie.describe(); } }