OOP面向对象第一次作业总结

OOP面向对象第一次作业总结

目录

 · 前言

 · 设计与分析

 · 踩坑心得

 · 改进建议

 · 总结

 

一、前言

此次作业总结为前三次PTAjava训练题集,前三次题目较为简单,前两次还大部分为C语言做过的题目,第三此作业涉及面向对象相关知识,如类的设计等等。

主要目的是让我们配置好java环境,初步熟悉java语言,以及从C语言粗浅的语法学习向程序设计学习过渡,进而启发后续更深层次的面向对象学习。探寻适合自己,适合编程学习的一套更为成熟高效的方法。在我们学科众多,项目与知识学习并行的模式中,找到一个更为高效更为适合自己的方法与道路。

南昌航空大学-222019-刘嘉轩

2023-03-23

二、设计与分析 

pta第一次7-8

题目为:

 

 

 主要问题:

不够熟悉java中关于Stringbuider中相关用法

以及对类中方法使用流程以及效果较陌生

启示:

多用一些Java中方便的容器及工具,以增加开发效率

增加对java的熟练程度

 

最终代码

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String str = input.nextLine();
        String sign = input.nextLine();
        StringBuilder stringBuilder = new StringBuilder();
        for (int j = 0; j < str.length(); j++) {
            if(sign.indexOf(str.charAt(j)) == -1){
                stringBuilder.append(str.charAt(j));
            }
        }
        System.out.println(stringBuilder.toString());
    }
}

pta第三次7-3

题目为:

 

 

 

中途错误:

 

小结:在boolean isLeapyear()中想直接修改二月的天数,后违反了题意,将boolean类改为了void类。

   在下一题Date优化题目更复杂的情境中中才明白得用boolean类更好操作,仔细一想,这应该是单一职责原则,在细节中的体现。

 

最终代码:

import java.util.Scanner;
class Date{
    private int year = 0;
    private int month = 0;
    private int day = 0;
    private int[] mon_max = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    void isLeapYear() {
        if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
            mon_max[2] = 29;
        }
    }
    boolean checkInputValidity(){
        if( month >12 ||month < 1 || day > mon_max[month] || day < 1 || year > 2000 || year < 1900 ){
            System.out.println("Date Format is Wrong");
            return false;
        }else{
            return true;
        }
    }
    void getNextDate(){
        int n_year = year, n_month = month, n_day = day;
        isLeapYear();
       if(checkInputValidity()){
           if(month == 12 && day == 31){
               n_day = 1;
               n_month = 1;
               n_year = year + 1;
           }
           else if(day == mon_max[month]){
               n_month = month + 1;
               n_day = 1;

           }else{
               n_day = day + 1;
               n_month = month;
               n_year = year;
           }
           System.out.println("Next day is:" + n_year +"-"+ n_month + "-" + n_day);

       }
    }
    Date(){

    }
    Date(int year,int month,int day){
        this.day = day;
        this.month = month;
        this.year = year;
    }
    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 class Main {
    public static void main(String[] args) {
        Date date = new Date();
        Scanner scanner = new Scanner(System.in);

        date.setYear(scanner.nextInt());
        date.setMonth(scanner.nextInt());
        date.setDay(scanner.nextInt());
        date.getNextDate();
    }
}

 

pta第二次7-8

题目为:

 

 中途错误

 

主要为中途一次 :判断直角三角形时,没考虑到计算机不能精确计算小数

 如图中划线部分

public static boolean if_right(double a,double b,double c){
        if(a * a + b * b - c * c < 0.1)return true;
        else if(c * c + b * b - a * a < 0.1)return true;
        else if(a * a + c * c - b * b< 0.1)return true;
        else return false;
    }

 

 

 最终代码:

import java.util.Arrays;
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double a = input.nextDouble(), b = input.nextDouble();
        double c = input.nextDouble();
        if(a < 1 || a > 200 || b < 1 || b > 200 || c < 1 || c > 200){
            System.out.println("Wrong Format");
        }
        else if(!if_tri(a,b,c)){
            System.out.println("Not a triangle");
        }
        else if(a == b && a == c && b == c){
            System.out.println("Equilateral triangle");
        }
        else if(if_angle(a,b,c) && if_right(a,b,c)){
            System.out.println("Isosceles right-angled triangle");
        }
        else if(if_angle(a,b,c)){
            System.out.println("Isosceles triangle");
        }
        else if (if_right(a,b,c)){
            System.out.println("Right-angled triangle");
        }
        else System.out.println("General triangle");
 
 
    }
    public static  boolean if_tri(double a, double b, double c){
        if(a + b <= c)return false;
        else if(a + c <= b)return false;
        else if(b + c <= a)return false;
        else return true;
    }
    public static boolean if_right(double a,double b,double c){
        if(a * a + b * b - c * c < 0.1)return true;
        else if(c * c + b * b - a * a < 0.1)return true;
        else if(a * a + c * c - b * b< 0.1)return true;
        else return false;
    }
    public static boolean if_angle(double a,double b,double c){
        if(a == b)return true;
        else if(b == c)return true;
        else if(a == c)return true;
        else return false;
    }
}

pta第三次7-4

题目为:

 

类的设计:

 

 

public boolean checkInputValidity();//检测输入的年、月、日是否合法
public boolean isLeapYear(int year);//判断year是否为闰年
public DateUtil getNextNDays(int n);//取得year-month-day的下n天日期
public DateUtil getPreviousNDays(int n);//取得year-month-day的前n天日期
public boolean compareDates(DateUtil date);//比较当前日期与date的大小(先后)
public boolean equalTwoDates(DateUtil date);//判断两个日期是否相等
public int getDaysofDates(DateUtil date);//求当前日期与date之间相差的天数
public String showDate();//以“year-month-day”格式返回日期值

 

中途错误:

 

 

主要为中途一次 :2月天数未初始化为28天

         少了下面代码中划线的两段代码

 

public int getDaysofDates(DateUtil date) {
        int n = 0;
        if (equalTwoDates(date)) {
            return n;
        } else if (!compareDates(date)) {
            for (;;) {
                if (equalTwoDates(date)) break;
                n++;
                if (isLeapYear(year)) {
                    mon_max[2] = 29;
                } else {
                    mon_max[2] = 28;
                }
                if (day < mon_max[month]) day++;
                else if (month == 12 && day == mon_max[12]) {
                    year++;
                    if (isLeapYear(year)) {
                        mon_max[2] = 29;
                    } else {
                        mon_max[2] = 28;
                    }
                    month = 1;
                    day = 1;
                } else if (day == mon_max[month]) {
                    month++;
                    day = 1;
                }
            }
        } else if (compareDates(date)) {
            if (isLeapYear(year)) {
                mon_max[2] = 29;
            }
            for (; ; ) {
                if (equalTwoDates(date)) break;
                n++;
                if (isLeapYear(year)) {
                    mon_max[2] = 29;
                } else {
                    mon_max[2] = 28;
                }
                if (day > 1) day--;
                else if (month == 1 && day == 1) {
                    year--;
                    if (isLeapYear(year)) {
                        mon_max[2] = 29;
                    } else {
                        mon_max[2] = 28;
                    }
                    month = 12;
                    day = 31;
                } else if (day == 1) {
                    month--;
                    day = mon_max[month];
                }
            }
        }
        return n;
    }//求当前日期与date之间相差的天数

 

 

 最终代码:

import java.util.Scanner;
class DateUtil{
    private int year = 0;
    private int month = 0;
    private int day = 0;
    private int[] mon_max = {0,31,28,31,30,31,30,31,31,30,31,30,31};

    public boolean checkInputValidity(){
        if( month >12 ||month < 1 || day > mon_max[month] || day < 1 || year > 2020 || year < 1820 ){
            return false;
        }else{
            return true;
        }
    }//检测输入的年、月、日是否合法
    public boolean isLeapYear(int year){
        if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
            return true;
        }else{
            return false;
        }
    }//判断year是否为闰年
    public DateUtil getNextNDays(int n){
        int new_year = year;
        int new_day = day;
        int new_month = month;
        if(isLeapYear(year)){
            mon_max[2] = 29;
        }
        for (int i = 0; i < n; i++) {
            if (new_day < mon_max[new_month])new_day++;
            else if(new_month == 12 && new_day == mon_max[12]){
                new_year++;
                if(isLeapYear(new_year)){
                    mon_max[2] = 29;
                }else{
                    mon_max[2] = 28;
                }
                new_month = 1;
                new_day = 1;
            }
            else if(new_day == mon_max[new_month]){
                new_month++;
                new_day = 1;
            }

        }
        DateUtil new_Date = new DateUtil(new_year, new_month, new_day);
        return new_Date;
    }//取得year-month-day的下n天日期
    public DateUtil getPreviousNDays(int n){
        int new_year = year;
        int new_day = day;
        int new_month = month;
        if(isLeapYear(year)){
            mon_max[2] = 29;
        }
        for (int i = 0; i < n; i++) {
            if (new_day > 1)new_day--;
            else if(new_month == 1 && new_day == 1){
                new_year--;
                if(isLeapYear(new_year)){
                    mon_max[2] = 29;
                }else{
                    mon_max[2] = 28;
                }
                new_month = 12;
                new_day = 31;
            }
            else if(new_day == 1){
                new_month--;
                new_day = mon_max[new_month];
            }

        }
        DateUtil new_Date = new DateUtil(new_year, new_month, new_day);
        return new_Date;
    }//取得year-month-day的前n天日期
    public boolean compareDates(DateUtil date){
        //比形参大或相等,返回true
        if(year > date.year)return true;
        else if(year < date.year) return false;
        else if(month > date.month)return true;
        else if(month < date.month)return false;
        else if(day > date.day)return true;
        else if(day < date.day)return false;
        else return true;
    }//比较当前日期与date的大小(先后)
    public boolean equalTwoDates(DateUtil date){
        if(year == date.year && month == date.month && day == date.day){
            return true;
        }else return false;
    }//判断两个日期是否相等
    public int getDaysofDates(DateUtil date) {
        int n = 0;
        if (equalTwoDates(date)) {
            return n;
        } else if (!compareDates(date)) {
            for (;;) {
                if (equalTwoDates(date)) break;
                n++;
                if (isLeapYear(year)) {
                    mon_max[2] = 29;
                } else {
                    mon_max[2] = 28;
                }
                if (day < mon_max[month]) day++;
                else if (month == 12 && day == mon_max[12]) {
                    year++;
                    if (isLeapYear(year)) {
                        mon_max[2] = 29;
                    } else {
                        mon_max[2] = 28;
                    }
                    month = 1;
                    day = 1;
                } else if (day == mon_max[month]) {
                    month++;
                    day = 1;
                }
            }
        } else if (compareDates(date)) {
            if (isLeapYear(year)) {
                mon_max[2] = 29;
            }
            for (; ; ) {
                if (equalTwoDates(date)) break;
                n++;
                if (isLeapYear(year)) {
                    mon_max[2] = 29;
                } else {
                    mon_max[2] = 28;
                }
                if (day > 1) day--;
                else if (month == 1 && day == 1) {
                    year--;
                    if (isLeapYear(year)) {
                        mon_max[2] = 29;
                    } else {
                        mon_max[2] = 28;
                    }
                    month = 12;
                    day = 31;
                } else if (day == 1) {
                    month--;
                    day = mon_max[month];
                }
            }
        }
        return n;
    }//求当前日期与date之间相差的天数
    public String showDate(){
        return(year + "-" + month + "-" + day);
    }//以“year-month-day”格式返回日期值

    DateUtil(){

    }
    DateUtil(int year,int month,int day){
        this.day = day;
        this.month = month;
        this.year = year;
    }
    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 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);
        }
    }
}

最终效果:

 

 

三、踩坑心得

  1. 注意单一变量原则(srp)(如上文提到boolen函数里就值判断真假,不许要输出或改动数据之类的),注意java书写规范(命名以及类名的书写,代码可读性)
  2. 注意计算机的浮点数是有误差的,无法进行精确的计算。(在pta第二次判断三角形题目中,有一个判断是否为等腰直角三角形,因过多精确的小数,就需要设置精度,否则无法实现相等)
  3. 注意熟练运用各种测试方法来确保程序质量(如边界值测试等许多的测试方法)
  4. 主动去熟悉并熟练运用StringBuider等java里较方便的工具(多多看java里面类的文档)
  5. 多看java里面自带的类的文档

四、改进建议

  1. 英语基础要好,努力学英语,英语不仅在整个大学的学习有着十分重要的作用,更功利更现实一点,在语言学习过程中,对自己读文档,以及更好的学习语言起着关键的作用。
  2. 养成良好的写代码的习惯,参考阿里巴巴开发手册(如类,函数,变量的命名规则以及写注释的习惯等等)
  3. 提高学习的主动性,主动去探索更多未知的领域,积极去学习(hashset,hashmap,什么什么之类的)
  4. 合理安排时间,在众多学科中以及项目中也要合理安排时间,提高学习效率(提高听课效率,提高自习效率,增加自习的专注度,主动积极的去思考)

五、总结

匆匆略过C语言,自以为基础并不牢固,以及还有许多都没学会。就想着一定要好好学习java。努力运用各java较C语言更为简便的类和方法。更加踏实更加脚踏实地的学习语言,打好基础,提高编程思想,程序设计思想。提高解决实际问题的能力。我一直看重内在能力的提升,我一直积极的从表面的题集中尽量在每一题的思考,训练中,提升自己对编程语言学习的技巧,以及增加自己对编程的理解,到最终在利用这些表面的训练,真正提升自己实实在在的编程能力。路漫漫其修远兮,吾将上下而求索。漫漫的编程学习道路上,过程将注定并不全是愉快的,唯有我们保持一颗追求卓越的心,方可不屈不挠,在编程道路上取得很好的能力提升。

最近看了一篇文章,大学教育能给你带来什么,如果仅仅是像很多人认为一些简单编程语言语法,或者是认为来大学混日子,每次作业简单应付,大可不必来大学,一些不错的网课,一些职校都可以满足你的需求。像段老师说的他教的我们都听不懂,我们懂的他都不教,这样一种启发性的教育,唯有我们认真去思考,课后愿意花功夫,才可更好的提升实力。

 

posted @ 2023-03-26 11:31  Keep-G0ing  阅读(38)  评论(0)    收藏  举报