Untitled
plain_text
a month ago
1.8 kB
1
Indexable
Never
// Step 1: Create the Shape interface public interface Shape { double calculateArea(); } // Step 2: Implement the RectangleAdapter class public class RectangleAdapter implements Shape { private Square square; private double width; private double length; public RectangleAdapter(Square square, double width, double length) { this.square = square; this.width = width; this.length = length; } @Override public double calculateArea() { // Calculate the area of a rectangle using width and length return width * length; } } // Existing Square class public class Square { private double sideLength; public Square(double sideLength) { this.sideLength = sideLength; } public double getSideLength() { return sideLength; } public double calculateArea() { return sideLength * sideLength; } } // Main application public class Main { public static void main(String[] args) { // Create a Square object Square square = new Square(5); // Assume a side length of 5 // Create a RectangleAdapter object, adapting the Square to work with rectangles RectangleAdapter rectangleAdapter = new RectangleAdapter(square, 4, 6); // Example width and length // Calculate the area of the square using the adapter double squareArea = square.calculateArea(); System.out.println("Area of the square: " + squareArea); // Calculate the area of a rectangle using the same adapter double rectangleArea = rectangleAdapter.calculateArea(); System.out.println("Area of the rectangle: " + rectangleArea); } }