**Computer Science 15-110 (Lecture 4), Spring 2010Homework 3
Due: Thu 4-Feb-2009 at 10pm (email copy to koz)
(no late submissions accepted).
Hw3 Submission Coordinator: koz**

**Follow the same instructions as hw1, only (of
course) replace "hw1" with "hw3" throughout.**

**1-on-1 Tutoring****Writing Static Methods (with doubles)**-
**Writing Graphics Helper Methods (with Polygons)** **Bonus/Optional: quadrantOfIntersection****Bonus/Optional: Alice**

**1-on-1 Tutoring**

This week, we will experiment with*optional*1-on-1 private tutoring with a CA or the instructor. This is*not required*, but we strongly suggest the following:

quiz1 score optional tutoring time 80+ (B or A) 0 (none) 70-79 (C) 30 minutes 60-69 (D) 60 minutes 0-59 (R) 90 minutes For those in the 80+ range, we still welcome your tutoring requests, of course, but your performance on quiz1 suggests that you do not require tutoring at this time.

For those in the 0-79 range, please arrange your tutoring sessions either directly with a course staff member or via email to the course help mailing list.

While this is optional, it is our*hope*that we will provide upwards of 25 hours of private 1-on-1 tutoring this week. We'll see...

**Writing Static Methods (with doubles)**For each of these methods, and any additional helper methods you write, you should also write both a robotic test method and a manual test method. As usual, you will be graded on the quality of your robotic test method. It should be carefully considered so that it contains the right combination of test cases so that it is likely to catch most common errors in the actual method being tested. Your manual test method should also have a well-designed user interface, with clear prompts for input and clear, understandable output. Your main method in each program should first call your robotic test methods and then call your manual test methods.

**Hint:**Don't forget to use almostEqual (which you wrote in recitation) and do not use == when testing doubles for equality!

**isRightTriangle**In a program named Hw3IsRightTriangle.java, write a static method named isRightTriangle. This method takes three doubles and returns true if these can form the sides of a right triangle (that is, if they are all positive and they abide by the Pythagorean Theorem) and false otherwise. Note: the sides may be in any order, so you may not assume that the third parameter is necessarily the hypotenuse!

**nthFibonacci (with round)**In a program named Hw3NthFibonacci.java, write a static method named nthFibonacci. This method takes a non-negative int n and returns the nth Fibonacci number. The Fibonacci numbers are 1, 1, 2, 3, 5, 8, 13, 21, 34, .... As you can see, each number is the sum of the previous two. From mathworld's Fibonacci page, we see a simple formula for computing the nth Fibonacci number is:

Aside: the astute observer may notice the*golden ratio*lurking in this formula. Fascinating! In any case, note that the brackets around this expression indicate that you must*round*the result to the nearest integer. For this, you must use a helper method named**round**that takes a (possibly-negative) double d and returns an int (not a double!), which is the nearest integer value to d. Note that this helper method must work over negative values even though it will not be used that way in this problem. This is an example of robust programming -- anticipating a method may be useful in other contexts, we write it to work as generally as possible (within reason). Be sure to write robotic and manual test methods for your helper method, too!

**threeLinesArea (with lineIntersection, distance, and triangleArea)**

Write a program, Hw3ThreeLinesArea.java, that (along with suitable robotic and manual test methods) includes a method named threeLinesArea that takes 6 doubles -- m1, b1, m2, b2, m3, b3 -- describing 3 lines, and returns a double -- the area of the triangle described by the intersections of these three lines. You are guaranteed no two lines are parallel, so each line intersects each of the other two lines (and so a triangle is always formed). To solve this problem, you must write these three helper methods (be sure to write robotic and manual test methods for each of these, too!):**lineIntersection**This method takes four doubles -- m1, b1, m2, and b2 -- describing two lines, and returns a double -- the x value of the point of intersection of the two lines. It is guaranteed that the two lines are not parallel or identical (and so they definitely intersect in a single point).

**distance**This method takes four doubles -- x1, y1, x2, and y2 -- and returns a double -- the distance from (x1,y1) to (x2,y2).

**triangleArea**This method takes three doubles -- s1, s2, and s3 -- describing the lengths of three sides of a triangle, and returns a double -- the area of that triangle. It is guaranteed that the three lengths do in fact form a triangle (that is, they satisfy the triangle inequality). Hint: you may wish to use Heron's Formula here.

**Writing Graphics Helper Methods (with Polygons)**You do not need to write robotic test methods for your graphics methods, and your graphics programs basically are themselves manual test methods, so you do not have write those, either.

Note:

**Note:**To complete this assignment, you will have to use Polygons and the fillPolygon method. The following short example shows how these are used:

public void paint(Graphics page) {

page.setColor(Color.gray);

page.fillRect(100, 50, 200, 150);

Polygon p = new Polygon();

p.addPoint(100, 50); // left-top of rectangle

p.addPoint(200, 125); // center of rectangle

p.addPoint(300, 50); // right-top of rectangle

page.setColor(Color.black);

page.fillPolygon(p);

}

This code first fills a gray rectangle, then it creates a new Polygon object and adds 3 points to that object at the left-top, center, and right-top of the rectangle. It then fills that Polygon in black, producing this result:

Study the preceding example code carefully, change it as necessary (for example, changing the points or adding more or fewer points) to understand how Polygons work.**Note:**You should continue to replace any stars in flags with ovals (of the same size, location, and color).You must deal with the "thin white stripe" that occurs between stripes when the screen size is not an even multiple of the stripe size.

Note:

**paintFlagOfDjibouti**In the file Hw3FlagOfDjibouti.java, write the graphics helper method paintFlagOfDjibouti. This method takes a page and a left, top, width, and height (all integers), and paints the flag of Djibouti so that it fills the given bounding box. Do not assume there is a white background (so you must also paint the white triangular area in the flag). Test this method by having your program paint the window entirely black and then paint four flags of Djibouti, one in each quadrant, with a 5-pixel margin around each flag (and so there should be a 10-pixel margin between the interiors of the flags).

**paintFlagOfTheGrenadines (with fillDiamond)**In the file.Hw3FlagOfTheGrenadines.java, repeat the previous problem except this time for the flag of the Grenadines (technically "Saint Vincent and the Grenadines"). In this case, you must first write another graphics helper, fillDiamond (using well-chosen parameters). Your paintFlagOfTheGrenadines method must call the fillDiamond method three times (once for each diamond in the flag). Your program should display 4 flags, one in each quadrant, again with a 5-pixel margin.

**Bonus/Optional: quadrantOfIntersection (up to 2.5 pts)**In the file Hw3Bonus.java, write the following method:

public static int quadrantOfIntersection(double a1, double b1, double c1,

double a2, double b2, double c2)

This method takes six doubles representing the two parabolas y=a_{1}x^{2}+b_{1}x+c_{1}and y=a_{2}x^{2}+b_{2}x+c_{2}and returns an int value representing the quadrant where these two parabolas intersect (if they intersect in two points, return the quadrant of the leftmost point of intersection (the smaller x value)). You are guaranteed they do intersect in at least one point and at most two points. Specifically, your method should return a 1 if the parabolas intersect in the top-right quadrant, a 2 for the top-left, a 3 for the bottom-left, and a 4 for the bottom-right. If the two parabolas intersect at (or "very nearly" at) the origin, you should return a 0. Also, you should write your own test method for this problem:

public static void testQuadrantOfIntersection()

Be thoughtful about your test cases, trying to test all the different conditions that might arise.

**Hint:**You may wish to use trigonometry (Math.sin, Math.cos, Math.tan) to find the angle to the point of intersection, and then you can divide this appropriately to convert from an angle between 0 and 2pi (in radians) and the quadrant number.*Alternatively:*on only this problem, you may (for a small deduction) use an "if" statement (which of course is otherwise not yet allowed this week).

**Note:**You may make any reasonable assumption as to how to handle intersections lying precisely on the x or y axes.

**Bonus/Optional: Alice (up to 5 pts)**Like Scratch (http://scratch.mit.edu/), that some of you investigated last week, Alice (http://www.alice.org/) is also an interesting and increasingly popular programming language/environment designed mainly for pre-collegiate (perhaps even pre-high-school) programmers. Of course, Alice also has CMU roots! Go to the Alice page, download it, try it out. Write a few programs in a subdirectory hw3-alice (in your hw3 directory). Include some programs you wrote along with a brief but well-considered write-up comparing the programming experience in Alice versus that in Java (again, such as you know Java to this point) and, if you studied it, Scratch.

**Carpe diem! **