test

#一、前言

三次作业又易到难、深入浅出,对java程序的基本使用如:字符串的使用,排序的使用、面向对象的思想及正则表达式的各种运用  等进行了考察。前面的数量多,难度低。到后面难度抬高,数量降低。安排合理



#二.设计与分析

接下来分别对题目集1的7-8,
题目集2的7-4,7-5
题目集3的7-2,7-3进行分析

7-8 判断三角形类型

输入格式:
在一行中输入三角形的三条边的值(实型数),可以用一个或多个空格或回车分隔,其中三条边的取值范围均为[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”。

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


![avatar](pic\1-7-81.jpg)
![avatar](pic\3-7-31.jpg)

7-4 求下一天

输入年月日的值(均为整型数),输出该日期的下一天。 其中:年份的合法取值范围为[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) ; //求输入日期的下一天
 
输入格式:
在一行内输入年月日的值,均为整型数,可以用一到多个空格或回车分隔。

输出格式:
当输入数据非法及输入日期不存在时,输出“Wrong Format”;
当输入日期合法,输出下一天,格式如下:Next date is:年-月-日
   
     import java.util.Scanner;
   
      public class Main {
       public static void main(String[] args) {
           Scanner input=new Scanner(System.in);
           int year,month,day;
           int total;
           // int theDay;
           String dayName="";
           year=input.nextInt();
           month=input.nextInt();
           day=input.nextInt();
   
           if(year<=2020&&year>=1820&&month<=12&&month>=1&&day<=31&&day>=1)
           {
               if(!isLeapYear( year)&&month ==2&&day>=29)
               {
   
                   System.out.print("Wrong Format");
               }
               else
               {
                   nextDate( year, month, day) ;
               }
   
   
   
           }
           else
           {
               System.out.print("Wrong Format");
           }
   
   
   
       }
       public static void nextDate(int year,int month,int day)
       {
   
           if(month==2)
           {
               if(isLeapYear(year) )
               {
                   if(day==29)
                   {
                       month++;
                       day=1;
                   }
                   else
                   {
                       day++;
                   }
               }
               else
               {
                   if(day==28)
                   {
                       month++;
                       day=1;
                   }
                   else
                   {
                       day++;
                   }
               }
           }
           else if(month==1||month==3||month==5||month==7||month==8||month==10)
           {
               if(day==31)
               {
                   month++;
                   day=1;
               }
               else
               {
                   day++;
               }
           }
           else if(month ==12)
           {
               if(day==31)
               {
                   year++;
                   month=1;
                   day=1;
               }
               else
               {
                   day++;
               }
           }
           else if(month==4||month==6||month==9||month==11)
           {
               if(day==30)
               {
                   month++;
                   day=1;
               }
               else
               {
                   day++;
               }
           }
           System.out.print("Next date is:"+year+"-"+month+"-"+day);
       }
       public static String getDayName(int Day)
       {
           String DayName = "";
           switch(Day)
           {
               case 1: DayName = "Monday";break;
               case 2: DayName = "Tuesday";break;
               case 3: DayName = "Wednesday";break;
               case 4: DayName = "Thursday";break;
               case 5: DayName = "Friday";break;
               case 6: DayName = "Saturday";break;
               case 7: DayName = "Sunday";
   
           }
           return DayName;
       }
   
   
   
       public static boolean isLeapYear(int year)
       {
           return year%400 == 0 ||(year%4 == 0 && year%100 != 0);
       }
   
   }


![avatar](pic\2-7-41.jpg)
![avatar](pic\2-7-42.jpg)
 

7-5 求前N天 (30 分)
 
输入年月日的值(均为整型数),同时输入一个取值范围在[-10,10] 之间的整型数n,输出该日期的前n天(当n > 0时)、该日期的后n天(当n<0时)。
其中年份取值范围为 [1820,2020] ,月份取值范围为[1,12] ,日期取值范围为[1,31] 。
注意:不允许使用Java中任何与日期有关的类或方法。

输入格式:
在一行中输入年月日的值以及n的值,可以用一个或多个空格或回车分隔。

输出格式:
当输入的年、月、日以及n的值非法时,输出“Wrong Format”;
当输入数据合法时,输出“n days ago is:年-月-日”



     import java.util.Scanner;

     public class Main {
       public static void main(String[] args) {
           Scanner input = new Scanner(System.in);
           int year, month, day, n;
           int total;
           // int theDay;
           String dayName = "";
           year = input.nextInt();
           month = input.nextInt();
           day = input.nextInt();
           n = input.nextInt();
           if (year <= 2020 && year >= 1820 && month <= 12 && month >= 1 && day <= 31 && day >= 1) {
               if (!isLeapYear(year) && month == 2 && day >= 29) {

                   System.out.print("Wrong Format");
               } else {
                   nextDate(year, month, day, -n);
               }


           } else {
               System.out.print("Wrong Format");
           }


       }

       public static void nextDate(int year, int month, int day, int n) {

           if (n > 0) {
               if (month == 2) {
                   if (isLeapYear(year)) {
                       if (day + n > 29) {
                           month++;
                           day = (day + n) % 29;
                       } else {
                           day += n;
                       }
                   } else {
                       if (day + n > 28) {
                           month++;
                           day = (day + n) % 28;
                       } else {
                           day += n;
                       }
                   }
               } else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10) {
                   if (day + n > 31) {
                       month++;
                       day = (day + n) % 31;
                   } else {
                       day += n;
                   }
               } else if (month == 12) {
                   if (day + n > 31) {
                       year++;
                       month = 1;
                       day = (day + n) % 31 + 1;
                   } else {
                       day += n;
                   }
               } else if (month == 4 || month == 6 || month == 9 || month == 11) {
                   if (day + n > 30) {
                       month++;
                       day = (day + n) % 30;
                   } else {
                       day += n;
                   }
               }
           }
           if (n < 0) {
               n = 0 - n;
               if (month == 3) {
                   if (isLeapYear(year)) {
                       if (day - n < 0) {
                           month--;
                           day = (day - n) + 29;
                       } else {
                           day -= n;
                       }
                   } else {
                       if (day - n < 0) {
                           month--;
                           day = (day - n) + 28;
                       } else {
                           day -= n;
                       }
                   }
               } else if (month == 2 || month == 4 || month == 6 || month == 8 || month == 9 || month == 11) {
                   if (day - n < 0) {
                       month--;
                       day = (day - n) + 31;
                   } else {
                       day -= n;
                   }
               } else if (month == 1) {
                   if (day - n < 0) {
                       year--;
                       month = 12;
                       day = (day - n) + 31;
                   } else {
                       day -= n;
                   }
               } else if (month == 5 || month == 7 || month == 10 || month == 12) {
                   if (day - n < 0) {
                       month--;
                       day = (day - n) + 30;
                   } else {
                       day -= n;
                   }
               }
           }

           System.out.print(n + " days ago is:" + year + "-" + month + "-" + day);
       }

       public static String getDayName(int Day) {
           String DayName = "";
           switch (Day) {
               case 1:
                   DayName = "Monday";
                   break;
               case 2:
                   DayName = "Tuesday";
                   break;
               case 3:
                   DayName = "Wednesday";
                   break;
               case 4:
                   DayName = "Thursday";
                   break;
               case 5:
                   DayName = "Friday";
                   break;
               case 6:
                   DayName = "Saturday";
                   break;
               case 7:
                   DayName = "Sunday";

           }
           return DayName;
       }


       public static boolean isLeapYear(int year) {
           return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
       }

     }



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

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

类图.jpg

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

输出格式:
当输入数据非法及输入日期不存在时,输出“Date Format is Wrong”;
当输入日期合法,输出下一天,格式如下:Next day is:年-月-日
复制代码
    
     import java.util.Scanner;
   
     public class Main {
       public static void main(String[] args) {
           Scanner input=new Scanner(System.in);
           int year,month,day;
           int total;
           // int theDay;
           String dayName="";
           year=input.nextInt();
           month=input.nextInt();
           day=input.nextInt();
   
           if(year<=2000&&year>=1900&&month<=12&&month>=1&&day<=31&&day>=1)
           {
               if(!isLeapYear( year)&&month ==2&&day>=29)
               {
   
                   System.out.print("Date Format is Wrong");
               }
               else
               {
                   nextDate( year, month, day) ;
               }
   
   
   
           }
           else
           {
               System.out.print("Date Format is Wrong");
           }
   
   
   
       }
       public static void nextDate(int year,int month,int day)
       {
   
           if(month==2)
           {
               if(isLeapYear(year) )
               {
                   if(day==29)
                   {
                       month++;
                       day=1;
                   }
                   else
                   {
                       day++;
                   }
               }
               else
               {
                   if(day==28)
                   {
                       month++;
                       day=1;
                   }
                   else
                   {
                       day++;
                   }
               }
           }
           else if(month==1||month==3||month==5||month==7||month==8||month==10)
           {
               if(day==31)
               {
                   month++;
                   day=1;
               }
               else
               {
                   day++;
               }
           }
           else if(month ==12)
           {
               if(day==31)
               {
                   year++;
                   month=1;
                   day=1;
               }
               else
               {
                   day++;
               }
           }
           else if(month==4||month==6||month==9||month==11)
           {
               if(day==30)
               {
                   month++;
                   day=1;
               }
               else
               {
                   day++;
               }
           }
           System.out.print("Next day is:"+year+"-"+month+"-"+day);
       }
       public static String getDayName(int Day)
       {
           String DayName = "";
           switch(Day)
           {
               case 1: DayName = "Monday";break;
               case 2: DayName = "Tuesday";break;
               case 3: DayName = "Wednesday";break;
               case 4: DayName = "Thursday";break;
               case 5: DayName = "Friday";break;
               case 6: DayName = "Saturday";break;
               case 7: DayName = "Sunday";
   
           }
           return DayName;
       }
   
   
   
       public static boolean isLeapYear(int year)
       {
           return year%400 == 0 ||(year%4 == 0 && year%100 != 0);
       }
   
   }

![avatar](pic\3-7-21.jpg)
![avatar](pic\3-7-22.jpg)



7-3 一元多项式求导(类设计) (50 分)
 
编写程序性,实现对简单多项式的导函数进行求解。详见作业指导书。 OO作业3-3题目说明.pdf

输入格式:
在一行内输入一个待计算导函数的表达式,以回车符结束。

输出格式:
如果输入表达式不符合上述表达式基本规则,则输出“Wrong Format”。
如果输入合法,则在一行内正常输出该表达式的导函数,注意以下几点: 结果不需要排序,也不需要化简;
当某一项为“0”时,则该项不需要显示,但如果整个导函数结果为“0”时,则显示为“0”;
当输出结果第一项系数符号为“+”时,不输出“+”;
当指数符号为“+”时,不输出“+”;
当指数值为“0”时,则不需要输出“x^0”,只需要输出其系数即可。
    import java.util.*;
    import java.util.regex.*;
    import java.util.ArrayList;
    import java.util.List;
   
    public class Main {
       public static void main(String[] args) {
   
           String re1 =  "^(([-+]([1-9][0-9]*))|(([1-9][0-9]*))|([-+]([1-9][0-9]*)\\*x((\\^[+-]?([1-9][0-9]*))?))|(([1-9][0-9]*)\\*x((\\^[+-]?([1-9][0-9]*))?))|([-+](x(\\^[+-]?([1-9][0-9]*))?))|((x(\\^[+-]?([1-9][0-9]*))?)))+$";
           String re2 = "-?([1-9][0-9]*)\\*x(\\^[+-]?([1-9][0-9]*))?";
           String re3 = "-?[1-9][0-9]*";
           ArrayList list = new ArrayList<>();
           int intSum = 0;//常量总数
           int a = 1,b = 1;
   
           int[][] xi = new int[20][3];
           int x1=1,x2=1;
   
           Scanner in = new Scanner(System.in);
           String s = in.nextLine();
           s =s.replace(" ","");//删除
           if(s.matches(re3)) {
               System.out.print(0);
           }
           if(s.matches(re1)) {
   
               Pattern pattern = Pattern.compile(re2);
               Matcher matcher = pattern.matcher(s);
               while(matcher.find()) {
                   list.add(matcher.group(0));
               }
   
   
               for(int i = 0;i  < list.size();i++) {
                   if(i == 1) {
                       String[] stt= list.get(i).split("(\\*x\\^)|(\\*x)");
                       //  *x^    *x
                       if(stt.length == 1) {
                           intSum += Integer.parseInt(stt[0]);
                       }else {
                           a = Integer.parseInt(stt[0]);
                           b = Integer.parseInt(stt[1]);
                           a *= b;
                           b --;
                           if(b == 1) {
                               xi[x1][1]=a;
                               xi[x2][2]=b;
                               x1++;
                               x2++;
                               System.out.print("+" + a + "*x^" + b);
                           }else {
                               if(b!=0)
                               {
                                   xi[x1][1]=a;
                                   xi[x2][2]=b;
                                   x1++;
                                   x2++;
                                   System.out.print("+" + a + "*x^" +b);
                               }
   
                               else
                               {
   
                                   xi[x1][1]=a;
                                   xi[x2][2]=b;
                                   x1++;
                                   x2++;
                                   System.out.print("+"+ a);
                               }
   
                           }
                       }
                   }else {
                       String[] stt= list.get(i).split("(\\*x\\^)|(\\*x)");
                       if(stt.length == 1) {
                           intSum += Integer.parseInt(stt[0]);
                       }else {
                           a = Integer.parseInt(stt[0]);
                           b = Integer.parseInt(stt[1]);
                           a *= b;
                           b --;
                           if(b == 1) {
                               xi[x1][1]=a;
                               xi[x2][2]=b;
                               x1++;
                               x2++;
                               System.out.print(a + "*x" );
                           }else {
                               if(b!=0)
                               {
                                   xi[x1][1]=a;
                                   xi[x2][2]=b;
                                   x1++;
                                   x2++;
                                   System.out.print(a + "*x^" +b);
                               }
   
                               else
                               {
                                   xi[x1][1]=a;
                                   xi[x2][2]=b;
                                   x1++;
                                   x2++;
                                   System.out.print(a );
                               }
   
                           }
                       }
                   }
                   if(intSum != 0&&i  == list.size()-1) {
                       System.out.print(intSum);
                   }
   
               }
           }else {
               System.out.print("Wrong Format");
           }
   
   
   
           System.out.print( "\n" );
           for(int i=1;i<=x1;i++)
           {
   
               if(i==2)
               {
                   if(xi[i][2] == 1)
                   {
   
                       //System.out.print("+" +xi[i][1] + "*x" + xi[i][2]);
                   }else
                   {
                       if(xi[i][2]!=0)
                       {
   
                           // System.out.print("+" +xi[i][1] + "*x^" +xi[i][2]);
                       }
   
                       else
                       {
   
                           // System.out.print("+" +xi[i][1] );
                       }
   
                   }
               }
               else
               {
                   if(xi[i][2] == 1)
                   {
   
                       // System.out.print(xi[i][1] + "*x" );
                   }else
                   {
                       if(xi[i][2]!=0)
                       {
   
                           //System.out.print(xi[i][1] + "*x^" +xi[i][2]);
                       }
   
                       else
                       {
   
                           //System.out.print("!!1"+xi[i][1]+"!!1" );
                       }
   
                   }
               }
   
   
           }
   
   
       }
   
   
   
   
   
   
   }

![avatar](pic\3-7-3.jpg)


![avatar](pic\3-7-31.jpg)



#三、采坑心得

每次写题目的时候,由于初学java,对于语法很不熟练,而且每次都涉及新的语法。我觉得应该里面加入提示,关于该题所涉及的语法。这样可以我们就可以有目的性的学习,更有效率。
#四、改进建议

#五、总结
这三次题目集让开始慢慢接触java,开始掌握java语法,让我学会了如何在网上找学习资料。

老师的教学方法很独特,这是我从未见过的,但是也是最有效的方法,鞭策自己学习java。

作业的难度和量刚刚好。

实验不是很有目的性,实验里的题目可以放到题目集里,只要规定交实验报告就行,这样将课后习题和实验分开多此一举。

课下慕课感觉也不用强制性,慕课的根本目的是让同学们学好这门课程,这个最终结果就好了,因为大家可

上一篇:题目集1~3Blog


下一篇:根据上市日期生成上市年龄