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.
Note... It must follow this method
// CS 0401 Lab 05 Driver Program // Compile and run this program to test your MyRectangle class. // Compare the output of your program with the provided file // lab5out.txt
public class Lab05 { public static void testInside(MyRectangle R, int x, int y) { System.out.print("Point (" + x + "," + y + ")"); if (R.isInside(x, y)) System.out.println(" is INSIDE " + R); else System.out.println(" is OUTSIDE " + R); }
public static void main(String [] args) { MyRectangle R1, R2, R3;
R1 = new MyRectangle(100, 50, 80, 20); R2 = new MyRectangle(); R3 = new MyRectangle(60, 60, 100, 100); // In Java, when Objects are printed (as shown below), the toString() // method is implicitly called. Thus the statements below will call // toString() for each of the three MyRectangle objects System.out.println("R1: " + R1 + " Area: " + R1.area()); System.out.println("R2: " + R2 + " Area: " + R2.area()); System.out.println("R3: " + R3 + " Area: " + R3.area());
int x1 = 120, y1 = 70; int x2 = 130, y2 = 110;
// Verify with a pencil and paper which of these should be true and // which should be false. testInside(R1, x1, y1); testInside(R3, x1, y1); testInside(R1, x2, y2); testInside(R3, x2, y2);
R1.setSize(120, 240); R3.setPosition(400, 350); testInside(R1, x2, y2); testInside(R3, x2, y2); } }
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