编写一个完整的Java Application 程序。包含类Circle、Cylinder、Main,具体要求如下。

(1)编写类Circle,表示圆形对象,包含以下成员
①属性:
1)  radius:私有,double型,圆形半径;
②方法:
1)  Circle(double radius), 构造方法,用参数设置圆的半径
2)  Circle(),构造方法,将圆形初始化为半径为0。
3)  void setRadius(double r):用参数r设置radius的值
4)  double getRadius():返回radius的值
5)  double getArea(),返回圆形的面积
6)  double getPerimeter(),返回圆形的周长
7)  public String toString( ),将把当前圆对象的转换成字符串形式,例如圆半径为10.0,返回字符串"Circle(r:10.0)"。
(2)编写一个类Cylinder,表示圆柱形对象,包含以下成员
①属性:
1)  height:私有,double型,圆柱体高度;
2)  circle:私有,Circle类型,圆柱体底面的圆形;
②方法:
1)  Cylinder(double height,Circle circle), 构造方法,用参数设置圆柱体的高度和底面的圆形
2)  Cylinder(),构造方法,将圆柱体的高度初始化为0,底面圆形初始化为一个半径为0的圆形。
3)  void setHeight(double height):用参数height设置圆柱体的高度
4)  double getHeight():返回圆柱体的高度
5)  void setCircle(Circle circle):用参数circle设置圆柱体底面的圆形
6)  Circle getCircle():返回圆柱体底面的圆形
7)  double getArea(),重写Circle类中的area方法,返回圆柱体的表面积
8)  double getVolume(),返回圆柱体的体积
9)  public String toString( ),将把当前圆柱体对象的转换成字符串形式,例如半径为10.0,高为5.0,返回字符串"Cylinder(h:5.0,Circle(r:10.0))"。
(3)编写公共类Main,在main()方法中实现如下功能
输入一个整数n,表示有n个几何图形。
对于每一个几何图形,先输入一个字符串,“Circle”表示圆形,“Cylinder”表示圆柱体。
如果是圆形,输入一个浮点数表示其半径。要求计算其面积和周长并输出。
如果是圆柱体,输入两个浮点数分别表示其半径和高度。要求计算其面积和体积并输出。

将以下代码附在自己的代码后面:

public class Main{
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        for(int i = 0; i < n; i++) {
            String str = input.next();
            if(str.equals("Circle")) {
                Circle c = new Circle(input.nextDouble());
                System.out.println("The area of " + c.toString() + " is " + String.format("%.2f",c.getArea()));
                System.out.println("The perimeterof " + c.toString() + " is "+ String.format("%.2f",c.getPerimeter()));
            } else if(str.equals("Cylinder")) {
                Cylinder r = new Cylinder(input.nextDouble(), new Circle(input.nextDouble()));
                System.out.println("The area of " + r.toString() + " is " + String.format("%.2f",r.getArea()));
                System.out.println("The volume of " + r.toString() + " is " + String.format("%.2f",r.getVolume()));
            }
        }
    }
}

输入格式:

第一行输入一个整数n,表示有n个几何图形。
以下有n行,每行输入一个几何图形的数据。
每行先输入一个字符串表示几何图形的类型,“Circle”表示圆形,“Cylinder”表示圆柱体。
如果是圆形,输入一个浮点数表示其半径。
如果是圆柱体,输入两个浮点数分别表示其半径和高度。

输出格式:

如果是圆形,要求计算其面积和周长并输出。
如果是圆柱体,要求计算其面积和体积并输出。
注意,圆周率用Math.PI

输入样例:

在这里给出一组输入。例如:

4
Circle 10.0
Cylinder 10.0 10.0
Circle 1.1
Cylinder 1.1 3.4

输出样例:

在这里给出相应的输出。例如:

The area of Circle(r:10.0) is 314.16
The perimeterof Circle(r:10.0) is 62.83
The area of Cylinder(h:10.0,Circle(r:10.0)) is 1256.64
The volume of Cylinder(h:10.0,Circle(r:10.0)) is 3141.59
The area of Circle(r:1.1) is 3.80
The perimeterof Circle(r:1.1) is 6.91
The area of Cylinder(h:1.1,Circle(r:3.4)) is 96.13
The volume of Cylinder(h:1.1,Circle(r:3.4)) is 39.95



答案

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int year = 0;
        int month = 0;
        int day = 0;

        int choice = input.nextInt();

        if (choice == 1) { // test getNextNDays method
            int m = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            DateUtil date = new DateUtil(year, month, day);

            if (!date.checkInputValidity()) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            m = input.nextInt();

            if (m < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:");
            System.out.println(date.getNextNDays(m).showDate());
        } else if (choice == 2) { // test getPreviousNDays method
            int n = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            DateUtil date = new DateUtil(year, month, day);

            if (!date.checkInputValidity()) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            n = input.nextInt();

            if (n < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            System.out.print(
                    date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:");
            System.out.println(date.getPreviousNDays(n).showDate());
        } else if (choice == 3) {    //test getDaysofDates method
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            int anotherYear = Integer.parseInt(input.next());
            int anotherMonth = Integer.parseInt(input.next());
            int anotherDay = Integer.parseInt(input.next());

            DateUtil fromDate = new DateUtil(year, month, day);
            DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);

            if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
                System.out.println("The days between " + fromDate.showDate() +
                        " and " + toDate.showDate() + " are:"
                        + fromDate.getDaysofDates(toDate));
            } else {
                System.out.println("Wrong Format");
                System.exit(0);
            }
        }
        else{
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }
}

class DateUtil {
    private int year;
    private int month;
    private int day;

    public DateUtil(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public DateUtil(){}

    public void setYear(int year) {
        this.year = year;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public int getMonth() {
        return month;
    }

    public int getDay() {
        return day;
    }

    private final int[] DAY_OF_MONTH = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    private int getDayOfMonth(int year, int month) {
        int days = DAY_OF_MONTH[month - 1];
        if (month == 2 && isLeapYear(year)) {
            days = 29;
        }
        return days;
    }

    public boolean checkInputValidity()//检测输入的年、月、日是否合法
    {
        if (year < 1820 || year > 2020) return false;
        if (month < 1 || month > 12) return false;
//        int _day = this.getDayOfMonth(year, month);
//        return day <= _day;
        return day >= 1 && day <= 31;
    }

    public boolean isLeapYear(int year)//判断year是否为闰年
    {
        return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    }


    public DateUtil getNextNDays(int n)//取得year-month-day的下n天日期
    {
        int year = this.year;
        int month = this.month;
        int day = this.day;
//        day = Math.min(day, this.getDayOfMonth(year, month));
        for (int i = 0; i < n; i++) {
            day++;
            if (day > getDayOfMonth(year, month)) {
                day = 1;
                month++;
                if (month > 12) {
                    month = 1;
                    year++;
                }
            }
        }
        return new DateUtil(year, month, day);
    }

    public DateUtil getPreviousNDays(int n)//取得year-month-day的前n天日期
    {
        int year = this.year;
        int month = this.month;
        int day = this.day;
        for (int i = 0; i < n; i++) {
            day--;
            while (day < 1) {
                month--;
                if (month < 1) {
                    month = 12;
                    year--;
                }
                day += getDayOfMonth(year, month);
            }
        }
        return new DateUtil(year, month, day);
    }

    public boolean compareDates(DateUtil date)//比较当前日期与date的大小(先后)
    {
        if (this.year > date.year) return true;
        if (this.year == date.year) {
            if (this.month > date.month) return true;
            if (this.month == date.month) {
                if (this.day >= date.day) return true;
            }
        }
        return false;
    }

    public boolean equalTwoDates(DateUtil date)//判断两个日期是否相等
    {
        if (date != null) {
            if (year == date.year && month == date.month && day == date.day) {
                return true;
            }
        }
        return false;
    }
    
    private static final int[] mon = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
    public int getDaysofDates(DateUtil date)//求当前日期与date之间相差的天数
    {
        DateUtil dateUtil1 = this; // 小
        DateUtil dateUtil2 = date; // 大
        if (this.compareDates(date)) {
            dateUtil1 = date;
            dateUtil2 = this;
        }

        int days;
        int leapYearNum = 0;
        for (int i = dateUtil1.getYear(); i < dateUtil2.getYear(); i++) {
            if (isLeapYear(i)) {
                leapYearNum++;
            }
        }

        days = 365 * (dateUtil2.getYear() - dateUtil1.getYear()) + leapYearNum;

        int d1 = mon[dateUtil1.getMonth() - 1] + dateUtil1.getDay() + (dateUtil1.getMonth() > 2 && isLeapYear(dateUtil1.getYear()) ? 1 : 0);
        int d2 = mon[dateUtil2.getMonth() - 1] + dateUtil2.getDay() + (dateUtil2.getMonth() > 2 && isLeapYear(dateUtil2.getYear()) ? 1 : 0);
        return days - d1 + d2;
    }
    
    public String showDate()//以“year-month-day”格式返回日期值
    {
        return year + "-" + month + "-" + day;
    }
}
最后修改:2020 年 10 月 13 日 07 : 03 PM
愿君多打钱,此物最相思