Exercise5_Rectangle.java

← Back to Course
Java Source File Download
/**
 * EXERCISE 5: Rectangle Class (Geometry)
 * PT821: Object-Oriented Programming - Classes and Objects
 *
 * TASK: Complete the Rectangle class for geometric calculations
 *
 * Requirements:
 * 1. Add the following private attributes:
 *    - length (double)
 *    - width (double)
 *    - color (String)
 *    - isFilled (boolean)
 *
 * 2. Create constructors:
 *    - Default constructor (length=1, width=1, color="white", isFilled=false)
 *    - Constructor with length and width only
 *    - Full parameterized constructor
 *
 * 3. Add getters for all attributes
 *
 * 4. Add setters with validation:
 *    - length must be > 0
 *    - width must be > 0
 *
 * 5. Add behavior methods:
 *    - calculateArea() - returns length * width
 *    - calculatePerimeter() - returns 2 * (length + width)
 *    - calculateDiagonal() - returns sqrt(length^2 + width^2)
 *    - isSquare() - returns true if length equals width
 *    - resize(double factor) - multiplies both dimensions by factor
 *    - compare(Rectangle other) - returns "larger", "smaller", or "equal" based on area
 *    - displayInfo() - shows all rectangle details with calculations
 *
 * 6. Test your class with at least 3 rectangles
 *
 * HINT: Use Math.sqrt() for square root and Math.pow() for power
 */
public class Exercise5_Rectangle {

    // TODO: Add private attributes here


    // TODO: Add default constructor


    // TODO: Add constructor with length and width


    // TODO: Add full parameterized constructor


    // TODO: Add getters


    // TODO: Add setters with validation


    // TODO: Add calculateArea() method


    // TODO: Add calculatePerimeter() method


    // TODO: Add calculateDiagonal() method


    // TODO: Add isSquare() method


    // TODO: Add resize() method


    // TODO: Add compare() method


    // TODO: Add displayInfo() method


    public static void main(String[] args) {
        System.out.println("=== Rectangle Geometry Exercise ===\n");

        // TODO: Create rect1 using default constructor


        // TODO: Create rect2 with length=10, width=5


        // TODO: Create rect3 as a square with length=7, width=7, color="blue", filled=true


        // TODO: Display info for all rectangles


        // TODO: Check which rectangles are squares


        // TODO: Compare rect1 with rect2


        // TODO: Resize rect1 by factor of 3


        // TODO: Display rect1 info after resize


        // TODO: Calculate and display total area of all rectangles


        System.out.println("\n=== Exercise Complete ===");
    }
}