Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

!!! Help Help Help !!!! **** Any Computer Science Genius Please I Need Help With

ID: 3835177 • Letter: #

Question

                                                                                       !!! Help Help Help !!!!

                                  **** Any Computer Science Genius Please I Need Help With This Assignment****

Objectives

Programmer defined objects

Composition of objects

Specification

You will be defining two classes, creating new types. The first class is Heading. 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, GPS, will be composed of a CSC142Point and a Heading.  

class Heading: 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 specification for this class:


How to convert a compass heading to a compass bearing
A compass bearing consists of 3 items:

the direction you face (North or South)

an angle to turn between 0 and 90° (inclusive)

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.

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, just use the digits. No word degree or ° symbol

separate the initials from the degree with a space

In the example above, the bearing for a heading for 110° is the String "S 70 E".

class GPS: Our GPS needs to know its location and its heading. To simplify things, the location is represented by a CSC142Point. You need to implement a class to encapsulate this data and associated operations. Here is the specification for our GPS class:

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. Using right-triangle trigonometry

delta x = distance to move * sin(heading angle)
delta y = distance to move * cos(heading angle)

As you look in the Math class in the Java API, you'll find a sin() and cos() method. Notice though that the units for the angle parameter is radians, not degrees. Yet the data in our object is degrees not radians. We understand how to convert from angles to radians from homework 1. 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 Heading class first, then go on to the GPS 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 definitely 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.

Documentation, Style

Make sure you have documented your classes well. From this assignment on, we will be using JavaDoc style comments for class and method descriptions. Please see the documentation and style guidlines on the class website.

Use appropriate style that we've discussed in class. This includes (but not limited to) named constants, indenting, etc.

Grading

/7 Heading class
/8 GPS class
/5 documentation & style

                                                                   **** Here the CSC142 point class****

class Heading
- compass heading in degrees
+ Heading(double initialDegrees) -- construct a Heading object with the given degrees. If that value is out of bounds, throw an IllegalArgumentException
+ double getHeading() -- return the current heading

+ void setHeading(double update) -- set the heading 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 Heading as a String. delta x --o New location delta y heading Old location

Explanation / Answer

PROGRAM CODE:

Heading.java

package gps;

public class Heading {

  

   private double degree;

  

   public Heading(double initialDegrees)

   {

       setHeading(initialDegrees);

   }

  

   public double getHeading()

   {

       return degree;

   }

  

   public void setHeading(double update)

   {

       if(update <0 || update >360)

           throw new IllegalArgumentException();

       else this.degree = update;  

   }

  

   public String getBearing()

   {

       String bearing = "";

       if(degree<180)

           bearing += "S ";

       else bearing += "N ";

      

       if(degree<180)

           bearing += (180 - degree) + " ";

       else bearing += (360 - degree) + " ";

      

       if(degree>=90 && degree <270 )

           bearing += "E";

       else bearing += "W";

       return bearing;

   }

  

   @Override

   public String toString() {

       // TODO Auto-generated method stub

       return "compass degree = " + degree;

   }

}

GPS.java

package gps;

public class GPS {

  

   private Heading direction;

   private CSC142Point location;

  

   public GPS(Heading currentDir, CSC142Point currentLoc)

   {

       if(currentDir == null || currentLoc == null)

       {

           direction = currentDir;

           location = currentLoc;

       }

       else throw new NullPointerException();

   }

  

   public void move(double units)

   {

       if(units<0)

           throw new IllegalArgumentException();

       else

       {

           double deltaX = units*Math.sin(Math.toRadians(direction.getHeading()));

           double deltaY = units*Math.cos(Math.toRadians(direction.getHeading()));

          

           location.setX(location.getX() + deltaX);

           location.setY(location.getY() + deltaY);

       }

   }

  

   public void turn(double degrees)

   {

       if(degrees<-180 || degrees>180)

           throw new IllegalArgumentException();

       else

           direction.setHeading(direction.getHeading() + degrees);

   }

   public Heading getHeading()

   {

       return direction;

   }

  

   public CSC142Point getLocation()

   {

       return location;

   }

}