Java小程序之计算三角形/圆形/矩形的周长和面积

题目:用Java编写一个计算随意给定值的三角形/圆形/矩形的周长和面积
代码如下:
文件名:Shape.java

/**
 * 抽象类Shape 是其他三个形状的父类 
 * 其他三个类要继承重写getArea()和getPerimeter()方法
 *
 */
public abstract class Shape {
    public static final double PI=3.14;

    public abstract double getArea();

    public abstract double getPerimeter();
}

文件名:Circle.java

/**
 * 继承Shape类
 *
 */
public class Circle extends Shape{

    double r;
    public Circle(double r){
        this.r=r;
    }

    @Override   
    public double getArea(){
        return Shape.PI*r*r;
    }

    @Override
    public double getPerimeter(){
        return Shape.PI*r*2;
    }

}

文件名:Rectangle.java

/**
 * 继承Shape类
 *
 */

public class Rectangle extends Shape {

    double m, n;

    public Rectangle(double m, double n) {

        this.m = m;
        this.n = n;

    }

    @Override
    public double getArea() {
        return m * n;
    }

    @Override
    public double getPerimeter() {
        return 2 * (m + n);
    }

}

文件名:Triangle.java

/**
 * 继承Shape类
 *
 */
public class Triangle extends Shape {

    double a, b, c;

    public Triangle(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    @Override
    public double getArea() {
        double p = (a + b + c) / 2;

        if ((a + b) > c && (a + c) > b && (b + c) > c) {

            return Math.sqrt(p * (p - a) * (p - b) * (p - c));

        } else {

            System.out.println("wrong values");
            return -1;

        }
    }

    @Override
    public double getPerimeter() {
        if ((a + b) > c && (a + c) > b && (b + c) > c) {

            return a + b + c;

        } else {

            System.out.println("wrong values");
            return -1;

        }

    }
}

文件名:Test.java

package task.daily.April.TwentyFirst;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("choose the shape which is the one you want calculate it's area and Perimeter:");
        System.out.println("1.Circle\t2.Triangle\t3.Rectangle");
        try {
            int choice = sc.nextInt();
            switch (choice) {
            case 1:
                System.out.println("input r:");
                try {
                    double r = sc.nextDouble();
                    Shape s = new Circle(r);
                    System.out.println("the area of the circle is :" + s.getArea()
                            + "\nthe perimeter of the circle is :" + s.getPerimeter());
                } catch (Exception e) {
                    System.out.println("wrong");
                }
                break;
            case 2:
                System.out.println("input a,b,c:");
                double a, b, c;
                try {
                    a = sc.nextDouble();
                    b = sc.nextDouble();
                    c = sc.nextDouble();
                    Shape s = new Triangle(a, b, c);
                    System.out.println("the area of the circle is :" + s.getArea()
                            + "\nthe perimeter of the circle is :" + s.getPerimeter());

                } catch (Exception e) {
                    System.out.println("wrong");
                }
                break;
            case 3:
                System.out.println("input m,n:");
                double m, n;
                try {
                    m = sc.nextDouble();
                    n = sc.nextDouble();
                    Shape s = new Rectangle(m, n);
                    System.out.println("the area of the circle is :" + s.getArea()
                            + "\nthe perimeter of the circle is :" + s.getPerimeter());

                } catch (Exception e) {
                    System.out.println("wrong");
                }
                break;
            }
        } catch (Exception e) {
            System.out.println("please choose a correct number");
        }
        sc.close();
    }
}

通过子类继承父类和子类的构造方法对不同的形状需要的值进行赋值,对继承和抽象类做了一个小小的应用


初学Java,欢迎大家对错误批评指正,指点迷津

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值