Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Why does my equals method return false when I have two objects with same values? Java Programming. This question includes 2 classes Here is
Why does my equals method return false when I have two objects with same values? Java Programming. This question includes 2 classes Here is CoordinatePair Class import java.lang.Math; class CoordinatePair { private int x; private int y: CoordinatePair(int x, int y){ this.x=x; this.y=y: } CoordinatePair(){ } //Getters and Setters public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void sety (int y) { this.y = y; } //toString public String toString( return "CoordinatePair" + "("+ getX()+""+getY()+")": } //tostring public String toString({ return "CoordinatePair" + "("+ getX() +","+getY()+")": } public boolean equals(CoordinatePair x, CoordinatePair y){ } if((x.getX()==y.getX()&& x.getY() == y.getY()){ return true; } } else return false; public double getEuclidianDistance(CoordinatePair otherPair) double distance: double xDifference = otherPair.getX()-getX(): double yDifference = otherPair.getY() - getY(); distance = Math.sqrt(Math.pow(xDifference,2)+Math.pow(yDifference,2)); return distance; } Here is second testing class public class Driver { public static void main(String[] args) CoordinatePair pair1= new CoordinatePair(0,2): CoordinatePair pair2 = new CoordinatePair(0,2): System.out.println(pair1.getEuclidianDistance(pair2)); System.out.println("Pair 1: "+ pair1.toString(): System.out.println("Pair 2:" + pair2.toString()); System.out.println(pair1.equals(pair2));
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