rec2

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.3 kB
0
Indexable
Never
public class JavaApplication5 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Rec r1 = new Rec();
        
r1.setLength(2.0);
r1.setWidth(4.0);
		System.out.printf("Rectangle length = %,.1f%n", r1.getLength());
	System.out.printf("Rectangle width = %,.1f%n", r1.getWidth());
        System.out.printf("Rectangle area = %,.1f%n", r1.area());
	System.out.printf("Rectangle perimiter = %,.1f ", r1.perimiter());
    }
    
}

public class Rec {
  private double length;
	private double width;
      
    public double getLength() {
        return length;
    }

    public void setLength(double length) {
        if (length <= 0.0 || length >= 20.0)
			throw new IllegalArgumentException("length must be 0-20");
        this.length = length;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        if (width <= 0.0 || width >= 20.0)
			throw new IllegalArgumentException("length must be 0-20");
        this.width = width;
    }
        
    public double area() {
		return getWidth() * getLength();
	}

	public double perimiter() {
		return 2 * (getWidth() + getLength());
	}
}