IShape.java
---------------------------
public interface IShape {
double area();
}
Circle.java
--------------------------
public class Circle implements IShape{
private double radius;
public Circle() {}
public Circle(double radius) {
super();
this.radius = radius;
}
public double area() {
return radius*radius*3.14;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
}
Rect.java
-------------------------------------
public class Rect implements IShape {
private double l;
private double h;
public Rect() {
}
public Rect(double l, double h) {
super();
this.l = l;
this.h = h;
}
public double area() {
return l*h;
}
public double getL() {
return l;
}
public void setL(double l) {
this.l = l;
}
public double getH() {
return h;
}
public void setH(double h) {
this.h = h;
}
}
TestShape.java
-------------------------------------
public class TestShape {
public static void main(String[] args) {
IShape circle = new Circle(3);
IShape rect = new Rect(3,4);
if (circle.area()>rect.area() ) {
System.out.println("circle's area is larger than rect's");
}else {
System.out.println("rect's area is larger than circle's");
}
}
}