BLOG-1

一、前言

三次作业又易到难、深入浅出,对java程序的基本使用如:字符串的使用,排序的使用、面向对象的思想及正则表达式的各种运用 等进行了考察。前面的数量多,难度低。到后面难度抬高,数量降低,安排合理。题目集1比较简单,几乎没有遇到什么问题,但几乎都是用面相过程解决的(感觉没必要面相对象);而第二次题目集开始加大难度,考察了类与对象,但在慕课和课堂讲解的基础上,这些也能解决;
第三次题目集使用了正则表达式,自己去查各种资料,花了不少时间。最后迷迷糊糊写了很多bug,但也都解决了。

二.设计与分析

接下来分别对题目集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;
       }





   }

}

主要利用最基本的数学原理对三角形进行判断,一通判断就行了

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);
   }

}

这道题主要就是把握结构,确立各种功能的引用完成最终的目的,可以借助画图构建。

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);
   }

 }

和上题差不多,稍微做修改即可

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);
   }

}

7-3 一元多项式求导(类设计) (50 分)

编写程序性,实现对简单多项式的导函数进行求解。详见作业指导书。 OO作业3-3题目说明.pdf

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

输出格式:
如果输入表达式不符合上述表达式基本规则,则输出“Wrong Format”。
如果输入合法,则在一行内正常输出该表达式的导函数,注意以下几点: 结果不需要排序,也不需要化简;
当某一项为“0”时,则该项不需要显示,但如果整个导函数结果为“0”时,则显示为“0”;
当输出结果第一项系数符号为“+”时,不输出“+”;
当指数值为“0”时,则不需要输出“x^0”,只需要输出其系数即可。
如果输入表达式不符合上述表达式基本规则,则输出“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<string> 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" );
                   }

               }
           }


       }


   }

}
运用了正则表达式,匹配输入的模式进行判断,再匹配数字及符号进行运算。难度较大,容易出现错漏

三、采坑心得

刚开始写的时候对面相对象的思维方式不够重视,总是写着写着面向过程去了。最后一堆的代码没点java的特性。
最初写题目的时候都是直接上手,边写边思考。这样子有时候不会出现什么问题,但当问题复杂起来后就及其容易错漏
部分点位。那一两个测试点的错漏往往让我头皮发麻。 但如果我能一开始就设计好结构、确定好边际点的问题,就没那么
多麻烦了

四、改进建议

在初学java时,我们对于语法并不熟练,因此每次都涉及新的语法都应该进行记录。这样可以我们就可以更有效率、目的性地学习。
java提供了继承、多态、封装、抽象等多种方法,让类与类之间的使用和访问更加灵活,并且程序在执行的过程中也更加安全。
我们应该在在懂得如何去使用它们的同时,知道它们是如何运行的,官网的代码就是我们最好的学习材料

五、总结

Java编程首先感觉到的区别就是Java环境配置比较复杂,编程工具安装比较麻烦。

刚开始写题目集1的时候,对于java的语法不怎么了解,看来比较简单。但对那些初次接触的方式,例如,实现输入输出要用到Scanner,但是Scanner是如何工作的呢?不太懂。与C语言的语法相差很远的,是思维上的不匹配。

题目集1比较简单,几乎没有遇到什么问题,但几乎都是用面相过程解决的(感觉没必要面相对象);
而第二次题目集开始加大难度,考察了类与对象,但在慕课和课堂讲解的基础上,这些也能解决;
第三次题目集使用了正则表达式,自己去查各种资料,花了不少时间。最后迷迷糊糊写了很多bug
,但也解决了。

使用java的一件幸福的事就是不需要担心内存不足问题,因为其有着强大的垃圾回收(GC)机制,不用像c语言一样麻烦。
这三次题目集让我渐渐熟悉java了,这个过程挺轻松。毕竟学习unity游戏开发让我有了c#的基础,

同为面相对象语言,它们的相似点极多。

java最核心的核心就是面向对象思想,只要能够掌握好面向对象这一核心概念,基本上java的学习就算入门了

老师的教学方法很有趣,并不是死讲语法,而是对思想和原理着重开发,让我们学得更加深入,而非浅尝辄止。
与此同时

作业的布置也比较合理。

慕课的学习是基础,课堂的学习是升华。

posted @ 2021-10-15 11:37  触碰雨  阅读(174)  评论(0编辑  收藏  举报