client program
so basically these is it,
it is suppost to first
create an array of type string as the one I did
then create 10 rectangles with random height and width and color ( the color I couldnīt figure it out yet, but I am getting ther noe)
then print the number of rectangles created which basically does but, in a road, which is not good, but nvm I canfix that.
the PROBLEM I truly need help with is I am trying to let the code print all the rectangles and including their areas and perimeters
however I try to do it, but I believe I am doing something wrong because I debugged it and it ends it at the very first try it is testing it, also I am nt sure whether If I am suppost to use the for loop like that, because I am printing the rectangles previwesly created.
also I am required to print the information of the rectangle with the largest area, but I am not sure how to
these is my class and then goes my client program follow by the current output
THANK YOU!!!! honeslty I am really new at these and really appreciate some help.
import java.util.*; public class Rectangle{ private double width, height; private static String color ="white"; private Date date; private static int rectangleCreated = 0; 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; } }
import java.util.*; public class TestRectangle{ public static void main (String[]args) { String[] colors = {"White","Blue","Yellow","Red","Green"}; Rectangle[] array = new Rectangle[10]; // rectangle of 10 for(int i = 0; i < 10; i++) { Rectangle r = new Rectangle(); r.setWidth((Math.random()*40)+10); r.setHeight((Math.random()*40)+10); // colors[r.nextString(colors.length)]; // print number of rectangles created System.out.println(Rectangle.rectangleCreated()); } // area of rectangles created for(int i = 0; i < 10; i++) { Rectangle r = new Rectangle(); System.out.println( r.toString()+ "has area of " + r.getArea()); //" and perimeter of" + r.getPerimeter() ); } // g through the array and find the information of a rectangle with largest area. } }
OUTPUT
[COD ----jGRASP exec: java TestRectangle
1
2
3
4
5
6
7
8
9
10
Exception in thread "main" java.lang.NullPointerException
at Rectangle.toString(Rectangle.java:101)
at TestRectangle.main(TestRectangle.java:35)
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
E][/CODE]