I have been trying to do these program for so long I think I almost got it all but there are some things I am not sure about,
so I am trying to print the number of rectangles created, but I do not understand how to do it, because I have defined already the number of rectangles created (10). Also I need to find and print the information of a rectangle with the largest area, I do not know how to do it . I am really new at these, I would really apreciate some help
these is my main class
public class Rectangle{ private double width, height; private static String color ="white"; private Date date; private static int rectangleCreated = 0; /// why is static? Rectangle(){ // no arg constructor that creates a default rectangle width =1; height =1; rectangleCreated++; date: new Date(); } Rectangle( double w, double h, String c ){ // CONSTRUCTOR width = w; height = h; color = c; date = new Date(); rectangleCreated++; } public static int rectangleCreated(){ // a method that returns number of rectangle created return rectangleCreated; } public double getHeight(){ return height; } public void setHeight ( double h) { height = h; } 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(){ // returns area return width*height; } public double getPerimeter(){ // returns perimeter return (2*width* height); } public String toString(){ String S; S = "Rectangle width of" + width; S = S + "and height of" +height; S = S + " was created on " + date.toString(); return S; } }
and these the client program I am working on
import java.util.*; public class TestRectangle{ public static void main (String[]args) { String[] colors = {"White","Blue","Yellow","Red","Green"}; int k; 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; k = (int)(Math.random()*4)+1; System.out.println(colors[k]); } } }