Hello need help with this Java assignment Thanks in advance
Hello, need help with this Java assignment. Thanks in advance.
Let us begin to apply OOD by creating a Class called Waypoint that extends the class Point2D.Double (from the package java.awt.geom) that is already provided with the Java language (you should check the online Java API Documentation for more details). That will provide us with access to x and y coordinates implemented as double-precision values, but we will also add fields for the speed (as a double) from that location to the next, and the street (as a String) that will be used to get from that location to the next.
The speed will be the velocity to the next waypoint and the street will be the name of the street to take to the next waypoint. In the \"real world\" we would probably create another data structure that associated two waypoints with the speed and street between them, but we are simplifying this for our current assignment. As a result, we will treat routes as if there is only one way to get from any one waypoint to the next, and therefore can store the speed and street with the \"from\" waypoint, and calculate the distance and time to the \"to\" waypoint.
The Point2D class that Java provides is a bit strange looking at first because it contains nested classes for the implementation of a Point with either type double or type float members, but you just import the java.awt.geom.Point2D class and then extend the Point2D.Double nested class (that is, the specific class nested inside Point2D that is designed for double values), so don\'t worry about the strange syntax with the extra dot (.) for now – it will work. We will learn more about nested classes and inner classes when we study Java Event management during our introduction to GUIs.
Now consider (but don\'t implement) method toDistance (that takes another Waypoint as a parameter and returns the distance from this Waypoint to the one passed as a parameter, as a type double value; and method toTime that also takes another Waypoint as a parameter and returns the travel time from this Waypoint to the one passed as a parameter, as a type double value, by using the speed of this Waypoint as the velocity to factor in the calculation. Of course these calculations are from the same Pythagorean Theorem we\'ve already developed and coded in the previous lab. We will be adding this feature (in a special way) next time - so just think about how you would do it now.
In summary, be sure Waypoint extends Point2D.Double. Our Waypoint will encapsulate an x and y coordinate (via inheritance) and a speed and street name (just called street). It will also provide for a toString() that formats the Waypoint as a single-line String (no embedded newlines) formatted as:
At ( x , y ) moving s MPH on street
Where x, y, s and street are the x, y, speed and street fields, respectively, and a 4-parameter Constructor. What other gets and sets do you need? Make sure you have what you need and don\'t rewrite what you don\'t (i.e., if it already exists and works in the superclass).
Solution
import java.awt.geom.Point2D;
public class WayPoint extends Point2D.Double{
private double speed;
private String street;
//constructor to initialize the field
public WayPoint(double x,double y,double speed,String street){
super(x,y);// calling the super class constructor
this.speed=speed;
this.street=street;
}
// to string method
@Override
public String toString() {
return \"At ( \"+x+\" , \"+y+\" ) moving \"+speed+\" MPH on \"+street+\"\";
}
public double getSpeed() {
return speed;
}
public String getStreet() {
return street;
}
public static void main(String [] args){
WayPoint point=new WayPoint(4.5, 2.5, 45, \"NY\");
System.out.println(point);
}
}
----------------------------output-------------------------------
At ( 4.5 , 2.5 ) moving 45.0 MPH on NY

