공부/Object-Oriented Design

Create Java classes that implement the FigureInterface interface:

혼밥맨 2022. 3. 12. 13:45
반응형

Create Java classes that implement the FigureInterface interface:

a. Square - constructor accepts a single argument of type double which indicates the length of a side of the square.

b. RightTriangle - constructor accepts two arguments of type double that indicate the lengths of the two legs.

c. IsoscelesTriangle - constructor accepts two arguments of type double that indicate the height and the base.

d. Parallelogram - constructor accepts three arguments of type double that indicate the height, the base, and the angle between the nonbase side and the base.

 

a.

public class Square implements FigureInteface {

    //length of the side of the square

    private double side;

 

    // constructor

    public Square(double side) {

        this.side = side;

    }

 

    // perimeter method

    public double perimeter() {

        return side * 4;

    }

 

    // are method

    public double area() {

        return side * side;

    }

}

 

b. 

public class RightTriangle implement FigureInterface {

    private double base;

    private double height;

    

    // constructor

    public RightTriangle(double base, double height) {

        this.base = base;

        this.height = height;

    }

 

    // perimeter method

    public double perimeter () {

        return base + height + Math.sqrt(base * base + height * height);

    }

 

    // area method

    public double area() {

        return base * height / 2;

    }

}

 

 

c. 

public class IsoscelesTriangle implements FigureInterface {

    // two sides of the triangle 

    private double base;

    private double height;

 

    // constructor

    public IsoscelesTriangle(double base, double height) {

        this.base = base;

        this.height = height;

    }

    // perimeter method

    public double perimeter () {

        // calculate the hypotenuse

        double hypotenuse = Math.sqrt(base * base + height * height);

        return 2 * hypotenuse + base;

    }

 

    // area method

    public double area() {

        return base * height / 2;

    }

}

 

d. 

public class Parallelogram implements FigureInterface { 

    private double height;

    private double base;

    private double angle;

 

    // constructor

    public Parallelogram (double height, double base, double angle) {

        this.height = height;

        this.base = base;

        this.angle = angle;

    }

 

    // perimeter method

    public double perimeter() { 

        return 2 * height * Math.sin(angle) + 2 * base;

    }

 

    // area method 

    public double area() { 

        return height * base;

    }

}

 

 

반응형