饼干妹妹的第一次博客作业
一、三次作业的知识点、题量、难度
考察的知识点
第一次作业的主要知识点是考察有关Java的输入输出还有对一些基本语法(例如选择,循环等等)的应用;
第二次作业的主要知识点是考察对字符串的操作,写一些简单算法以及使用Java解决一些简单的问题应用;
第三次作业的主要知识点是考察面向对象的思想,利用面向对象的方法解决问题,拆分问题,使问题简化。
作业的题量与难度
题的难度和作业的题量分配的很合理,非常适合有编程基础且正在学习Java的同学。(太简单了没有一点难度,题还少根本不够做)
二、设计与分析
第一次作业:
7-8:
1 import java.util.Scanner; 2 public class Main { 3 public static boolean sgn(double x,double y) { 4 double eps=1e-7; 5 if(Math.abs(x-y)<=eps) return true; 6 else return false; 7 } 8 public static void main(String[] args) { 9 Scanner input = new Scanner(System.in); 10 double a = input.nextDouble(); 11 double b = input.nextDouble(); 12 double c = input.nextDouble(); 13 14 while(true) { 15 16 if( a < 1 || a > 200 || b < 1 || b > 200 || c < 1 || c > 200 ) { 17 System.out.println("Wrong Format"); 18 break; 19 } 20 21 if( (a + b) <= c || (a + c) <= b || (b + c) <= a ) { 22 System.out.println("Not a triangle"); 23 break; 24 } 25 26 if( a == b && b == c ) { 27 System.out.println("Equilateral triangle"); 28 break; 29 } 30 31 if( (a == b && sgn( (Math.pow(a, 2) + Math.pow(b, 2)) , Math.pow(c, 2) )) || (a == c && sgn( (Math.pow(a, 2) + Math.pow(c, 2)) , Math.pow(b, 2) )) || (b == c && sgn( (Math.pow(b, 2) + Math.pow(c, 2)) , Math.pow(a, 2) )) ){ 32 System.out.println("Isosceles right-angled triangle"); 33 break; 34 } 35 36 if( a == b || b == c || a == c ) { 37 System.out.println("Isosceles triangle"); 38 break; 39 } 40 41 if( sgn( (Math.pow(a, 2) + Math.pow(b, 2)) , Math.pow(c, 2) ) || sgn( (Math.pow(a, 2) + Math.pow(c, 2)) , Math.pow(b, 2) ) || sgn( (Math.pow(b, 2) + Math.pow(c, 2)) , Math.pow(a, 2) ) ) { 42 System.out.println("Right-angled triangle"); 43 break; 44 } 45 46 System.out.println("General triangle"); 47 break; 48 49 } 50 } 51 52 }

第一次作业的7-8比较简单,没有复杂的结构设计,只是单纯的进行选择和判断,很容易就可以做出来,但是由于过多进行选择使得代码的复杂性提高了。
第二次作业:
7-4:
1 import java.util.Scanner; 2 public class Main { 3 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 int year = input.nextInt(); 7 int month = input.nextInt(); 8 int day = input.nextInt(); 9 while(true) { 10 if( (year > 2020 || year <1820) || (month > 12 || month < 1) || (day > 31 || day < 1) ) { 11 System.out.print("Wrong Format"); 12 break; 13 } 14 15 if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) { 16 if(month == 2 && day > 29) { 17 System.out.print("Wrong Format"); 18 break; 19 } 20 if( (month == 4 || month == 6 || month == 9 || month == 11) && day > 30) { 21 System.out.print("Wrong Format"); 22 break; 23 } 24 }//判断闰年输入 25 26 if(year % 400 != 0 || year % 4 != 0 || year % 100 == 0) { 27 if(month == 2 && day > 28) { 28 System.out.print("Wrong Format"); 29 break; 30 } 31 32 if( (month == 4 || month == 6 || month == 9 || month == 11) && day > 30) { 33 System.out.print("Wrong Format"); 34 break; 35 } 36 }//判断平年输入 37 38 if( (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && day + 1 <= 31) { 39 day = day + 1; 40 System.out.print("Next date is:" + year + "-" + month + "-" + day); 41 break; 42 } 43 else if( (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10) && day + 1 > 31) { 44 month++; 45 day = 1; 46 System.out.print("Next date is:" + year + "-" + month + "-" + day); 47 break; 48 } 49 else if(month == 12 && day + 1 > 31) { 50 year++; 51 month = 1; 52 day = 1; 53 System.out.print("Next date is:" + year + "-" + month + "-" + day); 54 break; 55 } 56 else if( (month == 4 || month == 6 || month == 9 || month == 11) && day + 1 <= 30) { 57 day = day + 1; 58 System.out.print("Next date is:" + year + "-" + month + "-" + day); 59 break; 60 } 61 else if( (month == 4 || month == 6 || month == 9 || month == 11) && day + 1 > 30) { 62 month++; 63 day = 1; 64 System.out.print("Next date is:" + year + "-" + month + "-" + day); 65 break; 66 } 67 else if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) { 68 if(month == 2 && day + 1 > 29) { 69 month++; 70 day = 1; 71 System.out.print("Next date is:" + year + "-" + month + "-" + day); 72 break; 73 } 74 else if(month == 2 && day + 1 <= 29) { 75 day = day + 1; 76 System.out.print("Next date is:" + year + "-" + month + "-" + day); 77 break; 78 } 79 } 80 else if(year % 400 != 0 || year % 4 != 0 || year % 100 == 0) { 81 if(month == 2 && day + 1 > 28) { 82 month++; 83 day = 1; 84 System.out.print("Next date is:" + year + "-" + month + "-" + day); 85 break; 86 } 87 else if(month == 2 && day + 1 < 28) { 88 day = day + 1; 89 System.out.print("Next date is:" + year + "-" + month + "-" + day); 90 break; 91 } 92 } 93 } 94 95 } 96 }

第二次作业的7-4相较于第一次作业的难度有提升,但是整体设计结构并没有变难,仍然是对条件进行选择判断。由于当时没有去学习使用面向对象的方法,使得这次作业的复杂度较高,可读性较差,代码仍有较大的改进空间。
7-5:
1 import java.util.Scanner; 2 public class Main { 3 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 int year = input.nextInt(); 7 int month = input.nextInt(); 8 int day = input.nextInt(); 9 int n = input.nextInt(); 10 while(true) { 11 if( (year > 2020 || year <1820) || (month > 12 || month < 1) || (day > 31 || day < 1) || (n > 10 || n < -10) ) { 12 System.out.print("Wrong Format"); 13 break; 14 } 15 16 if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) { 17 if(month == 2 && day > 29) { 18 System.out.print("Wrong Format"); 19 break; 20 } 21 if( (month == 4 || month == 6 || month == 9 || month == 11) && day > 30) { 22 System.out.print("Wrong Format"); 23 break; 24 } 25 }//判断闰年输入 26 27 if(year % 400 != 0 || year % 4 != 0 || year % 100 == 0) { 28 if(month == 2 && day > 28) { 29 System.out.print("Wrong Format"); 30 break; 31 } 32 33 if( (month == 4 || month == 6 || month == 9 || month == 11) && day > 30) { 34 System.out.print("Wrong Format"); 35 break; 36 } 37 }//判断平年输入 38 39 if(n < 0) { 40 if( (month == 4 || month == 6 || month == 9 || month == 11) && day + -n > 30) { 41 month++; 42 day = -n + day - 30; 43 System.out.print(n + " days ago is:" + year + "-" + month + "-" + day); 44 break; 45 } 46 else if( (month == 4 || month == 6 || month == 9 || month == 11) && day + -n <= 30) { 47 day = -n + day; 48 System.out.print(n + " days ago is:" + year + "-" + month + "-" + day); 49 break; 50 } 51 else if(month == 12 && day + -n > 31) { 52 year++; 53 month = 1; 54 day = -n + day - 31; 55 System.out.print(n + " days ago is:" + year + "-" + month + "-" + day); 56 break; 57 } 58 else if( (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10) && day + -n > 31) { 59 month++; 60 day = -n + day - 31; 61 System.out.print(n + " days ago is:" + year + "-" + month + "-" + day); 62 break; 63 } 64 else if( (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && day + -n <= 31){ 65 day = -n + day; 66 System.out.print(n + " days ago is:" + year + "-" + month + "-" + day); 67 break; 68 } 69 else if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) { 70 if(month == 2 && -n + day > 29) { 71 month++; 72 day = -n + day - 29; 73 System.out.print(n + " days ago is:" + year + "-" + month + "-" + day); 74 break; 75 } 76 else if(month == 2 && -n + day <= 29) { 77 day = -n + day; 78 System.out.print(n + " days ago is:" + year + "-" + month + "-" + day); 79 break; 80 } 81 } 82 else if(year % 400 != 0 || year % 4 != 0 || year % 100 == 0) { 83 if(month == 2 && -n + day > 28) { 84 month++; 85 day = -n + day - 28; 86 System.out.print(n + " days ago is:" + year + "-" + month + "-" + day); 87 break; 88 } 89 else if(month == 2 && -n + day <= 28) { 90 day = -n + day; 91 System.out.print(n + " days ago is:" + year + "-" + month + "-" + day); 92 break; 93 } 94 } 95 } 96 97 else if(n > 0) { 98 if( (month == 2 || month == 4 || month == 6 || month == 8 || month == 9 || month == 11) && day + -n < 1) { 99 month--; 100 day = -n + day + 31; 101 System.out.print(n + " days ago is:" + year + "-" + month + "-" + day); 102 break; 103 } 104 else if(month == 1 && day + -n < 1) { 105 year--; 106 month = 12; 107 day = -n + day + 31; 108 System.out.print(n + " days ago is:" + year + "-" + month + "-" + day); 109 break; 110 } 111 else if( (month == 5 || month == 7 || month == 10 || month == 12) && day + -n < 1) { 112 month--; 113 day = -n + day + 30; 114 System.out.print(n + " days ago is:" + year + "-" + month + "-" + day); 115 break; 116 } 117 else if( (month == 1 || month == 2 || month == 4 || month == 6 || month == 8 || month == 9 || month == 11 || month == 5 || month == 7 || month == 10 || month == 12) && day + -n >= 1){ 118 day = -n + day; 119 System.out.print(n + " days ago is:" + year + "-" + month + "-" + day); 120 break; 121 } 122 else if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) { 123 if(month == 3 && -n + day < 1) { 124 month--; 125 day = -n + day + 29; 126 System.out.print(n + " days ago is:" + year + "-" + month + "-" + day); 127 break; 128 } 129 else if(month == 3 && -n + day >= 1) { 130 day = -n + day; 131 System.out.print(n + " days ago is:" + year + "-" + month + "-" + day); 132 break; 133 } 134 } 135 else if(year % 400 != 0 || year % 4 != 0 || year % 100 == 0) { 136 if(month == 3 && -n + day < 1) { 137 month--; 138 day = -n + day + 28; 139 System.out.print(n + " days ago is:" + year + "-" + month + "-" + day); 140 break; 141 } 142 else if(month == 3 && -n + day >= 1) { 143 day = -n + day; 144 System.out.print(n + " days ago is:" + year + "-" + month + "-" + day); 145 break; 146 } 147 } 148 } 149 else if(n == 0) { 150 System.out.print(n + " days ago is:" + year + "-" + month + "-" + day); 151 break; 152 } 153 } 154 } 155 }

7-5的作业内容是在7-4的基础上增加了题目的要求,但其实内容变化也不大,需要增加题目的错误判断范围,其他的没有什么变化。代码复杂度仍然高。
第三次作业:
7-2:
1 import java.util.Scanner; 2 class Date{ 3 4 private int year; 5 private int month; 6 private int day; 7 private int[] mon = {0,31,28,31,30,31,30,31,31,30,31,30,31}; 8 9 public Date() { 10 this.year = 1900; 11 this.month = 1; 12 this.day = 1; 13 } 14 15 public Date(int year,int month,int day){ 16 this.year = year; 17 this.month = month; 18 this.day = day; 19 } 20 21 public int getYear() { 22 return this.year; 23 } 24 25 public void setYear(int year) { 26 this.year = year; 27 } 28 29 public int getMonth() { 30 return this.month; 31 } 32 33 public void setMonth(int month) { 34 this.month = month; 35 } 36 37 public int getDay() { 38 return this.day; 39 } 40 41 public void setDay(int day) { 42 this.day = day; 43 } 44 45 public boolean isLeapYear(int year) { 46 if(this.year % 400 == 0 || (this.year % 4 == 0 && this.year % 100 != 0)) 47 return true; 48 else 49 return false; 50 }//判断year是否为闰年,返回boolean类型; 51 52 public boolean checkinputValidity() { 53 if( (this.year > 2000 || this.year < 1900) || (this.month > 12 || this.month < 1) || (this.day > 31 || this.day < 1) ) { 54 return false; 55 }//判断年月日越界 56 57 if( isLeapYear(this.year) ) { 58 if(this.month == 2 && this.day > 29) { 59 return false; 60 } 61 if( (this.month == 4 || this.month == 6 || this.month == 9 || this.month == 11) && this.day > 30) { 62 return false; 63 } 64 }//判断闰年输入 65 66 if( !isLeapYear(this.year) ) { 67 if(this.month == 2 && this.day > 28) { 68 return false; 69 } 70 71 if( (this.month == 4 || this.month == 6 || this.month == 9 || this.month == 11) && this.day > 30) { 72 return false; 73 } 74 }//判断平年输入 75 return true; 76 } 77 78 public void getNextDate() { 79 while(true) { 80 81 if( (this.month == 1 || this.month == 3 || this.month == 5 || this.month == 7 || this.month == 8 || this.month == 10 || this.month == 12) && this.day + 1 <= 31) { 82 this.day = this.day + 1; 83 System.out.print("Next day is:" + this.year + "-" + this.month + "-" + this.day); 84 break; 85 } 86 else if( (this.month == 1 || this.month == 3 || this.month == 5 || this.month == 7 || this.month == 8 || this.month == 10) && this.day + 1 > 31) { 87 this.month++; 88 this.day = 1; 89 System.out.print("Next day is:" + this.year + "-" + this.month + "-" + this.day); 90 break; 91 } 92 else if(this.month == 12 && this.day + 1 > 31) { 93 this.year++; 94 this.month = 1; 95 this.day = 1; 96 System.out.print("Next day is:" + this.year + "-" + this.month + "-" + this.day); 97 break; 98 } 99 else if( (this.month == 4 || this.month == 6 || this.month == 9 || this.month == 11) && this.day + 1 <= 30) { 100 this.day = this.day + 1; 101 System.out.print("Next day is:" + this.year + "-" + this.month + "-" + this.day); 102 break; 103 } 104 else if( (this.month == 4 || this.month == 6 || this.month == 9 || this.month == 11) && this.day + 1 > 30) { 105 this.month++; 106 this.day = 1; 107 System.out.print("Next day is:" + this.year + "-" + this.month + "-" + this.day); 108 break; 109 } 110 else if( isLeapYear(this.year) ) { 111 if(this.month == 2 && this.day + 1 > 29) { 112 this.month++; 113 this.day = 1; 114 System.out.print("Next day is:" + this.year + "-" + this.month + "-" + this.day); 115 break; 116 } 117 else if(this.month == 2 && this.day + 1 <= 29) { 118 this.day = this.day + 1; 119 System.out.print("Next day is:" + this.year + "-" + this.month + "-" + this.day); 120 break; 121 } 122 } 123 else if( !isLeapYear(this.year) ) { 124 if(this.month == 2 && this.day + 1 > 28) { 125 this.month++; 126 this.day = 1; 127 System.out.print("Next day is:" + this.year + "-" + this.month + "-" + this.day); 128 break; 129 } 130 else if(this.month == 2 && this.day + 1 < 28) { 131 this.day = this.day + 1; 132 System.out.print("Next day is:" + this.year + "-" + this.month + "-" + this.day); 133 break; 134 } 135 } 136 } 137 } 138 } 139 140 public class Main { 141 142 public static void main(String[] args) { 143 Scanner input = new Scanner(System.in); 144 Date l = new Date(); 145 l.setYear(input.nextInt()); 146 l.setMonth(input.nextInt()); 147 l.setDay(input.nextInt()); 148 if(!l.checkinputValidity()) { 149 System.out.println("Date Format is Wrong"); 150 return; 151 } 152 l.getNextDate(); 153 } 154 155 }

第三次作业的要求与第二次作业7-4一样,值得注意的是这次作业的非法输入范围与第二次作业7-4不同,且这次作业采用面向对象的方法来写,需要我们定义一个Date类来完成判断非法输入、输出下一天等一系列操作。由于采用了面向对象的方法编写,使得代码的复杂性大大降低,可读性高。
7-3:
1 import java.math.BigInteger; 2 import java.util.ArrayList; 3 import java.util.Scanner; 4 import java.util.regex.Matcher; 5 import java.util.regex.Pattern; 6 public class Main { 7 public static void main(String[] args) { 8 Scanner input = new Scanner(System.in); 9 String initExpress = input.nextLine(); 10 String express = initExpress.replaceAll("\\s", ""); 11 Express textExpress = new Express(express); 12 if (textExpress.check()) 13 textExpress.show(); 14 else 15 System.out.println("Wrong Format"); 16 } 17 } 18 class Express { 19 private String express; 20 private String term; 21 private String ratStr;// 系数部分 22 private String indexStr;// 指数部分 23 private BigInteger rat; 24 private BigInteger index; 25 private ArrayList<String> containTerm = new ArrayList<>(); 26 public Express(String express) { 27 this.express = express; 28 } 29 String termExpress = "([+-]?[1-9][0-9]*)?" + "(\\*?[+-]?x(\\^([+-]?[1-9][0-9]*))?)?"; 30 String wholeExpress = "(([+-]?[1-9][0-9]*)?" + "(\\*?[+-]?x(\\^([+-]?[1-9][0-9]*))?)?)+"; 31 String constNum = "[+-]?[0-9]+"; 32 public boolean check() { 33 return Pattern.matches(wholeExpress, express); 34 } 35 public void show() { 36 if (Pattern.matches(constNum, express)) { 37 System.out.println("0"); 38 System.exit(0); 39 } 40 Pattern p = Pattern.compile(termExpress); 41 Matcher m = p.matcher(express); 42 int flag = 0; 43 while (m.find()) { 44 //System.out.println(m.group(2)); 45 ratStr = m.group(1); 46 indexStr = m.group(4); 47 if (ratStr != null) { 48 rat = new BigInteger(ratStr); 49 if (m.group(2) != null && m.group(2).startsWith("-")) { 50 rat = BigInteger.valueOf(-1); 51 //System.out.println(rat); 52 } 53 else if (m.group(2) != null && m.group(2).startsWith("+")) { 54 rat = BigInteger.valueOf(1); 55 //System.out.println(rat); 56 } 57 } else { 58 rat = BigInteger.valueOf(1); 59 if (m.group() != null && m.group().startsWith("-")) { 60 rat = BigInteger.valueOf(-1); 61 //System.out.println(rat); 62 } 63 else if (m.group() != null && m.group().startsWith("+")) { 64 rat = BigInteger.valueOf(1); 65 //System.out.println(rat); 66 } 67 } 68 if (indexStr != null) { 69 index = new BigInteger(indexStr); 70 rat = rat.multiply(index); 71 index = index.subtract(new BigInteger("1")); 72 if (rat.compareTo(new BigInteger("0")) > 0 && flag == 1) 73 System.out.print("+" + (rat.equals(new BigInteger("-1")) ? "-x" : rat + "*x") + (index.equals(new BigInteger("1")) ? "" : ("^" + index))); 74 else { 75 flag = 1; 76 System.out.print((rat.equals(new BigInteger("-1")) ? "-x" : rat + "*x") + (index.equals(new BigInteger("1")) ? "" : ("^" + index))); 77 } 78 } else if (m.group(2) != null) { 79 if(flag == 1 && rat.compareTo(new BigInteger("0")) > 0) 80 System.out.print(rat); 81 else{ 82 flag = 1; 83 System.out.print(rat); 84 } 85 } 86 } 87 } 88 }

7-3这一题有难度,我个人认为难在非法输入的判断下,并不是很好做,需要对指数和系数还有一系列符号进行输入判断,需要大量分析,代码复杂性也较高。
三、踩坑心得(坑我心得)
作业一7-8:
在判断直角三角形的这一步中出现了报错。
1 if( sgn( (Math.pow(a, 2) + Math.pow(b, 2)) , Math.pow(c, 2) ) || sgn( (Math.pow(a, 2) + Math.pow(c, 2)) , Math.pow(b, 2) ) || sgn( (Math.pow(b, 2) + Math.pow(c, 2)) , Math.pow(a, 2) ) ) { 2 System.out.println("Right-angled triangle"); 3 break; 4 }
初次写的时候在PTA上提交出现报错,经过排查后发现是浮点数进行运算后有误差,导致条件判断出现差错而报错,于是写了一个sgn()方法来判断误差。
1 public static boolean sgn(double x,double y) { 2 double eps=1e-7; 3 if(Math.abs(x-y)<=eps) return true; 4 else return false; 5 }
这样就快速(艰难)的解决了这个问题。
作业二7-1:
一开始输入IP地址的时候我用的是long类型输入,32位数据太大出现了越界导致报错,后采用字符串输入解决了这个问题。
1 String s = input.nextLine(); 2 while(true) { 3 if(s.length() != 32) { 4 System.out.print("Wrong Format"); 5 break; 6 }
其他的题目写起来没有什么大问题,要说有,就是有时候输入格式不对找半天才找到。(PTA上Java的编译器只报“答案错误”不报“格式错误”,真让人摸不着头脑(ˉ▽ˉ;)...)
四、改进建议
作业二的7-3、7-4、7-5代码复杂度可以再降低,长度也可以再降低,有较大的优化空间。
五、总结
写了三次题目集,感觉题目前几次还没有特别大的困难,与之前学习的C语言没有特别多的差别,只是语法上有一些不同。但是学习到了面向对象的方法,体会到再真正面向客户的要求时,不只是单纯为了完成要求,更要从多个方面去思考你的代码还有哪些可以改进的地方,比如代码的通用性,不会因为面对不同的对象而导致代码不适用,还有在增加和删减一些需求的时候,不用大幅度的删改代码,只需要在相应的功能区域修改相应的功能便可。
到现在的学习还是不够,对Java的理解还是不够透彻,多操作多实践多提问才能更好学习和研究。

浙公网安备 33010602011771号