![]() |
CSE 143 Spring 2001Homework 2Due: Electronic submission by 10 pm, Wednesday April 11. Paper receipt and written report due in quiz section on Thursday April 12. |
The clients are pleased! Homework 1 is a success! Of course, now that they've seen the program, they've got some ideas to improve it, and they want some internal changes made. They've heard about classes and information hiding, and think this would be a really good thing to have, in addition to changes in the external specifications. For this assignment, you should make the requested changes to your solution to homework 1.
As with all assignments, please read the instructions all the way through before beginning. Read them once again before you turn in your assignment, to be sure you've left nothing out.
The goal of this assignment is to gain experience using the following new concepts, as well as continue to work with previous programming and C++ ideas.
As before, the program has two phases, but the details are slightly different.
For each pair of place names, the program should print the following:
The reported direction should be the one closest to the line from the first location to the second. Due north is 0 degrees, due east is 90 degrees, north east is 45 degrees, etc. Each direction describes a 45 degree arc. For example, "east" means any direction between 67.5 degrees (= 90 - 22.5) and 112.5 degrees (= 90 + 22.5). If the line between the first place and the second is exactly on the dividing line between two directions, your program may report either one. For example, if the angle is exactly 67.5, it can be reported as either "northeast" or "east".
This time, print a blank line between each interaction.
As before, the place name must exactly match the name in the database, including capitalization. When the user is done, she should enter a single period (".") to indicate that the program should terminate execution. Distances should be computed as floating-point numbers, not integers. The number of decimal places to print is not specified - feel free to use the default precision provided by C++ output streams.
Example: Suppose we have a data file named peculiar.txt
containing the following places
and locations (a very artificial example):
1.0 0.0 Easton 1.0 1.0 Noreaster 0.0 0.0 Centerville -1.0 1.0 PNW 0.1 1.0 .
Here's an example of what the console window might look like when we run the program. User input is underlined and in italics, like this.
Enter file name: peculiar.txt Enter start location: Centerville Enter destination: Easton Easton is 1.0 miles east of Centerville Enter start location: PNW Enter destination: Centerville Centerville is 1.414 miles southeast of PNW Enter start location: Belltown Enter destination: Centerville Never heard of Belltown Enter start location: Fremont Enter destination: Ballard Never heard of Fremont Never heard of Ballard Enter start location: PNW Enter destination: Noreaster Noreaster is 2.0 miles east of PNW Enter start location: Centerville Enter destination: Centerville Distance from Centerville to Centerville is 0.0 miles Enter start location: .
A key goal of this assignment is to learn how to construct and use C++ classes with member functions. The assignment is a variation of homework 1, but this time you must use C++ classes and member functions for the main data structures. Much of the design is up to you, but there are a few specific requirements. Your program must include the following classes and members.
Location
that represents a
single x, y coordinate. Location
should include at
least the following data and function members:
distanceTo(Location other)
that returns
the distance between this Location
and
Location
other
.
directionTo(Location other)
that returns a string
giving the direction from this Location
to Location
other
.
Location
to 0.0, 0.0.
PlaceList
that can store a list of place names and
their locations. There are only two specific requirements for this class:
PlaceList
to an empty list when it is created (declared).
Location
to store the
coordinates of the places in the list.
These classes should, of course, contain additional data and functions as needed. Use public and private to restrict access to implementation details so they are not visible outside the class.
The source code for each class should be stored in a pair of files with
appropriate names: classname.h
for the class declaration
(interface specification), and classname.cpp
for the
implementation (function code).
cin
, cout
, ifstream
,
etc.). Do not use printf/scanf/fscanf
or other C library
functions for I/O.#include <string>
), not the
C library functions strcpy
, strcmp
, and so forth.Given the locations of two places, you need to calculate the direction from one to the other. For those of us whose trig is a bit rusty, here are a few useful facts.
Function atan2
from the cmath
library can be used to calculate an angle given coordinate
information. atan2(x,y)
yields the angle in radians whose tangent is
x/y, and it correctly handles the case where y=0. If PI is the
constant 3.14159..., then the formula
atan2(x,y)*180.0/PI
returns the angle in degrees from the origin (0,0) to the point (x,y). The values returned are between 180 and -180. Values for the four main compass directions are:
Direction |
Value |
north |
0 |
east |
90 |
west |
-90 |
south |
180 or -180 |
Experiment! Try writing a small program to read x and y
values and print the value of the formula for those numbers. Then expand
on this to implement function Location::directionTo
. Challenge: an obvious
implementation is a huge nest of if statements to figure out which of the 8
possible directions should be returned. Can you come up with a simpler way
to do it?
In homework 1 we opened the input file with a line of code that looked like
ifstream inFile("places.txt");
This initializes the stream using an ifstream
constructor whose parameter
is a C-string giving the name of the file to be opened. Unfortunately,
ifstream
does not provide a constructor that accepts a C++ <string>
as an
argument. Fortunately, the C++ string
class includes a member function
c_str()
that returns a C-string representation of the C++ string. If
variable filename
is a C++ <string>
containing the name of the file to be
opened, you can convert it to a C-string and open the file as follows:
ifstream inFile(filename.c_str());
As in homework 1, you should create and run appropriate tests to be sure your code works properly.
Also, as before, there is no sample program for this assignment. In cases where the specification is not clear, you may need to make some reasonable assumptions. Feel free to discuss issues about the specifications on the course newsgroup, in sections, with your colleagues, etc.
You should start with the program you submitted for homework 1. It's probably simplest to first change the data structures and operations into classes. Then you can add the code to calculate directions and make the other necessary changes.
As before, when you turn in your program, you must also turn in a short report that discusses the following issues:
This report need not be long, but it should be complete enough to give us a good picture of what you accomplished.
When you've finished your program, turn in the source program (.cpp and .h files only) using this turnin form. Print out the receipt that appears, staple it and your written report together, and hand it in during quiz section.