Question
Making a rectangle class in java write a simple class that will represent a rectangle. Later on, we will see how to do this with
Making a rectangle class in java
write a simple class that will represent a rectangle. Later on, we will see how to do this with graphics, but for now, we will just have to use our imaginations for the visual representations ofWthe rectangles. Instance Variables Recall that an object is constructed using a class as its blueprint. So, think about an rectangle on your monitor screen. To actually draw a rectangle on your monitor screen, we need a number of data. Clearly, a rectangle requires the dimension of the sides. We will call these dimensions width (for the X-dimension) and height (for the Y-dimension). The types of these dimensions could be either floating point (double) or integer (int). Since pixels on the computer display are discrete, we will make these dimensions int. In addition to its size dimensions, a MyRectangle must have a position (location) on the screen. We will base this position on the upper left corner of the MyRectangle. The graphic coordinate space for computers start a (0, 0) and proceeds to the right for positive X and down for positive Y. Call the X position startX and call the Y position startY, and like the dimension variables, make these both ints. For example, consider the following instance variable values for a MyRectangle:
startX 100 startY 50 width 80 height 20 This would define the MyRectangle
Recall that constructor methods are used to create new objects of a given class. They are special methods in that they have no return type (not even void). For the MyRectangle class, you will have two constructors. One is a default constructor this is used to create objects when no arguments are used. For MyRectangle, the default constructor will initialize all 4 of its instance variables to 0. The second constructor will have four parameters (in order): X position, Y position, new width, and new height, and it will simply initialize the instance variables from the parameters. For example, the MyRectangle above could be created by the following: MyRectangle rect = new MyRectangle(100, 50, 80, 20); and a default MyRectangle could be created by the following: MyRectangle rect = new MyRectangle();
Accessor methods allow us to get information from objects or have them perform tasks that do not alter the object themselves. There is no special designation for accessor methods; rather we label them as accessors based on what we use them for. For MyRectangle, we will have the following accessors: public int area(): returns the area of the given MyRectangle public String toString(): returns the MyRectangles information in the form of a String. See sample output for details. public boolean isInside(int x, int y): returns true if point (x, y) is inside the MyRectangle, and false otherwise.
Mutators allow us to change the data within an object. As with accessors, there is no special designator for mutators; we label them based on what we use them for. For MyRectangle, we will have the following mutators:
public void setSize(int newWidth, int newHeight): changes width and height of this MyRectangle to the values passed in.
public void setPosition(int newX, int newY): changes X and Y positions of this MyRectangle to the values passed in.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started