Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.3 kB
2
Indexable
Never
class Rectangle {
    private int length;
    private int width;
    
    public Rectangle(int length, int width) {
        this.length = length;
        this.width = width;
    }
    
    public int getLength() {
        return this.length;
    }
    
    public int getWidth() {
        return this.width;
    }
    
    public int calculateArea() {
        return this.length * this.width;
    }
    
    public Rectangle increaseSize(int increment) {
        this.length += increment;
        this.width += increment;
        return this; // 'this' keyword used as return statement
    }
}

public class Main {
    public static void main(String[] args) {
        Rectangle rectangle = new Rectangle(5, 7);
        
        System.out.println("Length: " + rectangle.getLength());
        System.out.println("Width: " + rectangle.getWidth());
        System.out.println("Area: " + rectangle.calculateArea());
        
        rectangle.increaseSize(3);
        
        System.out.println("Length after size increase: " + rectangle.getLength());
        System.out.println("Width after size increase: " + rectangle.getWidth());
        System.out.println("Area after size increase: " + rectangle.calculateArea());
    }
}