Java: The Rectangle class
package chapter9_9_1; /** * * @author Siavash Bakhshi */ public class Chapter9_9_1 { /** * @param args the command line arguments */ /** Main method */ public static void main(String[] args) { //Create a Rectangle with width 1 & height 1 SimpleRectangle rectangle1 = new SimpleRectangle(); System.out.println("The area of the rectangle of width " + rectangle1.getWidth() + "and height " + rectangle1.getHeight() + " is " + rectangle1.getArea()); // if you want to use setWidth or setHeight functions to change values //rectangle1.setWidth(5); //System.out.println("The area of the rectangle of width " + rectangle1.getWidth() + "and height " + rectangle1.getHeight() + " is " + rectangle1.getArea()); //Create a Rectangle with width 4 & height 40 SimpleRectangle rectangle2 = new SimpleRectangle(4, 40); System.out.println("Width: " + rectangle2.getWidth() + ", Height: " + rectangle2.getHeight() + ", Area: " + rectangle2.getArea() + ", Primeter: " + rectangle2.getPerimeter()); //Create a Rectangle with width 3.5 & height 35.9 SimpleRectangle rectangle3 = new SimpleRectangle(3.5, 35.9); System.out.println("Width: " + rectangle3.getWidth() + ", Height: " + rectangle3.getHeight() + ", Area: " + rectangle3.getArea() + ", Primeter: " + rectangle3.getPerimeter()); } } /** Construct a Rectangle with radius 1 */ class SimpleRectangle { double width; double height; /** Construct a Rectangle with a default constructor */ SimpleRectangle() { this.width = 1; this.height = 1; } /** Construct a Rectangle with a specified width & height */ SimpleRectangle(double width, double height) { this.width = width; this.height = height; } /** Return width */ double getWidth() { return width; } /** Set a new width */ void setWidth(double width) { this.width = width; } /** Return height */ double getHeight() { return height; } /** Set a new height */ void setHeight(double height) { this.height = height; } /** Return the area of this Rectangle */ double getArea() { return width * height; } /** Return the perimeter of this Rectangle */ double getPerimeter() { return 2 * (width + height); } }Java: The Rectangle class (575 downloads)