**** Any Computer Science Genius Out There Please I Need Help With This Assignme
ID: 3831413 • Letter: #
Question
**** Any Computer Science Genius Out There 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
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() ); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.