dmetal23
dmetal23

Reputation: 141

How to create an array of Rectangles with a Rectangle class?

I'm working on an assignment that is supposed to return an array of 10 rectangles with a random height, random width, and random color selected from a string.

The program works fine to return the objects for ONE rectangle, but how would I implement this to create an array of 10 rectangles and THEN return each one in a loop?

Here's my class file with my objects:

import java.util.*;

public class Rectangle 
{
    private double width;
    private double height;
    public static String color = "White";
    private Date date;

Rectangle() {
    width = 1;
    height = 1;
    date = new Date(); 
    }

Rectangle (double w, double h) {
    width = w;
    height = h;
    date = new Date();
    }

public double getHeight() {
    return height;
    }

public void setHeight(double h) {
    height = h;
    }

public double getWidth() {
    return width;
    }

public void setWidth(double w) {
    width = w;
    }

public static String getColor() {
    return color;
    }

public static void setColor(String c) {
    color = c;
    }

public Date getDate() {
    return date;
    }

public void setDate (Date d) {
    date = d; 
    }

public double getArea() {
    return width * height;
    }

public double getPerimeter() {
    return 2  * (width + height);
    }

public String toString() {
    String S;
    S = "Rectangle with width of " + width;
    S = S + " and height of " + height;
    S = S + " was created on " + date.toString();
    return S;
    }

}

Here is my client program so far. I am setting a random height and a random width and selecting a random color from the colors String.

I would like to be able to do this for an array of 10 different rectangles:

import java.util.*;

public class ClientRectangle 
{
    public static void main(String[] args)
    {
        String[] colors = {"White","Blue","Yellow","Red","Green"};

        Rectangle r = new Rectangle();
        int k;
        for(int i = 0; i < 10; i++)
        {
            r.setWidth((Math.random()*40)+10); 
            r.setHeight((Math.random()*40)+10);
            System.out.println(r.toString() + " has area of " + r.getArea() + " and perimeter of " + r.getPerimeter());
            k = (int)(Math.random()*4)+1;
            System.out.println(colors[k]);

        }



    }
}       

Thanks!

Upvotes: 0

Views: 23362

Answers (3)

Jeroen Vannevel
Jeroen Vannevel

Reputation: 44449

Create an array of rectangles and add a rectangle to each index.

Rectangle[] arr = new Rectangle[10];

for(int i = 0; i < 10; i++)
{
 Rectangle r = new Rectangle();
 r.setWidth((Math.random()*40)+10); 
 r.setHeight((Math.random()*40)+10);

 arr[i] = r;
}

Upvotes: 6

Code-Apprentice
Code-Apprentice

Reputation: 83547

You already seem to know how to declare an array of objects because you did so with an array of Strings. The declartion for an array of Rectangles is very similar:

Rectangle[] rects;

Note that you must also initialize the array. Since arrays are themselves objects you use the new operator, somewhat like when you initialize a reference variable to a single Rectangle:

Rectangle[] rects = new Rectangle[SIZE];

The only difference, as you can see, is that you put the size of the array inside the []s. This only creates an array of references to Rectangle objects. The references are all automatically set to null. This means that you need to create the Rectangles themselves. You can do this in for loop:

for (int i = 0; i <= SIZE; ++i) {
    rects[i] = new Rectangle();
}

You can also set the width and height of each Rectangle as you want. (I will leave the exact details about how to do this to the reader.)

Finally, you need to put this all in a method other than main() so that you can return the whole array (You don't return one Rectangle at a time) as the instructions state:

return rects;

Upvotes: 0

Pradeep Pati
Pradeep Pati

Reputation: 5919

Move the Rectangle r = new Rectangle(); inside the for loop. Initialise a Array(List) outside the loop, and keep adding the Rectangles in the loop to the array.

Upvotes: 0

Related Questions