面向对象学习阶段性课程总结2
一、前言
距离上次写博客已经过去了将近一个月了,我们的学习又深入了一些,不仅学会了正则表达式的简单应用,还深入了解了如何利用聚合、继承等功能进行运算。
-
关于题目集4:这次的题目集题量不多,但难度很大,尤其是对于那个时候正则还没解决的我来说,做起来十分煎熬,最后的成绩也不如人意
- 关于题目集5:在经历了题目集4后,老师对我们进行了问卷调查,根据我们反映的情况,这次的题目集老师明显放低了难度,虽然题量稍有增加,但是难度大幅度减少
-
关于题目集6:有了上次的经历后(指题目集5),我对这次题目也有了信心,也在老师不知以后早早地就完成了作业,并获得了比较满意的成绩
二、设计与分析
1.题目集4(7-2)、题目集5(7-4)两种日期类聚合设计的优劣比较
题目集4(7-2)的代码、类图、圈复杂度如下:
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); int y = 0; int m = 0; int d = 0; int choice = input.nextInt(); if(choice == 1) { int a; y = Integer.parseInt(input.next()); m = Integer.parseInt(input.next()); d = Integer.parseInt(input.next()); DateUtil date = new DateUtil(y, m, d); if (!date.checkInputValidity()) { System.out.println("Wrong Format"); System.exit(0); } a = input.nextInt(); if (m < 0) { System.out.println("Wrong Format"); System.exit(0); } System.out.print(date.getY() + "-" + date.getM() + "-" + date.getD() + " next " + m + " days is:"); System.out.println(date.getNextDays(a).showDate()); } if(choice == 2) { int a; y = Integer.parseInt(input.next()); m = Integer.parseInt(input.next()); d = Integer.parseInt(input.next()); DateUtil date = new DateUtil(y, m, d); if (!date.checkInputValidity()) { System.out.println("Wrong Format"); System.exit(0); } a = input.nextInt(); if (a < 0) { System.out.println("Wrong Format"); System.exit(0); } System.out.print(date.getY() + "-" + date.getM() + "-" + date.getD() + " next " + m + " days is:"); System.out.println(date.getNextDays(a).showDate()); } else{ System.out.println("Wrong Format"); System.exit(0); } } } class DateUtil { Day day; private int d , m , y; public DateUtil() { super(); // TODO Auto-generated constructor stub } public DateUtil(int d, int m, int y) { super(); this.d = d; this.m = m; this.y = y; } public Day getDay() { return day; } public void setDay(Day day) { this.day = day; } public boolean checkInputValidity() { return (day.validate() && day.month.validate() && day.month.year.validate()); } public boolean equalTwoDates(DateUtil date)//判断两个日期是否相等 { if (date != null) { if (d == date.d && m == date.m && y == date.y) { return true; } }return false; } public boolean compareDates(DateUtil date)//比较当前日期与date的大小(先后) { if (this.y > date.y) return true; if (this.y == date.y) { if (this.m > date.m) return true; if (this.m == date.m) { if (this.d >= date.d) return true; } } return false; } public String showDate() { return y + "-" + m + "-" + d; } // 下n天 public DateUtil getNextDays(int n) { for(int i = 0;i < n;i++) { d++; if(d > day.mon_maxnum[m]) { d = 1; m ++; if(m > 12) { m = 1; y ++; } } }return new DateUtil(y, m, d); } public int getD() { return d; } public void setD(int d) { this.d = d; } public int getM() { return m; } public void setM(int m) { this.m = m; } public int getY() { return y; } public void setY(int y) { this.y = y; } // 前n天 public DateUtil getPreviousNDays(int n) { for(int i = 0;i < n;i++) { d--; while(d < 1) { m --; if(m < 1) { m = 12; y --; }d += day.mon_maxnum[m]; } }return new DateUtil(y, m, d); } } class Day { private int value; Month month; int []mon_maxnum = {31,28,31,30,31,30,31,31,30,31,30,31}; private int yearValue; private int monthValue; private int dayValue; public Day() { super(); // TODO Auto-generated constructor stub } public Day(int yearValue, int monthValue, int dayValue) { super(); this.yearValue = yearValue; this.monthValue = monthValue; this.dayValue = dayValue; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public Month getMonth() { return month; } public void setMonth(Month month) { this.month = month; } public void resetMin() { value = 1; } public void resetMax() { if(month.year.isLeapYear(month.year.getValue())) mon_maxnum[1] = 29; value = mon_maxnum[monthValue]; } public void dayIncrement() { value += 1; } public void dayReduction() { value -= 1; } public boolean validate() { return (value > 0 && value <= mon_maxnum[month.getValue()]); } } class Month { private int value; Year year; private int yearValue; private int monthValue; public Month() { super(); // TODO Auto-generated constructor stub } public Month(int yearValue, int monthValue) { super(); this.yearValue = yearValue; this.monthValue = monthValue; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public Year getYear() { return year; } public void setYear(Year year) { this.year = year; } public void resetMin() { value = 1; } public void resetMax() { value = 12; } public boolean validate() { return (value <= 12 && value >=1); } public void monthIncrement() { value +=1; } public void monthReduction() { value -=1; } } class Year{ private int value; public Year() { super(); // TODO Auto-generated constructor stub } public Year(int value) { super(); this.value = value; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public boolean isLeapYear(int value) { return (value%400==0||(value%4==0&&value%100!=0)); } public boolean validate() { return (value >= 1900 && value <= 2050) ; } public void yearIncrement() { value += 1; } public void yearReduction() { value -= 1; } }


题目集5(7-4)的代码、类图、圈复杂度如下:
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); int y,m,d; int choice = input.nextInt(); if(choice == 1) { y = input.nextInt(); m = input.nextInt(); d = input.nextInt(); DateUtil date = new DateUtil(y,m,d); if (!date.checkInputValidity()) { System.out.println("Wrong Format"); } int n = input.nextInt(); if(n < 0) { System.out.println("Wrong Format"); } date.getNextNDays(n); } else if(choice == 2) { y = input.nextInt(); m = input.nextInt(); d = input.nextInt(); DateUtil date = new DateUtil(y,m,d); if (!date.checkInputValidity()) { System.out.println("Wrong Format"); } int n = input.nextInt(); if(n < 0) { System.out.println("Wrong Format"); } System.out.print(date.year.getValue() + "-" + date.month.getValue() + "-" + date.day.getValue() + " next " + n + " days is:"); // System.out.println(date.getPreviousNDays(n).showDate()); } else if(choice == 3) { y = input.nextInt(); m = input.nextInt(); d = input.nextInt(); int anothery = input.nextInt(); int anotherm = input.nextInt(); int anotherd = input.nextInt(); DateUtil date1 = new DateUtil(y,m,d); DateUtil date2 = new DateUtil(anothery,anotherm,anotherd); if(date1.checkInputValidity() && date2.checkInputValidity()) { // System.out.println("The days between " + date1.showDate() + // " and " + date2.showDate() + " are:" // + date1.getDaysofDates(date2)); }else { System.out.println("Wrong Format"); } }else { System.out.println("Wrong Format"); } } } class DateUtil { private int y; private int m; private int d; Year year = new Year(y); Month month = new Month(m); Day day = new Day(d); int []mon_maxnum = {31,28,31,30,31,30,31,31,30,31,30,31}; public int getY() { return y; } public void setY(int y) { this.y = y; } public int getM() { return m; } public void setM(int m) { this.m = m; } public int getD() { return d; } public void setD(int d) { this.d = d; } public DateUtil(int y, int m, int d) { super(); this.y = y; this.m = m; this.d = d; } public Year getYear() { return year; } public DateUtil() { super(); // TODO Auto-generated constructor stub } public void setYear(Year year) { this.year = year; } public Month getMonth() { return month; } public void setMonth(Month month) { this.month = month; } public Day getDay() { return day; } public void setDay(Day day) { this.day = day; } public void setDayMin() { this.d = 1; } public void setDayMax() { this.d = mon_maxnum[this.m - 1]; } // public boolean isMonth() {//判断输入的日期是否越界 // boolean p = false; // if(this.month.getValue() == 1||this.month.getValue() == 3||this.month.getValue() == 5||this.month.getValue() == 7||this.month.getValue() == 8||this.month.getValue() == 10||this.month.getValue() == 12) { // if(this.day.getValue() <= 31) { // p=true;} // else { // p=false; // } // }else if(this.month.getValue() == 2) { // if(this.year.isLeapYear(this.year.getValue())) { // if(this.day.getValue() <= 29) { // p = true; // }else { // p = false; // } // }else { // if(this.day.getValue() <=28) { // p = true; // }else { // p = false; // } // } // }else { // if(this.day.getValue() <= 30) { // p = true; // }else { // p = false; // } // } // return p; // } public boolean checkInputValidity() {//判断数据合法性 Year year = new Year(y); Month month = new Month(m); Day day = new Day(d); return (y>=1820&&y<=2020&&m>=1&&m<=12&&d>=1&&d<=31); } public void getNextNDays(int n) { Year year = new Year(y); Month month = new Month(m); // Day day = new Day(d); int year1 = y; int month1 = m; int day1 = d; if(year.isLeapYear()) { mon_maxnum[1] = 29; }else { mon_maxnum[1] = 28; } for (int i = 0; i < n; i++) { day.dayInorement(); d = day.getValue(); if (d > mon_maxnum[m-1]) { d = 1; month.monthIncrement(); m = month.getValue(); if (m > 12) { month.restMin(); m = month.getValue(); year.yearIncrement(); y = year.getValue(); }d = n; }else { d += n; } } System.out.print(year1 + "-" + month1 + "-" + day1 + " next " + n + " days is:" + y+ "-" + m + "-" + d); } public DateUtil getPreviousNDays(int n) { Year year = new Year(y); Month month = new Month(m); Day day = new Day(d); int year1 = this.year.getValue(); int month1 = this.month.getValue(); int day1 = this.day.getValue(); for (int i = 0; i < n; i++) { day1--; if (day1 < 1) { month1--; if (month1 < 1) { month1 = 12; year1--; }day1 = mon_maxnum[m-1]; } } return new DateUtil(year1, month1, day1); } public boolean compareDates(DateUtil date) { if (this.year.getValue() > date.year.getValue()) return true; if (this.year.getValue() == date.year.getValue()) { if (this.month.getValue() > date.month.getValue()) return true; if (this.month == date.month) { if (this.day.getValue() >= date.day.getValue()) return true; } } return false; } public boolean equalTwoDates(DateUtil date) { if (date != null) { if (year == date.year && month == date.month && day == date.day) { return true; } } return false; } public int getDaysofDates(DateUtil date) { DateUtil dateUtil1 = this; // 小 DateUtil dateUtil2 = date; // 大 int days; int leapYearNum = 0; if (this.compareDates(date)) { dateUtil1 = date; dateUtil2 = this; } for(int i = date.year.getValue();i < this.year.getValue();i++) { if(year.isLeapYear()) { leapYearNum += 1; } }days = (365 * (dateUtil2.year.getValue() - dateUtil1.year.getValue() - leapYearNum)) + (366 * leapYearNum); int d1 = mon_maxnum[dateUtil1.month.getValue() - 1] + dateUtil1.day.getValue() + (dateUtil1.month.getValue() > 2 && year.isLeapYear() ? 1 : 0); int d2 = mon_maxnum[dateUtil2.month.getValue() - 1] + dateUtil1.day.getValue() + (dateUtil1.month.getValue() > 2 && year.isLeapYear() ? 1 : 0); return days - d1 + d2; } // public String showDate(){ // return year + "-" + month + "-" + day; // } } class Year { int value; public int getValue() { return value; } public void setValue(int value) { this.value = value; } public Year(int year) { super(); this.value = year; } public Year() { super(); // TODO Auto-generated constructor stub } public boolean isLeapYear() { return (value%400==0||(value%4==0&&value%100!=0)); } public boolean validate() { return (this.value >= 1820 && this.value <= 2020); } public void yearIncrement() { this.value += 1; } public void yearReduction() { this.value -= 1; } } class Month { int value; public Month(int value) { super(); this.value = value; } public Month() { super(); // TODO Auto-generated constructor stub } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public void restMin() { value = 1; } public void restMax() { value = 12; } public boolean validate() { return (this.value > 0 && this.value < 13); } public void monthIncrement() { this.value += 1; } public void monthReduction() { this.value -= 1; } } class Day { int value; public Day() { super(); // TODO Auto-generated constructor stub } public Day(int value) { super(); this.value = value; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public void dayInorement() { this.value += 1; } public void dayReduction() { this.value -= 1; } }


关于这两题,他们要解决的问题是一样的,只不过是各个类之间的调用关系有所不同。题目集4(7-2)中使用的是俄罗斯套娃式的逐级调用的方法,在书写代码时明显感觉很麻烦,这样的设计不利于程序员书写也不利于用户阅读,而且这样写出来的代码耦合度很高,拓展性很差,不符合面向对象设计原则的开闭原则。相比较来说,题目集5(7-4)中的设计就更加人性化;在这个题目中,它首先使用了一个DateUtil类作为中介,将其他需要进行调用的类联系在一起,在这个DateUtil类中完成了主要的功能。这样操做不仅利于代码的书写,也降低了代码的耦合度,使得其1拓展性得到提升。通过比较两次题目集我也明白了类型设计的重要性:一个好的类型设计是完成一个好的项目的前提,以后需要进行类型设计时必须三思而后行,避免出现题目集4(7-2)中出现的问题。
2.题目集4(7-3)、题目集6(7-5、7-6)三种渐进式图形继承设计的思路与技术运用(封装、继承、多态、接口等)
题目4(7-3)代码、类图、圈复杂度如下:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); if(n == 1||n == 2 || n == 3 ||n == 4) { switch(n) { case 1:{ double a = input.nextDouble(); if(a > 0) { Circle cir = new Circle(); cir.setRadius(a); System.out.printf("Circle's area:" ); System.out.printf("%.2f",cir.getArea()); }else { System.out.println("Wrong Format"); } }case 2:{ double a = input.nextDouble(); double b = input.nextDouble(); if(a > 0 && b > 0 ) { Rectangle rect = new Rectangle(); rect.setWidth(a); rect.setLength(b); System.out.printf("Rectangle's area:"); System.out.printf("%.2f", rect.getArea()); }else { System.out.println("Wrong Format"); } }case 3:{ double a = input.nextDouble(); if( a > 0) { Ball ball = new Ball(); ball.setRadius(a); System.out.printf("Ball's surface area:"); System.out.printf("%.2f\n", ball.getArea()); System.out.print("Ball's volume:"); System.out.printf("%.2f", ball.getVolume()); }else { System.out.println("Wrong Format"); } }case 4:{ double a = input.nextDouble(); double b = input.nextDouble(); double c = input.nextDouble(); if (a > 0 && b > 0 && c > 0) { Box box = new Box(); box.setWidth(a); box.setLength(b); box.setHeight(c); System.out.printf("Box's surface area:"); System.out.printf("%.2f\n", box.getArea()); System.out.print("Box's volume:"); System.out.printf("%.2f\n", box.getVolume()); }else { System.out.println("Wrong Format"); } } } }else { System.out.println("Wrong Format"); } } } abstract class Shape { public Shape() { super(); System.out.println("Constructing Shape"); } public double getArea() { return 0.0; } } class Circle extends Shape{ public Circle() { super(); System.out.println("Constructing Circle"); } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } private double radius; public double getArea() { return Math.PI * Math.pow(radius, 2); } } class Rectangle extends Shape{ public Rectangle() { super(); System.out.println("Constructing Rectangle"); } private double width,length; public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } public double getArea() { return width * length; } } class Ball extends Circle{ public Ball() { super(); System.out.println("Constructing Ball"); } public double getArea() { return 4 * super.getArea(); } public double getVolume() { return Math.PI * Math.pow(getRadius(),3) * 4 / 3; } } class Box extends Rectangle{ double height; public double getArea() { return 2 * (getLength() * getWidth() + getLength()* height +getWidth() * height); } public double getVolume() { return height * super.getArea(); } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public Box() { super(); System.out.println("Constructing Box"); } }



题目集6(7-5)的代码、类图、圈复杂度如下
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub ArrayList<Double> in = new ArrayList<Double>(); Scanner input = new Scanner(System.in); int a = input.nextInt(); int b = input.nextInt(); int c = input.nextInt(); int n = a + b + c; double sum = 0; boolean p = false; boolean o = false; Shape []shape = new Shape[n]; if(a < 0 || b < 0 || c < 0) { p = true; o = true; System.out.println("Wrong Format"); }else { // double []num = new double[n]; for(int i = 0;i < a;i++) { double radius = input.nextDouble(); Circle circ = new Circle(radius); if(circ.validate()) { shape[i] = circ; in.add(circ.getArea()); sum += shape[i].getArea(); }else { p = true; } }for(int i = a;i < a + b;i++) { double width = input.nextDouble(); double length = input.nextDouble(); Rectangle recta = new Rectangle(width, length); if(recta.validate()) { shape[i] = recta; in.add(recta.getArea()); sum += shape[i].getArea(); }else { p = true; } }for(int i = a + b;i < a + b + c;i++) { double side1 = input.nextDouble(); double side2 = input.nextDouble(); double side3 = input.nextDouble(); Triangle tria = new Triangle(side1,side2,side3); if(tria.validate()) { shape[i] = tria; in.add(tria.getArea()); sum += shape[i].getArea(); }else { p = true; } } } if(p && !o) { System.out.print("Wrong Format"); }else if(!p) { Collections.sort(in); System.out.println("Original area:"); for(int i = 0;i < n;i++) { System.out.printf((String.format("%.2f",shape[i].getArea())) + " "); }System.out.print("\n"); System.out.println("Sum of area:" + String.format("%.2f",sum)); System.out.println("Sorted area:"); for(int i = 0;i < in.size();i++) { System.out.print(String.format("%.2f",in.get(i)) + " "); }System.out.print("\n"); System.out.println("Sum of area:" + String.format("%.2f",sum)); } } } abstract class Shape { public abstract double getArea(); public abstract boolean validate(); @Override public String toString() { return "Shape []"; } } class Circle extends Shape{//圆 double radius; public Circle() { super(); // TODO Auto-generated constructor stub } public Circle(double radius) { super(); this.radius = radius; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } @Override public double getArea() { // TODO Auto-generated method stub return Math.PI * Math.pow(radius,2); } @Override public boolean validate() { // TODO Auto-generated method stub return radius > 0; } } class Rectangle extends Shape{//正方形 double width,length; public Rectangle(double width, double length) { super(); this.width = width; this.length = length; } public Rectangle() { super(); // TODO Auto-generated constructor stub } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } @Override public double getArea() { // TODO Auto-generated method stub return width * length; } @Override public boolean validate() { // TODO Auto-generated method stub return (width > 0 && length >0); } } class Triangle extends Shape{//三角形 double side1,side2,side3; public Triangle(double side1, double side2, double side3) { super(); this.side1 = side1; this.side2 = side2; this.side3 = side3; } public Triangle() { super(); // TODO Auto-generated constructor stub } public double getSide1() { return side1; } public void setSide1(double side1) { this.side1 = side1; } public double getSide2() { return side2; } public void setSide2(double side2) { this.side2 = side2; } public double getSide3() { return side3; } public void setSide3(double side3) { this.side3 = side3; } @Override public double getArea() { // TODO Auto-generated method stub return Math.sqrt((side1+side2+side3) * (side1+side2-side3) * (side1+side3-side2) * (side2+side3-side1)) * 1 /4; } @Override public boolean validate() { // TODO Auto-generated method stub boolean p; if(side1>0&&side2>0&&side3>0&&side1+side2>side3&&side1+side3>side2&&side2+side3>side1) { p = true; }else { p = false; } return p; } }




题目集6(7-6)的类图、代码、圈复杂度如下:
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); Circle1 cir = new Circle1(); Rectangle1 rect = new Rectangle1(); double a = input.nextDouble(); double b = input.nextDouble(); double c = input.nextDouble(); if(a > 0 && b > 0 && c > 0) { cir.setRadius(a); rect.setWidth(b); rect.setLength(c); System.out.println((String.format("%.2f",cir.getArea()))); System.out.println((String.format("%.2f",rect.getArea()))); }else { System.out.println("Wrong Format"); } } } interface GetArea { public double getArea(); } class Circle1 implements GetArea{ double radius; public Circle1(double radius) { super(); this.radius = radius; } public Circle1() { super(); // TODO Auto-generated constructor stub } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } @Override public double getArea() { // TODO Auto-generated method stub return Math.PI * radius* radius; } } class Rectangle1 implements GetArea{ double width,length; public Rectangle1(double width, double length) { super(); this.width = width; this.length = length; } public Rectangle1() { super(); // TODO Auto-generated constructor stub } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } @Override public double getArea() { // TODO Auto-generated method stub return width * length; } }






在题目集4(7-3)中,我们用到了继承,首先有一个shape抽象类,在这个类里只有构造方法和求图形面积的方法(未实现),而后又以它为父类创建了两个子类circl和rectangle,而circle和rectangle又分别有一个子类ball和box,层层继承(就像人类社会中的亲缘关系),这使得各个类之间的关系更加密切与实际,从功能和语法上都是可以实现的。而题目集6(7-5)比较题目集4(7-3)多了一个要求,即求所有图形的面积及面积之和,并根据面积大小排序。这里对于shape类的设计还是大同小异,依旧采用抽象类设计,通过重写父类中的方法实现了面向对象设计中的多态性,使得程序的耦合度大大减少,也有利于后续对系统的维护。题目集6(7-6)中增加了创建接口的要求,而不使用创建父类进而继承父类重写方法的操做。抽象类与接口的差别通过查询主要如下:
(1)接口只有定义,不能有方法的实现,java 1.8中可以定义default方法体,而抽象类可以有定义与实现,方法可在抽象类中实现。
(2)实现接口的关键字为implements,继承抽象类的关键字为extends。一个类可以实现多个接口,但一个类只能继承一个抽象类。所以,使用接口可以间接地实现多重继承。
(3)接口强调特定功能的实现,而抽象类强调所属关系。
(4)接口成员变量默认为public static final,必须赋初值,不能被修改;其所有的成员方法都是public、abstract的。抽象类中成员变量默认default,可在子类中被重新定义,也可被重新赋值;抽象方法被abstract修饰,不能被private、static、synchronized和native等修饰,必须以分号结尾,不带花括号。
通过使用接口发现程序的圈复杂度有所下降,这也提示我在以后的学习中需要多去了解接口的用处,多对接口进行使用。
3.对三次题目集中用到的正则表达式技术的分析总结
1、正则表达式一直是我的硬伤,由于开始接触时难度比较大,导致我一直心有余悸。但在老师降低难度以后又让我重新拾起了学习的热情。现在对于正则表达式我已经渐渐进入状态,但距离精通正则还是又很长的路要走。
2、这几次的题目集大量应用了正则表达式,使得我们对正则表达式有了一个初步的认识。而对于正则的大型题目(南水北调水文数和表达式求导等)还是没有解决的能力,只能解决一些比较简单的问题,比如QQ号校验,验证码校验,学号校验等。具体实现如下:
QQ号校验:
import java.util.Scanner; public class Main{ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); String qq = input.nextLine(); String real = "[1-9][0-9]{4,14}"; boolean p = qq.matches(real); if(p) { System.out.print("你输入的QQ号验证成功"); } else System.out.print("你输入的QQ号验证失败"); } }
字符串训练(对输入的字符串中的字符进行排序并输出):
import java.util.Arrays; import java.util.Scanner; public class Main{ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); String str = input.nextLine(); String []sum = str.split(""); Arrays.sort(sum); for(int i = 0;i < sum.length;i++) System.out.print(sum[i]); } }
验证码校验:
import java.util.Scanner; public class Main{ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); String num = input.nextLine(); String real = "\\w{4}"; boolean p = num.matches(real); if(p) { System.out.println(num + "属于验证码"); } else System.out.println(num + "不属于验证码"); } }
学号校验:
import java.util.Scanner; public class Main{ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); String num = input.nextLine(); String real = "2020{1}(|1[1-7]|61|7[1-3]|8[1-2]){1}(0[1-9]|[1-3][0-9]|40)"; boolean p = num.matches(real); if(p) { System.out.println("正确"); } else System.out.println("错误"); } }
以上问题都很简单,但偶尔也会遇到一点小问题,这在之后的踩坑心得中会提到。
4.题目集5(7-4)中Java集合框架应用的分析总结
由于能力有限,这道题还未解决,这说明我对于正则表达式的学习还有待加强,需要在以后的学习中更加努力。
三、踩坑心得
由于此次的题目比较多,所以我挑了几个对我来说比较有代表性错误
- 在题目集4(7-3)中,对于类ball中的体积计算,一开始我按照习惯将数字放在前面,也就是:
public double getVolume() { return (4 / 3) * Math.PI * Math.pow(getRadius(),3); }
结果发现测试点无法通过,计算的结果有误,反复调试后我将代码改为如下:
public double getVolume() { return Math.PI * Math.pow(getRadius(),3) * 4 / 3; }
终于通过了测试点。这也提醒了我要注意Java中对于运算符的优先级需要着重考虑
- 在题目集6(7-4)中,一开始我对于学号的校验是这样的:
String real = "2020{1}(|1[1-7]|61|7[1-3]|8[1-2]){1}(0[1-9]|[1-3][0-9]|40)"; boolean p = num.matches(real); if(p) { System.out.println("正确"); } else System.out.println("错误"); }而后发现出错,主要是因为这样写无法取得区间内的所有数字,导致学号丢失,之后我将代码改成了这样:
String real = "2020{1}(|1[1-7]|61|7[1-3]|8[1-2]){1}(0[1-9]|[12]\\d|3[0-9]|40)"; boolean p = num.matches(real); if(p) { System.out.println("正确"); } else System.out.println("错误"); }也就是这样,我觉得做题更不能想当然,需要更加认真的去校验
- 在日期问题面向对象设计(聚合)中,一开始我没有对输入的每个日期合法性进行验证,导致程序出错,所以我加入了一个方法:
public boolean isMonth() {//判断输入的日期是否越界 boolean p = false; if(this.month.getValue() == 1||this.month.getValue() == 3||this.month.getValue() == 5||this.month.getValue() == 7||this.month.getValue() == 8||this.month.getValue() == 10||this.month.getValue() == 12) { if(this.day.getValue() <= 31) { p=true;} else { p=false; } }else if(this.month.getValue() == 2) { if(this.year.isLeapYear(this.year.getValue())) { if(this.day.getValue() <= 29) { p = true; }else { p = false; } }else { if(this.day.getValue() <=28) { p = true; }else { p = false; } } }else { if(this.day.getValue() <= 30) { p = true; }else { p = false; } } return p; }
用这个方法对日期进行合法校验,使得整个程序可以正常运行,也成功通过了每个测试点
四、改进建议
对于现在的面向对象学习,我的学习方式还是有一定的问题的,需要花费更多的时间在这方面的学习上。具体到每个细节上的话,也可以说出一二。比如现在我写的程序还是喜欢使用for和if语句,这也使得每个程序的圈复杂度都比较高,有些程序的圈复杂度甚至超过了10。在以后的学习过程中需要学会使用switch等语句代替如上语句,降低程序圈复杂度。且在类型设计上都需要花费心思,力求使每个类之间的耦合度降到最低,实现功能的独立性、单一性。对于题目的理解也需要更花时间,做到不缺少功能,该需要添加的功能则做到一次性看出,并添加到相应的类或方法中去。
五、总结
这几次的题目还是主要以聚合以及正则表达式为主,同时也对接口有了初步的了解。总体来说,我对于正则表达式的学习还不够,使得有很多问题还是无法解决,这也提醒我在以后的学习中需要更多的去学习了解正则表达式的应用。而对于Java中的链表也有了初步的使用,对于相应的功能也有了初步的应用。而对于这几次题目集,老师也出的比较有水平,有难有易,使得我对于Java的学习也有了信心,也很有收获。希望在以后的学习中可以更加努力,将这一门学科学精学好。

浙公网安备 33010602011771号