Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 implements Pairable { private T first, second; public OrderedPair(T firstItem, T secondItem) // NOTE: no after constructor name { first = firstItem; second = secondItem; } // end constructor /** Returns the first object in this pair. */ public T getFirst() { return first; } // end getFirst /** Returns the second object in this pair. */ public T getSecond() { return second; } // end getSecond /** Returns a string representation of this pair. */ public String toString() { return "(" + first + ", " + second + ")"; } // end toString /** Interchanges the objects in this pair. */ public void changeOrder() { T temp = first; first = second; second = temp; } // changeOrder } // end OrderedPair

HERE IS PAIRABLE:

public interface Pairable { public T getFirst(); public T getSecond(); public void changeOrder(); } // end 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

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions