public class Box { private double width; private double height; private static int boxCount = 0; public Box() { boxCount++; } public Box(double width, double height) { this.width = width; this.height = height; boxCount++; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } // toString method to display box information @Override public String toString() { return "Box{" + ", width=" + width + ", height=" + height + '}'; } public static int getBoxCount() { return boxCount; } } public class BoxDemo { public static void main(String[] args) { Box box1 = new Box(); Scanner sc=new Sacnner(System.in); Box[] boxarr=new Box[5]; System.out.println("Number of boxes created: " + Box.getBoxCount()); for(int i=0;i<boxarr.length;i++){ System.out.println("Enter "+(i+1)+" height width); double n1=sc.nextInt(); double n2=sc.nextInt(); boxarr[1]=new Box(n1,n2); } System.out.println("Boxes in array are: "); for(Box box:boxarr){ System.out.println(box); } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter