Project Details: For this project, you will have to write twoclasses. Class Rectangle is a class that represents an axis-alignedrectangle. Class RectangleTester is the driver class, used to testthe Rectangle class.
Class Rectangle: An instance of the class Rectangle represents avalid axis-aligned rectangle. It has several instance variablesthat one needs to use to describe a rectangle, as well as methodsthat operate on these variables.
Rectangle instance variables: Class Rectangle will have thefollowing instance variables:
• x – a double that represents the x coordinate of the lowerleft vertex of the rectangle
• y – a double that represents the y coordinate of the lowerleft vertex of the rectangle
• width – a double that represents the width of therectangle
• height – a double that represents the height of therectangle
• color – a String that represents the color of therectangle
For the rectangle to be valid, the coordinates of all verticesmust be between 0 and 1 inclusive. The color can be one of thefollowing: “black”, “red”, “green”, “blue”.
Rectangle constructor: The constructor should be: Rectangle(double x, double y, double height, double width, String color)
Parameters are, in order, the x and y coordinates of the lowerleft corner, the height, the width, and the color of the rectangle.The constructor should check that these values represent a validrectangle. If they do, then it should initialize the rectangle tothese 2 values. Otherwise, the date rectangle should be initializedto a rectangle of width 1, height 1, color “red” and lower leftvertex at coordinates (0, 0).
Rectangle methods:
double getWidth() Returns the width of this object. doublegetHeight() Returns the height of this object. String getColor()Returns the color of this object. void setX(double x) Sets the xcoordinate of this object to x. If this results in an invalidrectangle, it leaves the x coordinate unchanged. void setY(doubley) Sets the y coordinate of this object to y. If this results in aninvalid rectangle, it leaves the y coordinate unchanged. voidsetWidth(double w) Sets the width of this object to w. If thisresults in an invalid rectangle, it leaves the width unchanged.void setHeight(double h) Sets the height of this object to h. Ifthis results in an invalid rectangle, it leaves the heightunchanged. void setColor (String c) Sets the color of this objectto c. If c is invalid, it leaves the color coordinate unchanged.String toString() Returns a String representation of the rectanglecontaining the values of all instance variables. booleanequals(Rectangle rect) Returns true if rect represents the samerectangle as this object, and false otherwise. doublecomputePerimeter() Computes and returns the perimeter of thisrectangle double computeArea() Computes and returns the area ofthis rectangle boolean containsPoint(double x, double y) Returnstrue if this rectangle contains point (x, y) booleancontainsRectangle(Rectangle rect) Returns true if this rectanglecontains rectangle rect boolean intersects(Rectangle rect) Returnstrue if rectangle rect and this rectangle intersect. static booleanisValid(double x, double y, double w, double h) Static method.Returns true if the rectangle with lower left vertex at (x,y),width w and height h is valid, and false otherwise.
void show() Displays the rectangle on the screen. See detailsbelow.
Make sure that the names of your methods match those listedabove exactly, including capitalization. The number of parametersand their order must also match. You can change the parameter namesif you want. All methods should be public.
To display a rectangle, use the StdDraw class. You will need touse the following static methods:
• setPenColor(color) sets the color of the pen. You can use thefollowing colors: StdDraw.RED, StdDraw.BLUE, StdDraw.GREEN,StdDraw.BLACK. Example: StdDraw.setPenColor(StdDraw.RED);
• line(x0, y0, x1, y1) draw a line in the current pen color frompoint (x0, y0) to point(x1, y1). Example: StdDraw.line(x, y, x+w,y+h);
Class RectangleTester: This class should be your driver classand should contain the main method. For each method in your classRectangle, your main method in RectangleTester should have at leastone test case that tests that method.
Project Details: For this project, you will have to write two classes. Class Rectangle is a class that represents an axi
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am