BLOG-1

2022/4/10 6:15:30

本文主要是介绍BLOG-1,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

开学初识java到今天不知不觉已经快两个月了,从刚开始对java的不熟悉,不喜欢,不理解,到如今已渐渐了解了java的基本用法和它的部分优势。自己的进步在点滴积累,以下是我对自己近期学习的总结。

一.从c语言到java的转变

在大一上学期第一次学习了编程语言--c语言,尽管对c语言的掌握情况不算优秀,但基本的代码书写还算得熟练。因此,在开学前两周的java自学中碰了不少壁。从最简单的输入输出说起,c语言只需简单的scanf函数即可完成输入,而在java中却需要首先import java.util.Scanner包,再创建Scanner类对象,最后利用next()系列方法实现输入。输出也从printf转变为了System.out.println。对于输入的内容与c语言一致,需要变量来保存数据。变量的定义方式也c语言一致,<类型名称><变量名称> = <初始值>的形式。具体程序如下:

import java.util.Scanner;

 

public class Main {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        int a = input.nextInt();

        System.out.println(a);

    }

}

在接下来对浮点数的学习中也与c语言一致,在学习到类型转换时也让我重新回忆起了c语言中的内容。具体代码如下:

System.out.println((int)1,7);

将浮点数1.7转换为了1,截去了小数部分。

System.out.println((double) 1 / 2);

显示为0.5,因为1首先被转换为1.0,然后除以2得到0.5。

在选择的学习中,选择语句if语句,双分支的if-else语句,switch语句与c语言大同小异,循环学习中循环语句while,do while,for语句也是如此。直到学习字符串章节开始出现困难。对于字符串的定义,首先需创建String对象,再对此对象进行赋值。具体代码如下:

String str1 = new String("Hello Java");

String str2 = new String(str1);

String str3 = input.nextLine();

这分别表示了直接传入字符串,将一个字符串赋值给另一个字符串以及在控制台输入字符串的三种情况。

之后关于字符串的方法我也花了相当一部分时间去记忆与运用。常用方法如下:

str.length();//字符串长度获取

str.charAt(index);//获取指定位置的字符串

str.concat(str1);//连接两个字符串

str.equals(str1);判断两字符串是否相等

str.substring(beginindex,endindex+1)//获取子字符串

学习到方法时,我立马想起了c语言中的函数,两者主要的作用都在于可以使程序变得更简单和更清晰,提高了代码的重用性。对方法的理解首先从定义开始,在方法头需给出修饰符、返回值类型、方法名和形式参数,在方法体中则给出具体算法。方法在调用时可根据具体返回类型灵活使用。具体代码如下:

public int max (a,b) {

    int result = 0;

    if(a >= b)

        result = a;

    else

        result = b;

    return result;

}

学习数组时,也让我感受到数组在c语言和Java中的区别。在c中:数组[]中要给定一个常量,不能是变量。如果想不指定数组的确定大小就必须初始化,数组元素个数根据初始化内容来决定。在java中,一维数组和c中类似,二维数组可以不指定二维长度,每一行必须加{},有两两种创建和赋值的方法。具体代码如下:

c语言:

int arr[100];

Java:

int []arr = new int[100];

 

二.java独有特征

1.类与对象

对象代表一个单独的个体,它具有自己的属性(好比一个人有自己的名字),而对象的行为是通过方法实现的,调用对象的一个方法就是要求对象完成一个动作,例如定义一个圆对象,再定义一个getArea方法来返回圆的面积,即可通过调用getArea方法来求得圆的面积。

我们通常使用一个类来定义同一类型的对象类是一个模板,可以定义对象的数据域和方法。而对象就是类中的一个实例,一个类可以创建多个对象。

定义一个类需要权限修饰符,修饰符包括以下:

● public(公共的):具有公共访问权限。如果类中的属性或方法被public修饰,则此类中的属性或方法可以被任何类调用。

● protected(保护的) :被其修饰的类、属性、方法只能被类本身及子类访问,即使子类在不同包中也可以访问,但不能访问与子类同包的其他类。对不管是不是同包的子类公开,对同包下没有父子关系的其他类私有

● default(默认的(也可以不写)):具有包访问权限,被此修饰的属性或方法只允许在同一个包中进行访问。同包下相当于公开。

● private(私有的):被其修饰的类、属性、方法只能被该类对象访问,其子类不能访问,更不能跨包访问。

定义类需要<权限修饰符> class <类名>,而且一个类中必须包含构造方法,构造方法是java中用来初始化对象的方法,它与类同名且不需要返回类型。构造方法分为有参和无参,当出现同名的构造方法(即构造方法的重载时)系统会根据不同的参数来选择。

定义类完成后,可以创建类的对象来实现方法的调用。创建对象需要<类名> <对象名> = new <类名>();创建对象后可以通过<对象名>.属性来调用其属性或者用<对象名>.方法来调用方法。

实例如下:

public class Main {

    public static void main(String[] args) {

        double radius = 5.5;

        Circle circle = new Circle(radius);

        circle.getArea();

        }

    }

class Circle{

    private double radius;

 

    public Circle(double radius) {

        this.radius = radius;

    }

 

    public double getArea(){

        return Math.PI*radius*radius;

    }

}

  1. 继承

当定义了几个具有一些相同属性和方法时,为了防止多次出现重复代码,就可以利用到继承。继承就是子类继承父类的特征和行为,使得子类对象具有父类的实例域和方法,或子类从父类继承方法,使得子类具有父类相同的行为。

继承在java中只能是单继承。

继承过程中若子类对父类的方法不满意,可以重写此方法,调用此方法时优先调用子类。

具体代码如下:

class People{

    private int age;

    private String name;

 

    public People(int age,String name) {

        this.age = age;

        this.name = name;

    }

 

    public void id() {

        System.out.println(age+" "+name);

    }

}

 

class Man extends People{

    public Man(int age, String name) {

        super(age, name);

    }

 

    public void show(){

        System.out.println("I am a man");

    }

}

public class Test {

    public static void main(String[] args) {

        String str = "Zhaolv";

        Man man = new Man(19,str);

        man.id();

        man.show();

    }

}

  1. 多态

 

class People{

    private int age;

    private String name;

 

    public People(int age,String name) {

        this.age = age;

        this.name = name;

    }

 

    public void id() {

        System.out.println(age+" "+name);

    }

 

    public void show() {

    }

}

 

class Man extends People{

    public Man(int age, String name) {

        super(age, name);

    }

 

    public void show(){

        System.out.println("I am a man");

    }

}

 

class Woman extends People{

    public Woman(int age, String name) {

        super(age, name);

    }

 

    public void show(){

        System.out.println("I am a woman");

    }

}

public class Main {

    public static void main(String[] args) {

        String str = "Zhaolv";

        People man = new Man(19,str);

        People woman = new Woman(29,str);

        man.id();

        man.show();

        woman.show();

    }

}

 

 

 

三.总结前三次题目集

(1)前言:

题目集1:

知识点:if语句,if-else语句,浮点数,for循环语句,swtich语句,数组的定义。

题量:小

难度:易

题目集2:

知识点:for循环语句,if语句,字符串方法调用

题量:中

难度:中

题目集3:

知识点:类与对象,

题量:中

难度:较大

(2)设计与分析:

 

题目集2:

 

 

7-2 串口字符解析:首先用for循环判断1的个数,如果串口字符全为1或者串口字符中字符数不足10位,输出null data;然后用for循环计算串口字符每个字符相加后的和sum,sum取余2即可判断是否为奇校验。若为偶效验且最后数字为1,则输出no+":"+"parity check error";若最后数字为0,则输出no+":"+"validate error";若为奇效验且最后数字为1,则输出no + ":" + str.substring(j + 1, j + 9)。输出一次no+1,并跳过这10位字符。

 

 

 

题目集3:

 

7-1:用类解一元二次方程:根据题目提供的Main函数创建类QuadraticEquation,当方程只有一个解时,解为(-b+Math.sqrt(b * b - 4 * a * c))/2*a;当方程有两个解时,输出(-b+Math.sqrt(b * b - 4 * a * c))/2*a和(-b-Math.sqrt(b * b - 4 * a * c))/2*a两个解。

 

 

7-2:日期类设计:根据题目提供的Main函数创建类DateUtil,checkInputValidity方法检测输入的年、月、日是否合法;isLeapYear方法判断year是否为闰年;getNextNDay方法中先用while循环判断年数,再用for循环确定月份和日期;getPreviousNDays方法中大体依照getNextNDay的方法,但在年的计算中需注意二月份的特殊判断;compareDates方法比较两个日期大小;equalTwoDates方法判断两日期是否相等;sum方法计算从0001年1月1日到当前日期的天数;getDaysofDates利用sum方法可轻松计算相隔天数;showDate方法以“year-month-day”格式返回日期值。

 

 

7-3:日期问题面向对象设计(聚合一):本题核心算法与7-2一致,只是在调用对象时需要用其他类进行调用。

(3)踩坑心得

 

题目集2:

 

7-2:遇到的问题:

  1. 在进行奇偶判断时,应该将所有数字相加而不是仅仅判断第9位数字是否为奇数
  2. 输出时对每个串口的编号忽略

 

题目集3:

 

7-1:未出现问题

源码如下:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        char[] arr = new char[100];
        int length = str.length();
        int num = 0;
        int sum = 0;
        int sum1 = 0;
        int no = 1;
        for (int i = 0; i < length; i++) {
            arr[i] = str.charAt(i);
            if (arr[i] == '1')
                num++;
        }
        if (length < 11 || num == length)
            System.out.println("null data");
        else{
            for(int j = 0;j < length;j++){
                if(arr[j] == '0'){
                    for(int k = j+1;k < j+10;k++){
                        sum = sum + arr[k];
                    }
                    sum1 = sum % 2;
                    sum = 0;
                    if(sum1 == 0&&arr[j+10] == '1'){
                        System.out.println(no+":"+"parity check error");
                        no++;
                    }
                    if(arr[j+10] == '0'){
                        System.out.println(no+":"+"validate error");
                        no++;
                    }
                    if(arr[j+10] == '1'&&sum1 != 0) {
                        System.out.println(no + ":" + str.substring(j + 1, j + 9));
                        no++;
                    }
                    j = j + 10;
                }
            }
        }
    }
}

 

7-2:

  1. 在计算后n天时,在利用while循环计算年数的过程中,没有判断闰年与平年的年数而循环错误
  2. 在计算前n天时,在利用while循环计算年数的过程中,没有考虑到2月的特殊情况

源码如下:

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 int getYear() {
        return year;
    }

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

    public int getMonth() {
        return month;
    }

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

    public int getDay() {
        return day;
    }

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

    public boolean isLeapYear(int year){
        if(year % 400==0||(year%4==0&&year%100!=0))
            return true;
        else
            return false;
    }

    public boolean checkInputValidity() {
        int[] d=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
        if(year>=1820&&year<=2020&&month>=1&&month<=12&&day>=1&&day <= d[month])
            return true;
        else
            return false;
    }

    public int yearnum(int year){
        if(isLeapYear(year))
            return 366;
        else
            return 365;
    }

    public DateUtil getNextNDays(int n) {
        int[] d=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
        while (n > yearnum(year)) {
            if (isLeapYear(year)) {
                n = n - 366;
                year++;
            } else {
                n = n - 365;
                year++;
            }
        }

        for(int i = 1; i <= n; i++) {
            if(isLeapYear(year)) {
                d[2] = 29;
            }
            day++;
            if(day > d[month]) {
                month++;
                day = 1;
                if(month > 12){
                    month = 1;
                    year++;
                }
            }
        }
        return  new DateUtil(year, month, day);
    }

    public DateUtil getPreviousNDays(int n) {
        int[] d=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
        while (n > yearnum(year-1)){
            if(isLeapYear(year) && month > 2){
                n = n -366;
                year--;
            }
            else if(isLeapYear(year-1) && month <= 2) {
                n = n -366;
                year--;
            }
            else {
                n = n -365;
                year--;
            }
        }

        for(int i = 1; i <= n; i++){
            day--;
            if(day <= 0){
                month--;
                if(month <= 0) {
                    month = 12;
                    year--;
                }
                if(isLeapYear(year) && month == 2)
                    day = 29;
                else
                    day = d[month];
            }
        }
        return  new DateUtil(year, month, day);
    }

    public boolean compareDates(DateUtil date) {
        if(this.year > date.year)
            return true;
        else if(this.year == date.year ) {
            if(this.month > date.month)
                return true;
            else if(this.month == date.month){
                if(this.day == date.day)
                    return true;
                else
                    return false;
            }
            else
                return false;
        }
        else
            return false;
    }

    public boolean equalTwoDates(DateUtil date) {
        if(this.year == date.year && this.month == date.month && this.day == date.day)
            return true;
        else
            return false;
    }

    public int sum(DateUtil date){
        int num = 0;
        int[] d=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
        for(int i = 1 ;i < date.year;i++) {
            if(isLeapYear(i))
                num = num + 366;
            else
                num = num +365;
        }
        if(isLeapYear(date.year))
            d[2] = 29;
        for(int j = 1;j < date.month;j++) {
            num = num + d[j];
        }
        num = num + date.day;
        return num;
    }

    public int getDaysofDates(DateUtil date) {
        int[] d = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int amount = 0;
        if (compareDates(date) == false)
            amount = sum(date) - sum(this);
        else
            amount = sum(this) - sum(date);
        return amount;
    }

    public String showDate() {
        return year + "-" + month + "-" + day;
    }
}

 

7-3:

  1. 输出时,格式更改
  2. 错误使用类私有变量导致程序过不了编译阶段

源码如下:

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.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.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(fromDate.getDaysofDates(toDate));
            } else {
                System.out.println("Wrong Format");
                System.exit(0);
            }
        }
        else{
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }
}

class DateUtil{
    Day day;

    public DateUtil() {
    }

    public DateUtil(int y, int m, int d){
        this.day = new Day(y,m,d);
    }

    public Day getDay() {
        return day;
    }

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

    /*public void setDayMin() {
       day.setValue(1);
    }*/

    /*public void setDatMax() {
        int[] d=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
        if(this.getDay().getMonth().getYear().isLeapYear(getDay().getMonth().getYear().getValue()))
            d[2] = 29;
        this.getDay().getValue() = d[this.getDay().getMonth().getValue()];

    }*/

    /*public boolean isLeapYear(int year){
        if(year % 400==0||(year%4==0&&year%100!=0))
            return true;
        else
            return false;
    }*/

    public boolean checkInputValidity() {
        int[] d=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
        if(this.getDay().getMonth().getYear().isLeapYear(getDay().getMonth().getYear().getValue()))
            d[2] = 29;
        if(this.getDay().getMonth().getYear().validate() && this.getDay().getMonth().validate() && this.getDay().validate())
            return true;
        else
            return false;
    }

    public int yearnum(int y){
        if(this.getDay().getMonth().getYear().isLeapYear(getDay().getMonth().getYear().getValue()))
            return 366;
        else
            return 365;
    }

    public DateUtil getNextNDays(int n) {
        int[] d=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
        while (n > yearnum(getDay().getMonth().getYear().getValue())) {
            if (getDay().getMonth().getYear().isLeapYear(getDay().getMonth().getYear().getValue())) {
                n = n - 366;
                getDay().getMonth().getYear().yearIncement();
            } else {
                n = n - 365;
                getDay().getMonth().getYear().yearIncement();
            }
        }

        for(int i = 1; i <= n; i++) {
            if(getDay().getMonth().getYear().isLeapYear(getDay().getMonth().getYear().getValue())) {
                d[2] = 29;
            }
            getDay().dayIncement();
            if(getDay().getValue() > d[getDay().getMonth().getValue()]) {
                getDay().getMonth().monthIncement();
                getDay().resetMin();
                if(getDay().getMonth().getValue() > 12){
                    getDay().getMonth().resetMin();
                    getDay().getMonth().getYear().yearIncement();
                }
            }
        }
        return  new DateUtil(getDay().getMonth().getYear().getValue(), getDay().getMonth().getValue(), getDay().getValue());
    }

    public DateUtil getPreviousNDays(int n) {
        int[] d=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
        while (n > yearnum(day.month.year.getValue()-1)){
            if(this.getDay().getMonth().getYear().isLeapYear(getDay().getMonth().getYear().getValue()) && getDay().getMonth().getValue() > 2){
                n = n -366;
                getDay().getMonth().getYear().yearReduction();
            }
            else if(this.getDay().getMonth().getYear().isLeapYear(getDay().getMonth().getYear().getValue() - 1) && getDay().getMonth().getValue() <= 2) {
                n = n -366;
                getDay().getMonth().getYear().yearReduction();
            }
            else {
                n = n -365;
                getDay().getMonth().getYear().yearReduction();
            }
        }

        for(int i = 1; i <= n; i++){
            getDay().dayReduction();
            if(getDay().getValue() <= 0){
                getDay().getMonth().monthReduction();
                if(getDay().getMonth().getValue() <= 0) {
                    getDay().getMonth().resetMax();
                    getDay().getMonth().getYear().yearReduction();
                }
                getDay().resetMax();
            }
        }
        return  new DateUtil(getDay().getMonth().getYear().getValue(), getDay().getMonth().getValue(), getDay().getValue());
    }

    public boolean compareDates(DateUtil date) {
        if(this.getDay().getMonth().getYear().getValue() > date.getDay().getMonth().getYear().getValue())
            return true;
        else if(this.getDay().getMonth().getYear().getValue() == date.getDay().getMonth().getYear().getValue() ) {
            if(this.getDay().getMonth().getValue() > date.getDay().getMonth().getValue())
                return true;
            else if(this.getDay().getMonth().getValue() == date.getDay().getMonth().getValue()){
                if(this.getDay().getValue() == date.getDay().getValue())
                    return true;
                else
                    return false;
            }
            else
                return false;
        }
        else
            return false;
    }

    public boolean equalTwoDates(DateUtil date) {
        if(this.getDay().getMonth().getYear().getValue() == date.getDay().getMonth().getYear().getValue() && this.getDay().getMonth() == date.getDay().getMonth() && this.getDay().getValue() == date.getDay().getValue())
            return true;
        else
            return false;
    }

    public int sum(DateUtil date){
        int num = 0;
        int[] d=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
        for(int i = 1 ;i < date.getDay().getMonth().getYear().getValue(); i++) {
            if(this.getDay().getMonth().getYear().isLeapYear(i))
                num = num + 366;
            else
                num = num +365;
        }
        if(this.getDay().getMonth().getYear().isLeapYear(date.getDay().getMonth().getYear().getValue()))
            d[2] = 29;
        for(int j = 1;j < date.getDay().getMonth().getValue();j++) {
            num = num + d[j];
        }
        num = num + date.getDay().getValue();
        return num;
    }

    public int getDaysofDates(DateUtil date) {
        int[] d = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int amount = 0;
        if (compareDates(date) == false)
            amount = sum(date) - sum(this);
        else
            amount = sum(this) - sum(date);
        return Math.abs(amount);
    }

    public String showDate() {
        return getDay().getMonth().getYear().getValue() + "-" + getDay().getMonth().getValue() + "-" + getDay().getValue();
    }
}

class Year {
    private int value;

    public Year() {
    }

    public Year(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

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

    public boolean validate() {
        if(value >= 1900 && value <= 2050 )
            return true;
        else
            return false;
    }

    public boolean isLeapYear(int value) {
        if(value % 400==0||(value % 4 == 0 && value % 100 != 0 ))
            return true;
        else
            return false;
    }

    public void yearIncement() {
        value++;
    }

    public void yearReduction() {
        value--;
    }
}

class Month {
    private int value;
    Year year;

    public Month() {
    }

    public Month(int yearValue, int monthValue) {
        this.year = new Year(yearValue);
        this.value = 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;
    }

    public void resetMin() {
        value = 1;
    }

    public void resetMax() {
        value = 12;
    }

    public boolean validate() {
        if(value >= 1 && value <= 12 )
            return true;
        else
            return false;
    }

    public void monthIncement() {
        value++;
    }

    public void monthReduction() {
        value--;
    }
}

class Day {
    private int value;
    Month month;

    public Day() {
    }

    public Day(int yearValue, int monthValue, int dayValue) {
        this.value = dayValue;
        this.month = new Month(yearValue,monthValue);
    }

    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;
    }

    public void resetMin () {
        value = 1;
    }

    public void resetMax () {
        int[] d=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
        if(month.getYear().isLeapYear(month.getYear().getValue())) {
            d[2] = 29;
        }
        this.value = d[month.getValue()];
    }

    public boolean validate() {
        int[] d=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
        if(month.getYear().isLeapYear(month.getYear().getValue())){
            d[2] = 29;
        }
        if(value >= 1 && value <= d[month.value] )
            return true;
        else
            return false;
    }

    public void dayIncement() {
        value++;
    }

    public void dayReduction() {
        value--;
    }
}

 

4.改进建议

对题目集2:7-2没有改进意见

对题目集3:7-1没有改进意见

对题目集3:7-2没有改进意见

对题目集3:7-3类的使用太过复杂,改为7-4中类的使用方法更佳。

 

5.总结:对于前三次题目集,每个题目集都能带给我不同的收获。第一次题目集虽然对难度不大,却对前八章学习的知识点进行了较为全面的复习,让我逐渐分清c语言与java的差别。第二次题目集重点考察字符串,对于字符串中方法的使用让我记忆犹新,实实在在将书本中的知识运用在了自己的代码中,并且此次题目集难度加大不少,对于边界值的测试让我花费了不少时间。第三词题目集难度进一步加大,光是7-2就花费了我一天多的时间,在想出后n天的计算方法后,一味地搬到求前n天上,导致忽略了二月份这一特殊情况,花费了一上午才修改正确。在计算两天数的差值时,一开始选择先判断两日期的大小,再运用for循环从小的日期开始一天一天加到大的日期,但是在过程中对于闰年出现后2月份的计算一直出现错误,不得已而放弃,最后使用现在的方法。虽然花费了很长的时间,但自己对类的使用越发熟练了。

存在的不足:在设计多个类时,容易出现许多耦合的地方,解耦做的不足。对于程序的封装性理解不够深刻。

 

第一次写博客,如有错误敬请提出!

 



这篇关于BLOG-1的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程