Write the entire implementation for the class Rectangle as defined below. This c
ID: 3730722 • Letter: W
Question
Write the entire implementation for the class Rectangle as defined below. This class represents an axis-aligned rectangle in two-dimensional space. A rectangle is uniquely defined by its center point (x, y) and its size w-by-h. A bounding box is equal to another if it has the same center point and size. The width and height of a bounding box may never be negative. If the user attempts to set a negative width or height, the value 1 should be used instead (this may happen in multiple places). If user does not specify its center point (x, y) (by calling the first constructor), then you should set it to the origin point (0, 0) class Rectanglef private int x, y, w, h; Rectangle (int x, int y) Rectangle (int x, int y, int w, int h) 1 int getArea() [ void setCenter (int x, int y) I void setExtent (int w, int h) public boolean equals (Object that) fExplanation / Answer
public class Rectangle {
private int x, y, w, h;
public Rectangle(int x, int y) {
this.x = x;
this.y = y;
this.w = 0;
this.h = 0;
}
public Rectangle(int x, int y, int w, int h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
int getArea() {
return w*h;
}
void setCenter(int x, int y) {
this.x = x;
this.y = y;
}
void setExtent(int w, int h) {
this.w = w;
this.h = h;
}
public boolean equals(Object obj) {
if(obj instanceof Rectangle) {
Rectangle oth = (Rectangle)obj;
if(x == oth.x && y == oth.y && w == oth.w && h == oth.y)
return true;
}
return false;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.