Why does my equals method return false when I have two objects with same values? Java...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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)); 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));
Expert Answer:
Related Book For
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill
Posted Date:
Students also viewed these programming questions
-
(a) Explain how a limit worth may be tended to at run-time, both in a syntaxtree middle person and in collected code. What is the importance word "settled" already? [3 marks] (b) Give a model program...
-
Data set Theory Assume an informational record with one association parent including matches (a, b) where a can't try not to be a parent of b. (a) Write a Datalog demand which gives the graph of...
-
The atoms that constitute your body are mostly empty space, and structures such as the chair you're sitting on are composed of atoms that are also mostly empty space. So why don't you fall through...
-
When does a company recognize revenue for a sale of a gift card?
-
Assess direct cost variances compared to overhead cost variances as it relates to a manufacturing companies. What would be examples? From a shareholder stand point, how would you use 2 of the...
-
The adjusted trial balance of Lighthouse Realty, Inc., at December 31,2010, follows: Requirements 1. Prepare Lighthouse Realtys 2010 income statement, statement of retained earnings, and year-end...
-
Two flow control loops axe shown in the drawing. Indicate whether each system is either a feedback or a feed-forward control system. Justify your answer. It can be assumed that the distance between...
-
John has just turned 20 and his rich relatives are thinking of giving him an investment while he is young, which will pay for his retirement. They hope that by investing early, the investment they...
-
A perishable dairy product is ordered daily at a particular supermarket. The product, which costs $1.19 per unit, sells for $1.65 per unit. If units are unsold at the end of the day, the supplier...
-
Which of the following for loop declaration is not valid? Select one: a. for ( int i = 99; i >= 0; i / 9 ) b. for ( int i = 20; i >= 2; - -i ) c. for ( int i = 2; i
-
How does the strategic integration of visual elements, such as infographics and data visualization, augment the communicative efficacy of business writing in the digital era ?
-
Trevino concludes that "the sociology of law has come of age" and is now a recognized academic field that impacts both the field of sociology and the profession of law. Throughout this term you have...
-
How can the judicious incorporation of rhetorical devices elevate the cogency and resonance of executive-level reports and analyses ?
-
-43 Find the complexity of the below function. int Function (int n) { if(n = 2) return 1; else return (Function (floor(sqrt(n))) + 1);
-
Aaron Heath is seeking part-time employment while he attends school. He is considering purchasing technical equipment that will enable him to start a small training services company that will offer...
-
Stress (MPa) 500 400 300 200 100 0 Ciled Tensile strength 450 MPa (65,000 psi) MPa 40 200 100 0 10 psi 0.10 30 20 10 0. 0 0.005 Yield strength 250 MPa (36,000 psi) 0.30 E 1 a) Determine the modulus...
-
A glass manufacturer produces hand mirrors. Each mirror is supposed to meet company standards for such things as glass thickness, ability to reflect, size of handle, quality of glass, color of...
-
On July 1, 2012, Ted, age 73 and single, sells his personal residence of the last 30 years for $365,000. Ted's basis in his residence is $35,000. The expenses associated with the sale of his home...
-
Skyler is covered by his company's health insurance plan. The health insurance costs his company $3,500 a year. During the year, Skyler is diagnosed with a serious illness and the health insurance...
-
While preparing Massie Miller's 2012 Schedule A, you review the following list of possible charitable deductions provided by Massie: Cash contribution to a family whose house burned...
-
A wheel of radius \(r\) rolls on a flat surface without slipping. Determine the angular motion of the wheel in terms of the linear motion of its center \(O\). Also determine the acceleration of a...
-
The right-angle bar rotates clockwise with an angular velocity which is decreasing at the rate of \(4 \mathrm{rad} / \mathrm{s}^{2}\). Write the vector expressions for the velocity and acceleration...
-
The load \(L\) is being hoisted by the pulley-and-cable arrangement shown. Each cable is wrapped securely around its respective pulley so it does not slip. The two pulleys to which \(L\) is attached...
Study smarter with the SolutionInn App