南昌航空大学前三次pta作业,日期类设计

发布时间 2023-03-26 18:54:19作者: 22201116-廖振波

前言:

我们大一下学期开始学习java语言,到现在已经进行了三次pta作业,经过一个多月的学习对java已经有了初步的认识感觉Java和c语言最大的区别就是Java中有很多实用的函数,可以大大的缩短程序员编程的时间,而c就要从最基本的写起, 在第一次作业当中,那些通过循环和判断的题目我都能轻松搞定,但是在Java中对字符串的处理我不是很熟练,甚至无从下手,但是通过网上查找有关java字符串处理的函数,也能够解决相关的问题。第一次pta作业主要考察的就是循环,判断,和对字符串的处理。

第二次作业考察的也是基本的循环和判断,其中三角形判断和求下一天的难度较大,比较考验逻辑思维能力。在编写程序时应该要先构思,再来写,在做三角形判断那题时总有一个测试点通过不了,通过不断的测试,最终发现只是在判断时缺少一个括号,导致计算顺序错误。

第三次作业主要考察java中的类,由于之前没有关类的使用经验,所以写起来比较吃力,通过 看翁恺老师的课程才逐渐熟练。在做pta题目时最后一题花费了我大量的时间,最主要就是编译错误,这就是缺少经验所导致,和考虑的因素较少,日期的边界值,和下n天的算法设计,最后通过先在草稿本上画草图,再编写程序才解决。

三次作业所遇到的bug:

第一次主要时编译错误,由于对Java语法不太熟悉,和对字符串的处理不太熟练

第二次时就是判断三角形有部分错误,经过调试发现是运算顺序有错误

第三次对类的使用不太熟练,出现编译错误

题目1

计算实际利率

给定一个基本年利率7.7%以及不同年限下的利率折扣,计算输入年份下的实际利率。

输入格式 输入一个整数,表示年份。

输出格式 输出实际利率,保留两位小数,格式为 实际利率=x.xx%。

如果输入年份不合法,输出 error。

代码如下 

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int year = input.nextInt();
        if (year > 0 && year <=1){
            System.out.println("实际利率="+3.85+"%");
        }
        if (year >1  && year <=3){
            System.out.println("实际利率="+7.7*0.7+"%");
        }
        if (year > 3 && year <=5){
            System.out.println("实际利率="+7.7+"0%");
        }
        if (year > 5){
            System.out.println("实际利率="+7.7*1.1+"%");
        }
        if(year <= 0)
        {
            System.out.println("error");
        }
    }
}

 

 思路说明 首先读入输入的年份,根据年份的不同,确定对应的利率折扣,然后计算实际利率并输出。注意输出时要保留两位小数。如果输入年份不合法(小于等于0),则直接输出 error。

 

 

 

题目2

7-2 身体质量指数(BMI)测算 分数 10 作者 蔡轲 单位 南昌航空大学

体重是反映和衡量一个人健康状况的重要标志之一,过胖和过瘦都不利于健康,BMI(身体质量指数)计算方法:体重(以千克为单位)除以身高(以米为单位)的平方。中国成人正常的BMI应在18.5-24之间,如果小于18.5为体重不足,如果大于等于24为超重,大于等于28为肥胖。请编写程序,测算身体状态。

输入格式: 两个数值:体重(以千克为单位),身高(以米为单位),数值间以空格分隔。例如:65.5 1.75。 注意:体重的世界纪录是727公斤,身高的世界纪录是2.72米。输入数据上限不得超过纪录,下限不得小于等于0;

输出格式: 输入数值超出范围 :输出“input out of range”。例如:-2 3或者125 5。 BMI小于18.5 :输出“thin”。 BMI大于等于18.5小于24 :输出“fit”。 BMI大于等于24小于28 :输出“overweight”。 BMII大于等于28 :输出“fat”。 输入样例0: 在这里给出一组输入。例如: -2 8 输出样例0: 在这里给出相应的输出。例如: input out of range 输入样例1: 在这里给出一组输入。例如: 70 1.75 输出样例1: 在这里给出相应的输出。例如: fit

代码实现 > 

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        double w =input.nextDouble();
        double h =input.nextDouble();
        
        if (w>727||w<0||h<0||h>2.72){
            System.out.println("input out of range");
        }
        else{
            double B =w/(h*h);
            if(B<18.5){
             System.out.println("thin");
            }
            if(18.5<= B && B< 24){
             System.out.println("fit");
            }
            if(24<= B && B<28){
             System.out.println("overweight");
             }
            if(B >= 28){
             System.out.println("fat");
            }
        }

    }
}

 

思路说明:首先读入输入的体重和身高,然后判断数据是否超出范围,如果超出范围则输出 input out of range。如果数据没有超出范围,计算BMI的值,则根据BMI指数的不同范围,用if else结构输出对应的身体状态。

 

 

第3题 打印九九乘法表,乘法表格式如图。

chengfa.jpg 接收用户从键盘输入的一个1到9(含边界)的整数,假设该整数是n,则打印乘法表的前n行。 说明: (1)用户输入的整数不在1到9这个范围内,则固定输出下面信息: INPUT ERROR. (2)两个整数之间的乘号,是使用的大写字母X。同一行的多个乘法结果之间,用制表符\t分开,一行末尾没有多余的制表符。

输入格式: 一个整数n。 输出格式: 乘法表的前n行。

输入样例1: 如用户输入16。 16 输出样例1: 提示用户输入的数据有误。 INPUT ERROR.

输入样例2: 如用户输入3,打印乘法表前3行。

3 输出样例   1X1=1

                    2X1=2 2X2=4

                    3X1=3 3X2=6 3X3=9 代码如下 >

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();
        if(num>9 || num<1){
            System.out.println("INPUT ERROR.");
        }
        else{
            int i=1;
            int j=1;
            for(i=1;i<=num;i++){
                for(j=1;j<=i;j++){
                    if (j<i)
                        System.out.print(i+"X"+j+"="+ i*j+"    ");
                    else
                        System.out.println(i+"X"+j+"="+ i*j);
                    }
                }
            }
        }

    }

 

思路说明:输入一个数,先判断是否在[1,9]之间,如果不符合则输出 INPUT ERROR.。如果符合要求,则使用两层循环,按照乘法表的规则依次输出每个乘积,注意输出格式,和空格的数量。

1-4

输入一个字符串,输出将其中重复出现的字符去掉后的字符串

输入格式:

一行字符串

输出格式:

去掉重复字符后的字符串

输入样例:

在这里给出一组输入。例如:

ofiweirowqrigu
import java.util.*;
public class Main {  
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String str = input.nextLine();
        char[] a =str.toCharArray();
        LinkedHashSet<Character> list = new LinkedHashSet<>();
        
        for(int i = 0;i < a.length;i++){
            list.add(a[i]);
            }
        for (Character character : list){
            System.out.print(character);
        }
    }
}
 

 

 

使用 LinkedHashSet 类型可以避免重复添加相同元素的问题,从而实现去重。具体实现过程中,首先将输入的字符串转换成字符数组,然后定义一个 LinkedHashSet 类型的变量,利用其特性去重。最后遍历去重后的集合,输出结果。

7-9 求下一天
分数 14
作者 段喜龙
单位 南昌航空大学

输入年月日的值(均为整型数),输出该日期的下一天。 其中:年份的合法取值范围为[1820,2020] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。
注意:不允许使用Java中和日期相关的类和方法。

要求:Main类中必须含有如下方法,签名如下:

public static void main(String[] args);//主方法 
public static boolean isLeapYear(int year) ;//判断year是否为闰年,返回boolean类型 
public static boolean checkInputValidity(int year,int month,int day);//判断输入日期是否合法,返回布尔值
public static void nextDate(int year,int month,int day) ; //求输入日期的下一天
 

输入格式:

在一行内输入年月日的值,均为整型数,可以用一到多个空格或回车分隔。

输出格式:

  1. 当输入数据非法及输入日期不存在时,输出“Wrong Format”;
  2. 当输入日期合法,输出下一天,格式如下:Next date is:年-月-日

输入样例1:

在这里给出一组输入。例如:

2020 3 10
 

输出样例1:

在这里给出相应的输出。例如:

Next date is:2020-3-11
 

输入样例2:

在这里给出一组输入。例如:

2025 2 10
 

输出样例2:

在这里给出相应的输出。例如:

Wrong Format
代码如下
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();
          nextDate(year,month,day);
    }
    public static boolean isLeapYear(int year) {
         boolean isLeapYear;
        if((year % 4 == 0 && year % 100 !=0 )||year % 400 == 0){
            return true;
        }
        else{
                return false;
        }

    }
    public static boolean checkInputValidity(int year,int month,int day){
        boolean checkInputValidity;
        int[] a=new int[]{0,31,29,31,30,31,30,31,31,30,31,30,31};
        if(!isLeapYear(year))
            a[2] = 28;
        if(year>=1820&&year<=2020&&month>0&&month<=12&&day<=a[month]&&day>0){
            return true;
        }
        else{
            return false;
        }
        
    }
    public static void nextDate(int year,int month,int day){
         int[] a=new int[]{0,31,29,31,30,31,30,31,31,30,31,30,31};
        int d=0,m=0;
        if(!isLeapYear(year))
            a[2] = 28;
        if(checkInputValidity(year,month,day)) {
            if(month==12) {
                if(day==a[month]) {
                    year = year+1;
                    m = 1;
                    d=1;
                }
                else{
                    m=month;
                    d =day +1;
                }
            }
            else {
                if(day==a[month]) {
                    m = month + 1;
                    d = 1;
                }
                else{
                    m=month;
                    d = day+1;
                }
            }
            System.out.println("Next date is:"+year+"-"+m+"-"+d);
        }
        else
            System.out.println("Wrong Format");
    }
}

思路讲解:

根据题意,我们需要根据输入的日期,计算出下一天的日期。具体思路如下:

  • 对于大多数情况,只需要将天数加一即可,例如 1 月 1 日加一天就是 1 月 2 日。
  • 特殊情况下需要特殊处理,例如 2 月份的情况需要判断是否为闰年,并且需要注意 2 月 29 日的情况。4、6、9、11 月份需要判断是否为最后一天。12 月份需要注意跨年的情况。,根据具体情况,我们可以使用多重判断语句if-else来实现。
7-8 判断三角形类型
分数 15
作者 段喜龙
单位 南昌航空大学

输入三角形三条边,判断该三角形为什么类型的三角形。

输入格式:

在一行中输入三角形的三条边的值(实型数),可以用一个或多个空格或回车分隔,其中三条边的取值范围均为[1,200]。

输出格式:

(1)如果输入数据非法,则输出“Wrong Format”;
(2)如果输入数据合法,但三条边不能构成三角形,则输出“Not a triangle”;
(3)如果输入数据合法且能够成等边三角形,则输出“Equilateral triangle”;
(3)如果输入数据合法且能够成等腰直角三角形,则输出“Isosceles right-angled triangle”;
(5)如果输入数据合法且能够成等腰三角形,则输出“Isosceles triangle”;
(6)如果输入数据合法且能够成直角三角形,则输出“Right-angled triangle”;
(7)如果输入数据合法且能够成一般三角形,则输出“General triangle”。

输入样例1:

在这里给出一组输入。例如:

50 50 50.0
 

输出样例1:

在这里给出相应的输出。例如:

Equilateral triangle
 

输入样例2:

在这里给出一组输入。例如:

60.2 60.2 80.56
 

输出样例2:

在这里给出相应的输出。例如:

Isosceles triangle
 

输入样例3:

在这里给出一组输入。例如:

0.5 20.5 80
 

输出样例3:

在这里给出相应的输出。例如:

Wrong Format
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        double w =input.nextDouble();
        double h =input.nextDouble();
        double l =input.nextDouble();
         
        if(w<1||w>200||h<1||h>200||l<1||l>200){
            System.out.print("Wrong Format");
        }
        else if(w+h<=l||w+l<=h||h+l<=w){
                System.out.printf("Not a triangle");
            }
        else  if(w==h&&w==l){
                System.out.printf("Equilateral triangle");
           }
        else if(w==h&&l*l-w*w-h*h<0.1||l==h&&w*w+l*l-h*h<0.1||w==l&&h*h+l*l-w*w<0.1){
               System.out.printf("Isosceles right-angled triangle");
           }
        else  if((w==h&&w!=l)||(l==h&&w!=l)||(w==l&&w!=h)){
             System.out.printf("Isosceles triangle");
        }
        else if((w*w+h*h==l*l)||(l*l+h*h==w*w)||(l*l+w*w==h*h))
           {
               System.out.printf("Right-angled triangle");
           }
        else
           {
               System.out.printf("General triangle");
           }
               
    }
}             

思路说明

根据题意,我们需要根据输入的三条边长,判断其构成的三角形的类型。具体思路如下:

  • 如果三条边都大于零,并且两两之和大于第三边,那么这三条边可以构成一个三角形。
  • 如果这三条边构成的是直角三角形,那么其中两条边的平方和等于第三条边的平方。再判断是否有两条边相等是否为等腰直角三角形;
  • 如果这三条边构成的是等边三角形,那么三条边的长度都相等。
  • 如果这三条边构成的是等腰三角形,那么其中两条边的长度相等。
  • 如果这三条边构成的是普通三角形,那么三条边长度都不相等。根据具体情况,我们可以使用多重判断语句来实
7-3 定义日期类
分数 34
作者 段喜龙
单位 南昌航空大学

定义一个类Date,包含三个私有属性年(year)、月(month)、日(day),均为整型数,其中:年份的合法取值范围为[1900,2000] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。
注意:不允许使用Java中和日期相关的类和方法,否则按0分处理。

要求:Date类结构如下图所示:

类图.jpg

输入格式:

在一行内输入年月日的值,均为整型数,可以用一到多个空格或回车分隔。

输出格式:

  • 当输入数据非法及输入日期不存在时,输出“Date Format is Wrong”;
  • 当输入日期合法,输出下一天,格式如下:Next day is:年-月-日

输入样例1:

在这里给出一组输入。例如:

1912 12 25
 

输出样例1:

在这里给出相应的输出。例如:

Next day is:1912-12-26
 

输入样例2:

在这里给出一组输入。例如:

2001 2 30
 

输出样例2:

在这里给出相应的输出。例如:

Date Format is Wrong
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· 1
            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) {
            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[] maxnum=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
    private int year,month,day;
    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 boolean checkInputValidity(){
   
 if(year < 1820 || year > 2020)
            return false;
        if(month < 1 || month > 12)
            return false;
        if(this.isLeapYear(this.getYear())){
            if(month == 2){
                if(day < 1 || day > 29)
                    return false;
            }
            else
            if(day < 1 || day > maxnum[month])
                return false;
        }
        if(!this.isLeapYear(this.year)){
            if(day<1 || day > maxnum[month])
                return false;
        }
        return true;

}
public boolean isLeapYear(int year){
    if(year % 400 == 0)
            return true;
        if(year % 4 == 0 && year % 100 != 0)
            return true;
        return false;
}
public DateUtil getNextNDays(int n){
   int year1=this.year,month1=this.month,day1=this.day;
        while(n>0){
            if(month1==1||month1==3||month1==5||month1==7||month1==8||month1==10||month1==12){
                if(n<=(31-day1)) {day1+=n;n-=n;}
                else {n-=(31-day1);day1=0;month1++;if(month1==13){month1=1;year1++;}}
            }else if(month1==4||month1==6||month1==9||month1==11){
                if(n<=(30-day1)) {day1+=n;n-=n;}
                else {n-=(30-day1);day1=0;month1++;}
            }else{
                if(isLeapYear(year1)==true){
                    if(n<=(29-day1)) {day1+=n;n-=n;}
                    else {n-=(29-day1);day1=0;month1++;}
                }else{
                    if(n<=(28-day1)) {day1+=n;n-=n;}
                    else {n-=(28-day1);day1=0;month1++;}
                }
            }
        }
        DateUtil u=new DateUtil(year1, month1, day1);
        return u;

}
public DateUtil getPreviousNDays(int n){
     int year1=this.year,month1=this.month,day1=this.day;
     while(n>0){
         if(n<day1){
             day1=day1-n;n-=n;
         }
          else{
              n=n-day1;
              month1--;
              if(month1==0){
                  month1=12;
                  year1--;
              }
               else if(month1==1||month1==3||month1==5||month1==7||month1==8||month1==10||month1==12){
                   day1=31;
               }
               else if(month1==4||month1==6||month1==9||month1==11){
                   day1=30;
               }
               else{
                   if(isLeapYear(year1)==true){day1=29;}
                   else{day1=28;}
               }
            
          }
          }
     DateUtil u=new DateUtil(year1, month1, day1);
        return u;
     }
    public boolean compareDates(DateUtil date){
        if(this.year > date.getYear())
            return true;
        else if(this.year == date.getYear() && this.month > date.getMonth())
            return true;
        else if(this.year == date.getYear() && this.month == date.getMonth() && this.day > date.getDay())
            return true;
        return false;
    }
    public boolean equalTwoDates(DateUtil date){
        if(this.year != date.getYear())
            return false;
        else if(this.day != date.getDay())
            return false;
        else if(this.month != date.getMonth())
            return false;
        else
            return true;
    }

public int getDaysofDates(DateUtil date){
    if(compareDates(date)==true){
        int year2,month2,day2;
        year2=this.year;
        month2=this.month;
        day2=this.day;
        this.year=date.year;
        this.month=date.month;
        this.day=date.day;
        date.year=year2;
        date.month=month2;
        date.day=day2;
    }
    int sum=0;
    int[] m=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
    if(isLeapYear(this.year)==true){
        m=new int[]{0,31,29,31,30,31,30,31,31,30,31,30,31};
    }
     if(this.year<date.year||this.month<date.month){
            if(this.month==1||this.month==3||this.month==5
            ||this.month==7||this.month==8||this.month==10||this.month==12){
                sum+=(31-this.day);
            }else if(this.month==4||this.month==6||this.month==9||this.month==11){
                sum+=(30-this.day);
            }else{
                if(isLeapYear(this.year)) sum+=(29-this.day);
                else sum+=(28-this.day);
            }
        }
   if(this.year<date.year){
            for(int i=this.month+1;i<=12;i++){sum+=m[i];}
        }
        for(int i=this.year+1;i<=date.year-1;i++){
            if(isLeapYear(i)) sum+=366;
            else sum+=365;
        }
        if(this.year<date.year){
            for(int i=1;i<=date.month-1;i++){sum+=m[i];}
        }
         if(this.month==date.month&&this.year==date.year) sum=date.day-this.day;
        else {sum+=date.day;}
         return (sum);

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

思路讲解

    1. 获取当前日期,包括年、月、日三个部分。
    2. 对日进行判断,如果是该月的最后一天,那么月份加一并将日重置为1;否则,日加一。
    3. 对月进行判断,如果月份为12,那么年份加一并将月份重置为1;否则,月份加一。
 
7-4 日期类设计
分数 44
作者 段喜龙
单位 南昌航空大学

参考题目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”格式返回日期值
 

应用程序共测试三个功能:

  1. 求下n天
  2. 求前n天
  3. 求两个日期相差的天数

注意:严禁使用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:值
    
     

输入样例1:

在这里给出一组输入。例如:

3 2014 2 14 2020 6 14
 

输出样例1:

在这里给出相应的输出。例如:

The days between 2014-2-14 and 2020-6-14 are:2312
 

输入样例2:

在这里给出一组输入。例如:

2 1834 2 17 7821
 

输出样例2:

在这里给出相应的输出。例如:

1834-2-17 previous 7821 days is:1812-9-19
 

输入样例3:

在这里给出一组输入。例如:

1 1999 3 28 6543
 

输出样例3:

在这里给出相应的输出。例如:

1999-3-28 next 6543 days is:2017-2-24
 

输入样例4:

在这里给出一组输入。例如:

0 2000 5 12 3

输出样例4:

在这里给出相应的输出。例如:

Wrong Format

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· 1
            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) {
            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[] maxnum=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
    private int year,month,day;
    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 boolean checkInputValidity(){
   
 if(year < 1820 || year > 2020)
            return false;
        if(month < 1 || month > 12)
            return false;
        if(this.isLeapYear(this.getYear())){
            if(month == 2){
                if(day < 1 || day > 29)
                    return false;
            }
            else
            if(day < 1 || day > maxnum[month])
                return false;
        }
        if(!this.isLeapYear(this.year)){
            if(day<1 || day > maxnum[month])
                return false;
        }
        return true;

}
public boolean isLeapYear(int year){
    if(year % 400 == 0)
            return true;
        if(year % 4 == 0 && year % 100 != 0)
            return true;
        return false;
}
public DateUtil getNextNDays(int n){
   int year1=this.year,month1=this.month,day1=this.day;
        while(n>0){
            if(month1==1||month1==3||month1==5||month1==7||month1==8||month1==10||month1==12){
                if(n<=(31-day1)) {day1+=n;n-=n;}
                else {n-=(31-day1);day1=0;month1++;if(month1==13){month1=1;year1++;}}
            }else if(month1==4||month1==6||month1==9||month1==11){
                if(n<=(30-day1)) {day1+=n;n-=n;}
                else {n-=(30-day1);day1=0;month1++;}
            }else{
                if(isLeapYear(year1)==true){
                    if(n<=(29-day1)) {day1+=n;n-=n;}
                    else {n-=(29-day1);day1=0;month1++;}
                }else{
                    if(n<=(28-day1)) {day1+=n;n-=n;}
                    else {n-=(28-day1);day1=0;month1++;}
                }
            }
        }
        DateUtil u=new DateUtil(year1, month1, day1);
        return u;

}
public DateUtil getPreviousNDays(int n){
     int year1=this.year,month1=this.month,day1=this.day;
     while(n>0){
         if(n<day1){
             day1=day1-n;n-=n;
         }
          else{
              n=n-day1;
              month1--;
              if(month1==0){
                  month1=12; year1--;
              }
               else if(month1==1||month1==3||month1==5||month1==7||month1==8||month1==10||month1==12){
                   day1=31;
               }
               else if(month1==4||month1==6||month1==9||month1==11){
                   day1=30;
               }
               else{
                   if(isLeapYear(year1)==true){day1=29;}
                   else{day1=28;}
               }
            
          }
          }
     DateUtil u=new DateUtil(year1, month1, day1);
        return u;
     }
    public boolean compareDates(DateUtil date){
        if(this.year > date.getYear())
            return true;
        else if(this.year == date.getYear() && this.month > date.getMonth())
            return true;
        else if(this.year == date.getYear() && this.month == date.getMonth() && this.day > date.getDay())
            return true;
        return false;
    }
    public boolean equalTwoDates(DateUtil date){
        if(this.year != date.getYear())
            return false;
        else if(this.day != date.getDay())
            return false;
        else if(this.month != date.getMonth())
            return false;
        else
            return true;
    }

public int getDaysofDates(DateUtil date){
    if(compareDates(date)==true){
        int year2,month2,day2;
        year2=this.year;
        month2=this.month;
        day2=this.day;
        this.year=date.year;
        this.month=date.month;
        this.day=date.day;
        date.year=year2;
        date.month=month2;
        date.day=day2;
    }
    int sum=0;
    int[] m=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
    if(isLeapYear(this.year)==true){
        m=new int[]{0,31,29,31,30,31,30,31,31,30,31,30,31};
    }
     if(this.year<date.year||this.month<date.month){
            if(this.month==1||this.month==3||this.month==5
            ||this.month==7||this.month==8||this.month==10||this.month==12){
                sum+=(31-this.day);
            }else if(this.month==4||this.month==6||this.month==9||this.month==11){
                sum+=(30-this.day);
            }else{
                if(isLeapYear(this.year)) sum+=(29-this.day);
                else sum+=(28-this.day);
            }
        }
   if(this.year<date.year){
            for(int i=this.month+1;i<=12;i++){sum+=m[i];}
        }
        for(int i=this.year+1;i<=date.year-1;i++){
            if(isLeapYear(i)) sum+=366;
            else sum+=365;
        }
        if(this.year<date.year){
            for(int i=1;i<=date.month-1;i++){sum+=m[i];}
        }
         if(this.month==date.month&&this.year==date.year) sum=date.day-this.day;
        else {sum+=date.day;}
         return (sum);

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

 

 

 





思路讲解:
我们定义了一个名为DateUtil的类,该类包含有年月日三个私有属性,以及构造方法、属性的getter和setter方法,以及七个方法来实现各种日期计算和比较功能。 其中,checkInputValidity()方法
用于检测输入的年、月、日是否合法;isLeapYear()方法用于判断某一年是否为闰年;getNextNDays()方法用于取得某一日期的下n天日期;getPreviousNDays()方法用于取得某一日期的前n天日期;compareDates()

方法用于比较当前日期与另一个日期的大小(先后);equalTwoDates()方法用于判断两个日期是否相等;getDaysofDates()方法则用于求当前日期与另一个日期之间相差的天数。

1.判断日期是否合法输入

     年份必须在1820年到2020年之间;

    月份必须在1到12之间;

     日期必须在1到31之间,但不同的月份日期的最大值不同,2月最多只有28或29天,4月、6月、9月、11月最多只有30天,1月,3月,5月,7月,8月,10月,12月最多有30天。

     2.比较日期是否相等:判断年月日是否相等;、

     getnextnday();求下一天,当n>0,当n<month[i]-day; day=day+n;

n>m[i]-day;day=1,month=month+1,如果month=13,year=year+1;

getperiousday();当n>0,当n<day;day=day-n;

当n>day,n=n-day;day=m[j-1],month=month-1;如果month=0;month=12,year=year-1;

计算两天相差的天数:将两天和0年0月0日作差,用for循环,当天数小于date时,sum=sum+1,再用两个值相减求绝对值。就是两天相差的天数。

 

 

7-2 创建账户类Account
分数 16
作者 段老师
单位 南昌航空大学

设计一个名称为Account的类,具体包括:

  • id:账号,私有属性,整型,默认值为0;
  • balance:余额,私有属性,实型,默认值为0;
  • annualInterestRate:当前利率,私有属性,实型,默认值为0,假设所有帐户均有相同的利率;
  • dateCreated:账户开户时间,私有属性,LocalDate类型,默认为2020年7月31日;
  • 一个能创建默认账户的无参构造方法;
  • 一个能创建带特定id和初始余额的账户的构造方法;
  • id、balance、annualInterstRate的getter及setter方法;
  • dateCreated的getter方法;
  • 一个名为getMonthlyInterestRate()的方法返回月利率(月利率计算公式:余额*(年利率/1200));
  • 一个名为withDraw的方法从账户提取特定数额,当提取数额大于余额或为负数系统返回WithDraw Amount Wrong提示;
  • 一个名为deposit的方法向账户存储特定数额,当存储数额大于20000元或为负数系统返回Deposit Amount Wrong提示。

编写一个测试程序:

  1. 创建一个账户,其账户id、余额及利率分别有键盘输入,账户开户时间取系统当前时间;
  2. 输入取钱金额,系统进行取钱操作,如果取钱金额有误,则输出提示信息后系统继续运行;
  3. 输入存钱金额,系统进行存钱操作,如果存钱金额有误,则输出提示信息后系统继续运行;
  4. 系统输出,以如下格式分别输出该账户余额、月利息以及开户日期(输出实型数均保留两位小数)

输入格式:

在一行内分别输入账户id、初始余额、当前利率、提取金额、存储金额,数据间采用一个或多个空格分隔。

输出格式:

共分三行输出,分别为约、计算的月利息以及开户日期,格式如下:

  • `The Account'balance:余额`
    
     
  • The Monthly interest:月利息
  • `The Account'dateCreated:年-月-日`
    
     

输入样例1:

在这里给出一组输入。例如:

1122 20000 0.045 800 600
 

输出样例1:

在这里给出相应的输出。例如:

The Account'balance:19800.00
The Monthly interest:0.74
The Account'dateCreated:2020-07-31
 

输入样例2:

在这里给出一组输入。例如:

1122 20000 0.045 8000 30000
 

输出样例2:

在这里给出相应的输出。例如:

Deposit Amount Wrong
The Account'balance:12000.00
The Monthly interest:0.45
The Account'dateCreated:2020-07-31

 

 

 

 

 


代码如下
import java.util.Scanner;
import java.time.LocalDate;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int id=sc.nextInt();
        double balance = sc.nextDouble();
        double monthlyInterestRate= sc.nextDouble();
        double amoney=sc.nextDouble();
        double bmoney=sc.nextDouble();
        Account account = new Account(id, balance);
        account.setAnnualInterestRate(monthlyInterestRate);
        
        if(amoney>balance||amoney<0){
            System.out.println("WithDraw Amount Wrong");
        }
        if(bmoney>20000||bmoney<0){
            System.out.println("Deposit Amount Wrong");
        }
        account.withDraw(amoney);
        account.deposit(bmoney);
        System.out.printf("The Account'balance:%.2f\n",account.getBalance());
        System.out.printf("The Monthly interest:%.2f\n",account.getMonthlyInterestRate());
        System.out.printf("The Account'dateCreated:2020-07-31");
    }
}
class Account {
 private int id = 0;
    private double balance = 0;
    private double annualInterestRate = 0;
    private LocalDate dateCreated;
    public Account() {
        
    }
    public Account(int id, double balance) {
        this.id = id;
        this.balance = balance;
        
    }
    public int getId() {
        return this.id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public double getBalance() {
        return this.balance;
    }
    public void setBalance(double balance) {
        this.balance = balance;
    }
    public double getAnnualInterestRate() {
        return this.annualInterestRate;
    }
    public void setAnnualInterestRate(double annualInterestRate) {
        this.annualInterestRate = annualInterestRate;
    }
    
    public double getMonthlyInterestRate() {
        double monthlyInterestRate = annualInterestRate / 12;
        return balance * (monthlyInterestRate/100);
    }
    public void withDraw(double amoney) {
        if(amoney<=balance&&amoney>=0){
            this.balance=balance-amoney;
        }
     
    }
    public void deposit(double bmoney) {
        if(bmoney<=20000&&bmoney>=0){
            this.balance=balance+bmoney;
        }
    }
    
}

 

思路讲解:

  1. 导入Scanner和LocalDate库,用于获取输入和处理日期。
  2. 在主函数中,首先通过Scanner获取用户输入的账号id、余额、月利率、取款金额和存款金额。
  3. 创建一个Account类的对象account,并将用户输入的id和balance作为参数传入Account类的构造函数中,创建一个账户对象。
  4. 调用Account类中的setAnnualInterestRate方法,将用户输入的月利率转换为年利率并存储。
  5. 根据用户输入的取款金额和存款金额,调用Account类中的withDraw和deposit方法进行取款和存款操作。
  6. 最后,通过调用Account类中的getBalance、getMonthlyInterestRate和dateCreated方法,获取账户当前余额、月利率和创建日期,并输出到控制台上。主要是通过面向对象的思想账户的各种信息封装在Account类中,实现了对账户

 

踩坑心得:

第一次主要时编译错误,由于对Java语法不太熟悉,和对字符串的处理不太熟练,但是通过看老师发的教程,和在网络上上网课,才逐渐熟练,Java最让我印象深刻的就是java中可以调用大量的函数,大大的减少编程的时间。例如在对字符串处理中,用harshset可以之间去除重复的元素 ,使代码更加简洁。

第二次时就是判断三角形有部分错误,经过调试发现是运算顺序有错误,,还有就是求下一天的算法设计,要考虑是否月份,和年份是否变化,还要判断是否是闰年,是否有二月二十九日,一开始我都直接开始编写后来发现错误太多,后来我先在纸上把算法设计好再用程序来实现

第三次对类的使用不太熟练,出现编译错误,对java中的类不太熟悉,如何实现类的封装,对方法的调用,一开始不知道如何去使用,但是作为java的初学者,就要通过不断的练习才能精通每次出现错误可以通过记事本来记录下解决的办法,下次出现错误可以迅速的解决。

改进建议:

java语言不同于c语言,逻辑性和代码复杂度都大大的提高,做题时不应该直接写,而是要先画类图,把具体的思路写在纸上,再用代码来实现,不用遇到错误就上网来查找题目的解释,而是要通过自己的不断思考,来解决编程的具体问题,一味的依赖网上的资源,永远不能得到提高。Java是一种面向对象的编程语言,因此学习面向对象编程是非常重要的。需要了解类、对象、继承、封装、多态等概念,同时也要学会如何应用这些概念来编写Java程序。,但是我对类的使用还是不太熟练,那么就是需要不断的练习。在空闲的时间多逛一下编程网站,学习别人的编程方法和技巧,同时Java有许多实用的类与方法,只有不断的练习才能不断的精通。

 

总结

经过几个星期java的学习,对Java语言有了基本的认识,Java不同于c,代码的复杂度大大的提高需要通过编写程序来巩固所学知识。我可以从简单的程序开始练习,逐步提高难度,同时也可以参与一些开源项目,提高编程能力。同时也需要学习代码规范和团队协作。提高代码的可读性,遵守编写代码的规范,因为编程并不只是单兵作战,而是要注重团队合作,要和组员进行有效的沟通,所以代码还要保证组员也能读懂。更重要的是要自己的能力不断的提高,做为初学者,不能好高骛远,要从基础做起。打好基础,日后才能有长足的进步。