Question: A GPS(Global Positioning System) is a navigation tool that knows its location and heading. This information helps a person navigate. In this assignment you are
A GPS(Global Positioning System) is a navigation tool that knows its location and heading. This information helps a person navigate. In this assignment you are going to create an object that behaves like a simple GPS.
Objectives
- Programmer defined objects
- Composition of objects
Specification
You will be defining two classes, creating new types. The first class is CompassHeading. One of the objectives of this assignment is composition, defining classes that have references as instance variables (similar to our LineSegmentexample). The second class, GPSUnit, will be composed of (x,y) coordinates and a heading. NOTE - you are not writing a complete program. For this assignment, you are the supplier programmer, defining classes for another client to use.
class CompassHeading: A compass heading is a direction within the range [ 0 to 360) ( means degrees). 0 is North, 90 is East, 180 is South, and 270 is West. You need to implement a class to encapsulate this data and associated operations. Here is the class diagram:
| class CompassHeading |
| - compass heading in degrees |
| + CompassHeading(int initialDegrees) -- construct a CompassHeading object with the given degrees. If that value is out of bounds, throw an IllegalArgumentException |
| + int getHeading() -- return the current heading + void setHeading(int update) -- set the CompassHeading to the given value. If that value is out of bounds, throw an IllegalArgumentException. + String getBearing() -- return the compass bearing for this Heading as a String. + String toString() -- return the current state of this CompassHeading as a String. |
How to convert a compass heading to a compass bearing (or a quadrant bearing) A compass bearing, also known as a quadrant bearing, consists of 3 items:
- the direction you face (North or South)
- an angle to turn in the interval [0, 90]
- and the direction you turn (East or West)
For example, starting with a compass heading of 110, here is how to determine the equivalent bearing: face South then turn 70 East (180 - 70 = 110). Therefore the bearing is South 70 degrees East, or S70E. The method getBearing() needs to do this conversion and return the result as one String. The String must follow a specific format:
- Use only the first initial for the directions N, S, E, W
- The initial should be capitalized
- For the degree symbol, use ASCII code 176.
- Do not include any spaces
- If the heading is due North (0), South (180), East (90), or West(270), return the String "(due)X" where X is the direction N,S,E, or W.
In the example above, the bearing for a heading for 110 is the String "S70E".
class GPSUnit: A GPSUnit object knows its location and its heading. To simplify things, represent the location as a CSC142Point. You need to implement a class to encapsulate this data and associated operations. Here is the specification for our GPS class:
| class GPS |
| + GPS(CompassHeading currentDir, CSC142Point currentLoc) -- construct a GPS object with the given data. Throw a NullPointerException if either parameter is null. |
| + move(double units) -- move this GPS object the given amount of units in the direction it is heading. If units is |
How to move from one point to another in a given direction The method move() requires some complex math that I want to explain here.
| Given the old location of (x,y) we need to calculate a new location (x + delta x, y + delta y). Therefore, we need to calculate the change in x (delta x) and the change in y (delta y). Notice that our heading measures the angle from the vertical (0 degrees is along the positive y-axis). Using right-triangle trigonometry delta x = distance to move * sin( angle ) delta y = distance to move * cos( angle ) You may have learned in a math class that delta x = distance * cos(angle). But that's when the angle is measured from the horizontal (the x-axis). We are measuring the angle from the vertical. |
The Math class in the Java API contains sin() and cos() methods. Notice though that the units for the angle parameter is radians, not degrees. Yet the data in our object is degrees not radians. Look further in the Math class and you'll find a convenient method to convert from degrees to radians.
If anyone has questions about these calculations, please don't hesitate to ask me.
Suggestions
As always, you will have success if you code and test in pieces. Start with the CompassHeading class first, then go on to the next class. You can test either by using the BlueJ interface of instantiating objects and calling methods on them, or writing yourself a small test method. You should hand caclulate some turn() and move() operations in order to compare expected results with the program's actual results. Remember, just because something runs doesn't mean it's working properly.
Here the csc142 Point class java
/** Represents a 2-dimensional CSC142Point * @author CSC 142 * @version 2006 */ public class CSC142Point { private double x, y; // the coordinates
// two different constructors /** * Create a CSC142Point at (0, 0) */ public CSC142Point() { x = 0; y = 0; } /** * Create a CSC142Point with the given coordinates * @param initialX the x-coordinate * @param initialY the y-coordinate */ public CSC142Point (double initialX, double initialY) { x = initialX; y = initialY; }
// update (mutator) methods // that change the state of a CSC142Point object
/** * Set the x-coordinate of this CSC142Point * @param updateX the new x-coordinate */ public void setX(double updateX) { x = updateX; } /** * Set the y-coordinate of this CSC142Point * @param newY the new y-coordinate */ public void setY(double updateY) { y = updateY; }
/** * Set the x and y coordinates of this CSC142Point * @param newX the new x-coordinate * @param newY the new y-coordinate */ public void setPoint( double newX, double newY){ x = newX; y = newY; } // query (accessor) methods // that somehow report the state of a CSC142Point // without changing it
/** * Get the x-coordinate of this CSC142Point * @return the x-coordinate */ public double getX() { return x; } /** * Get the y-coordinate of this CSC142Point * @return the y-coordinate */ public double getY() { return y; }
/** * Calculate the distance between this CSC142Point and the origin * @return the distance to (0, 0) */ public double distanceToOrigin() { return Math.sqrt(x * x + y * y); } /** Calculate the distance between this CSC142Point and some other CSC142Point * @param other the other CSC142Point * @return the distance between the 2 CSC142Points */ public double distance(CSC142Point other) { double diffX = x - other.x; double diffY = y - other.y; return Math.sqrt(diffX * diffX + diffY * diffY); } /** * Find the midpoint between this CSC142Point and another CSC142Point * @param p the other CSC142Point * @return the CSC142Point midway between the two CSC142Points */ public CSC142Point midPoint(CSC142Point other) { double midX = (x + other.x) / 2; double midY = (y + other.y) / 2; return new CSC142Point(midX, midY); } /** * The String version of this CSC142Point * @return the String representation */ public String toString() { return "(" + x + ", " + y + ")"; } /** * a test method - do not change this code! * it should display 3 results in a terminal window */ public static void test (){ CSC142Point alpha = new CSC142Point( 5, 5 ); CSC142Point beta = new CSC142Point( -3, 7 ); System.out.println( "alpha is " + alpha.distanceToOrigin() + " from the origin" ); System.out.println( "The x coordinate of beta is " + beta.getX() ); beta.setY( 72 ); System.out.println( "The y coordinate of beta is " + beta.getY() ); } }
delta x New location delta y heading Old locationStep by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
