Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Simple JAVA programming Assignment Please help with the RectangleTester Rectangle.Java // This class defines a rectangle public class Rectangle { // Create two private integer

Simple JAVA programming Assignment Please help with the RectangleTester

Rectangle.Java

// This class defines a rectangle public class Rectangle { // Create two private integer variables, one called x and one called y. // These will store the upper left corner point of the rectangle private int x, y; // Create two private integer variables called width and height private int width, height; // Create a private static integer variable called numRectangles and initialize it to 0 // This variable will keep a count of the number of Rectangle objects created private static int numRectangles = 0; // Create a default constructor method with no parameters that initializes instance variables // x and y above to 0, width to 1.0 and height to 1.0 // Don't forget to increment the numRectangles variable public Rectangle() { x = 0; y = 0; width = 1; height = 1; numRectangles += 1; } // Create a constructor method that takes two integer parameters, one to set instance variable x and // one to set instance variable y. Set the width and height instance variables to 1.0 // *One* way to do this is to use the "this" keyword and first call the default constructor method // above and then set instance variables x,y using the parameters // Make sure the numRectangles variable has been incremented only once public Rectangle(int xInput, int yInput) { this(); this.x = xInput; this.y = yInput; } // Create a constructor method that takes four parameters, one for instance variable x, one for y, // one for width and one for height // Don't forget to increment the numRectangles variable public Rectangle(int xInput, int yInput, int wInput, int hInput) { x = xInput; y = yInput; width = wInput; height = hInput; } // Create a static method that returns the number of Rectangle objects created public static int getRectangles() { return numRectangles(); } // Create an accessor method (i.e. get method) for the width instance variable public int getWidth() { return width; } // Create an accessor method (i.e. get method) for the height instance variable public int getHeight() { return height; } // Create a mutator method (i.e. "set" method) to set the width // The set method should have one parameter (width). Don't forget that if your // parameter variable name is width then use the this. operator to initialize the width // instance variable public void setWidth(int wInput) { width = wInput; } // Create a mutator method (i.e. "set" method) to set the height // The set method should have one parameter (height) public void setHeight(int hInput) { height = hInput; } // Create an accessor method (i.e. get method) for the x instance variable public int getX() { return x; } // Create an accessor method (i.e. get method) for the y instance variable public int getY() { return y; } // Create an instance method called getArea() to compute the area of this Rectangle object // The method should return an integer value public int getArea() { return height * weight } // Create a static method also called getPerimeter() that takes two integer parameters - // one for width and one for height // This method uses the given parameters to compute and return the length of the perimeter // of a rectangle with the given width and height public static int getPerimeter(int wInput, int hInput) { return (wInput * 2) + (hInput * 2); } // Create a method called getDescription that returns a String // The string returned contains a concatenation of: // The string "Rectangle[ Width = " followed by the width of this Rectangle object followed by // the string " Height = " followed by the height of this Rectangle object followed by the string // " X = " followed by the x value of this Rectangle object followed by the string " Y = " followed by // the y value of this Rectangle followed by the string "]" // NOTE: do not call System.out.println() in here, return a String!!!! public string getDescription() { return "Rectangle[ Width = " + width + " Height = " + height + " X = " + x + " Y= " + y + "]"; } }

Please help with the RectangleTester

public class RectangleTester { public static void main(String[] args) { // Create a new Rectangle object called r // i.e. r is a reference variable that "points to" the Rectangle object // Use the default constructor i.e. no parameters Rectangle r = new Rectangle(); // Call the setWidth and setHeight methods of class Rectangle to set the width to 10 // and the height to 5 of Rectangle object r // Use the getDescription() method of class Rectangle and print out the data stored in r // Expected Output: Rectangle[Width = 10 Height = 5 X = 0 Y = 0] r.getDescription(); // Get area of Rectangle r using the instance method getArea() in class Rectangle // Print out "Area = " followed by the area // Expected: Area = 50 // Get and then print the perimeter length of a rectangle with width 20 and height 10 using // class Rectangle's static method getPerimeter() // Print out "Perimeter using static method =" followed by the perimeter length // Expected: Perimeter using static method = 60 // Create an ArrayList of Rectangle object references // You will need to import java.util.ArrayList // Add the r Rectangle object to the array list (using the add method of class ArrayList) and // then create 4 more new Rectangle objects, add each to the array list // Use the following Rectangle parameters: // x=2,y=3, width = 4 height = 2 // x=4,y=7, width = 10 height = 20 // x=6,y=5, width = 8 height = 4 // x=0,y=8, width = 16 height = 10 // Print a message "Number of Rectangles = " followed by the number of Rectangle objects by calling the // static method getNumRectangles() of class Rectangle // Expected output: "Number of Rectangles = 5" // Use a for loop and print out the data stored in the 5 Rectangle objects, one Rectangle per line // Expected Output: // Rectangle[Width = 10 Height = 5 X = 0 Y = 0] // Rectangle[Width = 4 Height = 2 X = 2 Y = 3] // Rectangle[Width = 10 Height = 20 X = 4 Y = 7] // Rectangle[Width = 8 Height = 4 X = 6 Y = 5] // Rectangle[Width = 16 Height = 10 X = 0 Y = 8] } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Describe Watsons attempts to popularize the science of behavior?

Answered: 1 week ago