Question
GENERICS ASSIGNMENT: BELOW ARE THE GIVEN CLASSES READ ALL You are provided with the code for Main, OrderedPair and Pairable. Your job is to create
GENERICS ASSIGNMENT: BELOW ARE THE GIVEN CLASSES
READ ALL
You are provided with the code for Main, OrderedPair and Pairable. Your job is to create an adapter class, called Point, that represents a point on a cartesian plane (x value and y value), that uses and OrderedPair as a private variable.
HERE IS MAIN: class Main { public static void main(String[] args) { Point A= new Point(15,4); System.out.println("After constructor Point A is: "+A); A.translate(5,16); System.out.println("After translate Point A should be (20,20): "+A); Double theDistance= A.distance(40,30); System.out.println("The distance between Point A and (40,30) should be approx 22.36 "+theDistance); } }
HERE IS ORDERED PAIR:
public class OrderedPair
HERE IS PAIRABLE:
public interface Pairable
All the functions you write must have pre and post conditions
1) First, Modify Ordered Pair to have two functions:
setFirst, which sets the first value in the ordered pair and
setSecond, which sets the second value int he ordered pair.
These should use T, our generic parameter.
2) Create a class called Point with an OrderedPair as the private variable specifying an Integer as the generic type (note you cannot do int because the type has to be a Java Object type). Create a Constructor for point that takes in two Integers and sets OrderedPair first to the first Integer and OrderedPair second to the second Integer.
2B) Create a two public functions called getX and getY in Point. getX returns the firstValue from the ordered pair and getY returns the second (make them return primitive ints, (otherwise the test case won't pass))
3) Create a public function in Point called translate that takes in two Integer numbers and translates the first point by the first parameter passed in and translates the second point by the second parameter passed in
4) Create a function in Point called distance which takes in two Integer numbers and returns a Double that represents the distance between the current point and the point passed in. Use Math to accomplish this
5) Create public String toString() that returns a string of the first point and second point in parenthesis separated by a comma i.e.
(5,4)
assume the first ordered pair value is X and the second is Y (i.e. (X,Y))
Sample output from the Client provided (Main.java) will look as follows:
After constructor Point A is: (15,4) After translate Point A should be (20,20): (20,20) The distance between Point A and (40,30) should be approx 22.36 22.360679774997898
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started