In java Define an interface called Shape with two methods that calculate perimet
ID: 3828652 • Letter: I
Question
In java Define an interface called Shape with two methods that calculate perimeter and area. Write a program to create the MyRectangle class that implements the Shape interface and contains:
The double data fields width and height with get and set methods.
A no-arg constructor that creates a default rectangle with 1 for both width and height.
A constructor that creates a rectangle with the specified width, and height.
A method getArea() that returns the area of the rectangle.
A method getPerimeter() that returns the perimeter of the rectangle.
Explanation / Answer
Shape.java
public interface Shape {
public double calculatePerimiter();
public double calculateArea();
}
MyRectangle.java
public class MyRectangle implements Shape{
private double width;
private double height;
public MyRectangle() {
this(1,1);
}
public MyRectangle(double width, double height)
{
this.width = width;
this.height = height;
}
public double calculatePerimiter() {
return 2*(width + height);
}
@Override
public double calculateArea() {
return width*height;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.