CS 201 - Computer Science I - Spring 2003
Project 1 - Using objects
Loyola College >
Department of Computer Science >
CS 201 >
Projects >
Project 1
Due
Tuesday, February 25th Wednesday, February
26th Thursday, February 27th
at 11:59pm. Late projects will be assessed a 20%
penalty for each day past the due date. Projects will not be accepted
more than four days past the due date.
Objectives
- to use classes from the Java libraries
- to use classes supplied by other programmers
Introduction
The applet for this project allows the user create a map showing the locations
of any street addresses entered. To add a location to the map, the user
must type the address in the input field at the top of the applet and
click the "Add" button. The "Clear" button clears all entered locations
off the map.
In the example given below, legal house numbers are from 1 to 999 with 1
being on the left of the screen and 999 being on the right. Street names
are, from south to north, "Amherst", "Benokraitis", "Clocktower", "Denver",
"Eastman", "Foxboro", "Glenn", "Halfling", "Iverson", "James", "Karen",
"Lafayette", "Midway", "Northern", "Ohio", "Pennsylvania", "Quiet Ways",
"Reeds Landing", "Sea Shadow", "Terrapin", "Ugly Spring", "Vilnius",
"Whitetail", "Xanadu", "Yocco", and "Zayre".
Entering "500 Northern Parkway"
would then put an icon in the middle of the map.
This applet will not run under Internet Explorer (it will run under most
current versions of other browsers). To run it on a lab machine, open
a Command Prompt window with the Start menu, and type
cd \jdk1.3\bin
and then
appletviewer http://gunpowder.cs.loyola.edu/~jglenn/201/S2003/Projects/P1/proj1.html
(you can copy the URL from the browser and paste
it into the Command Prompt window).
Assignment
Most of the applet has been written as three classes: MapApplet
which, as the applet class, sets up the user interface components,
Geocoder, which translates from street addresses to
longitude and latitude, and HouseView, which is a custom component
that draws the map that makes up the bulk of the applet's window.
A few of the methods needed for the applet have been omitted; you must write
these in a new class called Project1. Your code will have to
interact with the classes described above as well as with classes from
the Java libraries including Graphics, Point2D,
and String.
The three methods you must write are
- static void drawHouseIcon(Graphics g, int x, int y, int iconSize),
which should draw a picture of a house and the given coordinates on the
Graphics object. The house should have a roof and a door;
- static void addHouse(String addr, HouseView view), which
takes a string containing the user's input, breaks it into parts, uses a
Geocoder object to translate the address to geographic
coordinates, and then tells the HouseView object to add a house
at those coordinates; and
- static void clearMap(HouseView view), which should remove all the
icons from view (this method will only contain one line).
Note that all three methods are static and so when you write them you they
should start public static void.
The supplied classes include the following methods and constructors that
will be useful to your code.
- Geocoder's constructor Geocoder(), which
creates a Geocoder object (remember that in order to use
the methods in a class you must have an instance of that class).
- Geocoder's Point2D geocode(int num, String
sName), which, given an address split into a house number and
street name, returns the coordinates (longitude and latitude) of the
address. Point2D is a class from the java.awt.geom
package that is used to represent coordinated in 2-D space (x is longitude
and y is latitude).
- HouseView's void plotHouse(Point2D loc), which
adds a house icon to the map at the longitude and latitude stored inside the
given Point2D.
- HouseView's void clear(), which clears all the icons
from the map.
The supplied classes have more complete documentation
(get used to seeing things like this). Documentation for the Java library
classes used in this project is, of course, included in the Java API
documentation.
Suggestions
Start by creating an applet that draws a house. Modify the code
from the paint method of that applet to create your
drawHouseIcon method in the Project1 class.
To test the resulting method with the applet, create an empty
clearMap method and put only the following line in
your addHouse method
view.plotHouse(new Point2D.Double(-77.05, 39.05));
When you run the applet and click "Add", you should see a small house in the
middle of the screen. By changing the two numbers (keep the first between
39.0 and 39.1 and the second between -77.0 and -77.1) you should be able
to move the house around the screen.
Next work on reading the address from the TextField and splitting it
into a house number and street name. You can use System.out.println
to see the result of your efforts.
When you have the address split properly, use the Geocoder
class to convert to coordinates. Pass the coordinates to plotHouse
and you should be able to enter addresses and see them drawn on the map.
Finally, complete your clearMap method.
Files
Save all three files to the same directory (right-click on the link and choose "Save Link As"). Create your
Project1.java file in that same directory. To run the applet,
open the MapApplet.java window and choose "Compile" and then
"Run as applet" from the menu.
Grading
- 60% Execution. Partial credit will be granted depending on how
many of your methods work correctly.
- 30% Design. The design score is based on how easy it is to
follow the logic of your code, how well you avoided repetitive code,
and how easy it would be to change your code if certain specifications
changed.
- 10% Style. Style includes comments, indentation, and choice of
variable and method names.
- Substantial progress must be made towards correct execution to
earn the points for design, and style.
Submissions
Submit the source code (.java file) for your Project1 class.