public class shape implements area{
public double Area;
public double getArea(){
return Area;
}
public void setArea(){
}
public void setArea(double Area){
this.Area=Area;
}
public double calcArea(){//not good function
assert Area<=0 : "wrong shape";
return Area;
}
public void printInfo(){
System.out.println("This is shape: Area="+getArea());
}
}
public interface area {
public double getArea();
public double calcArea();
public void setArea(double Area);
}
public class square extends shape implements area{
private double sideLength;
public square(double sideLength){
this.sideLength=sideLength;
setArea();
}
public double getSideLength(){
return sideLength;
}
@Override
public void setArea() {
// TODO Auto-generated method stub
super.setArea(calcArea());
}
@Override
public double calcArea() {
assert sideLength>0 : "wrong sideLength";
// TODO Auto-generated method stub
return sideLength*sideLength;
}
public void printInfo(){
System.out.println("This is square:"+" sideLength="+sideLength+", Area="+getArea());
}
}
public class rectangular extends shape implements area{
private double width,height;
public rectangular(double width,double height){
this.width=width;this.height=height;
setArea();
}
public double getWidth(){
return width;
}
public double getHeight(){
return height;
}
@Override
public void setArea() {
// TODO Auto-generated method stub
super.setArea(calcArea());
}
@Override
public double calcArea() {
assert width > 0 : "wrong width ";
assert height > 0 : "wrong height ";
// TODO Auto-generated method stub
return width *height;
}
public void printInfo(){
System.out.println("This is rectangular:"+" width="+width+", height="+height+", Area="+getArea());
}
}
public class circle extends shape implements area{
private double radius;
private final double pi = 3.1415926;
public circle(double radius){
this.radius = radius;
setArea();
}
public double getRadius(){
return radius;
}
@Override
public void setArea() {
// TODO Auto-generated method stub
super.setArea(calcArea());
}
@Override
public double calcArea() {
assert radius > 0 : "wrong radius";
// TODO Auto-generated method stub
return pi*radius*radius;
}
public void printInfo(){
System.out.println("This is circle:"+" radius="+radius+", Area="+getArea());
}
}
public class shapeTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
shape myShape=new shape();
myShape.printInfo();
square mySquare=new square(1);
mySquare.printInfo();
rectangular myRectangular=new rectangular(2,3);
myRectangular.printInfo();
circle myCircle=new circle(4);
myCircle.printInfo();
shape myUnknown=(shape)new circle(10);
myUnknown.printInfo();
}
}