/***************************************************************** * Rectangle * * Member Variables: * int length - represents the length of the rectangle * int width - represents the width of the rectangle * * Private Methods: none * * Public Methods: * Rectangle() - constructor * Rectangle (int l, int w) - constructor * void setLength (int l) - set the rectangle's length * void setWidth (int w) - set the rectangle's width * int getLength() - returns the rectangle's length * int getWidth() - returns the rectangle's width * int computePerimeter() - returns the perimeter as an integer * int computeArea() - returns the area as an integer * void print() - prints the dimensions, perimeter, and area * Rectangle mySquare() - returns the smallest superscribed square ****************************************************************/ public class Rectangle { // data members private int length; private int width; /******************************************* * Rectangle Constructor * * Takes no parameters -- sets length and * width to 0 *******************************************/ public Rectangle () { length = 0; width = 0; } /******************************************* * Rectangle Constructor * * Takes length and width as parameters *******************************************/ public Rectangle (int l, int whatever) { length = l; width = whatever; } /******************************************* * setLength * * Takes length as a parameter and sets the * member variable length *******************************************/ public void setLength (int l) { length = l; } /******************************************* * setWidth * * Takes width as a parameter and sets the * member variable width *******************************************/ public void setWidth (int w) { width = w; } public void setDims(int l, int w) { width = w; length = l; } public String toString() { String s = "(" + length + " x " + width + " rectangle)"; return s; } /******************************************* * getLength * * Returns the rectangle's length *******************************************/ public int getLength() { return length; } /******************************************* * getWidth * * Returns the rectangle's width *******************************************/ public int getWidth() { return width; } /******************************************* * computePerimeter * * Returns the rectangle's perimeter, * computed by summing the sides *******************************************/ public int computePerimeter() { return (width*2 + length*2); } /******************************************* * computeArea * * Returns the rectangle's area, computed * by multiplying length by width *******************************************/ public int computeArea() { return (length*width); } /******************************************* * print * * Prints the length, width, perimeter, and * area of the rectangle *******************************************/ public void print() { System.out.print ("The perimeter of the " + length + "x" + width + " rectangle is " + computePerimeter() + " and the area is " + computeArea() + "\n"); } public Rectangle mySquare() { Rectangle r = new Rectangle(length, width); if (length < width) { r.setLength(width); } else { r.setWidth(length); } return r; } public void copy(Rectangle r) { length = r.getLength(); width = r.getWidth(); } public void printBox() { for (int i = 1; i <= width; i++) System.out.print("%"); System.out.println( ); for (int i = 1; i <= length - 2; i++) { System.out.print("%"); for (int j = 1; j <= width - 2; j++) System.out.print(" "); System.out.println("%"); } for (int i = 1; i <= width; i++) System.out.print("%"); System.out.println( ); } public void printBox(String s) { for (int i = 1; i <= width; i++) System.out.print(s); System.out.println( ); for (int i = 1; i <= length - 2; i++) { System.out.print(s); for (int j = 1; j <= (s.length()-1)*width - 2; j++) System.out.print(" "); System.out.println(s); } for (int i = 1; i <= width; i++) System.out.print(s); System.out.println( ); } } // end of Rectangle class