Question: I'm totally stuck on this assignment. How can i solve this? A GPS(Global Positioning System) is a navigation tool that knows its location and heading.
I'm totally stuck on this assignment. How can i solve this?
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 LineSegment example). 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 |
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 |
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.
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
