Every method should have a commented header. It doesn\'t have to be long, but yo
ID: 3853724 • Letter: E
Question
Every method should have a commented header. It doesn't have to be long, but you should be able to use the method without having to read all the code in it.
Write a class called Rectangle that represents a rectangular two-dimensional region. Your Rectangle objects should have the following methods:
Constructs a new rectangle whose top-left corner is specified by the given coordinates and with the given width and height. Throw an IllegalArgumentException on a negative width or height.
Returns this rectangle's height.
Returns this rectangle's width.
Returns this rectangle's x-coordinate.
Returns this rectangle's y-coordinate.
Returns a string representation of this rectangle, such as "Rectangle[x=1,y=2,width=3,height=4]".
Explanation / Answer
public class Rectangle {
int x;
int y;
int width;
int height;
public Rectangle(int x, int y, int width, int height) {
if(width < 0 || height < 0)
throw new IllegalArgumentException();
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public String toString() {
return "Rectangle[x=" + x + ",y=" + y + ",width=" + width + ",height="
+ height + "]";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.