首页 > 代码库 > Demo1

Demo1

package figure;

public class circular {
private double r;
circular(){}
public void set(double r){this.r = r;}
public double get_r(){return r;}
public double perimeter(){return 3.14*2*r;}
public double area(){return 3.14*r*r;}
}

 

package figure;

public class rectangular {
private double a, b;
rectangular(){}
public void set(double a,double b){
this.a = a;this.b = b;
}
public double get_a(){return a;}
public double get_b(){return b;}
public double perimeter(){return 2*(a+b);}
public double area(){return a*b;}
}

 

package figure;

public class triangle {
private double a, b, c;
triangle(){}
public void set(double a,double b,double c){this.a = a;this.b = b;
this.c = c;}
public double get_a(){return a;}
public double get_b(){return b;}
public double get_c(){return c;}
public double perimeter(){return a+b+c;}
public double area(){
double p = 0.5*(a+b+c);
return Math.sqrt(p*(p-a)*(p-b)*(p-c));
}
}

 

 

 

package figure;

public class Main {
public static void main(String args[]){
circular cir = new circular();
rectangular rec = new rectangular();
triangle tri = new triangle();
cir.set(1);
rec.set(2, 3);
tri.set(4, 5, 6);
System.out.println("三角形的三条边分别是"+tri.get_a()+" "+tri.get_b()+" "
+tri.get_c());
System.out.println("三角形的周长是"+tri.perimeter());
System.out.println("三角形的面积是"+tri.area());
System.out.println("矩形的周长是"+rec.perimeter());
System.out.println("矩形的面积是"+rec.area());
System.out.println("圆的半径是"+cir.get_r());
System.out.println("圆的周长是"+cir.perimeter());
System.out.println("圆的面积是"+cir.area());
}
}

Demo1