Question
Attached is the code with instructions and my code.Can anyone advise in fixing errors.I am receiving error at translate as (Point translate or OrderedPair setFirst
Attached is the code with instructions and my code.Can anyone advise in fixing errors.I am receiving error at translate as (Point translate or OrderedPair setFirst or setSecond failed A.translate(10,20); assertEquals(A.getX(),0); assertEquals(A.getY(),60);) and point Constructor(Point A constructor or getFirst or getSecond failed assertEquals(A.getX(),-10); assertEquals(A.getY(), 40);)
provided with the code for OrderedPair and Pairable, 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.
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), using intValue() member function on an object of type Integer)
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
Main.java
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); } }
Orderedpair.java
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 public void setFirst(T f) { first = f; } public void setSecond(T s) { second = s; } /** Interchanges the objects in this pair. */ public void changeOrder() { T temp = first; first = second; second = temp; } }
Pairable.java
public interface Pairable { public T getFirst(); public T getSecond(); public void changeOrder(); }
Point.java
public class Point { OrderedPair pair; public Point(int x,int y) { pair = new OrderedPair(x,y); } public int getX() { return pair.getFirst(); } public int getY() { return pair.getFirst(); } public void translate(int f, int s) { pair.setFirst(pair.getFirst()+f); pair.setSecond(pair.getSecond()+s); } public double distance(int x,int y) { return Math.sqrt((pair.getFirst()-x)*(pair.getFirst()-x )+(pair.getSecond()-y)*(pair.getSecond()-y )); } public String toString() { return "("+pair.getFirst()+","+pair.getSecond()+")"; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered 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