In: Computer Science
Define an interface called Shape that requires methods to get x and y, and a method to calculate the area. Call these methods getX(), getY(), and getArea(). Write a class called MyRectangle that implements the Shape interface and contains:
the double data fields x, y , and width with get and set methods
a no-arg constructor that creates a default rectangle with 0 for both x and y and 1 for both length and width
a constructor that creates a rectangle with the specified x, y , length, width
a method getX() that returns x location value of the rectangle
a method getY() that returns y location value of the rectangle
a method getArea() that returns the area of the rectangle
//Shape.java
public interface Shape {
public double getX();
public double getY();
public double getArea();
}
//////////////////////////////////////////////
//MyRectangle.java
public class MyRectangle implements Shape {
private double x,y, width, length;
public MyRectangle() {
x = 0;
y = 0;
width = 1;
length = 1;
}
public MyRectangle(double x, double y, double width, double length) {
this.x = x;
this.y = y;
this.width = width;
this.length = length;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getArea() {
return width*length;
}
}