Homework 34
You will write a simple program to track ship movement over
time. You are provided with the struct Point defined in dbpoint.h
and dbpoint.cpp.
You can use this point struct to help track a ship’s
position.
Your program will read information from the user for a single boat. Specifically, you will be given its name, position (as an (x,y) pair giving its position in nautical miles from some fixed point), its heading (in degrees from north) and its speed in knots. In this scenario, the boat never changes speed or heading once set by the user. Your program will then read a number of hours from the user, update the boat's position, and print a position report after that number of hours. It will keep reading a number of hours and printing a position report over and over until the user indicates that he or she wants to quit by entering a -1. Below is a sample run of the program (user input shown in red). NOTE: several hints are given below!!!!!
Enter boat info: Slowpoke (140.3,33.7) 37.0 degrees 10 ktsCurrently: Slowpoke (140.3,33.7) 37 degrees 10 kts
Time elapsed in hours: 0.5Currently: Slowpoke (143.309,37.6932) 37 degrees 10 kts
Time elapsed in hours: 2.5Currently: Slowpoke (158.354,57.6591) 37 degrees 10 kts
Time elapsed in hours: -1
Requirements:
You must make a struct Boat that stores all the necessary information about a
boat, and make all the work (like reading and writing and moving the boat) into
functions. There should be a file Boat.h and Boat.cpp that implements all of this
stuff. Your main.cpp should
be very simple, and should include Boat.h (but not Boat.cpp). If you
do this right, it should be trivial to transform this program so that it would
track many boats simultaneously!
Turn
In: A screen capture of your program running on the given sample input,
and a printout of Boat.h, Boat.cpp and main.cpp.
HINTS/HELPS:
1. Making a directory just for this homework would be a good idea.
2. Download the two files linked above and make sure they are in the current directory AND have names dbpoint.h and dbpoint.cpp
3. When you create boat.h and boat.cpp, make sure they also are in this directory.
4. Your
boat.h file should have this at the very top:
#ifndef BOAT_H
#define BOAT_H
#include "dbpoint.h"
and this at the very bottom:
#endif
See class 35 for details on this.
5. You will
have more than one “source” file to compile and link (main.cpp, boat.cpp, and dbpoint.cpp). If there are only a few files, it is
reasonable to have g++ do all of this at once, which you can do by executing
the following command (result will be in a.out):
g++ -Wall -Wextra
main.cpp boat.cpp dbpoint.cpp
6. Remember that a compile error at, say main.cpp line 12, may actually be caused by something in another file that was included. For instance, did you forget a semicolon somewhere in boat.h? (this can cause obscure errors in main.cpp)
7. Consider how the following function might help you. You are free to use it or you may create your own from scratch.
Point move(Point position, double heading, double speed, double time){double PI = 3.14159;
Point delta; // the displacement of the boat over the specified time
delta.x = time*speed*sin(heading*PI/180); // displacement in x direction
delta.y = time*speed*cos(heading*PI/180); // displacement in y direction
return position + delta; // requires overload of operator+ for struct Point
}