oop前三次java作业总结
前言
Pta的作业已经做到完了前三次了。
第一次的作业,我个人认为是最烦的,因为他题量比较大,虽然整体上来说题目都比较简单,但是毕竟是第一次写Java的作业,有一些非常基础的东西,还需要当时去学习,所以写的也比较慢。包括一开始犯了一些非常低级的错误,关于一些主方法以及类名的用不清楚,甚至编译时还用C语言进行编译。包括当时编译环境也并未安装完成。最大的遗憾就是第十题没有完成,题目描述比较长,当时也是放弃了。但第一次作业还是学到了很多吧,至少算是接触到了Java最基本的一些语句表达。从C语言的一些表达方式上。成功进入了Java的编写。也复习了C语言的一些东西,比如数组,强制类型转换。
第二次作业的题量还是比较大但题型还是比较简单的,只考验了一些简单的赋值,输出,循环,以及Switch ,if else语句。但是同样的,我也偷懒了,没有做最后一道题,后来经过段老师的提醒,才知道最后一道题,这种求下一天是涉及到我们后面还会做迭代的。所以说非常重要以后尽量要做到每一道题都去完成。
第三次的作业题量就变小了很多,但是题目确实难了一些,尤其是三四题完全就是上一次作业。日期的两次迭代。这里面也存在,有的问题就是嗯,经过段老师的提醒,发现我的代码虽然得到了Pta测试点的满分,但是还是用了一些取巧的方法。并没有百分百的符合“需求”。并没有根据题目要求,把每一个需要写的方法给运用到。以后要注意根据题目要求来编写代码。而不是去取巧。第三次作业相较于之前学会了如何在编写一个额外的类名来包含一些属性以及一些特定的方法方便调用。不再是像之前所有的代码都在同一个类里面,且第三次作业写的新的类也更加完善具体和实用。
设计与分析
作业3 7-3
定义一个类Date,包含三个私有属性年(year)、月(month)、日(day),均为整型数,其中:年份的合法取值范围为[1900,2000] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。
注意:不允许使用Java中和日期相关的类和方法,否则按0分处理。
要求:Date类结构如下图所示:
输入格式:
在一行内输入年月日的值,均为整型数,可以用一到多个空格或回车分隔。
输出格式:
- 当输入数据非法及输入日期不存在时,输出“Date Format is Wrong”;
- 当输入日期合法,输出下一天,格式如下:Next day is:年-月-日
SourceMonitor 报表
Metrics Details For File 'Main.java'
--------------------------------------------------------------------------------------------
Parameter Value
========= =====
Project Directory C:\Users\limbol\eclipse-workspace\pta7-3\src\pt\
Project Name 123
Checkpoint Name Baseline
File Name Main.java
Lines 85
Statements 60
Percent Branch Statements 16.7
Method Call Statements 9
Percent Lines with Comments 0.0
Classes and Interfaces 2
Methods per Class 5.50
Average Statements per Method 3.73
Line Number of Most Complex Method 75
Name of Most Complex Method Date.checkInputValidity()
Maximum Complexity 7
Line Number of Deepest Block 19
Maximum Block Depth 3
Average Block Depth 1.77
Average Complexity 2.36
--------------------------------------------------------------------------------------------
Most Complex Methods in 2 Class(es): Complexity, Statements, Max Depth, Calls
Date.checkInputValidity() 7, 6, 3, 0
Date.Date() 1, 0, 0, 0
Date.getDay() 1, 1, 2, 0
Date.getMonth() 1, 1, 2, 0
Date.getNextDate() 4, 10, 3, 1
Date.getYear() 1, 1, 2, 0
Date.isLeapYear() 5, 4, 2, 0
Date.setDay() 1, 1, 2, 0
Date.setMonth() 1, 1, 2, 0
Date.setYear() 1, 1, 2, 0
Main.main() 3, 15, 3, 8
--------------------------------------------------------------------------------------------
Block Depth Statements
0 4
1 15
2 32
3 9
4 0
5 0
6 0
7 0
8 0
9+ 0
--------------------------------------------------------------------------------------------
Statements:语句的行数,语句是以分号结尾的。这个C中有所不同。
Percent Branch Statement:分支数占总语句数的百分比
Method Call Statement:方法调用语句数
Percent Lines with Comments:注释语句占总语句数的百分比
Classes and Interfaces:类和接口数
Methods per Class:每个类平均包含函数个数
Average Statements per Method:每个函数平均包含的语句个数函数深度(Block Depth):函数深度是函数中分支嵌套的层数。
对应有最大深度(Max Depth)和平均深度(Avg Depth)。
Line Number of Complex Method:最复杂函数的行号(最复杂指的是McCabe复杂度值为最高)
Maximum Complexity:该类中最复杂函数的复杂度(最复杂指的是McCabe复杂度值为最高)
Line Number of Deepest Block:最深层语句块的行号
源代码:
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner input= new Scanner(System.in); int year=input.nextInt(); int month=input.nextInt(); int day=input.nextInt(); Date date =new Date(); date.setYear(year); date.setMonth(month); date.setDay(day); boolean flag1 = date.isLeapYear(year); boolean flag2 = date.checkInputValidity(); if(flag2==false) System.out.println("Date Format is Wrong"); else { System.out.print("Next day is:"); date.getNextDate(); } } } class Date{ private int year; private int month; private int day; int a[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; public Date(){ } public int getYear(){ return year; } public int getMonth(){ return month; } public int getDay(){ return day; } 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 void getNextDate(){ if(day+1<=a[month]) day=day+1; else if(month!=12) { month++; day=1; } else { year++; month=1; day=1; } System.out.print(year+"-"+month+"-"+day); } public boolean isLeapYear(int year){ if(year%400==0||(year%4==0&&year%100!=0)) return true; else return false; } public boolean checkInputValidity(){ if(year%400==0||(year%4==0&&year%100!=0)) a[2]=29; if(month >0&& month<13&&year>=1900&&year<=2000 ) { if(day<=a[month]&&day>0) return true; } return false; } }
踩坑心得:
本题还是比较好做的,只是求下一天,代码整体也比较好写一些极端情况嗯也比较好想.出现的问题是在那个月份的数组处理上。没有仔细阅读题目,因为宿主是从零开始数的,但月份是从1月开始记数的
其实可以在数组的第一个元素先放一个零,这样数组一号对应的也就是1月份的日期了。
作业3 7-4
参考题目3和日期相关的程序,设计一个类DateUtil,该类有三个私有属性year、month、day(均为整型数),其中,year∈[1820,2020] ,month∈[1,12] ,day∈[1,31] , 除了创建该类的构造方法、属性的getter及setter方法外,需要编写如下方法:
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”格式返回日期值
应用程序共测试三个功能:
- 求下n天
- 求前n天
- 求两个日期相差的天数
注意:严禁使用Java中提供的任何与日期相关的类与方法,并提交完整源码,包括主类及方法(已提供,不需修改)
程序主方法如下:
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);
}
}
}
输入格式:
有三种输入方式(以输入的第一个数字划分[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:值
SourceMonitor 报表
Metrics Details For File 'Main.java'
--------------------------------------------------------------------------------------------
Parameter Value
========= =====
Project Directory C:\Users\limbol\eclipse-workspace\pta7-4\src\pag\
Project Name 123
Checkpoint Name Baseline
File Name Main.java
Lines 252
Statements 180
Percent Branch Statements 26.7
Method Call Statements 43
Percent Lines with Comments 1.2
Classes and Interfaces 2
Methods per Class 8.00
Average Statements per Method 9.75
Line Number of Most Complex Method 101
Name of Most Complex Method DateUtil.getDaysofDates()
Maximum Complexity 19
Line Number of Deepest Block 23
Maximum Block Depth 4
Average Block Depth 2.48
Average Complexity 5.69
--------------------------------------------------------------------------------------------
Most Complex Methods in 2 Class(es): Complexity, Statements, Max Depth, Calls
DateUtil.checkInputValidity() 8, 6, 3, 0
DateUtil.DateUtil() 1, 3, 2, 0
DateUtil.DateUtil() 1, 0, 0, 0
DateUtil.getDay() 1, 1, 2, 0
DateUtil.getDaysofDates() 19, 25, 3, 0
DateUtil.getMonth() 1, 1, 2, 0
DateUtil.getNextDate() 4, 10, 3, 1
DateUtil.getNextNDays() 16, 22, 4, 0
DateUtil.getPreviousNDays() 18, 27, 4, 0
DateUtil.getYear() 1, 1, 2, 0
DateUtil.isLeapYear() 5, 4, 2, 0
DateUtil.setDay() 1, 1, 2, 0
DateUtil.setMonth() 1, 1, 2, 0
DateUtil.setYear() 1, 1, 2, 0
DateUtil.showDate() 1, 1, 2, 0
Main.main() 12, 52, 4, 42
--------------------------------------------------------------------------------------------
Block Depth Statements
0 4
1 20
2 61
3 75
4 20
5 0
6 0
7 0
8 0
9+ 0
--------------------------------------------------------------------------------------------

源代码:
package pag; 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;A 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; int a[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; public DateUtil(){ } public int getDaysofDates(DateUtil toDate) { int n=0; int m=0; int x=0; for(int i=1;i<year;i++) { if(i%400==0||(i%4==0&&i%100!=0)) n=n+366; else n=n+365; } if(year%400==0||(year%4==0&&year%100!=0)) a[2]=29; for(int i=1;i<month;i++) n=n+a[i]; n=n+day; for(int i=1;i<toDate.year;i++) { if(i%400==0||(i%4==0&&i%100!=0)) m=m+366; else m=m+365; } if(toDate.year%400==0||(toDate.year%4==0&&toDate.year%100!=0)) a[2]=29; for(int i=1;i<toDate.month;i++) m=m+a[i]; m=m+toDate.day; x=Math.abs(m-n); return x; } public DateUtil(int year, int month, int day) { this.year=year; this.month=month; this.day=day; } public int getYear(){ return year; } public int getMonth(){ return month; } public int getDay(){ return day; } 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 DateUtil getNextNDays(int n) { for(;n>366;) { year++; if(year%400==0||(year%4==0&&year%100!=0)) n=n-366; else n=n-365; } for(;n>31;) { if(year%400==0||(year%4==0&&year%100!=0)) a[2]=29; n=n-a[month]; month++; if(month>12) { year++; month=1; } } if(day+n>a[month]) { if(year%400==0||(year%4==0&&year%100!=0)) a[2]=29; day=day+n-a[month]; month++; } else day = day+n; return this; } public DateUtil getPreviousNDays(int n){ for(;n>366;) { year--; if(year%400==0||(year%4==0&&year%100!=0)) n=n-366; else n=n-365; } for(;n>31;) { if(year%400==0||(year%4==0&&year%100!=0)) a[2]=29; n=n-a[month-1]; month--; if(month<2) { year--; month=13; } } if(day-n<=0) { if(year%400==0||(year%4==0&&year%100!=0)) a[2]=29; if(month==1) { year--; month=12; day=day-n+a[month]; } else { day=day-n+a[month-1]; month--; } } else day = day-n; return this; } public String showDate() { return (year+"-"+month+"-"+day); } public void getNextDate(){ if(day+1<=a[month]) day=day+1; else if(month!=12) { month++; day=1; } else { year++; month=1; day=1; } System.out.print(year+"-"+month+"-"+day); } public boolean isLeapYear(int year){ if(year%400==0||(year%4==0&&year%100!=0)) return true; else return false; } public boolean checkInputValidity(){ if(year%400==0||(year%4==0&&year%100!=0)) a[2]=29; if(month >0&& month<13&&year>=1820&&year<=2020 ) { if(day<=a[month]&&day>0) return true; } return false; } }
踩坑心得:
本题目一开始。编程出现的问题是逻辑上有问题。不能不假思索的去编,要实际的自己带着例子去试一试,哪一个月哪一日放到里面会怎样?进行一个怎样的数据处理。就像那个循环,里面到底是做完这个循环,再使年份加1,还是先年份加1再去做这个循环,这个实际上是要去仔细思考一下的。这也是最后一个改正的错误点。
改进建议:
其实这道题并没有完全按照题目的意思来做,还是自己使用了一些取巧的方法,并没有像题目要求的那样使用到题目所提供的所有方法。例如,我编的代码并未判断2个日期是否相等,也并未比较2个日期的大小,而是直接对其进行相减取其绝对值,以求2天之间的差值。虽然也通过了测试点,但是这样的程序终究是不严谨,有问题,且在段老师那里拿不到满分的。以后写Pta的题目,一定要严格遵守题目要求,使用题目所给的每一个类和方法,才能写出最完善,最泛用的程序。
总结:
通过前三次的Java作业,以及一些慕课的学习,我大致学到了许多Java的简单编程。使自己的编程能力从C语言过渡到了Java上。一些C语言能编出来的题,现在都能编出来了,也了解了一些Java编程的特点,例如封装性与,唯一责任原则。包括一些类和方法的使用。包括如何使用Java编译环境,以及如何单步调试,以及如何使用工具来分析自己的代码。但是课堂上老师不讲任何语法,这一点对我来说也是一个挑战吧。很怕自己会听不懂跟不上。就一定要自己多去看书,去看网上的一些课,然后多练习一下一些方法的使用,毕竟老师也不可能手把手的教给你每一个东西。目前我最大的问题,就是写代码的时候脑袋会是空的,写代码总是会想着去照着一个模板去刻。就像是我写7-2的时候我会去看7-1是怎么写的?然后去模仿着那些结构去构建这个代码,就算题目完全不一样。还是希望自己能够提高学习Java的效率吧,毕竟时间确实比较紧。另外,还有就是之前提到的做题,一定要仔细阅读题目,尽量去完成题目的要求。毕竟,我们做软件开发的,首先是要去清楚需求,才能去做设计。
ps:我下载了Power designer。但是,实在是没办法使用,一登录进去就会闪退。所以这一次的博客纪录里就没有用到Power designer的类图。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~