CSE 143 Spring 2001

Homework 2

Due: Electronic submission by 10 pm, Wednesday April 11.  Paper receipt and written report due in quiz section on Thursday April 12.

 

Overview

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.

Concepts

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.

Program Specification

As before, the program has two phases, but the details are slightly different.

  1. When it starts, it should request a file name from the user, then open a file with that name in the current directory, read the contents of that file, and store the information in a suitable data structure.  The file format is exactly the same as in homework 1: information about places, consisting of an x and y coordinate and a name for each place.  The end of the input is marked by a place name of ".".  The difference this time is that you should ask the user for the file name, instead of always reading a file with the same name.
  2. After reading the file, the program should prompt the user to enter pairs of place names and report the distance and direction from the first place in each pair to the second.

Interaction

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: .

Implementation Requirements

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.

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). 

Other Implementation Requirements

As in homework 1,

Implementation Hints

Directions

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?

A note about the ifstream constructor

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());

Testing

As in homework 1, you should create and run appropriate tests to be sure your code works properly.

Sample Program

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.

Help!!! Where to I start?

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.

Report

As before, when you turn in your program, you must also turn in a short report that discusses the following issues:

  1. Planning - How did you plan your design of your program?
  2. Implementation - Describe the implementation.  What data structures did you use?   How did you structure the code?  In particular, what happened when you converted your old code to use classes?  How did the design decisions you made in assignment 1 make this assignment easier or harder?
  3. Testing - Describe the test cases you chose.  Describe the kinds of bugs you found using tests.  If some of your test cases still don't work, or there are parts of the program that are incomplete, describe that.  Attach some of your test cases and results to support your claims.
  4. What did you learn from completing this project?  This could include things you learned about program specification and design, C++ language issues, developing test cases, debugging, etc.

This report need not be long, but it should be complete enough to give us a good picture of what you accomplished.

Electronic Submission

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.