pta第一部分总结oop训练集01-03

发布时间 2023-03-25 15:43:26作者: 关关睡不着

(1)前言:

知识点:

oop训练集01(题目量大,难度大)

7-1:简单的输入输出以及if语句的运用。

7-2:简单的输入输出以及if语句的运用。

7-3:简单的输入输出以及if语句的运用和双重循环的运用。

7-4:简单的输入输出以及if语句的运用以及对输出精度的控制。

7-5:简单的输入输出以及if语句的运用和循环的利用字符串方法的应用。

7-6:简单的输入输出以及if语句的运用和循环的利用字符串方法的应用。

7-7:简单的输入输出以及if语句的运用和循环的利用字符串方法的应用。

7-8:简单的输入输出以及if语句的运用和循环的利用字符串方法的应用。

7-9:质数的生成。

7-10:GPS数据处理(这题我不会)。

7-11:循环的利用。

7-12:if判断难点在条件的确定。

oop训练集02(题目量适中,难度适中)

7-1:精度控制。

7-2:循环。

7-3:if判断以及输出精度的应用。

7-4:数组机及if判断的应用。

7-5:数组机及if判断和循环的应用。

7-6:对于精度的控制。

7-7:if判断以及循环的应用。

7-8:if判断以及相等时精度的控制。

7-9:if判断以及数组的应用。

oop训练集03(题目量合适,难度:最后几道题挺难)

7-1:class的应用,方法的创建。

7-2:class的初始化带参和不带参,class内数据和方法的调用。

7-3:class的初始换,class内数据和方法的调用,class内部不同方法的相互调用。

7-4:对7-3的功能的优化,真的很难。

(2)设计与分析:

 7-3

类图:

源码:

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        int year=in.nextInt();
        int month=in.nextInt();
        int day=in.nextInt();
        Date date=new Date(year,month,day);
        if(date.checkinputValidity()==false){
            System.out.print("Date Format is Wrong");
        }else{
            date.getNextDate();
        }
    }

}
class Date{
    private int year;
    private int month;
    private int day;
    int[] mon_maxnum=new int[]{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;
    }
    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(){
        if(isLeapYear(year)==true){
            if((year<1900||year>2000)||(month<1||month>12)||(day<1||day>31)){
                return false;
            }else if(month==2&&day>29){
                return false;
            }else{
                return true;
            }
        }else{
            if((year<1900||year>2000)||(month<1||month>12)||(day<1||day>31)){
                return false;
            }else if(month==2&&day>28){
                return false;
            }else{
                return true;
            }
        }
    }
    public void getNextDate(){
        
        if(isLeapYear(year)==true){
            this.mon_maxnum[2]=29;
        }else{
            this.mon_maxnum[2]=28;
        }
        if(month+1<=12){
                if(day+1<=mon_maxnum[month]){
                    day=day+1;
                }else{
                    day=1;
                    month+=1;
                }
            }else{
                if(day+1<=mon_maxnum[month]){
                    day=day+1;
                }else{
                    day=1;
                    month=1;
                    year+=1;
                }
            }
        System.out.print("Next day is:"+year+"-"+month+"-"+day);
    }
    
}

 

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;
    
    int[] mon_maxnum=new int[]{31,31,28,31,30,31,30,31,31,30,31,30,31};
    DateUtil(int year,int month,int day){
        this.year=year;
        this.month=month;
        this.day=day;
    }
    //检测输入的年、月、日是否合法
    public boolean checkInputValidity(){
        if(isLeapYear(year)==true){
            if((year<1820||year>2020)||(month<1||month>12)||(day<1||day>31)){
                return false;
            }else if(month==2&&day>29){
                return false;
            }else{
                return true;
            }
        }else{
            if((year<1820||year>2020)||(month<1||month>12)||(day<1||day>31)){
                return false;
            }else if(month==2&&day>28){
                return false;
            }else{
                return true;
            }
        }
    }
    //判断year是否为闰年
    public boolean isLeapYear(int year){
        if(year%400==0||(year%4==0&&year%100!=0)){
            return true;
        }else{
            return false;
        }
    }
    //取得year-month-day的下n天日期

    public DateUtil getNextNDays(int n){
        long newday=(long)this.day+(long)n;
        int newmonth=this.month;
        int newyear=this.year;
        if(isLeapYear(year)==true){
            mon_maxnum[2]=29;
            while(newday>mon_maxnum[newmonth]){
                
                newday-=mon_maxnum[newmonth];
                newmonth++;
                if(newmonth==13){
                    newmonth=1;
                    newyear++;
                    if(isLeapYear(newyear)==false){
                        mon_maxnum[2]=28;
                    }
                    if(isLeapYear(newyear)==true){
                        mon_maxnum[2]=29;
                    }
                }
            }
        }else{
            mon_maxnum[2]=28;
            while(newday>mon_maxnum[newmonth]){
                newday-=mon_maxnum[newmonth];
                newmonth++;
                if(newmonth==13){
                    newmonth=1;
                    newyear++;
                    if(isLeapYear(newyear)==true){
                        mon_maxnum[2]=29;
                    }
                    if(isLeapYear(newyear)==false){
                        mon_maxnum[2]=28;
                    }
                }
            }
        }
        
        DateUtil last=new DateUtil(newyear, newmonth, (int)newday);
        return last;
    }
    //取得year-month-day的前n天日期
    public DateUtil getPreviousNDays(int n){
        int newday=this.day-n;
        int newmonth=this.month;
        int newyear=this.year;
        if(isLeapYear(year)==true){
            mon_maxnum[2]=29;
            while(newday<=0){
                
                newday+=mon_maxnum[newmonth-1];
                newmonth--;
                if(newmonth==0){
                    newmonth=12;
                    newyear--;
                    if(isLeapYear(newyear)==false){
                        mon_maxnum[2]=28;
                    }
                    if(isLeapYear(newyear)==true){
                        mon_maxnum[2]=29;
                    }
                }
            }
        }else{
            mon_maxnum[2]=28;
            while(newday<=0){
                newday+=mon_maxnum[newmonth-1];
                newmonth--;
                if(newmonth==0){
                    newmonth=12;
                    newyear--;
                    if(isLeapYear(newyear)==true){
                        mon_maxnum[2]=29;
                    }
                    if(isLeapYear(newyear)==false){
                        mon_maxnum[2]=28;
                    }
                }
            }
        }
        DateUtil before=new DateUtil(newyear, newmonth, newday);
        return before;
    }
    //比较当前日期与date的大小(先后)
    public boolean compareDates(DateUtil date){
//         date大返回true
        if(this.year<date.getYear()){
            return true;
            
        }else if(this.year==date.getYear()){
            if(this.month<date.getMonth()){
                return true;
                
            }else if(this.month==date.getMonth()){
                if(this.day<date.getDay()){
                    return true;
                    
                }else{
                    return false;
                    
                }
            }else{
                return false;
                
            }  
        }else{
            return false;
            
        }
    }
    //判断两个日期是否相等
    public boolean equalTwoDates(DateUtil date){
    
        if(this.day==date.getDay()&&this.year==date.getYear()&&this.month==date.getMonth()){
            return true;
        }
        else {
            return false;
        }
    }

    //求当前日期与date之间相差的天数
    public int getDaysofDates(DateUtil date){
        int dayflag=0;
        int newday=this.day;
        int newmonth=this.month;
        int newyear=this.year;
        int myday=date.getDay();
        int mymonth=date.getMonth();
        int myyear=date.getYear();
        if(equalTwoDates(date)==true){
            dayflag=0;
        }else{
            if(compareDates(date)==true){//date大
                while(myyear>newyear){
                    if(isLeapYear(myyear)==true){
                        dayflag+=366;
                    }else{
                        dayflag+=365;
                    }
                    myyear--;
                }
                while(mymonth!=newmonth){
                    if(isLeapYear(newyear)==true){
                        mon_maxnum[2]=29;
                    }else{
                        mon_maxnum[2]=28;
                    }
                        if(mymonth>newmonth){
                            dayflag+=mon_maxnum[mymonth-1];
                            mymonth--;
                        }else{
                            dayflag+=mon_maxnum[newmonth-1];
                            newmonth--;
                        }
                    
                }
                dayflag=dayflag+myday-newday;
            }else{
                while(myyear<newyear){
                    if(isLeapYear(newyear)==true){
                        dayflag+=366;
                    }else{
                        dayflag+=365;
                    }
                    newyear--;
                }
                while(mymonth!=newmonth){
                    if(isLeapYear(newyear)==true){
                        mon_maxnum[2]=29;
                    }else{
                        mon_maxnum[2]=28;
                    }
                    if(mymonth>newmonth){
                        dayflag+=mon_maxnum[mymonth-1];
                        mymonth--;
                    }else{
                        dayflag+=mon_maxnum[newmonth-1];
                        newmonth--;
                    }
                }
                dayflag=dayflag-myday+newday;
            }
        }
        return dayflag;
    }
    //以“year-month-day”格式返回日期值
    public String showDate(){
        return year + "-" + month + "-" + day;
    }
    public int getYear(){
        return year;
    }
  public int getMonth(){
return month;
    }
    public int getDay(){
        return day;
    }
}

 

(3)采坑心得:

1,精度的保留时常出错注意题目要求是否要四舍五入。

2,注意是否会超过int 所能包含的最大值

3,注意判断条件是否覆盖完全情况,是否有错误。

4,是否出现数组越界的情况。

(4)改进建议:

对于可用Java中方法的代码进行简化从而学习Java中已经封装好的方法。

(5)总结:

学到了类的调用,私有和共有的区别,类里的方法如何调用,题量可以稍稍减少一点(考虑一下),确实学到了好多编程技巧,能独立完成几百行的代码编程。