代码如下:
创新互联主营古交网站建设的网络公司,主营网站建设方案,app软件开发,古交h5小程序制作搭建,古交网站营销推广欢迎古交等地区企业咨询
abstract class Geometry {
abstract double getArea();
}
// 三角形
class Triangle extends Geometry {
// 边
private double a;
// 边
private double b;
// 边
private double c;
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
@Override
double getArea() {
double p = (a + b + c) / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
}
// 圆
class Circle extends Geometry {
// 半径
private double r;
public Circle(double r) {
this.r = r;
}
@Override
double getArea() {
return Math.PI * r * r;
}
}
// 梯形
class Ladder extends Geometry {
// 上底
private double a;
// 下底
private double b;
// 高
private double h;
public Ladder(double a, double b, double h) {
this.a = a;
this.b = b;
this.h = h;
}
@Override
double getArea() {
return (a + b) * h / 2;
}
}
class Test {
double computeGeometryArea(Geometry g) {
return g.getArea();
}
}
public class Demo {
public static void main(String[] args) {
Test test = new Test();
Triangle triangle = new Triangle(3, 4, 6);
System.out.println("三角形面积:" + test.computeGeometryArea(triangle));
Circle circle = new Circle(10);
System.out.println("圆面积:" + test.computeGeometryArea(circle));
Ladder ladder = new Ladder(10, 20, 15);
System.out.println("梯形面积:" + test.computeGeometryArea(ladder));
}
}
运行结果:
扇形的周长和面积计算如下:
public class Sector {
Scanner scanner = new Scanner(System.in);
double pi = Math.PI;
// 计算扇形面积和周长的方法
public void sector() {
// 手动输入半径和角度
System.out.println("请输入扇形的半径");
double r = scanner.nextDouble();
System.out.println("请输入扇形的角度");
double n = scanner.nextDouble();
// 求扇形面积
double area = (n * pi * r * r) / 360;
System.out.println("扇形的面积是:" + area);
// 求扇形周长
double perimeter0 = 2 * r + (n * pi * r) / 180;
double perimeter = Math.floor(perimeter0);
System.out.println("扇形周长是:" + perimeter);
}
// 测试方法
public static void main(String[] args) {
Sector sector = new Sector();
sector.sector();
}
}
测试结果:
这个是数组越界的错误,我刚运行了下,只要传入命令行参数,就可以运行成功,具体方法是,右键-run as-run configuration,选择Arguments,在 program arguments 里面填出两个数字,用空格隔开,点击run按钮,就可以运行了。
给你修改好了..
public class Test {
public static void main(String[] args) {
Shape shape = new Triangle(10, 6);
int area = shape.computeArea();
System.out.println("triangle:" + area);
shape = new Rectangle(10, 10);
area = shape.computeArea();
System.out.println("Rectangle:" + area);
}
}
abstract class Shape {
public abstract int computeArea();
}
class Triangle extends Shape {
int width;
int heigth;
public Triangle(int width, int heigth) {
this.width = width;
this.heigth = heigth;
}
public int computeArea() {
return (width * heigth) / 2;
}
}
class Rectangle extends Shape {
int width;
int heigth;
public Rectangle(int width, int heigth)
{
this.width=width;
this.heigth=heigth;
}
public int computeArea(){
return this.width * this.heigth;
}
}
售后响应及时
7×24小时客服热线数据备份
更安全、更高效、更稳定价格公道精准
项目经理精准报价不弄虚作假合作无风险
重合同讲信誉,无效全额退款