CS1004 Homework #6
Due by Wednesday, May 5, at
5:00pm
There are two parts to this homework assignment: a theory
portion, worth 10 points, and a programming portion,
worth 15 points. Please be sure to review the submission instructions in
advance, and make sure to include a README.
This version is revised as of 5/2. Minor correction on
circle bounding-box calculations.
Written questions
- (2 points) Explain, in a few sentences and in the context of
the operating system's memory manager and the memory hierarchy
in a computer, why having low amounts of RAM in your computer
leads to a degradation in performance.
- (6 points) The "distance" between an Internet client and
server is often a important metric in determining how responsive
a particular host will be. Often, that distance is measured in
"hops" -- and the traceroute program (which you can run
from the Windows NT/2000/XP command line as tracert, or
/usr/sbin/traceroute on CUNIX) helps you to measure
this. traceroute takes one important command-line
parameter: the name (or IP) of the remote host to trace a route
to. (You can also type in man traceroute on CUNIX
for a detailed description of the tool.)
- (2 points) Try to find a computer "as near as possible"
to yours without actually being your computer. On CUNIX,
there are a number of machines that are exactly 1 hop away.
Show, using your traceroute results, that it is as
near as possible, and suggest why that machine is "so near".
- (2 points) Conversely, try to find a machine "as far
away as possible" to yours, and include your
traceroute results to demonstrate this. Note that,
at times, firewalls will make it difficult to obtain a
complete path: if the nth hop only shows stars
without actually finishing the traceroute, you'll have to
try another host that's not firewalled. Can you find
something that is at least 15 hops away? What "part" of the
Internet, topologically, might be "far away" from an
institution like Columbia?
- (2 points) Based on the characterizations of the two
subproblems above, what can you conclude about Internet
topology?
- (2 points) Brookshear Chapter Review Problem 10.16 (page
448).
Programming assignment: graphical shapes
In this assignment, we're going to take the Shapes program as
written in homework #5 and add primitives to it so that the
Shapes can actually be drawn on the screen. The key is to add
enough primitives to our objects to have useful a) coordinate
(i.e., x and y ints; we treat this as the upper-left of the
shape); and b) color information so that we can draw them on the
screen in a reasonable fashion. To keep the assignment
manageable, we will keep the text UI, and add a few items to it
(in particular, an option to "show" the GUI).
If you're not happy with your HW#5 submission, you will be able
to download the solutions to HW#5 on Thursday afternoon.
-
(2 points) Write a base class called BasicShape that
contains three data values: an x coordinate and a y coordinate
(both integers), and a color (of type Color from the java.awt
package). Next, modify the remaining shape classes so they
all extend BasicShape, thereby inhering those three
fields. (Make sure to import java.awt in all of these classes
so they know what that Color class is in the first place!)
-
(3 points) Modify the constructors for each class so they
take three additional parameters: the two int coordinates, and
the color. Have the constructor store them in the variables we
just created. For Point, this will now necessitate
the need for a constructor, so if you didn't have one before,
create one now.
- (4 points) Build a method called public void
paint(Graphics g) for each shape class. This will
contain the drawing primitives to draw that particular shape.
You'll want to first set the color by using the
setColor method in the Graphics class, and then
you'll want to draw the appropriate shape.
- For Point, draw a very small circle (i.e.,
using Graphics's drawOval class from x,y to
x+1,y+1).
- For Line, have it draw the line
horizontally.
- For Circle, we'll need to do a little math,
because Java draws circles from x,y to the "opposite
corner".
Through the use of Pythagoras, one can define
the "horizontal width" of a circle to be
sqrt((radius^2)/2), so you can plug that straight into the
drawOval method call. Simply calculate the
diameter (e.g., 2*radius) and use that for the width and
height parameters of the drawOval method call.
- For Polygon, you only need to design
primitives to draw squares. (That is, if it's a triangle
or pentagon, you don't need to bother.)
- (6 points) Modify the Shapes class to support the
modifications we just did.
- Have the Shapes class extend Frame so that it's
a first-class GUI component.
- (1 point) Write a method called stringToColor
that takes a String as a parameter and returns a Color (from
java.awt). The process for this is extremely simple: if the
string is equal to "black", return Color.black, for
"blue", return Color.blue, etc. for a few colors.
You do not need to support all the colors, but make
sure to support at least black, blue, green, and red... and
use a "default else" color of Color.black.
- (2 points) Modify each shape addition into the array by
asking not only what the radius/edge length is in the UI,
but also the x,y coordinates and the desired color. Convert
the String color input into a Color value, and finally pass
the x,y coordinates into the respective add methods
(you'll have to add those three parameters to the add
methods). Finally, have the add methods call the modified
constructors (with the three additional parameters) that we
did in step 1.
- (2 points) Write a new method called setupGUI
that takes no parameters. Have it set the size of the
window to be 400x400 pixels (or another size, should you
prefer), add one button to the bottom of the frame called
"Close" that closes the window, and make the Frame appear on
the screen. Add an option g to "display the GUI"
into the user interface, and have it call the
setupGUI method.
- (1 point) Write a paint method (e.g., public void
paint(Graphics g)). This method will simply call the
paint methods of every object in the array in a strategy
similar to the way we call computeArea and
computePerimeter. The only difference is
that it will go through the entire array instead of calling
one element's paint method.
Here are some hints you may find helpful.
(3 points extra credit) Modify the Shapes class so that
instead of an array, you're using an ArrayList to store
the shapes. If you do so, you'll no longer need a # of shapes
counter; you can simply call the size method in
ArrayList. Additionally, you don't need to worry about a fixed
capacity; the ArrayList is of unlimited capacity.