新手向--关于菜鸟的第一次博客作业
我的第一次博客作业
一.浅浅总结一下我的前三次PTA作业.
课时训练1:
知识点是对Java字符串输入及输出,if语句的使用,字符的查找的基础应用;题量一般,但是难度都挺小,主要是基础应用,没有挖坑,顺利通关。但是当时的我对于Java也是半个门外汉,犯了不少错误,甚至于不知道如何查找字符,最后是在b站里看翁恺老师的视频看明白的。
课时训练2:
知识点是对Java字符串查找的进阶与巩固,我利用到了indexof语句;题量很小,难度不大,较课时训练1 的题目难度大了一些,尤其是第二题卡我时间较长,主要是不太能看懂题目要求,后来是通过百度和csdn查清楚题意,才顺利写出来的。
课时训练3:
知识点是设计不同的类与方法,调用不同的类与对象。题量很小,但是难度对于我来说有点偏难,如果把课时训练1 比作过家家,课时训练2 是幼儿园,那么课时训练3 就是上初中了……直接给我挑一个阶段,没有过渡,我很不适应。甚至询问了许多同学,才勉强写出来第三题,第四题却已经没时间和精力做了。
二.我的不成熟的设计与分析.
课时训练2 的第2 题:
1 import java.util.Scanner; 2 3 public class Main { 4 public static void main(String[] args) { 5 Scanner in = new Scanner(System.in); 6 String str = in.nextLine(); 7 if(str.length()<11 || str.indexOf("0",0)==-1) {//如果输入的字符串没有11位,或是没有’0‘ 8 System.out.println("null data"); 9 } 10 else { 11 int x = str.indexOf("0");//x等于”0“返回的位置 12 int y = str.length(); 13 int c=1; 14 for(;y-x>10;) {//当”0“的后面仍有11位的情况将继续循环 15 int a=1; 16 int z=0; 17 int b = str.indexOf("0",x);//重置从x的位置往后检测 18 for(int i=b;i<b+11;i++) {//检验每一个字符 19 if(str.charAt(b+10)=='0') {//如果结束位不是0 20 System.out.println(c+":"+"validate error"); 21 a=0;//a相当于一个flag 22 break; 23 } 24 if(str.charAt(i)=='1') {//检测”1“出现了几次 25 z++; 26 } 27 } 28 if(z%2==0 && a!=0) { 29 System.out.print(c+":"); 30 for(int i=b+1;i<b+9;i++) { 31 System.out.print(str.charAt(i)); 32 } 33 System.out.println();//仅仅是个回车符 34 } 35 else if(z%2!=0 && a!=0){ 36 System.out.println(c+":"+"parity check error"); 37 } 38 x=b+11;//重置x 39 c++; 40 //System.out.println(); 41 } 42 } 43 } 44 }
本题并不算特别难,只要弄懂了串口字符的定义,了解了什么是就检验位和结束位分别是什么位置便能做出来。
课时训练3 的第1 题:
1 import java.util.Scanner; 2 3 public class Main { 4 public static void main(String[] args){ 5 Scanner input = new Scanner(System.in); 6 7 double a = Double.parseDouble(input.next()); 8 double b = Double.parseDouble(input.next()); 9 double c = Double.parseDouble(input.next()); 10 11 if(a == 0){ 12 System.out.println("Wrong Format"); 13 System.exit(0); 14 } 15 16 //create a QuadraticEquation object 17 QuadraticEquation equation = new QuadraticEquation(a, b, c); 18 //get value of b * b - 4 * a * c 19 double discriminant = equation.getDiscriminant(); 20 21 System.out.println("a=" + equation.getA() + 22 ",b=" + equation.getB() + 23 ",c=" + equation.getC()+":"); 24 25 if (discriminant < 0) { 26 System.out.println("The equation has no roots."); 27 } 28 else if (discriminant == 0) 29 { 30 System.out.println("The root is " + 31 String.format("%.2f", equation.getRoot1())); 32 } 33 else // (discriminant >= 0) 34 { 35 System.out.println("The roots are " + 36 String.format("%.2f", equation.getRoot1()) 37 + " and " + String.format("%.2f", equation.getRoot2())); 38 } 39 } 40 } 41 42 class QuadraticEquation{ 43 double a = 0;//二次项的系数 44 double b = 0;//一次项的系数 45 double c = 0;//常数 46 47 public QuadraticEquation(double a, double b, double c) {//构造函数 48 this.a = a; 49 this.b = b; 50 this.c = c; 51 } 52 public double getA() { 53 //String A = String.format("%.1f",a);//这里返回的是字符串,所以无法返回double 54 return a; 55 } 56 public double getB() { 57 //String B = String.format("%.1f",b); 58 return b; 59 } 60 public double getC() { 61 //String C = String.format("%.1f",c); 62 return c; 63 } 64 public double getDiscriminant() { 65 double D = b*b - 4.0*a*c;//b的平方-4ac 66 return D; 67 } 68 public double getRoot1() { 69 double R1 = (-b+Math.sqrt(b*b-4.0*a*c))/(2.0*a); 70 return R1; 71 } 72 public double getRoot2() { 73 double R2 = (-b-Math.sqrt(b*b-4.0*a*c))/(2.0*a); 74 return R2; 75 } 76 }
本题也无难点,单纯利用求根公式进行操作,设计一个类装着私有属性三元二次方程的系数,一步步操作即可。
课时训练3 的第2 题:
1 import java.util.Scanner; 2 3 public class Main { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 int year = 0; 7 int month = 0; 8 int day = 0; 9 10 int choice = input.nextInt(); 11 12 if (choice == 1) { // test getNextNDays method 13 int m = 0; 14 year = Integer.parseInt(input.next()); 15 month = Integer.parseInt(input.next()); 16 day = Integer.parseInt(input.next()); 17 18 DateUtil date = new DateUtil(year, month, day); 19 20 if (!date.checkInputValidity()) { 21 System.out.println("Wrong Format"); 22 System.exit(0); 23 } 24 25 m = input.nextInt(); 26 27 if (m < 0) { 28 System.out.println("Wrong Format"); 29 System.exit(0); 30 } 31 32 System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:"); 33 System.out.println(date.getNextNDays(m).showDate()); 34 } else if (choice == 2) { // test getPreviousNDays method 35 int n = 0; 36 year = Integer.parseInt(input.next()); 37 month = Integer.parseInt(input.next()); 38 day = Integer.parseInt(input.next()); 39 40 DateUtil date = new DateUtil(year, month, day); 41 42 if (!date.checkInputValidity()) { 43 System.out.println("Wrong Format"); 44 System.exit(0); 45 } 46 47 n = input.nextInt(); 48 49 if (n < 0) { 50 System.out.println("Wrong Format"); 51 System.exit(0); 52 } 53 54 System.out.print( 55 date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:"); 56 System.out.println(date.getPreviousNDays(n).showDate()); 57 } else if (choice == 3) { //test getDaysofDates method 58 year = Integer.parseInt(input.next()); 59 month = Integer.parseInt(input.next()); 60 day = Integer.parseInt(input.next()); 61 62 int anotherYear = Integer.parseInt(input.next()); 63 int anotherMonth = Integer.parseInt(input.next()); 64 int anotherDay = Integer.parseInt(input.next()); 65 66 DateUtil fromDate = new DateUtil(year, month, day); 67 DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay); 68 69 if (fromDate.checkInputValidity() && toDate.checkInputValidity()) { 70 System.out.println("The days between " + fromDate.showDate() + 71 " and " + toDate.showDate() + " are:" 72 + fromDate.getDaysofDates(toDate)); 73 } else { 74 System.out.println("Wrong Format"); 75 System.exit(0); 76 } 77 } 78 else{ 79 System.out.println("Wrong Format"); 80 System.exit(0); 81 } 82 } 83 } 84 class DateUtil { 85 private int x;//年 86 private int y;//月 87 private int z;//日 88 89 public DateUtil(int year,int month,int day) {//构造函数 90 x = year; 91 y = month; 92 z = day; 93 } 94 public int getYear() { 95 return x; 96 } 97 public int getMonth() { 98 return y; 99 } 100 public int getDay() { 101 return z; 102 } 103 public boolean checkInputValidity() { 104 boolean checkInputValidity = false;//初始化一个Boolean变量 105 if (x <= 2020 && x >= 1820 && y <= 12 && y >= 1 && z <= 31 && z >= 1) {//如果不是在1820至2020年,月不是在1月和12月之间,天不是在1至31天之内 106 if (y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12) {//如果是1,3,5,7,8,,10,12月 107 checkInputValidity = true; 108 } else if ((y == 4 || y == 6 || y == 9 || y == 11) && z < 31) {//如果是4,6,9,11月并且天数小于31天 109 checkInputValidity = true; 110 } else {//y==2 111 if (isLeapYear(x) == true && z < 30) {//是闰年并且天数小于30 112 checkInputValidity = true; 113 } 114 else if(isLeapYear(x) == false && z < 29) {//不是闰年并且天数小于29 115 checkInputValidity = true; 116 } 117 } 118 } 119 return checkInputValidity; 120 } 121 public boolean isLeapYear(int year) {//判断是否为闰年 122 boolean isLeapYear = ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0); //四年一闰,百年不润,四百年在润 123 return isLeapYear; 124 } 125 public DateUtil getNextNDays(int n) {//获取下n天 126 int[] arr1=new int[]{31,29,31,30,31,30,31,31,30,31,30,31};//闰年的月份天数 127 int[] arr2=new int[]{31,28,31,30,31,30,31,31,30,31,30,31};//平年的 128 long m = n ;//用long是因为接下来的判断条件会使得超过int最大整型值 129 for(;m > 0;) { 130 if(y == 12 && m + z > 31) {//如果12月,并且天数为32 131 y = 1;//重置 132 x ++; 133 m = m - 32 + z; 134 z = 1; 135 } 136 else { 137 if(isLeapYear(x)) {//如果是闰年 138 if(arr1[y-1] < m + z) {//在该月份里下m天超过本月份的最大天数 139 y ++;//令月份加1 140 m = m - arr1[y-2] + z - 1;//重置 141 z = 1; 142 } 143 else { 144 z = (int) (m + z);//直接让m与天数相加,求出来的便是所求日期中的日 145 m = 0; 146 } 147 } 148 else if(!isLeapYear(x)) {//如果不是闰年 149 if(arr2[y-1] < m + z) { 150 y ++; 151 m = m - arr2[y-2] + z - 1; 152 z = 1; 153 } 154 else { 155 z = (int) (m + z); 156 m = 0; 157 } 158 } 159 } 160 } 161 return this;//返回所有值 162 } 163 public DateUtil getPreviousNDays(int n) {//获取前n天,大致与下n天思路一致 164 int[] arr1=new int[]{31,29,31,30,31,30,31,31,30,31,30,31}; 165 int[] arr2=new int[]{31,28,31,30,31,30,31,31,30,31,30,31}; 166 for(;n > 0;) { 167 if(y == 1 && z - n <= 0) {//如果月份是一月,并且日减去所求n比1还小 168 y = 12;//重置为12月31日,年份减1 169 x --; 170 n = n - z; 171 z = 31; 172 } 173 else { 174 if(isLeapYear(x)) {//判断是否为闰年 175 if(n - z >= 0) {//如果月份减去前n天中的n比1还小 176 y --;//月份-1,日将变为上一个月的最大日期 177 n = n - z;//重置 178 z = arr1[y-1]; 179 } 180 else { 181 z = z - n; 182 n = 0; 183 } 184 } 185 if(!isLeapYear(x)) {//如果不是闰年 186 if(n - z >= 0) { 187 n = n - z; 188 y --; 189 z = arr2[y-1]; 190 } 191 else { 192 z = z - n; 193 n = 0; 194 } 195 } 196 } 197 } 198 return this; 199 } 200 public String showDate() { 201 202 return String.format("%d-%d-%d",x,y,z); 203 } 204 public int getDaysofDates(DateUtil toDate) {//判断日期相差多少天 205 int count1 = 0; 206 //判断两者谁大谁小 207 if(equalTwoDates(toDate)) {//如果上一个日期比这个日期晚 208 for(;compareDates(toDate) != true;toDate.z ++) { 209 if(toDate.y == 12 && toDate.z == 32) {//如果是12月31日 210 toDate.x ++;//年份+1,日期为1月1 211 toDate.y = 1; 212 toDate.z = 1; 213 } 214 else if((toDate.y == 1 ||toDate.y == 3 ||toDate.y == 5 ||toDate.y == 7 ||toDate.y == 8 ||toDate.y == 10) && toDate.z >31) { 215 toDate.y ++;//如果是1,3,5,7,8,10,12月份的其中一月并且日大于31,则月份+1,日变为1,以此类推 216 toDate.z = 1; 217 218 } 219 else if((toDate.y == 4 ||toDate.y == 6 ||toDate.y == 9 ||toDate.y == 11) && toDate.z >30) { 220 toDate.y ++; 221 toDate.z = 1; 222 223 } 224 else if(toDate.y == 2 && isLeapYear(toDate.x) == true && toDate.z >29) { 225 toDate.y ++; 226 toDate.z = 1; 227 228 } 229 else if(toDate.y == 2 && isLeapYear(toDate.x) == false && toDate.z >28) { 230 toDate.y ++; 231 toDate.z = 1; 232 233 } 234 count1 ++;//每一次循环,计数+1 235 } 236 } 237 else {//如果这个日期没有比下个日期晚 238 for(;compareDates(toDate) != true;z ++) { 239 if(y == 12 && z == 32) { 240 x ++; 241 y = 1; 242 z = 1; 243 } 244 else if((y == 1 ||y == 3 ||y == 5 ||y == 7 ||y == 8 ||y == 10) && z >31) { 245 y ++; 246 z = 1; 247 248 } 249 else if((y == 4 ||y == 6 ||y == 9 ||y == 11) && z >30) { 250 y ++; 251 z = 1; 252 253 } 254 else if(y == 2 && isLeapYear(x) == true && z >29) { 255 y ++; 256 z = 1; 257 258 } 259 else if(y == 2 && isLeapYear(x) == false && z >28) { 260 y ++; 261 z = 1; 262 263 } 264 count1 ++; 265 } 266 } 267 return count1; 268 } 269 public boolean compareDates(DateUtil date) {//判断两个日期是否相等 270 boolean compareDates = (x == date.x && y == date.y && z == date.z); 271 return compareDates; 272 } 273 public boolean equalTwoDates(DateUtil date) {//判断上一个日期是否比下一个日期晚 274 boolean equalTwoDates = (x > date.x ||x == date.x && y > date.y ||x == date.x && y == date.y && z > date.z) ; 275 return equalTwoDates; 276 } 277 }
本题绞尽了我好多脑细胞,让我掉了几白根秀发,确实有点难了,我找测试点都找了一下午。做本题做了超过12个小时。对于前n天与后n天的设计我按照以月为基数,满足本月的天数便+1月,或是-1月。一旦达到了临界值,如1月0号,12月32号,边进行一次重置。计算两个日期的相差天数时,我先找出谁的日期晚,让晚的日期按照前n天的方式进行计算,天数+1,判断条件是日期是否相等,并用count计数。
课时训练3 的第3 题:
1 import java.util.Scanner; 2 3 public class Main { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 int year = 0; 7 int month = 0; 8 int day = 0; 9 10 int choice = input.nextInt(); 11 12 if (choice == 1) { // test getNextNDays method 13 int m = 0; 14 year = Integer.parseInt(input.next()); 15 month = Integer.parseInt(input.next()); 16 day = Integer.parseInt(input.next()); 17 18 DateUtil date = new DateUtil(year, month, day); 19 20 if (!date.checkInputValidity()) { 21 System.out.println("Wrong Format"); 22 System.exit(0); 23 } 24 25 m = input.nextInt(); 26 27 if (m < 0) { 28 System.out.println("Wrong Format"); 29 System.exit(0); 30 } 31 System.out.println(date.getNextNDays(m).showDate()); 32 } else if (choice == 2) { // test getPreviousNDays method 33 int n = 0; 34 year = Integer.parseInt(input.next()); 35 month = Integer.parseInt(input.next()); 36 day = Integer.parseInt(input.next()); 37 38 DateUtil date = new DateUtil(year, month, day); 39 40 if (!date.checkInputValidity()) { 41 System.out.println("Wrong Format"); 42 System.exit(0); 43 } 44 45 n = input.nextInt(); 46 47 if (n < 0) { 48 System.out.println("Wrong Format"); 49 System.exit(0); 50 } 51 System.out.println(date.getPreviousNDays(n).showDate()); 52 } else if (choice == 3) { // test getDaysofDates method 53 year = Integer.parseInt(input.next()); 54 month = Integer.parseInt(input.next()); 55 day = Integer.parseInt(input.next()); 56 57 int anotherYear = Integer.parseInt(input.next()); 58 int anotherMonth = Integer.parseInt(input.next()); 59 int anotherDay = Integer.parseInt(input.next()); 60 61 DateUtil fromDate = new DateUtil(year, month, day); 62 DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay); 63 64 if (fromDate.checkInputValidity() && toDate.checkInputValidity()) { 65 System.out.println(fromDate.getDaysofDates(toDate)); 66 } else { 67 System.out.println("Wrong Format"); 68 System.exit(0); 69 } 70 } else { 71 System.out.println("Wrong Format"); 72 System.exit(0); 73 } 74 } 75 } 76 77 class DateUtil { 78 Day day; 79 public DateUtil(){ 80 } 81 public DateUtil(int d,int m,int y){ 82 this.day = new Day(d,m,y); 83 } 84 public Day getDay() { 85 return day; 86 } 87 public void setDay(Day d) { 88 this.day = d; 89 } 90 public boolean checkInputValidity(){ 91 boolean checkInputValidity = (this.getDay().getMonth().getYear().validate()&&this.getDay().getMonth().validate()&&day.validate()); 92 return checkInputValidity; 93 } 94 public DateUtil getNextNDays(int n) { 95 int z = this.getDay().getValue(); 96 int y = this.getDay().getMonth().getValue(); 97 int x = this.getDay().getMonth().getYear().getValue(); 98 int[] arr1=new int[]{31,29,31,30,31,30,31,31,30,31,30,31}; 99 int[] arr2=new int[]{31,28,31,30,31,30,31,31,30,31,30,31}; 100 long m = n ; 101 for(;m > 0;) { 102 if(y == 12 && m + z > 31) { 103 y = 1; 104 x ++; 105 m = m - 32 + z; 106 z = 1; 107 } 108 else { 109 if(getDay().getMonth().getYear().isLeapYear(x)) { 110 if(arr1[y-1] < m + z) { 111 y ++; 112 m = m - arr1[y-2] + z - 1; 113 z = 1; 114 } 115 else { 116 z = (int) (m + z); 117 m = 0; 118 } 119 } 120 else if(!getDay().getMonth().getYear().isLeapYear(x)) { 121 if(arr2[y-1] < m + z) { 122 y ++; 123 m = m - arr2[y-2] + z - 1; 124 z = 1; 125 } 126 else { 127 z = (int) (m + z); 128 m = 0; 129 } 130 } 131 } 132 } 133 DateUtil newdate = new DateUtil(x,y,z); 134 return newdate; 135 } 136 public DateUtil getPreviousNDays(int n) { 137 int z = this.getDay().getValue(); 138 int y = this.getDay().getMonth().getValue(); 139 int x = this.getDay().getMonth().getYear().getValue(); 140 int[] arr1=new int[]{31,29,31,30,31,30,31,31,30,31,30,31}; 141 int[] arr2=new int[]{31,28,31,30,31,30,31,31,30,31,30,31}; 142 for(;n > 0;) { 143 if(y == 1 && z - n <= 0) { 144 y = 12; 145 x --; 146 n = n - z; 147 z = 31; 148 } 149 else { 150 if(getDay().getMonth().getYear().isLeapYear(x)) { 151 if(n - z >= 0) { 152 y --; 153 n = n - z; 154 z = arr1[y-1]; 155 } 156 else { 157 z = z - n; 158 n = 0; 159 } 160 } 161 if(!getDay().getMonth().getYear().isLeapYear(x)) { 162 if(n - z >= 0) { 163 n = n - z; 164 y --; 165 z = arr2[y-1]; 166 } 167 else { 168 z = z - n; 169 n = 0; 170 } 171 } 172 } 173 } 174 DateUtil newdate = new DateUtil(x,y,z); 175 return newdate; 176 } 177 public String showDate() { 178 return String.format("%d-%d-%d",this.getDay().getMonth().getYear().getValue(),this.getDay().getMonth().getValue(),this.getDay().getValue()); 179 } 180 public int getDaysofDates(DateUtil date) { 181 int count1 = 0; 182 int x = this.getDay().getMonth().getYear().getValue(); 183 int y = this.getDay().getMonth().getValue(); 184 int z = this.getDay().getValue(); 185 int x1 = date.getDay().getMonth().getYear().getValue(); 186 int y1 = date.getDay().getMonth().getValue(); 187 int z1 = date.getDay().getValue(); 188 //判断两者谁大谁小 189 if(equalTwoDates(date)) { 190 for(;compareDates(date) != true;) { 191 z1 ++; 192 count1 ++; 193 if(y1 == 12 && z1 == 32) { 194 x1 ++; 195 y1 = 1; 196 z1 = 1; 197 } 198 else if((y1 == 1 ||y1 == 3 ||y1 == 5 ||y1 == 7 ||y1 == 8 ||y1 == 10) && z1 >31) { 199 y1 ++; 200 z1 = 1; 201 202 } 203 else if((y1 == 4 ||y1 == 6 ||y1 == 9 ||y1 == 11) && z1 >30) { 204 y1 ++; 205 z1 = 1; 206 207 } 208 else if(y1 == 2 && getDay().getMonth().getYear().isLeapYear(x) == true && z1 >29) { 209 y1 ++; 210 z1 = 1; 211 212 } 213 else if(y1 == 2 && getDay().getMonth().getYear().isLeapYear(x) == false && z1 >28) { 214 y1 ++; 215 z1 = 1; 216 217 } 218 } 219 } 220 else { 221 for(;compareDates(date) != true;) { 222 z ++; 223 count1 ++; 224 if(y == 12 && z == 32) { 225 x ++; 226 y = 1; 227 z = 1; 228 } 229 else if((y == 1 ||y == 3 ||y == 5 ||y == 7 ||y == 8 ||y == 10) && z == 32) { 230 y ++; 231 z = 1; 232 233 } 234 else if((y == 4 ||y == 6 ||y == 9 ||y == 11) && z >30) { 235 y ++; 236 z = 1; 237 238 } 239 else if(y == 2 && getDay().getMonth().getYear().isLeapYear(x) == true && z >29) { 240 y ++; 241 z = 1; 242 243 } 244 else if(y == 2 && getDay().getMonth().getYear().isLeapYear(x) == false && z >28) { 245 y ++; 246 z = 1; 247 248 } 249 } 250 } 251 return count1; 252 } 253 public boolean compareDates(DateUtil date) { 254 boolean compareDates = (this.getDay().getMonth().getYear().getValue() > date.getDay().getMonth().getYear().getValue() || 255 this.getDay().getMonth().getYear().getValue() == date.getDay().getMonth().getYear().getValue() && this.getDay().getMonth().getValue() > date.getDay().getMonth().getValue() || 256 date.getDay().getMonth().getYear().getValue() == this.getDay().getMonth().getYear().getValue() && date.getDay().getMonth().getValue() == this.getDay().getMonth().getValue() && this.getDay().getValue() > date.getDay().getValue()); 257 return compareDates; 258 } 259 public boolean equalTwoDates(DateUtil date){ 260 boolean equalTwoDates = (this.getDay().getValue() == date.getDay().getValue() && this.getDay().getMonth().getValue() == date.getDay().getMonth().getValue() && this.getDay().getMonth().getYear().getValue() == date.getDay().getMonth().getYear().getValue()); 261 return false; 262 } 263 } 264 class Day{ 265 int value; 266 Month month; 267 public int arr[]= {0,31,28,31,30,31,30,31,31,30,31,30,31}; 268 public Day(){ 269 } 270 public Day(int yearValue,int monthValue,int dayValue){ 271 this.month = new Month(yearValue,monthValue); 272 this.value = dayValue; 273 } 274 public int getValue(){ 275 return value; 276 } 277 public Month getMonth(){ 278 return month; 279 } 280 public void setValue(int value){ 281 this.value = value; 282 } 283 public void setMonth(Month value){ 284 this.month = value; 285 } 286 public void resetMin(){ 287 value = 1; 288 } 289 public void resetMax(){ 290 value = arr[month.getValue()]; 291 } 292 public boolean validate(){ 293 if(this.getMonth().getYear().isLeapYear()) 294 arr[2] = 29; 295 boolean validate = (value >= 1 && value <= arr[month.getValue()]); 296 return validate; 297 } 298 public void dayIncrement() { 299 value ++; 300 } 301 public void dayReduction() { 302 value --; 303 } 304 } 305 class Month { 306 int value; 307 Year year; 308 public Month(){ 309 } 310 public Month(int yearValue,int monthValue){ 311 this.year = new Year(yearValue); 312 this.value = monthValue; 313 } 314 public int getValue(){ 315 return value; 316 } 317 public Year getYear(){ 318 return year; 319 } 320 public void setValue(int value){ 321 this.value = value; 322 } 323 public void setYear(Year year){ 324 this.year = year; 325 } 326 public void resetMin(){ 327 value = 1; 328 } 329 public void resetMax(){ 330 value = 12; 331 } 332 public boolean validate(){ 333 boolean validate = (value >= 1 && value <= 12); 334 return validate; 335 } 336 public void MonthIncrement(){ 337 value ++; 338 } 339 public void MonthReduction(){ 340 value --; 341 } 342 } 343 class Year { 344 int value; 345 public Year(){ 346 } 347 public Year(int value){ 348 this.value = value; 349 } 350 public int getValue(){ 351 return value; 352 } 353 public void setValue(int value){ 354 this.value = value; 355 } 356 public boolean isLeapYear(){ 357 boolean isLeapYear = ((value % 4 == 0 && value % 100 != 0)|| value % 400 == 0); 358 return isLeapYear; 359 } 360 public boolean isLeapYear(int year){ 361 boolean isLeapYear = ((year % 4 == 0 && year % 100 != 0)||year % 400 == 0); 362 return isLeapYear; 363 } 364 public boolean validate(){ 365 boolean validate = (value <= 2050 && value >= 1900); 366 return validate; 367 } 368 public void yearIncrement(){ 369 value ++; 370 } 371 public void yearReduction(){ 372 value --; 373 } 374 }
本体是第二题的进阶版本,大致把新的类按照PowerDesigner图做成。方法没变,多了许多数据检验。按照我的理解,仿佛嵌套一样,或是链表一样,一点一点往里面调用。这题我真的时从早上看到晚上,才做出来框架啊。还好框架做出来,其他的只要在第二题里套用就行了。
三.我入的陨石坑.
1.关于字符串。我常用的查找是indexof(subString,[startIndex]),charAt(intdex)。其中前者返回索引位置,后者返回索引位置上的char值。首先类似数组一样,一个字符在字符串的位置相当于它是序数减1,比如“abcd”,a是第0位。而且一定不要超出数组的str.lenth()。否则会
2.关于最大边界测试,秃头小宝贝们一定要记得如果是int整型量,最大值为+2147483648或-2147483648,一旦int最大边界值加上任何一个整型值,便会变成-2147483648
这在我的判断条件中导致代码崩溃出错,该问题出在7-3的第二题上。
3.关于编写新的类时,私有变量一定要规范地写好,要不然会判定错误.比如我在写7-3的第二题时写了新的私有变量x并让输入进去的年份等于x。而在同一个类中另一个构造的方法isLeapYear(int year)中的year,我没让year=x,但我又用到了x,这导致编译器根本不知道我在调用哪一个。
刚开始的测试通过了,因为Java有自备的维护功能,就通过了测试点,但是一旦数据一多,便无法识别,导致无法正确判断哪一年是闰年。
四.我的一些不成熟的改进建议.
我觉得我题目集03的7-4做的一点也不好,并没有完全按照PowerDesigner图来完成,就是一次7-2的copy,我正在课后一点一点改进。
其次是代码还不够简洁,我可以在循环的判定里多加入一些条件,是的代码 更简单。
最后我觉得我的代码在判断条件上有重复,有冗杂,可以删改。
望与诸君共勉,共同进步。
五.谈谈自身.
也许是先学的c语言的问题,或者我本身的问题,有时候还是会陷入用c的格式,或者c的链表,c的函数来解决问题的脑回路,有时候转不过来脑子。同时,我感觉线上线下混合教学对于我来说,缺乏了鞭策的能力,我很难保质保量的完成学习透彻各个章节。同时有些不适应段老师的讲课方式,这种混合式的恐吓及压迫令我想到了高中面对高考的压迫感。我学到的东西很少,大部分还不是课上学到的,大部分都是i通过csdn的查找,或者b站的教学。我认为我的类与对象这部分急需研究透彻,我并不能分得清这么多构造函数的运作方式,与类方法之间的调用。同时懵懵懂懂大半个学期,现在有事疫情,总有些行业压力提前落在了肩头。难受哇。我认为线上讲的东西,其实老师也可以提一些重点,并不是老师说的线上讲了的,我是不会再讲的。