OOP前三次训练集总结
关于pta上OOP前三次训练集的总结:
前言:
前三次作业算是练手,第一次便是基本的了解java的大体主类的写法和基本操作。第二次则偏向于基本方法的调用,第三次是加入了其他类的方法的设计与调用。三次训练集一共25题,难度由简到难,但都不算复杂。
设计与分析:
大部分题都简单,就直接跳到第三次题目集的7-3,7-4吧
7-3的Source Monitor的报表显示其他指标都还好,就是最最大圈复杂度超了,得注意。

7-4的图问题就大多了,果然代码长了就容易出问题。

关于7-3代码
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
Date date=new Date(input.nextInt(),input.nextInt(),input.nextInt());
date.getNextDate();
}
}
class Date{
private int year;
private int month;
private int day;
private int[] mon_maxum= {0,31,28,31,30,31,30,31,31,30,31,30,31};
Date(){
}
Date(int year,int month, int day){
this.year=year;
this.month=month;
this.day=day;
}
int getYear() {
return this.year;
}
void setYear(int year) {
this.year=year;
}
int getMonth() {
return this.month;
}
void setMonth(int month) {
this.month=month;
}
int getDay() {
return this.day;
}
void setDay(int day) {
this.day=day;
}
boolean isLeapYear(int year) {
if(year%100==0&&year%400==0)
return true;
else if(year%100!=0&&year%4==0)
return true;
else
return false;
}
boolean checkInputValidity() {
if(this.year<1900||this.year>2000||this.month<1||this.month>12) {
return false;
}
else if(this.isLeapYear(this.year)==true&&this.month==2&&this.day==29) {
return true;
}
else if(this.day<=0||this.day>this.mon_maxum[this.month]) {
return false;
}
else {
return true;
}
}
void getNextDate(){
if(this.checkInputValidity()==true){
if(this.isLeapYear(this.year)==true&&this.month==2&&this.day==29) {
this.day=1;
this.month=3;
}
else if(this.isLeapYear(this.year)==true&&this.month==2&&this.day==28) {
this.day=29;
}
else if(this.month==12&&this.day==31) {
this.day=1;
this.month=1;
this.year=this.year+1;
}
else if(this.day==this.mon_maxum[this.month]) {
this.day=1;
this.month=this.month+1;
}
else {
this.day=this.day+1;
}
System.out.printf("Next day is:%d-%d-%d",this.year,this.month,this.day);
}
else {
System.out.println("Date Format is Wrong");
}
}
}
主要就是要注意细节,闰年之类的,没啥了。
关于7-4代码
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;
private int[] mon_maxum= {31,31,28,31,30,31,30,31,31,30,31,30,31,31};
DateUtil(int year,int month, int day){
this.year=year;
this.month=month;
this.day=day;
}
int getYear() {
return this.year;
}
void setYear(int year) {
this.year=year;
}
int getMonth() {
return this.month;
}
void setMonth(int month) {
this.month=month;
}
int getDay() {
return this.day;
}
void setDay(int day) {
this.day=day;
}
boolean isLeapYear(int year) {
if(year%100==0&&year%400==0)
return true;
else if(year%100!=0&&year%4==0)
return true;
else
return false;
}
boolean checkInputValidity() {
if(this.year<1820||this.year>2020||this.month<1||this.month>12) {
return false;
}
else if(this.isLeapYear(this.year)==true&&this.month==2&&this.day==29) {
return true;
}
else if(this.day<=0||this.day>this.mon_maxum[this.month]) {
return false;
}
else {
return true;
}
}
DateUtil getNextNDays(int n){
int model;
if((this.isLeapYear(this.year)==true&&(this.month<=2))||(this.isLeapYear(this.year+1)==true&&this.month>=3))
model=366;
else
model=365;
while(n>=model){
this.year=this.year+1;
n=n-model;
if((this.isLeapYear(this.year)==true&&(this.month==1||this.month<=2))||(this.isLeapYear(this.year+1)==true&&this.month>=3))
model=366;
else
model=365;
}
if(this.month==2&&this.isLeapYear(this.year)==true) {
model=29;
}
else {
model=this.mon_maxum[this.month];
}
while(n>=model) {
if(this.month==12) {
this.year=this.year+1;
this.month=1;
}
else {
this.month=this.month+1;
}
if(this.month==2&&this.isLeapYear(this.year)==true) {
model=29;
}
else {
model=this.mon_maxum[this.month];
}
n=n-model;
}
while(n>=1) {
if(this.month==2&&this.isLeapYear(this.year)==false&&this.day==29) {
this.month=this.month+1;
this.day=2;
}
if(this.month==2&&this.isLeapYear(this.year)==true&&this.day==28) {
this.day=29;
}
if(this.month==12&&this.day==31) {
this.year=this.year+1;
this.month=1;
this.day=1;
}
if(this.day>=this.mon_maxum[this.month]) {
this.month=this.month+1;
this.day=1;
}
else {
this.day=this.day+1;
}
n=n-1;
}
if(this.month==2&&this.day==29&&this.isLeapYear(this.year)==false) {
this.month=3;
this.day=1;
}
return this;
}
DateUtil getPreviousNDays(int n) {
int model;
if((this.isLeapYear(this.year-1)==true&&(this.month<=2))||(this.isLeapYear(this.year)==true&&this.month>=3))
model=366;
else
model=365;
while(n>=model){
this.year=this.year-1;
n=n-model;
if((this.isLeapYear(this.year-1)==true&&(this.month<=2))||(this.isLeapYear(this.year)==true&&this.month>=3))
model=366;
else
model=365;
}
if(this.month==3&&this.isLeapYear(this.year)==true) {
model=29;
}
else {
model=this.mon_maxum[this.month-1];
}
while(n>=model) {
if(this.month==1) {
this.year=this.year-1;
this.month=12;
}
else {
this.month=this.month-1;
}
n=n-model;
if(this.month==3&&this.isLeapYear(this.year)==true) {
model=29;
}
else {
model=this.mon_maxum[this.month-1];
}
}
while(n>=1) {
if(this.month==3&&this.isLeapYear(this.year)==true&&this.day==1) {
this.month=this.month-1;
this.day=29;
}
if(this.month==1&&this.day==1) {
this.year=this.year-1;
this.month=12;
this.day=31;
}
if(this.day==1) {
this.month=this.month-1;
this.day=this.mon_maxum[this.month];
}
else {
this.day=this.day-1;
}
n=n-1;
}
if(this.month==2&&this.day==29&&this.isLeapYear(this.year)==false) {
this.month=3;
this.day=1;
}
return this;
}
boolean equalTwoDates(DateUtil date) {
if(this.year==date.year&&this.month==date.month&&this.day==date.day) {
return true;
}
else {
return false;
}
}
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;
}
}
int getDaysofDates(DateUtil date){
DateUtil date1,date2;
int model,num=0;
if(this.equalTwoDates(date)==true) {
return 0;
}
else {
if(compareDates(date)==true) {
date1=date;
date2=this;
}
else {
date1=this;
date2=date;
}
}
while(date2.year-date1.year>=2||(date2.year-date1.year==1&&(date1.month<date2.month||(date1.month==date2.month&&date1.day<=date2.day)))) {
if((this.isLeapYear(date1.year)==true&&(date1.month==1||date1.month<=2))||(date1.isLeapYear(date1.year+1)==true&&date1.month>=3)) {
model=366;
}
else {
model=365;
}
date1.year=date1.year+1;
num=num+model;
}
while((date2.year-date1.year==1&&(date2.month>=2||(date2.month==1&&(date1.month<=11||(date1.month==12&&date1.day<=date2.day)))))||(date2.month-date1.month>=2||(date2.month-date1.month==1&&(date1.day<=date2.day)))) {
if(date1.month==2&&this.isLeapYear(date1.year)==true) {
model=29;
}
else {
model=this.mon_maxum[date1.month];
}
if(date1.month==12) {
date1.year=date1.year+1;
date1.month=1;
}
else {
date1.month=date1.month+1;
}
num=num+model;
}
if(date1.month==2&&this.isLeapYear(date1.year)==true&&date1.day>=30) {
date1.month=date1.month+1;
date1.day=date1.day-29;
}
else if(this.isLeapYear(date1.year)==false&&date1.day>this.mon_maxum[date1.month]) {
date1.day=date1.day-this.mon_maxum[date1.month];
date1.month=date1.month+1;
}
while(date2.year>date1.year||date2.month-date1.month==1||date1.day<date2.day) {
if(date1.month==2&&this.isLeapYear(date1.year)==true&&date1.day==29) {
date1.month=date1.month+1;
date1.day=1;
}
else if(date1.month==12&&date1.day==31) {
date1.year=date1.year+1;
date1.month=1;
date1.day=1;
}
else if(this.isLeapYear(date1.year)==false&&date1.day==this.mon_maxum[date1.month]) {
date1.month=date1.month+1;
date1.day=1;
}
else {
date1.day=date1.day+1;
}
num=num+1;
}
return num;
}
String showDate() {
return this.year+"-"+this.month+"-"+this.day;
}
}
似乎也没啥要强调的,慢慢检查着做就好了。可能就是对java可以直接用类的属性做变量用这点更熟悉了吧。
踩坑心得:
好像也没踩啥坑。。。。就慢慢磨就好了。
改进建议:
关于日期类的编码除了可以继续往秒挖以外,还可以把各种历史上的重要日子加进去。银行账户类可以添加项目投资,图形类可以考虑物理建模。
总结:
通过这三次训练集我学到了基本的设计和调用类的方法的手法,关于各种语法的学习需深入。改进建议和意见的话就都挺好。。。。

浙公网安备 33010602011771号