1. Engineering
  2. Computer Science
  3. im totally stuck on this assignment how can i solve...

Question: im totally stuck on this assignment how can i solve...

Question details

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
<properties -- choose an appropriate datatype and name>
- compass heading in degrees
<constructor>
+ CompassHeading(int initialDegrees) -- construct a CompassHeading object with the given degrees. If that value is out of bounds, throw an IllegalArgumentException
<methods>
+ 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:

  1. the direction you face (North or South)
  2. an angle to turn in the interval [0°, 90°]
  3. 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 S70°E.

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 "S70°E".  

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
<properties -- I leave this to you to determine>
<constructor>
+ GPS(CompassHeading currentDir, CSC142Point currentLoc) -- construct a GPS object with the given data.   Throw a NullPointerException if either parameter is null.
<methods>
+ move(double units) -- move this GPS object the given amount of units in the direction it is heading. If units is < 0, throw an IllegalArgumentException.

+ turn(int degrees) -- turn this GPS object the amount of degrees specified from its current heading. Legal values are in the range [-180, 180] (inclusive). Throw an IllegalArgumentException if the arugment is out of range.

// provided query behaviors for these methods
+ CompassHeading getHeading()
+ CSC142Point getLocation()
+ String toString()

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.

delta x New location delta y heading Old location

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.  

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution