CS100, Spring 2000, Project 4: Better Late Than Never Due at the START of lecture, THURSDAY, March 9. Early submissions (any day before the due date) MUST be turned in to a consultant in Carpenter -- make sure they log your submission! If you submit Project 4 on or before Tuesday and it earns 4/4 or better, we will give you 2 bonus points. =============================================================================== Part 1: Let the Good Times Roll! =============================================================================== Simulate rolling a pair of 6-sided dice 1000 times and at the end of the simulation print the number of times the pair of dice adds up to 7. Notes: + Do NOT use objects. + Use a named constant for the number of sides of a die. + Lewis&Loftus pages 203-205 might be helpful -- it has an OOP Die example, but remember, do NOT use objects. + Use $Math.random()$ -- see Lewis&Loftus pages 81-82. + "Simulate" does NOT mean drawing/printing the die rolls. + Print only the final count -- do NOT print the die rolls. =============================================================================== Part 2: 3 /W/i/s/e Snow Men =============================================================================== Use ideas from Lewis&Loftus pages 99-100 to complete method $paint$ in the code below so that it draws 3 snowmen in a row. Your code replaces the comment "(*) your code goes here". import java.awt.*; class Drawing extends Frame { public void paint(Graphics g) { // drop below window title bar g.translate(getInsets().left, getInsets().top); // (*) your code goes here } } public 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 the Drawing window (it starts off hidden) } } Notes: + Do not draw anything but the snowmen. + Don't draw hats and faces. + You may choose arbitrary sizes and positions as long as snowmen are recognizably snowmen and have clearly visible features -- See Lewis&Loftus. + Each snowman should have a head, an upper torso, a lower torso, and two arms connected to the upper torso. Represent one arm with one line. See Lewis&Loftus. + The leftmost, middle, and rightmost snowmen should have their arms raised, to the side, and downwards, respectively. They should look roughly like this: . . . \o/ -o- /o\ O O O + Make the leftmost, middle, and rightmost snowmen entirely red, green, and blue, respectively. BONUS: Reduce redundancy in your code. =============================================================================== Part 3: It's All in the Family =============================================================================== The alien race Parthon reproduces by parthogenesis, i.e. one adult can reproduce without the assistance of another Parthon. A family tree of a Parthon family might look like this: Ani age 60 | Ani is Naj's parent. | Naj age 40 / | \ Naj is Sil's, Ur's, and Wil's parent. / | \ Sil, Ur, and Wil are siblings. Sil Ur Wil age 20 age 18 age 17 These kinds of family trees can be represented by objects instantiated from a class $Parthon$ with the instance variables and methods shown below. Instance Variables for Class Parthon + $name$ : name of the Parthon. + $age$ : age of the Parthon. + $parent$ : parent, if any, of the Parthon + $olderSibling$ : older sibling, if any, closest in age to the Parthon + $youngestChild$ : youngest child, if any, of the Parthon Instance Methods for Class Parthon + $describe()$ : print the Parthon's name and age and the names of its following relatives: parent, grandparent, youngest child, oldest child. + $grandparent()$ : return (a reference to) the Parthon's grandparent + $oldestChild()$ : return (a reference to) the Parthon's oldest child How to Create a Family Tree A family tree will be created in some main class. That main class will instantiate Parthons and establish all the relationships by somehow "setting" all the instance variables appropriately. "Setting" can be done in a number of different ways. Ideally, the main class would invoke appropriately defined constructors and methods of the Parthon class, but you may instead use assignment statements inside the main class. =================================================================== BONUS: Use constructors or additional methods to reduce redundancy. =================================================================== Notes: + At no point do you actually draw/print the picture of a family tree! + The ONLY classes involved in this part are $Parthon$, $Project4_3a$, and $Project4_3b$. + Except for $name$ and $age$, each instance variable is a reference to a Parthon. + The special value $null$ is used to indicate that a relative does not exist. For example, Ani's parent would be set to $null$, and when there is no grandparent or oldest child, $null$ is returned by the corresponding methods. + $describe$ should call $grandparent$ and $oldestChild$. + $describe$ should print appropriate messages for missing relatives, e.g. "no grandparent". See Part 3a. + An only child is both youngest and oldest! + Use this basic algorithm in $oldestChild()$ to find the oldest child: $oldest$ <-- youngest child; if youngest child exists while $oldest$ has an older sibling $oldest$ <-- its older sibling For example, tracing this code to find the oldest child of Naj would successively assign Wil, Ur, and then Sil to $oldest$. ------------------------------------------------------------------------------- Part 3a: The Ani-Naj-Wil-Ur-Sil Family Tree ------------------------------------------------------------------------------- Use a main class of $Project4_3a$ to + create the family tree example shown at the start, and + $describe$ each Parthon. The output for this tree should look exactly like this, where we use the labels "P" for parent, "GP" for grandparent, "OS" for older sibling, "YC" for youngest child, and "OC" for oldest child: 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 REMEMBER: Do NOT edit your output! ------------------------------------------------------------------------------- Part 3b: The Other Family Tree ------------------------------------------------------------------------------- Use a main class of $Project4_3b$ to create the family tree shown below and $describe$ each Parthon. 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 =============================================================================== What to hand in =============================================================================== Follow the submission instructions and other procedures from the Projects page on the CS100 Course Website. Include all programs and unedited output: Part 1: + Listing of all the code. + The output. Part 2: + A listing of all the code. + A screen snapshot of the three snowmen -- you do NOT have to print in color. Part 3: + A listing of all the code. There should be only one copy of class $Parthon$, identical for parts (a) and (b). + Output from parts (a) and (b). Note: As with earlier projects, you may turn in a unified project that contains all the code and has a "master" main class that invokes each main class from parts 1, 2, 3a, and 3b.