题目集4-6的总结性Blog

一、前言

这三次题目较前三次来说难度增大了许多,在做题目的过程中,我遇到了许多问题,

解决过程中学到了很多,也有一些问题到最后也没有解决,还需不断学习。

尤其第四次作业时,第一次运用新学的知识继承、多态等,十分不熟练,导致了成绩也不够理想。

 

二、设计与分析

①题目集4(7-2)、题目集5(7-4)两种日期类聚合设计的优劣比较

题目集4  7-2日期问题面向对象设计(聚合一)
参考题目7-2的要求,设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1900,2050] ,month∈[1,12] ,day∈[1,31] , 设计类图如下:

类图.jpg

应用程序共测试三个功能:

  1. 求下n天
  2. 求前n天
  3. 求两个日期相差的天数

注意:严禁使用Java中提供的任何与日期相关的类与方法,并提交完整源码,包括主类及方法(已提供,不需修改)

输入格式:

有三种输入方式(以输入的第一个数字划分[1,3]):

  • 1 year month day n //测试输入日期的下n天
  • 2 year month day n //测试输入日期的前n天
  • 3 year1 month1 day1 year2 month2 day2 //测试两个日期之间相差的天数

输出格式:

  • 当输入有误时,输出格式如下: Wrong Format
  • 当第一个数字为1且输入均有效,输出格式如下:
    year-month-day
     
  • 当第一个数字为2且输入均有效,输出格式如下:
    year-month-day
     
  • 当第一个数字为3且输入均有效,输出格式如下:
    天数值
     

输入样例1:

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

3 2014 2 14 2020 6 14
 
结尾无空行

输出样例1:

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

2312
 
结尾无空行

输入样例2:

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

2 1935 2 17 125340
 
结尾无空行

输出样例2:

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

1591-12-17
 
结尾无空行

输入样例3:

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

1 1999 3 28 6543
 
结尾无空行

输出样例3:

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

2017-2-24
 
结尾无空行

输入样例4:

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

0 2000 5 12 30
 
结尾无空行

输出样例4:

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

Wrong Format

实现代码:

import java.util.Scanner;

class DateUtil {
    private int y = 0, m = 0, d = 0;
    Year year = new Year();
    Month month = new Month();
    Day day = new Day();

    // 默认构造方法
    public DateUtil() {
        super();
        // TODO Auto-generated constructor stub
    }

    // 带参构造方法
    public DateUtil(int d, int m, int y) {
        this.d = d;
        this.m = m;
        this.y = y;
        day.setValue(d);
        month.setValue(m);
        year.setValue(y);
        day.setMonth(month);
        month.setYear(year);
    }

    public int getYear() {
        return y;
    }

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

    public int getMonth() {
        return m;
    }

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

    public int getDay() {
        return d;
    }

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

    // 校验数据合法性
    public boolean checkInputValidity() {
        if(year.validate() && month.validate() && day.validate()) {
            return true;
        }
        return false;
    }

    // 比较两个日期的大小
    public boolean compareDates(DateUtil date) {
        if(this.y > date.y) {
            return true;
        }
        if(this.y == date.y) {
            if(this.m > date.m) {
                return true;
            }
            if(this.m == date.m) {
                if(this.d >= date.d) {
                    return true;
                }
            }
        }
        
        return false;
    }

    // 判定两个日期是否相等
    public boolean equalTwoDates(DateUtil date) {
        return false;
    }

    // 日期值格式化
    public String showDate() {
        return year.getValue()+"-"+month.getValue()+"-"+day.getValue();
    }

    // 求下n天
    public DateUtil getNextNDays(int n) {
        for(int i = 0; i < n; i++) {
            day.dayIncrement();
            if(!day.validate()) {
                day.resetMin();
                month.monthIncrement();
                if(!month.validate()) {
                    month.resetMin();
                    year.setValue(y+1);
                }
            }
        }
        return new DateUtil(day.getValue(), month.getValue(), year.getValue());

    }

    // 求前n天
    public DateUtil getPreviousNDays(int n) {
        int d = day.getValue();
        int m = month.getValue();
        int y = year.getValue();
        for(int i = 0; i < n; i++) {
            d--;
            while(d<1) {
                m--;
                if(m<1) {
                    m=12;
                    y--;
                }
                
                d += day.getMon_maxnum(month.getValue()-1);
                if(m==2 && (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)) {
                    d += 1;
                }
            }
        }
        return new DateUtil(d, m, y);


    }

    // 求两个日期之间的天数
    public int getDaysofDates(DateUtil date) {
        int[] mon_month= {0,31,59,90,120,151,181,212,243,273,304,334};
        DateUtil date1 = new DateUtil();
        DateUtil date2 = new DateUtil();
        if(this.compareDates(date)) {
            date1 = date;
            date2 = this;
        } else {
            date1 = this;
            date2 = date;
        }
        
        int sum = 0;  //记录相差天数
        
        int lynum = 0;  //闰年的数量
        for(int i = date1.getYear(); i<date2.getYear(); i++) {
            if((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0)) {
                lynum++;
            }
        }
        
        sum = 365*(date2.getYear() - date1.getYear()) + lynum;
        int sum1 = mon_month[date1.getMonth()-1]+date1.getDay()+(date1.getMonth()>2 && date1.year.isLeapYear()?1:0);
        int sum2 = mon_month[date2.getMonth()-1]+date2.getDay()+(date2.getMonth()>2 && date2.year.isLeapYear()?1:0);
        return (sum - sum1 + sum2);
    }
}

class Year {
    private int value = 0;

    // 默认构造方法
    public Year() {
        super();
        // TODO Auto-generated constructor stub
    }

    // 带参构造方法
    public Year(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

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

    // 校验数据合法性
    public boolean validate() {
        if (value >= 1900 && value <= 2050) {
            return true;
        }
        return false;
    }

    // 年份增1
    public void yearIncrement() {
        this.value++;
    }

    // 年份减1
    public void yearReduction() {
        this.value--;
    }
}

class Month {
    private int value = 0;
    private Year year;

    // 默认构造函数
    public Month() {
        super();
        // TODO Auto-generated constructor stub
    }

    // 带参构造函数
    public Month(int yearValue, int monthValue) {
        
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public Year getYear() {
        return year;
    }

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

    // 月份设置为1
    public void resetMin() {
        value = 1;
    }

    // 月份设置为12
    public void resetMax() {
        value = 12;
    }

    // 校验数据合法性
    public boolean validate() {
        if (value >= 1 && value <= 12) {
            return true;
        }
        return false;
    }

    // 月份增1
    public void monthIncrement() {
        this.value++;
    }

    // 月份减1
    public void monthReduction() {
        this.value--;
    }
}

class Day {
    private int value = 0;
    private Month month = new Month();
    private int[] mon_maxnum = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    public int getMon_maxnum(int n) {
        return mon_maxnum[n];
    }

    public void setMon_maxnum(int[] mon_maxnum) {
        this.mon_maxnum = mon_maxnum;
    }

    // 默认构造方法
    public Day() {
        super();
        // TODO Auto-generated constructor stub
    }

    // 带参构造方法
    public Day(int yearValue, int monthValue, int dayValue) {

    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public Month getMonth() {
        return month;
    }

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

    // 日期设置为1
    public void resetMin() {
        value = 1;
    }

    // 日期设置为最大
    public void resetMax() {
        value = mon_maxnum[month.getValue()-1];
    }

    // 校验数据合法性
    public boolean validate() {
        if(value>=1 && value<=mon_maxnum[month.getValue()-1]) {
            return true;
        }
        return false;
    }

    // 日期增1
    public void dayIncrement() {
        this.value++;
    }

    // 日期减1
    public void dayReduction() {
        this.value--;
    }

}

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int year = 0, month = 0, day = 0, choice = 0;
        Scanner in = new Scanner(System.in);
        
        choice = in.nextInt();
        year = in.nextInt();
        month = in.nextInt();
        day = in.nextInt();
        
        DateUtil date = new DateUtil(day, month, year);
        if(!date.checkInputValidity()) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
        switch (choice) {
        case 1:/* 输出日期的后n天 */
            System.out.println(date.getNextNDays(in.nextInt()).showDate());
            break;
        case 2:/* 输出日期的前n天 */
            System.out.println(date.getPreviousNDays(in.nextInt()).showDate());
            break;
        case 3:/* 两个日期之间相差的天数 */
            int toYear = in.nextInt();
            int toMonth = in.nextInt();
            int toDay = in.nextInt();
            DateUtil toDate = new DateUtil(toDay, toMonth, toYear);
            if(!toDate.checkInputValidity()) {
                System.out.println("Wrong Format");
                System.exit(0);
            }
            System.out.println(date.getDaysofDates(toDate));
            break;
        default:
            System.out.println("Wrong Format");
        }
        
    }

}
 
结构分析:

 

 

 
7-5 日期问题面向对象设计(聚合二) (40 分)
 

参考题目7-3的要求,设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1820,2020] ,month∈[1,12] ,day∈[1,31] , 设计类图如下:

类图.jpg

应用程序共测试三个功能:

  1. 求下n天
  2. 求前n天
  3. 求两个日期相差的天数

注意:严禁使用Java中提供的任何与日期相关的类与方法,并提交完整源码,包括主类及方法(已提供,不需修改)

输入格式:

有三种输入方式(以输入的第一个数字划分[1,3]):

  • 1 year month day n //测试输入日期的下n天
  • 2 year month day n //测试输入日期的前n天
  • 3 year1 month1 day1 year2 month2 day2 //测试两个日期之间相差的天数

输出格式:

  • 当输入有误时,输出格式如下: Wrong Format
  • 当第一个数字为1且输入均有效,输出格式如下:
    year1-month1-day1 next n days is:year2-month2-day2
     
  • 当第一个数字为2且输入均有效,输出格式如下:
    year1-month1-day1 previous n days is:year2-month2-day2
     
  • 当第一个数字为3且输入均有效,输出格式如下:
    The days between year1-month1-day1 and year2-month2-day2 are:值
     

输入样例1:

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

3 2014 2 14 2020 6 14
 
结尾无空行

输出样例1:

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

The days between 2014-2-14 and 2020-6-14 are:2312
 
结尾无空行

输入样例2:

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

2 1834 2 17 7821
 
结尾无空行

输出样例2:

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

1834-2-17 previous 7821 days is:1812-9-19
 
结尾无空行

输入样例3:

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

1 1999 3 28 6543
 
结尾无空行

输出样例3:

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

1999-3-28 next 6543 days is:2017-2-24
 
结尾无空行

输入样例4:

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

0 2000 5 12 30
 
结尾无空行

输出样例4:

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

Wrong Format
 
import java.util.regex.Matcher;
import java.util.Scanner;

class DateUtil {
    private int y = 0, m = 0, d = 0;
    Year year = new Year();
    Month month = new Month();
    Day day = new Day();
    int[] mon_maxnum = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    // 默认构造方法
    public DateUtil() {
        super();
        // TODO Auto-generated constructor stub
    }

    // 带参构造方法
    public DateUtil(int y, int m, int d) {
        this.d = d;
        this.m = m;
        this.y = y;
        day.setValue(d);
        month.setValue(m);
        year.setValue(y);
        day.setMonth(month);
        month.setYear(year);
    }

    public int getYear() {
        return y;
    }

    public void setYear(Year year) {
        year.setValue(y);
    }

    public int getMonth() {
        return m;
    }

    public void setMonth(Month month) {
        month.setValue(m);
        ;
    }

    public int getDay() {
        return d;
    }

    public void setDay(Day day) {
        day.setValue(d);
    }

    public void setDayMin() {
        d = 1;
    }

    public void setDayMax() {
        d = mon_maxnum[m-1];
    }

    // 校验数据合法性
    public boolean checkInputValidity() {
        if (year.validate() && month.validate() && day.validate()) {
            return true;
        }
        return false;
    }

    // 比较两个日期的大小
    public boolean compareDates(DateUtil date) {
        if (this.y > date.y) {
            return true;
        }
        if (this.y == date.y) {
            if (this.m > date.m) {
                return true;
            }
            if (this.m == date.m) {
                if (this.d >= date.d) {
                    return true;
                }
            }
        }

        return false;
    }

    // 判定两个日期是否相等
    public boolean equalTwoDates(DateUtil date) {
        return false;
    }

    // 日期值格式化
    public String showDate() {
        return year.getValue() + "-" + month.getValue() + "-" + day.getValue();
    }

    // 求下n天
    public DateUtil getNextNDays(int n) {
        for (int i = 0; i < n; i++) {
            d++;
            if (d > mon_maxnum[m-1]) {
                setDayMin();
                if (m == 2 && ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))) {
                    d--;
                }
                m++;
                if (m > 12) {
                    m = 1;
                    y++;
                }
            }
        }
        return new DateUtil(y, m, d);

    }

    // 求前n天
    public DateUtil getPreviousNDays(int n) {
        for (int i = 0; i < n; i++) {
            d--;
            if (d < 1) {
                m--;
                if (m < 1) {
                    y--;
                    m = 12;
                }
                setDayMax();
                if (m == 2 && ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))) {
                    d++;
                }
            }
        }
        return new DateUtil(y, m, d);

    }

    // 求两个日期之间的天数
    public int getDaysofDates(DateUtil date) {
        int[] mon_month = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
        DateUtil date1 = new DateUtil();
        DateUtil date2 = new DateUtil();
        if (this.compareDates(date)) {
            date1 = date;
            date2 = this;
        } else {
            date1 = this;
            date2 = date;
        }

        int sum = 0; // 记录相差天数

        int lynum = 0; // 闰年的数量
        for (int i = date1.getYear(); i < date2.getYear(); i++) {
            if ((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0)) {
                lynum++;
            }
        }

        sum = 365 * (date2.getYear() - date1.getYear()) + lynum;
        int sum1 = mon_month[date1.getMonth() - 1] + date1.getDay()
                + (date1.getMonth() > 2 && date1.year.isLeapYear() ? 1 : 0);
        int sum2 = mon_month[date2.getMonth() - 1] + date2.getDay()
                + (date2.getMonth() > 2 && date2.year.isLeapYear() ? 1 : 0);
        return (sum - sum1 + sum2);
    }

}

class Year {
    private int value = 0;

    // 默认构造方法
    public Year() {
        super();
        // TODO Auto-generated constructor stub
    }

    // 带参构造方法
    public Year(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

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

    // 校验数据合法性
    public boolean validate() {
        if (value >= 1820 && value <= 2020) {
            return true;
        }
        return false;
    }

    // 年份增1
    public void yearIncrement() {
        this.value++;
    }

    // 年份减1
    public void yearReduction() {
        this.value--;
    }
}

class Month {
    private int value = 0;
    private Year year;

    // 默认构造函数
    public Month() {
        super();
        // TODO Auto-generated constructor stub
    }

    // 带参构造函数
    public Month(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public Year getYear() {
        return year;
    }

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

    // 月份设置为1
    public void resetMin() {
        value = 1;
    }

    // 月份设置为12
    public void resetMax() {
        value = 12;
    }

    // 校验数据合法性
    public boolean validate() {
        if (value >= 1 && value <= 12) {
            return true;
        }
        return false;
    }

    // 月份增1
    public void monthIncrement() {
        this.value++;
    }

    // 月份减1
    public void monthReduction() {
        this.value--;
    }
}

class Day {
    private int value = 0;
    private Month month = new Month();
    private int[] mon_maxnum = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    public int getMon_maxnum(int n) {
        return mon_maxnum[n];
    }

    public void setMon_maxnum(int[] mon_maxnum) {
        this.mon_maxnum = mon_maxnum;
    }

    // 默认构造方法
    public Day() {
        super();
        // TODO Auto-generated constructor stub
    }

    // 带参构造方法
    public Day(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public Month getMonth() {
        return month;
    }

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

    // 日期设置为1
    public void resetMin() {
        value = 1;
    }

    // 日期设置为最大
    public void resetMax() {
        value = mon_maxnum[month.getValue() - 1];
    }

    // 校验数据合法性
    public boolean validate() {
        if (value >= 1 && value <= mon_maxnum[month.getValue() - 1]) {
            return true;
        }
        return false;
    }

    // 日期增1
    public void dayIncrement() {
        this.value++;
    }

    // 日期减1
    public void dayReduction() {
        this.value--;
    }

}

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int year = 0, month = 0, day = 0, choice = 0, n = 0;
        Scanner in = new Scanner(System.in);

        choice = in.nextInt();
        year = in.nextInt();
        month = in.nextInt();
        day = in.nextInt();

        DateUtil date = new DateUtil(year, month, day);
        if (!date.checkInputValidity()) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
        switch (choice) {
        case 1:/* 输出日期的后n天 */
            n = in.nextInt();
            System.out.println(date.showDate() + " next " + n + " days is:" + date.getNextNDays(n).showDate());
            break;
        case 2:/* 输出日期的前n天 */
            n = in.nextInt();
            System.out.println(date.showDate() + " previous " + n + " days is:" + date.getPreviousNDays(n).showDate());
            break;
        case 3:/* 两个日期之间相差的天数 */
            int toYear = in.nextInt();
            int toMonth = in.nextInt();
            int toDay = in.nextInt();
            DateUtil toDate = new DateUtil(toYear, toMonth, toDay);
            if (!toDate.checkInputValidity()) {
                System.out.println("Wrong Format");
                System.exit(0);
            }
            System.out.println("The days between " + date.showDate() + " and " + toDate.showDate() + " are:"
                    + date.getDaysofDates(toDate));
            break;
        default:
            System.out.println("Wrong Format");
        }

    }

}

结构分析:

 

 

两种日期类聚合之间的不同之处主要是UML类图,理清Year类,Month类,Day类,DateUtil类之间关系的变化,
题目集4(7-2)中的聚合类型是将Year、Month、Day,Dateutil一层一层聚合,再用函数访问,而题目集5(7-4)中的聚合是将Year、Month、Day全部聚合到
Dateutil里面,这种聚合方式结构更清晰,代码可读性更高。
聚合(Cohesion)是一个模块内部各成分之间相关联程度的度量。

这里有多个含义值得考虑。首先,聚合是对一个模块内部的度量,这也是许多情况下我们把聚合称之为内聚的原因。

第二,这里出现的模块是广义的模块,它可能是子系统,可能是功能模块,也可能是功能模块中的某一个类。

从不同的层次看,聚合的程度也会有所不同。

第三,模块的成分包括模块的行为和状态。要做到高聚合,那么模块内部的行为必须要与模块的内部状态紧密关联。通俗来讲,一个模块仅完成一个独立的功能
,模块内部不存在与该功能无关的操作或状态。
 

 ②题目集4(7-3)、题目集6(7-5、7-6)三种渐进式图形继承设计的思路与技术运用(封装、继承、多态、接口等)

 
题目集4(7-3) 图形继承 (15 分)
 

编写程序,实现图形类的继承,并定义相应类对象并进行测试。

  1. 类Shape,无属性,有一个返回0.0的求图形面积的公有方法public double getArea();//求图形面积
  2. 类Circle,继承自Shape,有一个私有实型的属性radius(半径),重写父类继承来的求面积方法,求圆的面积
  3. 类Rectangle,继承自Shape,有两个私有实型属性width和length,重写父类继承来的求面积方法,求矩形的面积
  4. 类Ball,继承自Circle,其属性从父类继承,重写父类求面积方法,求球表面积,此外,定义一求球体积的方法public double getVolume();//求球体积
  5. 类Box,继承自Rectangle,除从父类继承的属性外,再定义一个属性height,重写父类继承来的求面积方法,求立方体表面积,此外,定义一求立方体体积的方法public double getVolume();//求立方体体积
  6. 注意:
  • 每个类均有构造方法,且构造方法内必须输出如下内容:Constructing 类名
  • 每个类属性均为私有,且必须有getter和setter方法(可用Eclipse自动生成)
  • 输出的数值均保留两位小数

主方法内,主要实现四个功能(1-4): 从键盘输入1,则定义圆类,从键盘输入圆的半径后,主要输出圆的面积; 从键盘输入2,则定义矩形类,从键盘输入矩形的宽和长后,主要输出矩形的面积; 从键盘输入3,则定义球类,从键盘输入球的半径后,主要输出球的表面积和体积; 从键盘输入4,则定义立方体类,从键盘输入立方体的宽、长和高度后,主要输出立方体的表面积和体积;

假如数据输入非法(包括圆、矩形、球及立方体对象的属性不大于0和输入选择值非1-4),系统输出Wrong Format

输入格式:

共四种合法输入

  • 1 圆半径
  • 2 矩形宽、长
  • 3 球半径
  • 4 立方体宽、长、高

输出格式:

按照以上需求提示依次输出

输入样例1:

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

1 1.0
 
结尾无空行

输出样例1:

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

Constructing Shape
Constructing Circle
Circle's area:3.14
 
结尾无空行

输入样例2:

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

4 3.6 2.1 0.01211
 
结尾无空行

输出样例2:

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

Constructing Shape
Constructing Rectangle
Constructing Box
Box's surface area:15.26
Box's volume:0.09
 
结尾无空行

输入样例3:

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

2 -2.3 5.110
 
结尾无空行

输出样例2:

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

Wrong Format
 
import java.util.Scanner;

//定义Shape类
class Shape {
    // 求图形面积的方法
    public double getArea() {
        return 0.0;
    }

    // 构造方法
    public Shape() {
        System.out.println("Constructing Shape");
    }
}

//定义Circle类
class Circle extends Shape {
    private double radius = 0; // 圆半径

    // 构造方法
    public Circle() {
        super();
        System.out.println("Constructing Circle");
    }

    // @override
    public double getArea() {
        return 3.14 * Math.pow(radius, 2);
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

}

//定义Rectangle类
class Rectangle extends Shape {
    private double width = 0, length = 0; // 矩形宽、高

    // @override
    public double getArea() {
        return width * length;
    }

    // 构造方法
    public Rectangle() {
        super();
        System.out.println("Constructing Rectangle");
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getLength() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }

}

//定义Ball类
class Ball extends Circle {
    private double radius = 0; // 球半径

    // @override
    public double getArea() { // 球表面积
        return 4 * 3.14 * Math.pow(radius, 2);
    }

    public double getVolume() { // 球体积
        return 4 / 3 * 3.14 * Math.pow(radius, 3);
    }

    // 构造方法
    public Ball() {
        super();
        System.out.println("Constructing Ball");
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

}

//定义Box类
class Box extends Rectangle {
    private double width = 0, length = 0, height = 0; // 立方体宽、长、高

    // @override
    public double getArea() { // 立方体表面积
        return 2 * width * length + 2 * length * height + 2 * height * width;
    }

    // 立方体体积
    public double getVolume() {
        return width * length * height;
    }

    // 构造方法
    public Box() {
        super();
        System.out.println("Constructing Box");
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getLength() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

}

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        int choice = 0;
        choice = in.nextInt();
        double radius = 0, width = 0, length = 0, height = 0;
        switch (choice) {
        case 1:/* 圆 半径 */
            radius = in.nextDouble();
            if (radius > 0) {
                Circle circle = new Circle();
                circle.setRadius(radius);
                System.out.println("Circle's area:" + String.format("%.2f", circle.getArea()));
            } else {
                System.out.println("Wrong Format");
            }
            break;
        case 2:/* 矩形 宽 长 */
            width = in.nextDouble();
            length = in.nextDouble();
            if (width > 0 && length > 0) {
                Rectangle rectangle = new Rectangle();
                rectangle.setWidth(width);
                rectangle.setLength(length);
                System.out.println("Rectangle's area:" + String.format("%.2f", rectangle.getArea()));
            } else {
                System.out.println("Wrong Format");
            }
            break;
        case 3:/* 球 球半径 */
            radius = in.nextDouble();
            if (radius > 0) {
                Ball ball = new Ball();
                ball.setRadius(radius);
                System.out.println("Ball's surface area:" + String.format("%.2f", ball.getArea()));
                System.out.println("Ball's volume:" + String.format("%.2f", ball.getVolume()));
            } else {
                System.out.println("Wrong Format");
            }
            break;
        case 4:/* 立方体 宽 长 高 */
            width = in.nextDouble();
            length = in.nextDouble();
            height = in.nextDouble();
            if (width > 0 && length > 0 && height > 0) {
                Box box = new Box();
                box.setWidth(width);
                box.setLength(length);
                box.setHeight(height);
                System.out.println("Box's surface area:" + String.format("%.2f", box.getArea()));
                System.out.println("Box's volume:" + String.format("%.2f", box.getVolume()));
            } else {
                System.out.println("Wrong Format");
            }
            break;
        default:
            System.out.println("Wrong Format");
        }
    }
}

提交结果:

 

 

 有好几个测试点过不了

 
题目集6(7-5) 图形继承与多态 (50 分)
 

掌握类的继承、多态性及其使用方法。具体需求参见作业指导书。

2021-OO第06次作业-5指导书V1.0.pdf

输入格式:

从键盘首先输入三个整型值(例如a b c),分别代表想要创建的Circle、Rectangle及Triangle对象的数量,然后根据图形数量继续输入各对象的属性值(均为实型数),数与数之间可以用一个或多个空格或回车分隔。

输出格式:

  1. 如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边关系),则输出Wrong Format
  2. 如果输入合法,则正常输出,输出内容如下(输出格式见输入输出示例):
  • 各个图形的面积;
  • 所有图形的面积总和;
  • 排序后的各个图形面积;
  • 再次所有图形的面积总和。

输入样例1:

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

1 1 1 2.3 3.2 3.2 6.5 3.2 4.2
 
结尾无空行

输出样例1:

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

Original area:
16.62 10.24 5.68 
Sum of area:32.54
Sorted area:
5.68 10.24 16.62 
Sum of area:32.54
 
结尾无空行

输入样例2:

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

0 2 2 2.3 2.5 56.4 86.5 64.3 85.6 74.6544 3.2 6.1 4.5
 
结尾无空行

输出样例2:

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

Original area:
5.75 4878.60 2325.19 7.00 
Sum of area:7216.54
Sorted area:
5.75 7.00 2325.19 4878.60 
Sum of area:7216.54
 
结尾无空行

输入样例3:

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

0 0 1 3 3 6
 
结尾无空行

输出样例3:

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

Wrong Format
 
import java.util.Arrays;
import java.util.Scanner;

abstract class Shape{
    public abstract double getArea(); //求面积
    public abstract boolean validate(); //图形属性合法性校验
    public abstract String toString(); //输出图形的面积信息
}


class Circle extends Shape{
    private double radius = 0;

    @Override
    public double getArea() {
        // TODO Auto-generated method stub
        return Math.PI*radius*radius;
    }

    @Override
    public boolean validate() {
        // TODO Auto-generated method stub
        return (radius>0?true:false);
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return null;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }
    
}


class Rectangle extends Shape{
    private double width;
    private double length;
    @Override
    public double getArea() {
        // TODO Auto-generated method stub
        return width*length;
    }
    @Override
    public boolean validate() {
        // TODO Auto-generated method stub
        return (width>=0 && length>=0?true:false);
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return null;
    }
    public double getWidth() {
        return width;
    }
    public void setWidth(double width) {
        this.width = width;
    }
    public double getLength() {
        return length;
    }
    public void setLength(double length) {
        this.length = length;
    }
}


class Triangle extends Shape{
    private double side1 = 0;
    private double side2 = 0;
    private double side3 = 0;
    @Override
    public double getArea() {
        // TODO Auto-generated method stub
        return 0.25*Math.sqrt((side1+side2+side3)*(side1+side2-side3)*(side1+side3-side2)*(side2+side3-side1));
    }
    @Override
    public boolean validate() {
        // TODO Auto-generated method stub
        return ((side1>=0 && side2>=0 && side3>=0 && (side1+side2>side3) && (side1+side3>side2) && (side2+side3>side1))?true:false);
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return null;
    }
    public double getSide1() {
        return side1;
    }
    public void setSide1(double side1) {
        this.side1 = side1;
    }
    public double getSide2() {
        return side2;
    }
    public void setSide2(double side2) {
        this.side2 = side2;
    }
    public double getSide3() {
        return side3;
    }
    public void setSide3(double side3) {
        this.side3 = side3;
    }
}



public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        double sumArea = 0;
        int numCir,numRec,numTri,i,sumShape,j=0;
        
        Scanner in = new Scanner(System.in);
        numCir = in.nextInt();
        numRec = in.nextInt();
        numTri = in.nextInt();
        if(numCir<0 || numRec<0 || numTri<0) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
        sumShape = numCir + numRec +numTri;
        double areaArr[] = new double[sumShape];
        
        for(i = numCir; i>0; i--) {
            Circle circle = new Circle();
            circle.setRadius(in.nextDouble());
            if(!circle.validate()) { //半径不合法输出Wrong Format
                System.out.println("Wrong Format");
                System.exit(0);
            }
            sumArea+=circle.getArea();
            areaArr[j] = circle.getArea();
            j++;
        }
        
        for(i = numRec; i>0; i--) {
            Rectangle rectangle = new Rectangle();
            rectangle.setWidth(in.nextDouble());
            rectangle.setLength(in.nextDouble());
            if(!rectangle.validate()) { //宽、长不合法输出Wrong Format
                System.out.println("Wrong Format");
                System.exit(0);
            }
            sumArea+=rectangle.getArea();
            areaArr[j] = rectangle.getArea();
            j++;
        }
        
        for(i = numTri; i>0; i--) {
            Triangle triangle = new Triangle();
            triangle.setSide1(in.nextDouble());
            triangle.setSide2(in.nextDouble());
            triangle.setSide3(in.nextDouble());
            if(!triangle.validate()) { //宽、长不合法输出Wrong Format
                System.out.println("Wrong Format");
                System.exit(0);
            }
            sumArea+=triangle.getArea();
            areaArr[j] = triangle.getArea();
            j++;
        }
        
        System.out.println("Original area:");
        for(i = 0; i < sumShape; i++) {
            System.out.print(String.format("%.2f", areaArr[i])+" ");
        }
        System.out.println();
        System.out.println("Sum of area:"+String.format("%.2f", sumArea));
        Arrays.sort(areaArr);
        System.out.println("Sorted area:");
        for(i = 0; i < sumShape; i++) {
            System.out.print(String.format("%.2f", areaArr[i])+" ");
        }
        System.out.println();
        System.out.println("Sum of area:"+String.format("%.2f", sumArea));
    }

}

结构分析:

 

 

7-6 实现图形接口及多态性 (30 分)
 

编写程序,使用接口及类实现多态性,类图结构如下所示:

类图.jpg

其中:

  • GetArea为一个接口,无属性,只有一个GetArea(求面积)的抽象方法;
  • Circle及Rectangle分别为圆类及矩形类,分别实现GetArea接口
  • 要求:在Main类的主方法中分别定义一个圆类对象及矩形类对象(其属性值由键盘输入),使用接口的引用分别调用圆类对象及矩形类对象的求面积的方法,直接输出两个图形的面积值。(要求只保留两位小数)

输入格式:

从键盘分别输入圆的半径值及矩形的宽、长的值,用空格分开。

输出格式:

  • 如果输入的圆的半径值及矩形的宽、长的值非法(≤0),则输出Wrong Format
  • 如果输入合法,则分别输出圆的面积和矩形的面积值(各占一行),保留两位小数。

输入样例1:

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

2 3.6 2.45
 
结尾无空行

输出样例1:

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

12.57
8.82
 
结尾无空行

输入样例2:

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

9 0.5 -7.03
 
结尾无空行

输出样例2:

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

Wrong Format
 
import java.util.Scanner;
import java.util.Arrays;

//主类
public class Main{
  public static void main(String[] args){
      Scanner x = new Scanner(System.in);
     double yuanmj,chang,kuan;
     double scir,srec;
     yuanmj=x.nextDouble();
     chang=x.nextDouble();
     kuan=x.nextDouble();

      if(yuanmj<=0||chang<=0||kuan<=0){//如果不合法
          System.out.println("Wrong Format");
          System.exit(0);
      }
      GetArea yuanmianji=new Circle(yuanmj);
      GetArea juxingmianji=new Rectangle(chang,kuan);
      scir=yuanmianji.Mj();
      srec=juxingmianji.Mj();
      System.out.printf("%.2f\n",scir);
      System.out.printf("%.2f\n",srec);
  }
}


//GetArea类
interface GetArea{
    public  double Mj();
}
//Circle类
class Circle implements GetArea{
    private double r;
    //创建无参构造方法
    public Circle(){
    }
    //创建带参构造方法
    public Circle(double r){
        this.r=r;
    }
    //getter
    public double getR(){
        return r;
    }
    //setter
    public void setR(double r){
        this.r = r;
    }
    //计算圆面积
    public double Mj(){
        return Math.PI*r*r;
    }
}
//Rectangle类
class Rectangle implements GetArea{
    private double a,b;
    //创建无参构造方法
    public Rectangle(){
    }
    //创建带参构造方法
    public Rectangle(double a, double b){
        this.a=a;
        this.b=b;
    }
    //getter
    public double A(){
        return a;
    }
    public double B(){
        return b;
    }
    //setter
    public void setA(double a){
        this.a=a;
    }
    public void setB(double b){
        this.b=b;
    }
    //计算矩形面积
    public double Mj(){
        return a*b;
    }
}

class Wolf {

    public boolean isAlive = true;
    public boolean crossRiver = false;
    public boolean hasCross = false;
    public void showStatus() { // 显示生存、位置
        hasCross = crossRiver;
    }
    public static boolean checkInputValidity(int year,int month,int day)
    {
        int flag;
        
        if(year <1820 || year > 2020 || month < 1 || month > 12 || day < 1 || day > 31)
        {
            return false;
        }
        else
        {
            return true;            
        }    
    }
    public static boolean isLeapYear(int year)
    {
        int flag=-1;
        if(year%4==0||(year%4==0&&year%400==0))
        {
            flag=1;        
        }
        else if(year%4!=0||(year%4==0&&year%400!=0))
        {
            flag=0;
        }
        if(flag==1)
        {
            return true;    
        }
        else 
        {
            return false;    
        }
        
    }

}

class Cabbage {

    public boolean isAlive = true;
    public boolean crossRiver = false;
    public boolean hasCross = false;

    public void showStatus() { // 显示生存、位置
        hasCross = crossRiver;
    }
     
}

class Farmer {

    public boolean crossRiver = false;
    public boolean hasCross = false;

    public void showStatus() { // 显示生存、位置
        hasCross = crossRiver;
    }
}

class don {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String[] key = { "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const",
                "continue", "default", "do", "double", "else", "enum", "extends", "false", "final", "finally", "float",
                "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new",
                "null", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super",
                "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "void", "volatile",
                "while" };

        String in = null;
        StringBuilder sb = new StringBuilder();
        int sum = 0;
        try (Scanner input = new Scanner(System.in)) {
            //读取行数据
            while (input != null) {
                in = input.nextLine();
                if (!in.equals("exit")) {
                if (!in.matches("(.*)//(.*)")) {
                    sb.append(in + " ");
                } else {
                    String s0[] = in.split("//");
                    sb.append(s0[0] + " ");
                }
            } else {
                break;
            }
            }
        }

    }
}

结构分析:

 

 三种渐进式图形继承设计的思路与技术运用(封装、继承、多态、接口等):

封装是隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读和修改的访问级别。

封装的目的是增强安全性和简化编程,使用者不必了解具体的实现细节,而只是要通过外部接口,一特定的访问权限来使用类的成员。

封装的基本要求是把所有的属性私有化,对每个属性提供getter和setter方法,如果有一个带参的构造函数的话,那一定要写一个不带参的构造函数。

在开发的时候经常要对已经编写的类进行测试,所以在有的时候还有重写toString方法,但这不是必须的。

继承的目的是实现代码的复用。

当两个类具有相同的特征(属性)和行为(方法)时,可以将相同的部分抽取出来放到一个类中作为父类,其它两个类继承这个父类。

继承后子类自动拥有了父类的属性和方法,但特别注意的是,父类的私有属性和构造方法并不能被继承。

另外子类可以写自己特有的属性和方法,目的是实现功能的扩展,子类也可以复写父类的方法即方法的重写。子类不能继承父类中访问权限为private的成员变量和方法。

子类可以重写父类的方法,及命名与父类同名的成员变量。有时候我们需要这样的需求:我们需要将某些事物尽可能地对这个世界隐藏,但是仍然允许子类的成员来访问它们。这个时候就需要使用到protected。

接口是隐式公共方法和属性组合起来,以封装特定功能的一个集合。接口不能单独存在,不能像实例化一个类那样实例化接口,但可以建立接口类型的变量。

接口不能外含执行其成员的任何代码,而只能定义成员本身,执行过程必须在执行接口的类中实现。

abstract和 sealed不能在接口中使用,因为这两个修饰符在接口中定义是没有意义的(它们不包含执行代码,所以不能直接实例化,且必须是可以继承的)。

接口成员都是公共的。接口没有指定属性应如何存储,所以不能有域。最后,接口与类一样,可以定义为类的成员

(但与接口的其他成员不同,因为接口不能包含类型定义)

 

③对三次题目集中用到的正则表达式技术的分析总结

正则表达式技术的特点:

正则表达式定义了字符串的模式。

正则表达式可以用来搜索、编辑或处理文本。

正则表达式并不仅限于某一种语言,但是在每种语言中有细微的差别。

Java 正则表达式和 Perl 的是最为相似的。

在regex包中,包括了两个类,Pattern(模式类)和Matcher(匹配器类)。Pattern类是用来表达和陈述所要搜索模式的对象,Matcher类是真正影响搜索的对象。

另加一个新的例外类,PatternSyntaxException,当遇到不合法的搜索模式时,会抛出例外。

java.util.regex 包主要包括以下三个类:

  • Pattern 类:

    pattern 对象是一个正则表达式的编译表示。Pattern 类没有公共构造方法。要创建一个 Pattern 对象,你必须首先调用其公共静态编译方法,它返回一个 Pattern 对象。

  • 该方法接受一个正则表达式作为它的第一个参数。

  • Matcher 类:

    Matcher 对象是对输入字符串进行解释和匹配操作的引擎。与Pattern 类一样,Matcher 也没有公共构造方法。

  • 你需要调用 Pattern 对象的 matcher 方法来获得一个 Matcher 对象。

  • PatternSyntaxException:

    PatternSyntaxException 是一个非强制异常类,它表示一个正则表达式模式中的语法错误。

正则表达式语法:

在其他语言中,\\ 表示:我想要在正则表达式中插入一个普通的(字面上的)反斜杠,请不要给它任何特殊的意义。

在 Java 中,\\ 表示:我要插入一个正则表达式的反斜线,所以其后的字符具有特殊的意义。

所以,在其他的语言中(如 Perl),一个反斜杠 \ 就足以具有转义的作用,而在 Java 中正则表达式中则需要有两个反斜杠才能被解析为其他语言中的转义作用。也可以简单的理解在 Java 的正则表达式中,两个 \\ 代表其他语言中的一个 \,这也就是为什么表示一位数字的正则表达式是 \\d,而表示一个普通的反斜杠是 \\。

 

④题目集5(7-4)中Java集合框架应用的分析总结

7-4 统计Java程序中关键词的出现次数 (25 分)
 

编写程序统计一个输入的Java源码中关键字(区分大小写)出现的次数。说明如下:

  • Java中共有53个关键字(自行百度)
  • 从键盘输入一段源码,统计这段源码中出现的关键字的数量
  • 注释中出现的关键字不用统计
  • 字符串中出现的关键字不用统计
  • 统计出的关键字及数量按照关键字升序进行排序输出
  • 未输入源码则认为输入非法

输入格式:

输入Java源码字符串,可以一行或多行,以exit行作为结束标志

输出格式:

  • 当未输入源码时,程序输出Wrong Format
  • 当没有统计数据时,输出为空
  • 当有统计数据时,关键字按照升序排列,每行输出一个关键字及数量,格式为数量\t关键字

输入样例:

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

//Test public method
public HashMap(int initialCapacity) {
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
    }
    public HashMap(int initialCapacity, float loadFactor) {
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal initial capacity: " +
                                               initialCapacity);
        if (initialCapacity > MAXIMUM_CAPACITY)
            initialCapacity = MAXIMUM_CAPACITY;
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal load factor: " +
                                               loadFactor);
        this.loadFactor = loadFactor;
        this.threshold = tableSizeFor(initialCapacity);
    }
exit
 
结尾无空行

输出样例:

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

1	float
3	if
2	int
2	new
2	public
3	this
2	throw
 
结尾无空行
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String[] key = { "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const",
                "continue", "default", "do", "double", "else", "enum", "extends", "false", "final", "finally", "float",
                "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new",
                "null", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super",
                "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "void", "volatile",
                "while" };  //关键字数组key[]

        String in = null;
        StringBuilder sb = new StringBuilder();
        Map<String, Integer> hashMap = new HashMap<String, Integer>(); 
        //HashMap,这里的String用来存放出现的关键字key,Integer用来存放该关键字出现的次数
        int sum = 0;
        try (Scanner input = new Scanner(System.in)) {
            //读取行数据
            while (input != null) {  //如果还有输入内容
                in = input.nextLine();  //获得输入的数据
                if (!in.equals("exit")) {  //如果不是exit,表示输入还没有完毕,则处理该行输入内容
                if (!in.matches("(.*)//(.*)")) {  //如果该行没有出现注释符号“////这里matches里的内容意思为“只要出现//,无论几次”
                    sb.append(in + " "); 
                    //没出现“//”则表示该行内容都需要判断是否出现关键字,该行全部存入sb中
                } else {  //如果该行出现注释符号“//”,只在sb存入注释符号前的内容
                    String s0[] = in.split("//");  //在注释符号出将代码和注释内容分隔开
                    sb.append(s0[0] + " ");  //只存入该行注释符号前面的内容
                }
            } else {  //输入内容为exit
                break;  //结束获取输入文本
            }
            }
        }

        String str = sb.toString();  //sb里的内容放入字符串str中,下面这些个正则表达式大概的意思(接下一行)
        //大概意思是,出现了某个固定格式的字符串,就替换为“ ”,具体含义参看那个matches网址或者这些函数用法
        Pattern pattern = Pattern.compile("\"(.*?)\"");
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            str = str.replace(matcher.group(), " ");
            matcher = pattern.matcher(str);
        }
        pattern = Pattern.compile("/\\**(.*?)/");
        matcher = pattern.matcher(str);
        while (matcher.find()) {
            str = str.replace(matcher.group(), " ");
            matcher = pattern.matcher(str);
        }
        if (str.isEmpty()) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
        
        //这一串str.replace,前一个参数就是出现了这个字符,就把它换为第二个参数的字符
        str = str.replace("[", " "); //比如出现了“[”,就把它换成空格
        str = str.replace("]", " ");
        str = str.replace("+", "a");
        str = str.replace("-", "a");
        str = str.replace("*", "a"); //出现了“*”,就把它换成“a”
        str = str.replace("/", "a");
        str = str.replace("=", "a");
        str = str.replace(":", "a");
        str = str.replace("!", "a");
        str = str.replace("\\", "a");
        str = str.replace(">", "a");
        str = str.replaceAll("[^a-zA-Z]", " "); //除了小写a到z、大写A-Z字母,其他字符全换成空格

        String[] s1 = str.split("[  ' ']"); //分割为单个单词字符串
        for (int i = 0; i < s1.length; i++) {  //两个循环,用于核对是否出现关键字
            for (int j = 0; j < key.length; j++) {
                if (s1[i].equals(key[j])) {  //如果出现了key[]中的某个关键字
                    hashMap.put(key[j], 0);  
                    //把出现的关键字添加到hashMap里,第一个参数为key,第二个为value,即第一个为出现的(接下一行)
                    //关键字,第二个值为出现的次数,在这里先置为0,(这个部分好像也可以改进)
                }
            }
        }
        for (int i = 0; i < s1.length; i++) {  //两个循环,用于核对各关键字出现的次数
            for (int j = 0; j < key.length; j++)
                if (s1[i].equals(key[j])) {  //如果出现了key[]中的某个关键字
                    sum = hashMap.get(key[j]);  
                    //获取hashMap中key值对应的value值,即hashMap中的第二个参数所表示的 该关键字出现的次数
                    hashMap.put(key[j], sum + 1);  //value值+1,即该关键词出现的次数+1
                }
        }
        Set<String> set = hashMap.keySet(); //放入set集合
        Object[] arr = set.toArray();  //获取set集合数组形式
        Arrays.sort(arr);  //排序
        for (Object k : arr) {  //输出
            System.out.println(hashMap.get(k) + "\t" + k);
        }
    }
}

 

(3)总结:

经过这三个题目集的练习,我学会了通过改变类的设计提高代码的可读性。

学会了渐进式图形设计的思路与运用(继承、封装、多态、接口)。

学会了运用简单的java正则表达技术。

 

 

posted @ 2021-11-13 20:05  玛卡巴卡卡  阅读(57)  评论(0)    收藏  举报