Question: *JAVA BlueJ Help Add a method named equals to the Rectangle class. The equals method you will write is used in the CompareRectangles class in

*JAVA BlueJ Help

Add a method named equals to the Rectangle class.

The equals method you will write is used in the CompareRectangles class in the printComparisons( ) method. Look at where it is run for clues to the equals method's parameter and return type. The method should compare the field values in the current object with the field values in the Rectangle object parameter.

The code:

/** * A class that represents a rectangle at a given position */ public class Rectangle { private int xPosition; private int yPosition; private int width; private int height;

/** * Constructor for objects of class Rectangle */ public Rectangle(int xPosition, int yPosition, int width, int height) { this.xPosition = xPosition; this.yPosition = yPosition; this.width = width; this.height = height; }

public int getXPosition() { return xPosition; }

public int getYPosition() { return yPosition; }

public int getWidth() { return width; }

public int getHeight() { return height; }

public void setXPosition(int newXPosition) { xPosition = newXPosition; }

public void setYPosition(int newYPosition) { yPosition = newYPosition; }

public void setWidth(int newWidth) { width = newWidth; }

public void setHeight(int newHeight) { height = newHeight; } //-------------------Insert equals method here---------------- public void printDetails() { System.out.println("The rectangle height is: " + height); System.out.println("The rectangle width is: " + width); System.out.println("The rectangle xPosition is: " + xPosition); System.out.println("The rectangle yPosition is: " + yPosition); } }

How would I create an equals method if there isn't anything to compare to each other?

Here is the other code that contains the printComparisons( ) method:

public class CompareRectangles { private Rectangle cerealBox; private Rectangle oatmealBox; private Rectangle r;

/** * Constructor for objects of class CompareRectangles */ public CompareRectangles() { cerealBox = new Rectangle(5, 10, 20, 30); oatmealBox = new Rectangle(5, 10, 20, 30); r = cerealBox; }

public void printComparisons() { if(cerealBox.equals(oatmealBox)) { System.out.println("cerealBox and oatmealBox are equal"); System.out.println("Both boxes have the same height, width, xPosition, and yPosition"); } else { System.out.println("cerealBox and oatmealBox are not equal"); } if(cerealBox == oatmealBox) { System.out.println("cerealBox and oatmealBox are the same object");

} else { System.out.println("cerealBox and oatmealBox are not the same object"); } if(cerealBox == r) { System.out.println("cerealBox and r are the same object");

} else { System.out.println("cerealBox and r are not the same object"); } if(oatmealBox.equals(r)) { System.out.println("oatmealBox and r are equal"); System.out.println("Both boxes have the same height, width, xPosition, and yPosition"); } else { System.out.println("oatmealBox and r are not equal"); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!