We will begin to practice OOD by extending one of the JavaFX classes (Point2D) into an abstract
Question:
We will begin to practice OOD by extending one of the JavaFX classes (Point2D) into an abstract LocalPoint class we will create using inheritance and implementing a standardising interface we will also create called Travelable. Then we will create a Waypoint class that extends our abstract LocalPoint class.
Remember, a subclass has an "is a" relationship with all the superclasses in the hierarchy above it as well as the interfaces implemented by them (and therefore also from "above" it via the "extends" mechanism). I'll explain this more next time.
First, create an interface called Travelable that provides:
- Abstract method toDistance that takes another Travelable object as a parameter (i.e., a Waypoint would be one kind of Travelable object) and returns the distance from this Travelable to the one passed, as a type double.
- Abstract method toTime that also takes another Travelable (like for instance, a Waypoint) as a parameter and returns the travel time from this Travelable to the one passed, as a type double, by using the speed at this Waypoint as the velocity to factor in the calculation. Of course these calculations are from the same Pythagorean Theorum we've already developed and coded in previous lab activities (Hint: But do we really need to do all that work, now?).
Second, create an abstract class called LocalPoint that extends the class Point2D (from the JavaFX class javafx.geometry.Point2D) that is already provided in the Zulu Java bundle we installed as our JDK (you can check the online JavaFX API Documentation for more details) and implements the above mentioned Travelable interface you created. Note: For this to work you must have installed your JDK using the Zulu instructions I gave you earlier in the semester.
Third, create a concrete class called Waypoint that extends the above mentioned LocalPoint class you created and adds:
- a field for the speed (as a double) that will be used to get from this location to the next
- a field for the city (as a String) that names the location of this Waypoint
- overrides the toString and equals methods (inherited all the way down from class Object)
The result will provide a class with access to x and y coordinates (via inheritance) implemented as double-precision values and a speed (as a double) that will be used to get from this location to the next, and a city (as a String) that will be traveled-through at this location.
In the "real world" we would probably create another data structure that associated two waypoints with the speed and street (and perhaps more than one pair of these) 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 city with the "from" waypoint, and calculate the distance and time to the "to" waypoint.
To use the Point2D class that JavaFX provides you just need to import javafx.geometry.Point2D and then extend the Point2D class. Be sure to read-up in the documentation about all that you are inheriting from that class, as that is one of the main goals of this lab assignment.
Java 11 and the JavaFX documentation sites..
You will probably want to click on the [ALL CLASSES] button to see the list of the all classes in each API.
Now consider how you will implement method toDistance (that takes another Travelable as a parameter and returns the distance from this Travelable to the one passed as a parameter, as a type double value; and method toTime that also takes another Travelable as a parameter and returns the travel time from this Travelable 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 Theorum we've already developed and coded in the previou labs. I want you to be able to implement this yourself, but that may not be the best way to actually code the solution. Hint: take a look at was is already available to you as a result of our inheritance design.
Note that a Waypoint is a (that is, has an "is a" relationship with) Travelable, but the implementation of the toTime in Waypoint will use the speed that is only in the Waypoint (not the Travelable).
Also note that while Travelable is the type for all of our interface method parameters (so they can be shared consistently as we build the class hierarchy further) it is not a (that is, does not have an "is a" relationship with) Point2D. Therefore, these parameters will need to be cast to type Point2D before we can call any of the Point2D methods they actually have, as in:
Point2D p = (Point2D) myTravelableParameter;
Or else you will get a compile time error trying to access any Point2D methods through the Travelable instance parameter references. But in case we build later Travelables that don't inherit from Point2D, it is best to be sure these Travelable references are also Point2D objects (when used within the Waypoint class) as in:
if (myTravelableParameter instanceof Point2D) Point2D p = (Point2D) myTravelableParameter; else throw new RuntimeException("This Travelable is not a Point2D instance.");
Or else you risk the possibility of allowing a run-time fail as opposed to a compile-time error. That defeats of all the work the compiler is trying to do for you with these checks in the first place.
Attach all three files (Waypoint.java, LocalPoint.java and Travelable.java). I may compile them with my own test-driver application (that is, with a main method) to test your solution.
In summary, be sure Waypoint extends LocalPoint which extends Point2D and implements Travelable. Our Waypoint will encapsulate an x and y coordinate (via inheritance) and a speed and city name (just called city). 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 speed MPH in city
Where x, y, speed and city are the x, y, speed and city fields, respectively, an equals() method that compares Waypoints for matching fields, 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 a superclass). However, override if you need to.