Rectangle Class in Java


This class was written for an article discussing the Basics of Classes in Java.


import java.util.Scanner;

public class Rectangle{

	private double length;
	private double width;

	public Rectangle(){
		this.length = 1.0;
		this.width = 1.0;
	}

	public Rectangle(double length, double wide){

		this.length = length;
		this.width = wide;
	}

	//getters
	public double getLength(){
		return this.length;
    }

    public double getWidth(){
		return this.width;
    }

    //setters
    public void setLength(double length){
		if((length >= 1.0) && (length <= 10.0))             this.length = length; 	}      	public void setWidth(double width){ 		if((width >= 1.0) && (width <= 10.0))             this.width = width; 	}          //other behaviors 	public double perimeter(){ 		return 2 * (this.length + this.width);     }      	public double area(){ 		return this.getLength() * this.getWidth();     }          public String toString(){         return "Length: " + this.length +"\tWidth: " + this.width;     }     //test main     public static void main (String[] args) 	{          		Scanner userInput = new Scanner(System.in); 		         Rectangle r = new Rectangle(); 		 		System.out.print("Enter a length >>> ");
		r.setLength(userInput.nextDouble());
		System.out.print("Enter a width >>> ");
		r.setWidth(userInput.nextDouble());

        System.out.println("The Rectangle with the following attributes: \n" + r);
		System.out.println("Has perimeter is:  " + r.perimeter() + " and area is:  " + r.area());

    }

}