关于Java的问题?
圆形类:
package Demo;
public class Circle {
public double getCircleCircumference(double r){
return 2*r*Math.PI;
}
public double getCircleSquare(double r){
return Math.PI*r*r;
}
}
矩形类:
package Demo;
public class Rectangle {
public double getRectangleCircumference(double length,double width){
return 2*(length+width);
}
public double getRectangleSquare(double length,double width){
return length*width;
}
}
三角形类:
package Demo;
public class Triangle {
public double getTriangleCircumference(double a,double b,double c){
return a+b+c;
}
public double getTriangleSquare(double a,double b,double c){
double p = (a+b+c)/2;
double q = p*(p-a)*(p-b)*(p-c);
return Math.sqrt(q);
}
}
测试:
package Demo;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Circle circle = new Circle();
Rectangle rectangle = new Rectangle();
Triangle triangle = new Triangle();
double r;
double length;
double width;
double a;
double b;
double c;
System.out.println("请输入圆形半径:");
Scanner sc = new Scanner(System.in);
r = sc.nextDouble();
double circleCircumference = circle.getCircleCircumference(r);
double circleSquare = circle.getCircleSquare(r);
System.out.println("圆形周长是"+String.format("%.2f",circleCircumference)+",面积是"+String.format("%.2f",circleSquare)+"\n");
System.out.println("请输入长方形长:");
length = sc.nextDouble();
System.out.println("请输入长方形宽:");
width = sc.nextDouble();
double rectangleCircumference = rectangle.getRectangleCircumference(length, width);
double rectangleSquare = rectangle.getRectangleSquare(length, width);
System.out.println("矩形周长是"+String.format("%.2f",rectangleCircumference)+",面积是"+String.format("%.2f",rectangleSquare)+"\n");
System.out.println("请输入三角形第一条边长:");
a =sc.nextDouble();
System.out.println("请输入三角形第二条边长:");
b = sc.nextDouble();
System.out.println("请输入三角形第三条边长:");
c = sc.nextDouble();
double triangleCircumference = triangle.getTriangleCircumference(a, b, c);
double triangleSquare = triangle.getTriangleSquare(a, b, c);
System.out.println("三角形周长是"+String.format("%.2f",triangleCircumference)+",面积是"+String.format("%.2f",triangleSquare)+"\n");
}
}
测试结果: