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;
}
}
'공부 > Object-Oriented Design' 카테고리의 다른 글
XML, JSON 차이 (0) | 2022.07.09 |
---|---|
애플리케이션 현대화(Application Modernization)이란 (0) | 2022.07.06 |
3.8 – Model Checking (0) | 2021.09.28 |
3.7 – UML State Diagram (0) | 2021.09.27 |
3.6 – UML Sequence Diagram (0) | 2021.09.26 |
댓글