package org.edu2act.figure;
public abstract class Shape {
public abstract void area() ;
public abstract void perimeter();
}
package org.edu2act.figure;
public class Circle extends Shape{
private double r;
public Circle() {}
public Circle(double R) {
if(R<0){
System.out.println("非法的输入");
}
else{
r=R;
}
}
public void area() {
System.out.println("圆的面积为:"+3.14*r*r);
}
public void perimeter() {
System.out.println("圆的周长为:"+2*3.14*r);
}
}
package org.edu2act.figure;
public class Rectangle extends Shape{
private double l;
private double d;
public Rectangle() {}
public Rectangle(double l,double d) {
if(l<0 || d<0) {
System.out.println("非法的输入);
}
else{
this.l=l;
this.d=d;
}
}
public void area() {
System.out.println("长方形的面积为:"+l*d);
}
public void perimeter() {
System.out.println("长方形的周长为:"+(l+l+d+d));
}
}
package org.edu2act.figure;
public class Triangle extends Shape {
private double a;
private double b;
private double c;
public Triangle() {}
public Triangle(double a,double b,double c) {
if(a+b<=c || a+c<=b || b+c<=a || a<0 || b<0 || c<0) {
System.out.println("非法的输入");
}
else {
this.a=a;
this.b=b;
this.c=c;
}
}
public void area() {
double s=(a+b+c)/2;
double S=Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.println("三角形的面积为:"+S);
}
public void perimeter() {
System.out.println("三角形的周长为:"+(a+b+c));
}
}
package org.edu2act.figure;
public class ShapeText {
interface distance{
double getDistance();
}
public static void main(String[] args) {
Circle m=new Circle(4.0);
Triangle M=new Triangle(3.0,4.0,5.0);
Rectangle N=new Rectangle(3.0,4.0);
m.area();
M.area();
N.area();
m.perimeter();
M.perimeter();
N.perimeter();
}
}