Computer Science 15-110 (Lecture 4), Spring 2010
Homework 2b
Due: Thu 28-Jan-2009 at 10pm (email copy to koz)
(no late submissions accepted).
Hw2 Submission Coordinator: koz
Follow the same instructions as hw1, only (of
course) replace "hw1" with "hw2" throughout.
- Writing Static Methods
- nearestBusStop
- lineIntersection
- Writing Graphics Helper
Methods
- Central African Republic
- Netherlands Antilles
-
Bonus/Optional: Modern
Marvels
-
Bonus/Optional: Scratch
- Writing Static Methods
Note: In this section, you
will adapt your solutions to some hw1 problems so that they now use
appropriate helper methods and test methods.
- nearestBusStop
In a program named Hw2NearestBusStop.java, write a static method
named nearestBusStop that takes one parameter, an int value representing
a street number (which you may assume to be positive), and returns the
street number of the nearest bus stop, where buses stop on streets that
are multiples of 8 (8th, 16th, 24th, etc), and where ties are broken in
favor of the lower bus stop. So we see:
* nearestBusStop(11) returns 8
* nearestBusStop(12) returns 8
* nearestBusStop(13) returns 16
You can assume there is a bus stop on
0th street.
Then, write a static test method, testNearestBusStop, that follows the
pattern in the class notes for test methods, and which includes a
reasonable suite of test cases to be sure that your nearestBusStop
method works properly.
That test method is robotic. What if we want to test the program
manually, where we run the program like last week and enter different
values each time? For that, you will write yet another static
method named manuallyTestNearestBusStop. That method will take no
parameters and return nothing (that is, it is void). It will
operate exactly like the interactive program from hw1. That is, it
will be interactive, and it must match the following output exactly
(where the user input is underlined, and therefore can change each time
you run the program):
Enter the
street number: 12
------------------------
The nearest bus
stop to street 12 is at street 8.
Finally, your main method should first call the robotic test
method and then the manual test method, like so:
public static void
main(String[] args) {
testNearestBusStop();
manuallyTestNearestBusStop();
}
- lineIntersection
Using the
same techniques as the previous problem, convert the line intersection
problem into methods. Now, the lineIntersection clearly takes 4
parameters (m1, b1, m2, and b2, all int values), but what does it
return? A method can only return one value, and yet it
seems this has to return two values -- the x and y values of the
intersection. Later on, we'll learn how to combine multiple values
into a single object, and then return that object. We don't know
how to do that yet. So what to do? Answer: just
return the x value of the point of intersection. After all,
once we have that, computing the y value is really easy (right?).
So...
In a program named Hw2LineIntersection.java, write the methods
lineIntersection that takes 4 int values -- m1, b1, m2, and b2 -- and
returns an int value, the x value of the point of intersection of the
lines y=m1x+b1 and y=m2x+b2.
Also, write the methods testLineIntersection and
manuallyTestLineIntersection, and have your main method call each of
these (the robotic method first). Your manual method must match
the output as specified in last week's assignment..
Note: as with the previous
problem (and all future problems!), 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.
- Writing Graphics Helper
Methods
Note: As we are still limited to just rectangles and ovals this week, you
should replace any stars in flags with ovals (of the same size, location,
and color).
Note: 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.
a.
Central African Republic
(file:
Hw2FlagOfTheCAR1.java and
Hw2FlagOfTheCAR2.java)
(larger
image with details)
Write the following graphics helper method:
public
void paintFlagOfTheCAR(Graphics page, int left, int top, int width, int
height)
This method takes a page and a bounding box (a rectangular area
describing an outer boundary) and it paints the flag of the Central African
Republic so that it completely fills the given bounding box.
In the file Hw2FlagOfTheCAR1.java, include a paint method that uses this
graphics helper method to fill the entire window with the flag of the CAR.
Your paint method should look like this:
public
void paint(Graphics page) {
paintFlagOfTheCAR(page, 0, 0, getWidth(), getHeight());
}
Great! But it gets better! In another file named
Hw2FlagOfTheCAR2.java, include a paint method that calls that graphics
helper method 4 separate times -- once each to fill each quadrant of the
window with its own copy of the flag of the CAR, with a 3-pixel margin
around all sides of each flag. Note: you will have to include
a copy of your paintFlagOfTheCAR graphics helper method in each file that
uses it!
b.
Netherlands Antilles
(file:
Hw2FlagOfTheNetherlandsAntilles.java and Hw2FunkyFlag.java)
(larger
image with details)
In the file Hw2FlagOfTheNetherlandsAntilles.java, paint the flag of the
Netherlands Antilles again, only this time you must use a well-chosen helper
method to paint each star. The method should be called paintStar, and
it should take the page, the center x and y positions, and the radius of the
star. Of course, for now it will only paint a circle in place of the
star.
And now we're going all out: in the file Hw2FunkyFlag.java, paint a
flag of the Netherlands Antilles, only replace all the stars with same-sized
little flags of the CAR! To make this work, replace the body of your
paintStar method so that instead of painting a circle it makes a suitable
call to your paintFlagOfTheCAR helper method. You may need to make the
window rather large for this to look right (ok, it will look strange even
when it is "right"!). Remember: you will have to
include a copy of your paintFlagOfTheCAR graphics helper method in each file
that uses it!
-
Bonus/Optional:
Modern Marvels (up to 2.5 pts)
Modern Marvels: Great Inventions -- Computers
- Watch this 4-minute video:
http://www.hulu.com/watch/58830/modern-marvels-great-inventions---computers
- Write a Java program, Hw2BonusModernMarvels.java, that prints a
timeline of all the events listed in the video. For example,
System.out.println("1645: Blaise Pascal invents 50 computing
machines....");
Include every event listed in the show, in order, with enough
detail to make it obvious that you paid attention!
-
Bonus/Optional:
Scratch (up to 5 pts)
Scratch (http://scratch.mit.edu/) is
an interesting and increasingly popular drag-and-drop programming language
designed mainly for pre-collegiate (perhaps even pre-high-school)
programmers. Go to the Scratch page, download it, try it out.
Write a few programs in a subdirectory hw2-scratch (in your hw2 directory).
Include some programs you wrote along with a brief but well-considered
write-up comparing the programming experience in Scratch versus that in Java
(well, such as you know Java to this point).
Carpe diem!