Doing some deeper study on classes and objects. The object here is to get the dimensions of a rectangle, and give the area and perimeter of the rectangle. the tester is giving me a compilation error:
RectangleTest.java:14: non-static method getDimensions() cannot be referenced from a static context
System.out.println(Rectangle.getDimensions());
Can somebody give me some insight?
Thanks.
//Jeremiah A. Walker //Find the area and perimeter of a rectangle public class Rectangle { private int width = 1; private int length = 1; private int area; private int perimeter; //method to set dimensions for our rectangle; public void setDimensions(int w, int l) { width = w; length = l; area = w * l; perimeter = 2 * l * w; }//end method setDimensions public String getDimensions() { return String.format("%02d:%02d:%02d:%02d", width, length, area, perimeter); }//end method getDimensions }//end class Rectangle
//Jeremiah A. Walker //Test Rectangle class public class RectangleTest { public static void main(String[] args) { //create and initialize a Rectangle object Rectangle rect1 = new Rectangle();//invokes Rectangle constructor //output string representations of the time rect1.setDimensions(10,10); System.out.print("The dimensions of this rectangle are: "); System.out.println(Rectangle.getDimensions()); }//end main }//end class RectangleTest