Blog作业1
黄杨涛的第一个月java学习:
1.前言:
经过了一个月对Java编程语言的学习,应该算是正式入门了。接下来就对这一个月的学习成果做一个总结吧!
PTA01:总共九道题目,整体难度中等,题量偏多。还是用了c语言的编程习惯,一个main解决所有问题。。。完成这次作业之后,至少能够知道Scanner类的用法,以及大部分基础语法和c语言是类似的。
PTA02:总共三道题目,主要考察的是对字符串的截取处理。对String类的一些常用方法有了了解。整体难度比第一次有所提升,题量适中。
PTA03:总共四道题目,第一题是没难度的。后三题是同一个题的三种不同写法。在老师特定框架下,解决题目。题目难度明显提升,不过只要第一次逻辑没问题,后面也没有太大问题。应该是带我们进入从面向对象的世界吧。不得不佩服老师循循然善诱人,一步一步带我们感受。题量适中。
2.设计与分析:
PTA01_1-PTA01_9:(由于有些题目过于简单,这里省略,只放有心得的代码)
PTA01_9:
1 import java.util.Scanner; 2 public class Main { 3 public static void main(String[] args) { 4 Scanner in = new Scanner(System.in); 5 double a=in.nextDouble(); 6 double b=in.nextDouble(); 7 double c=in.nextDouble(); 8 if(a >= 1 && a <= 200 && b >= 1 && b <= 200 && c>=1 &&c<=200){ 9 if(a+b>c&&a+c>b&&b+c>a){ 10 if(a==b&&b==c) 11 System.out.println("Equilateral triangle"); 12 else { 13 if(a == b || b == c || a == c){ 14 if((a*a+b*b-c*c<0.1)||(a*a+c*c-b*b<0.1)||(b*b+c*c-a*a<0.1))//不要用a*a+b*b==c*c 15 System.out.println("Isosceles right-angled triangle"); 16 else 17 System.out.println("Isosceles triangle"); 18 } 19 else{ 20 if((a*a+b*b-c*c<0.1)||(a*a+c*c-b*b<0.1)||(b*b+c*c-a*a<0.1)) 21 System.out.println("Right-angled triangle"); 22 else 23 System.out.println("General triangle"); 24 } 25 } 26 } 27 else 28 System.out.println("Not a triangle"); 29 } 30 else 31 System.out.println("Wrong Format"); 32 } 33 }
小结:本题考查对字符串的截取和有效信息的截取;在本次作业中对一些String类的方法有了更深刻的理解。
检索下标为i的字符str.charAt(int i);
检索字符串长度str.length();
截取字符串:str.substing(int i, int j);返回一个String对象,括号左开右闭。
奇校验偶校验:奇偶校验码就是在信息码后面加一位校验码,添加一位校验码后,使得整个码字里面1的个数是奇数(偶数)。接收端收到数据后就校验数据里1的个数,若检测到奇数(偶数)个1,则认为传 输没有出错;
本题思路也是并未用面向对象的思维解决,所以没有类图。该题题目难度中等,众多的分支结构比较考验思维逻辑。
PTA03_2 - PTA03-4:
02:
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 85 86 class DateUtil { 87 private int year,month,day; 88 89 public DateUtil() { 90 super(); 91 } 92 93 public DateUtil(int year, int month, int day) { 94 super(); 95 this.year = year; 96 this.month = month; 97 this.day = day; 98 } 99 100 public int getDay() { 101 return day; 102 } 103 104 public int getYear() { 105 return year; 106 } 107 108 public int getMonth() { 109 return month; 110 } 111 112 public boolean isLeapYear(int year){ 113 //判断year是否为闰年 114 if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){ 115 return true; 116 }else 117 return false; 118 } 119 120 public boolean checkInputValidity(){ 121 //检测输入的年、月、日是否合法 122 if(year >= 1820 && year <= 2020){ 123 if((isLeapYear(year) && month ==2 &&day > 0 && day <= 29)||(!isLeapYear(year) && month ==2 && day > 0 && day <= 28)||((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)&&day<=31&&day>0)||((month == 4 || month == 6 || month == 9 || month == 11 )&&day<=30&&day>0)){ 124 return true; 125 }else 126 return false; 127 }else 128 return false; 129 } 130 131 public DateUtil getNextNDays(int n){ 132 //取得year-month-day的下n天日期 133 DateUtil date_next = new DateUtil(); 134 date_next.day = day; 135 date_next.month = month; 136 date_next.year = year; 137 int i = n; 138 for(; i >= 365 ; date_next.year += 1) { 139 if((isLeapYear(date_next.year) && date_next.month < 3 )||(!isLeapYear(date_next.year) && isLeapYear(date_next.year+1) && date_next.month > 2)) 140 i-=366; 141 else i -= 365; 142 } 143 for(;i > 0;i--){ 144 if(date_next.month == 12 && date_next.day == 31) { 145 date_next.year ++; 146 date_next.month = 1; 147 date_next.day = 1; 148 }else if((date_next.month == 4 || date_next.month == 6 || date_next.month == 9 || date_next.month == 11 )&& date_next.day == 30){ 149 date_next.day = 1; 150 date_next.month ++; 151 }else if((date_next.month == 1 || date_next.month == 3 || date_next.month == 5 || date_next.month == 7 || date_next.month == 8 || date_next.month == 10 ) && date_next.day == 31) { 152 date_next.day = 1; 153 date_next.month ++; 154 }else if(date_next.month == 2) { 155 if((isLeapYear(date_next.year) && date_next.day == 29) || (!isLeapYear(date_next.year) && date_next.day == 28)){ 156 date_next.day = 1; 157 date_next.month = 3; 158 }else 159 date_next.day++; 160 }else 161 date_next.day++; 162 } 163 return date_next; 164 } 165 public DateUtil getPreviousNDays(int n){ 166 //取得year-month-day的前n天日期 167 DateUtil date = new DateUtil(); 168 date.day = day; 169 date.month = month; 170 date.year = year; 171 int i = n; 172 for(; i >= 365 ; date.year -= 1) { 173 if((isLeapYear(date.year) && date.month > 2)||(!isLeapYear(date.year) && isLeapYear(date.year - 1) && date.month < 3)) 174 i-=366; 175 else i -= 365; 176 } 177 for(;i > 0;i--){ 178 if(date.month == 1 && date.day == 1) { 179 date.year --; 180 date.month = 12; 181 date.day = 31; 182 }else if((date.month == 2 || date.month == 4 || date.month == 6 || date.month == 8 || date.month == 9 || date.month == 11) && date.day == 1){ 183 date.day = 31; 184 date.month --; 185 }else if((date.month == 5 || date.month == 7 || date.month == 10 || date.month == 12 ) && date.day == 1) { 186 date.day = 30; 187 date.month --; 188 }else if(date.month == 3 && date.day == 1) { 189 if(isLeapYear(date.year)){ 190 date.day = 29; 191 date.month = 2; 192 }else{ 193 date.day = 28; 194 date.month = 2; 195 } 196 }else 197 date.day--; 198 } 199 return date; 200 } 201 202 public boolean compareDates(DateUtil date) {//前先于后返回true 203 if(date.year > year) 204 return true; 205 else if(date.year < year) 206 return false; 207 else { 208 if(date.month > month) 209 return true; 210 else if(date.month < month) 211 return false; 212 else { 213 if(date.day > day) 214 return true; 215 else 216 return false; 217 } 218 } 219 } 220 221 public boolean equalTwoDates(DateUtil date){ 222 //判断两个日期是否相等 223 if(date.year == year && date.month == month && date.day == day) 224 return true; 225 else return false; 226 } 227 228 public int getDaysofDates(DateUtil date){ 229 //求当前日期与date之间相差的天数 230 int numOfdays = 0; 231 if(!equalTwoDates(date)){ 232 while(date.year > year + 1) { 233 if(isLeapYear(date.year) && date.month > 2 ) 234 numOfdays += 366; 235 else numOfdays += 365; 236 date.year -= 1; 237 } 238 for(int i = 1;; i++) { 239 if(this.getNextNDays(i).equalTwoDates(date)){ 240 numOfdays++; 241 break; 242 } 243 numOfdays++; 244 } 245 } 246 return numOfdays; 247 } 248 249 public String showDate(){ 250 //以“year-month-day”格式返回日期值 251 String date; 252 date = String.valueOf(year) + "-" + String.valueOf(month) + "-" + String.valueOf(day); 253 return date; 254 } 255 256 }
总结:本体主函数已经写好,相当于需要我们补充一个类DateUtil,下面是类图,这种写法类似于c语言,思维简单,但是不是很合理。
03:
1 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 7 Scanner input = new Scanner(System.in); 8 9 int choice = input.nextInt(); 10 11 int year = 0; 12 int month = 0; 13 int day = 0; 14 15 if (choice == 1) { // test getNextNDays method 16 int m = 0; 17 year = Integer.parseInt(input.next()); 18 month = Integer.parseInt(input.next()); 19 day = Integer.parseInt(input.next()); 20 21 DateUtil date = new DateUtil(year, month, day); 22 23 if (!date.checkInputValidity()) { 24 System.out.println("Wrong Format"); 25 System.exit(0); 26 } 27 28 m = input.nextInt(); 29 30 if (m < 0) { 31 System.out.println("Wrong Format"); 32 System.exit(0); 33 } 34 35 // System.out.print(date.getDay().getMonth().getYear().getValue() + "-" + date.getDay().getMonth().getValue() + "-" + date.getDay().getValue() + " next " + m + " days is:"); 36 System.out.println(date.getNextNDays(m).showDate()); 37 }else if (choice == 2) { // test getPreviousNDays method 38 int n = 0; 39 year = Integer.parseInt(input.next()); 40 month = Integer.parseInt(input.next()); 41 day = Integer.parseInt(input.next()); 42 43 DateUtil date = new DateUtil(year, month, day); 44 45 if (!date.checkInputValidity()) { 46 System.out.println("Wrong Format"); 47 System.exit(0); 48 } 49 50 n = input.nextInt(); 51 52 if (n < 0) { 53 System.out.println("Wrong Format"); 54 System.exit(0); 55 } 56 // System.out.print(date.getDay().getMonth().getYear().getValue() + "-" + date.getDay().getMonth().getValue() + "-" + date.getDay().getValue() + " previous " + n + " days is:"); 57 System.out.println(date.getPreviousNDays(n).showDate()); 58 }else if (choice == 3) { //test getDaysofDates method 59 year = Integer.parseInt(input.next()); 60 month = Integer.parseInt(input.next()); 61 day = Integer.parseInt(input.next()); 62 63 int anotherYear = Integer.parseInt(input.next()); 64 int anotherMonth = Integer.parseInt(input.next()); 65 int anotherDay = Integer.parseInt(input.next()); 66 67 DateUtil fromDate = new DateUtil(year, month, day); 68 DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay); 69 70 if (fromDate.checkInputValidity() && toDate.checkInputValidity()) { 71 // System.out.print("The days between " + fromDate.showDate() + 72 // " and " + toDate.showDate() + " are:"); 73 System.out.println(fromDate.getDaysofDates(toDate)); 74 } else { 75 System.out.println("Wrong Format"); 76 System.exit(0); 77 } 78 } 79 else{ 80 System.out.println("Wrong Format"); 81 System.exit(0); 82 } 83 84 input.close(); 85 } 86 } 87 88 class DateUtil { 89 90 private Day day = new Day(); 91 92 public DateUtil(int y, int m, int d) { 93 super(); 94 day.setValue(d); 95 day.getMonth().setValue(m); 96 day.getMonth().getYear().setValue(y); 97 } 98 99 public DateUtil() { 100 super(); 101 } 102 103 public Day getDay() { 104 return day; 105 } 106 107 public void setDay(Day d){ 108 this.day = d; 109 } 110 111 public boolean checkInputValidity(){//校验数据合法性 112 if(day.validate() && day.getMonth().validate() && day.getMonth().getYear().validate()) 113 return true; 114 else return false; 115 } 116 117 public boolean compareDates(DateUtil date){//前先于后返回true 118 if(date.getDay().getMonth().getYear().getValue() > this.getDay().getMonth().getYear().getValue()) 119 return true; 120 else if(date.getDay().getMonth().getYear().getValue() < this.getDay().getMonth().getYear().getValue()) 121 return false; 122 else { 123 if(date.getDay().getMonth().getValue() > day.getMonth().getValue()) 124 return true; 125 else if(date.getDay().getMonth().getValue() < day.getMonth().getValue()) 126 return false; 127 else { 128 if(date.day.getValue() > day.getValue()) 129 return true; 130 else 131 return false; 132 } 133 } 134 135 } 136 137 public boolean equalTwoDates(DateUtil date) {//判定两日期是否相等 138 if(day.getValue() == date.getDay().getValue() 139 && day.getMonth().getValue() == date.getDay().getMonth().getValue() 140 && day.getMonth().getYear().getValue() == date.getDay().getMonth().getYear().getValue()) 141 return true; 142 else return false; 143 } 144 145 public String showDate(){//日期格式化 146 String date; 147 date = String.valueOf(day.getMonth().getYear().getValue()) + "-" + String.valueOf(day.getMonth().getValue()) + "-" + String.valueOf(day.getValue()); 148 return date; 149 } 150 151 public DateUtil getNextNDays(int n){//求下n天 152 DateUtil date_next = new DateUtil(); 153 date_next.day.setValue(this.day.getValue()); 154 date_next.day.getMonth().setValue(this.day.getMonth().getValue()); 155 date_next.day.getMonth().getYear().setValue(this.day.getMonth().getYear().getValue()); 156 int i = n; 157 for(; i >= 365 ; date_next.day.getMonth().getYear().yearIncrement()) { 158 if((date_next.day.getMonth().getYear().isLeapYear() 159 && date_next.day.getMonth().getValue() < 3 ) 160 ||(!date_next.day.getMonth().getYear().isLeapYear() 161 && date_next.day.getMonth().getYear().isLeapYear(date_next.day.getMonth().getYear().getValue()+1) 162 && date_next.day.getMonth().getValue() > 2)) 163 i-=366; 164 else i -= 365; 165 } 166 for(;i > 0;i--){ 167 if(date_next.day.getMonth().getValue() == 12 && date_next.day.getValue() == 31) { 168 date_next.day.getMonth().getYear().yearIncrement(); 169 date_next.day.getMonth().resetMin(); 170 date_next.day.resetMin(); 171 }else if(date_next.day.getMonth().getValue() == 2) { 172 if((date_next.day.getMonth().getYear().isLeapYear()&& date_next.day.getValue() == 29) 173 || (!date_next.day.getMonth().getYear().isLeapYear() && date_next.day.getValue() == 28)){ 174 date_next.day.resetMin(); 175 date_next.day.getMonth().setValue(3); 176 }else 177 date_next.day.dayIncrement(); 178 }else if(date_next.day.getValue() == date_next.day.mon_maxnum[date_next.day.getMonth().getValue()]){ 179 date_next.day.resetMin(); 180 date_next.day.getMonth().monthIcrement(); 181 }else 182 date_next.day.dayIncrement(); 183 } 184 return date_next; 185 } 186 187 public DateUtil getPreviousNDays(int n){//求前n天 188 DateUtil date_before = new DateUtil(); 189 date_before.day.setValue(this.day.getValue()); 190 date_before.day.getMonth().setValue(this.day.getMonth().getValue()); 191 date_before.day.getMonth().getYear().setValue(this.day.getMonth().getYear().getValue()); 192 int i = n; 193 for(; i >= 365 ; date_before.day.getMonth().getYear().yearReduction()) { 194 if((date_before.day.getMonth().getYear().isLeapYear() 195 && date_before.day.getMonth().getValue() > 2 ) 196 ||(!date_before.day.getMonth().getYear().isLeapYear() 197 && date_before.day.getMonth().getYear().isLeapYear(date_before.day.getMonth().getYear().getValue()-1) 198 && date_before.day.getMonth().getValue() < 3)) 199 i-=366; 200 else i -= 365; 201 } 202 for(;i > 0;i--){ 203 if(date_before.day.getMonth().getValue() == 1 && date_before.day.getValue() == 1) { 204 date_before.day.getMonth().getYear().yearReduction(); 205 date_before.day.getMonth().resetMax(); 206 date_before.day.resetMax(); 207 }else if(date_before.day.getMonth().getValue() == 3 && date_before.day.getValue() == 1) { 208 date_before.day.getMonth().setValue(2); 209 if(date_before.day.getMonth().getYear().isLeapYear()) 210 date_before.day.setValue(29); 211 else 212 date_before.day.setValue(28); 213 }else if(date_before.day.getValue() == 1){ 214 date_before.day.getMonth().monthReduction(); 215 date_before.day.resetMax(); 216 }else 217 date_before.day.dayReduction(); 218 } 219 return date_before; 220 } 221 222 public int getDaysofDates(DateUtil date) {//求两日期之间的天数 223 int numOfdays = 0; 224 DateUtil temp = new DateUtil(); 225 if(!this.compareDates(date)){ 226 temp.day.setValue(date.day.getValue()); 227 temp.day.getMonth().setValue(date.day.getMonth().getValue()); 228 temp.day.getMonth().getYear().setValue(date.day.getMonth().getYear().getValue()); 229 date.day.setValue(this.day.getValue()); 230 date.day.getMonth().setValue(this.day.getMonth().getValue()); 231 date.day.getMonth().getYear().setValue(this.day.getMonth().getYear().getValue()); 232 this.day.setValue(temp.day.getValue()); 233 this.day.getMonth().setValue(temp.day.getMonth().getValue()); 234 this.day.getMonth().getYear().setValue(temp.day.getMonth().getYear().getValue()); 235 236 } 237 for(;date.getDay().getMonth().getYear().getValue() > this.getDay().getMonth().getYear().getValue() + 1;) { 238 if(this.getDay().getMonth().getYear().isLeapYear() && this.getDay().getMonth().getValue() < 3 ) 239 numOfdays += 366; 240 else numOfdays += 365; 241 this.getDay().getMonth().getYear().yearIncrement(); 242 } 243 int i ; 244 for( i = 0;this.compareDates(date); i++){ 245 if(this.day.getMonth().getValue() == 12 && this.day.getValue() == 31) { 246 this.day.getMonth().getYear().yearIncrement(); 247 this.day.resetMin(); 248 this.day.getMonth().resetMin(); 249 }else if(this.day.getMonth().getValue() == 2) { 250 if((day.getMonth().getYear().isLeapYear()&& day.getValue() == 29) 251 || (!day.getMonth().getYear().isLeapYear() && day.getValue() == 28)){ 252 day.resetMin(); 253 day.getMonth().monthIcrement(); 254 }else 255 day.dayIncrement(); 256 }else if(this.day.getValue() == this.day.mon_maxnum[day.getMonth().getValue()]) { 257 this.day.resetMin(); 258 this.day.getMonth().monthIcrement(); 259 }else 260 this.day.dayIncrement(); 261 } 262 numOfdays += i; 263 return numOfdays; 264 } 265 } 266 class Day { 267 private int value; 268 private Month month = new Month(); 269 int[] mon_maxnum = {0,31,28,31,30,31,30,31,31,30,31,30,31}; 270 271 public Day() { 272 super(); 273 // TODO Auto-generated constructor stub 274 } 275 276 public Day(int yearValue, int dayValue, int monthValue) { 277 super(); 278 this.value = dayValue; 279 month.setValue(monthValue); 280 month.getYear().setValue(yearValue); 281 } 282 283 public int getValue() { 284 return value; 285 } 286 287 public void setValue(int value) { 288 this.value = value; 289 } 290 291 public Month getMonth() { 292 return month; 293 } 294 295 public void setMonth(Month month) { 296 this.month = month; 297 } 298 299 public void resetMin() { 300 this.value = 1; 301 } 302 303 public void resetMax() { 304 this.value = this.mon_maxnum[month.getValue()]; 305 } 306 307 public boolean validate() {//校验数据合法性 308 if(month.validate()) 309 if(value > 0 && value <= this.mon_maxnum[this.month.getValue()]) 310 return true; 311 else return false; 312 else return false; 313 } 314 315 public void dayIncrement() {//增一 316 value++; 317 } 318 319 public void dayReduction() {//减一 320 value--; 321 } 322 323 324 } 325 class Month { 326 private int value; 327 private Year year = new Year(); 328 329 public Month() { 330 super(); 331 // TODO Auto-generated constructor stub 332 } 333 334 public Month(int yearValue, int monthValue) { 335 super(); 336 year.setValue(yearValue); 337 this.value = monthValue; 338 } 339 340 public int getValue() { 341 return value; 342 } 343 344 public void setValue(int value) { 345 this.value = value; 346 } 347 348 public Year getYear() { 349 return year; 350 } 351 352 public void setYear(Year year) { 353 this.year = year; 354 } 355 356 public void resetMin() { 357 this.value = 1; 358 } 359 360 public void resetMax() {//月份设置为12 361 this.value = 12; 362 } 363 364 public boolean validate() {//校验数据合法性 365 if(value > 0 && value <= 12) 366 return true; 367 else return false; 368 } 369 370 public void monthIcrement() { 371 this.value++; 372 } 373 374 public void monthReduction() { 375 this.value--; 376 } 377 } 378 class Year { 379 private int value; 380 381 public Year() { 382 super(); 383 // TODO Auto-generated constructor stub 384 } 385 386 public Year(int value) { 387 super(); 388 this.value = value; 389 } 390 391 public int getValue() { 392 return value; 393 } 394 395 public void setValue(int value) { 396 this.value = value; 397 } 398 399 public boolean isLeapYear() {//判断是否润年 400 if((value % 4 == 0 && value % 100 != 0) || value % 400 == 0){ 401 return true; 402 }else 403 return false; 404 } 405 406 public boolean isLeapYear(int value) {//判断是否润年 407 if((value % 4 == 0 && value % 100 != 0) || value % 400 == 0){ 408 return true; 409 }else 410 return false; 411 } 412 413 public boolean validate() {//校验数据合法性 414 if(value >= 1900 && value <= 2050) { 415 return true; 416 } 417 else return false; 418 } 419 420 public void yearIncrement() {//年份增一 421 value++; 422 } 423 424 public void yearReduction() {//年份减一 425 value--; 426 } 427 }
类图:
04:
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.showDate() + " 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.showDate() + " 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 85 class DateUtil { 86 Year year = new Year(); 87 Month month = new Month(); 88 Day day = new Day(); 89 int [] mon_maxnum = {0,31,28,31,30,31,30,31,31,30,31,30,31}; 90 91 public DateUtil() { 92 super(); 93 // TODO Auto-generated constructor stub 94 } 95 96 public DateUtil(int y, int m, int d) { 97 super(); 98 year.setValue(y); 99 month.setValue(m); 100 day.setValue(d); 101 } 102 103 public Year getYear() { 104 return year; 105 } 106 107 public void setYear(Year year) { 108 this.year = year; 109 } 110 111 public Month getMonth() { 112 return month; 113 } 114 115 public void setMonth(Month month) { 116 this.month = month; 117 } 118 119 public Day getDay() { 120 return day; 121 } 122 123 public void setDay(Day day) { 124 this.day = day; 125 } 126 127 public void resetMin() { 128 this.day.setValue(1);; 129 } 130 131 public void resetMax() { 132 this.day.setValue(this.mon_maxnum[month.getValue()]); 133 } 134 135 public boolean checkInputValidity(){//校验数据合法性 136 if(year.validate() && month.validate()) 137 if(day.getValue() > 0 && day.getValue() <= this.mon_maxnum[this.month.getValue()]) 138 return true; 139 else return false; 140 else return false; 141 } 142 143 public boolean compareDates(DateUtil date){//前先于后返回true 144 if(date.year.getValue() > this.year.getValue()) 145 return true; 146 else if(date.year.getValue() < this.year.getValue()) 147 return false; 148 else { 149 if(date.getMonth().getValue() > this.getMonth().getValue()) 150 return true; 151 else if(date.getMonth().getValue() < this.getMonth().getValue()) 152 return false; 153 else { 154 if(date.day.getValue() > this.day.getValue()) 155 return true; 156 else 157 return false; 158 } 159 } 160 } 161 162 public String showDate(){//日期格式化 163 String date; 164 date = String.valueOf(this.getYear().getValue()) + "-" 165 + String.valueOf(this.getMonth().getValue()) + "-" 166 + String.valueOf(this.getDay().getValue()); 167 return date; 168 } 169 170 public boolean equalTwoDates(DateUtil date) {//判定两日期是否相等 171 if(day.getValue() == date.getDay().getValue() 172 && this.getMonth().getValue() == date.getMonth().getValue() 173 && this.getYear().getValue() == date.getYear().getValue()) 174 return true; 175 else return false; 176 } 177 178 public DateUtil getNextNDays(int n){//求下n天 179 DateUtil date_next = new DateUtil(); 180 date_next.day.setValue(this.day.getValue()); 181 date_next.getMonth().setValue(this.getMonth().getValue()); 182 date_next.getYear().setValue(this.getYear().getValue()); 183 int i = n; 184 for(; i >= 365 ; date_next.getYear().yearIncrement()) { 185 if((date_next.getYear().isLeapYear() 186 && date_next.getMonth().getValue() < 3 ) 187 ||(!date_next.getYear().isLeapYear() 188 && date_next.getYear().isLeapYear(date_next.getYear().getValue()+1) 189 && date_next.getMonth().getValue() > 2)) 190 i-=366; 191 else i -= 365; 192 } 193 for(;i > 0;i--){ 194 if(date_next.getMonth().getValue() == 12 && date_next.day.getValue() == 31) { 195 date_next.getYear().yearIncrement(); 196 date_next.getMonth().resetMin(); 197 date_next.resetMin(); 198 }else if(date_next.getMonth().getValue() == 2) { 199 if((date_next.getYear().isLeapYear()&& date_next.day.getValue() == 29) 200 || (!date_next.getYear().isLeapYear() && date_next.day.getValue() == 28)){ 201 date_next.resetMin(); 202 date_next.getMonth().setValue(3); 203 }else 204 date_next.day.dayIncrement(); 205 }else if(date_next.day.getValue() == date_next.mon_maxnum[date_next.getMonth().getValue()]){ 206 date_next.resetMin(); 207 date_next.getMonth().monthIcrement(); 208 }else 209 date_next.day.dayIncrement(); 210 } 211 return date_next; 212 } 213 214 public DateUtil getPreviousNDays(int n){//求前n天 215 DateUtil date_before = new DateUtil(); 216 date_before.day.setValue(this.day.getValue()); 217 date_before.getMonth().setValue(this.getMonth().getValue()); 218 date_before.getYear().setValue(this.getYear().getValue()); 219 int i = n; 220 for(; i >= 365 ; date_before.getYear().yearReduction()) { 221 if((date_before.getYear().isLeapYear() 222 && date_before.getMonth().getValue() > 2 ) 223 ||(!date_before.getYear().isLeapYear() 224 && date_before.getYear().isLeapYear(date_before.getYear().getValue()-1) 225 && date_before.getMonth().getValue() < 3)) 226 i-=366; 227 else i -= 365; 228 } 229 for(;i > 0;i--){ 230 if(date_before.getMonth().getValue() == 1 && date_before.day.getValue() == 1) { 231 date_before.getYear().yearReduction(); 232 date_before.getMonth().resetMax(); 233 date_before.resetMax(); 234 }else if(date_before.getMonth().getValue() == 3 && date_before.day.getValue() == 1) { 235 date_before.getMonth().setValue(2); 236 if(date_before.getYear().isLeapYear()) 237 date_before.day.setValue(29); 238 else 239 date_before.day.setValue(28); 240 }else if(date_before.day.getValue() == 1){ 241 date_before.getMonth().monthReduction(); 242 date_before.resetMax(); 243 }else 244 date_before.day.dayReduction(); 245 } 246 return date_before; 247 } 248 249 public int getDaysofDates(DateUtil date) {//求两日期之间的天数 250 int numOfdays = 0; 251 DateUtil temp = new DateUtil(); 252 if(!this.compareDates(date)){ 253 temp.day.setValue(date.day.getValue()); 254 temp.getMonth().setValue(date.getMonth().getValue()); 255 temp.getYear().setValue(date.getYear().getValue()); 256 date.day.setValue(this.day.getValue()); 257 date.getMonth().setValue(this.getMonth().getValue()); 258 date.getYear().setValue(this.getYear().getValue()); 259 this.day.setValue(temp.day.getValue()); 260 this.getMonth().setValue(temp.getMonth().getValue()); 261 this.getYear().setValue(temp.getYear().getValue()); 262 263 } 264 for(;date.getYear().getValue() > this.getYear().getValue() + 1;) { 265 if(this.getYear().isLeapYear() && this.getMonth().getValue() < 3 ) 266 numOfdays += 366; 267 else numOfdays += 365; 268 this.getYear().yearIncrement(); 269 } 270 int i ; 271 for( i = 0;this.compareDates(date); i++){ 272 if(this.getMonth().getValue() == 12 && this.day.getValue() == 31) { 273 this.getYear().yearIncrement(); 274 this.resetMin(); 275 this.getMonth().resetMin(); 276 }else if(this.getMonth().getValue() == 2) { 277 if((this.getYear().isLeapYear()&& day.getValue() == 29) 278 || (!this.getYear().isLeapYear() && day.getValue() == 28)){ 279 this.resetMin(); 280 this.getMonth().monthIcrement(); 281 }else 282 day.dayIncrement(); 283 }else if(this.day.getValue() == this.mon_maxnum[getMonth().getValue()]) { 284 this.resetMin(); 285 this.getMonth().monthIcrement(); 286 }else 287 this.day.dayIncrement(); 288 } 289 numOfdays += i; 290 return numOfdays; 291 } 292 } 293 class Year { 294 private int value; 295 296 public Year(int value) { 297 super(); 298 this.value = value; 299 } 300 301 public Year() { 302 super(); 303 // TODO Auto-generated constructor stub 304 } 305 306 public int getValue() { 307 return value; 308 } 309 310 public void setValue(int value) { 311 this.value = value; 312 } 313 314 public boolean isLeapYear() {//判断是否润年 315 if((value % 4 == 0 && value % 100 != 0) || value % 400 == 0){ 316 return true; 317 }else 318 return false; 319 } 320 321 public boolean validate() {//校验数据合法性 322 if(value >= 1820 && value <= 2020) { 323 return true; 324 } 325 else return false; 326 } 327 328 public void yearIncrement() {//年份增一 329 value++; 330 } 331 332 public void yearReduction() {//年份减一 333 value--; 334 } 335 336 public boolean isLeapYear(int value) { 337 if((value % 4 == 0 && value % 100 != 0) || value % 400 == 0){ 338 return true; 339 }else 340 return false; 341 } 342 } 343 class Month { 344 private int value; 345 346 public Month(int value) { 347 super(); 348 this.value = value; 349 } 350 351 public Month() { 352 super(); 353 // TODO Auto-generated constructor stub 354 } 355 356 public int getValue() { 357 return value; 358 } 359 360 public void setValue(int value) { 361 this.value = value; 362 } 363 364 public void resetMin() { 365 this.value = 1; 366 } 367 368 public void resetMax() {//月份设置为12 369 this.value = 12; 370 } 371 372 public boolean validate() {//校验数据合法性 373 if(value >= 1 && value <= 12) 374 return true; 375 else return false; 376 } 377 378 public void monthIcrement() { 379 this.value++; 380 } 381 382 public void monthReduction() { 383 this.value--; 384 } 385 } 386 class Day { 387 private int value; 388 389 public Day() { 390 super(); 391 // TODO Auto-generated constructor stub 392 } 393 394 public Day(int value) { 395 super(); 396 this.value = value; 397 } 398 399 public int getValue() { 400 return value; 401 } 402 403 public void setValue(int value) { 404 this.value = value; 405 } 406 407 public void dayIncrement() {//增一 408 value++; 409 } 410 411 public void dayReduction() {//减一 412 value--; 413 } 414 415 }
该题类图:
3.踩坑心得:
由于这三题解决的问题一样,故一起总结:这是在老师的框架下,编写的程序。
02:这题由于主函数已经编写完成,所以只能写一个类予以补充,结构上类似于c语言写法,写起来比较容易。
03:这题类之间是一条链式,个人觉得这样是不合理的类间关系,导致写法上表示一个数很复杂,不过基本上02题写好,03只是修改一下。
04:这题类间结构比较合理,也是只需在03题上基础做改动即可。
在写求下n天和上n天时由于分支结构比较复杂,在还未设计完全就着手写,导致写出来的代码繁杂,于是修修补补,在重新理清楚分支关系后重新完成。由于如果一天一天的推算,当输入数据较大比如10000天,会导致pta计算超时,于是就需要以年为单位删除相应天数,于是问题又出现,平年和闰年的天数不同。可能是pta老师未设置该测试点。判断扣除的天数是365还是366不单单是指考虑当年是否为闰年这么简单,比如2016年是闰年,但2016年3月1日到2017年3月1日只有365天,也就是说是否在2月29(28)日前成为了判断标志(往前算的情况同)。当然,如果年份比较多,那么这一天会被抵消,比如你2016年3月1日往后推一年多算一天,那么本该多算的2015/3/1到2016/3/1日会少算一天,也就是总的来说,不管如何天数最多相差1,但由于之前并未考虑该情况,于是在自己测试的时候,思考了非常久。该题还有两个测试点,便是测试两日期之间的天数,最后开始我默认是前面日期要先于后面日期,于是该测试点就导致我的算法推算一直无法结束,导致运行占用内存过大(我也不知道为什么不是超时),起先我认为是我算法的问题导致申请内存过多,但发现并没有多余申请的数据,甚至数组都没有新申请过,换了好几种写法依然如此,于是对照室友写法,发现他多考虑到先后日期的差别,但是在前两题并未出现该测试点(因为我过了)所以我便加了一段判断日期先后的步骤,然后就过了。。。所以还是阅读题目的问题,不能先入为主。
存在问题:总是忘记先设置边界值,测试的时候总是出现类似于数组越界的问题。在区分闰年和平年2月的时候,写出了太多重复可能性的代码。
4.收获:
1.对eclipse的调试功能更加熟悉,通过eclipse的快捷创建方法的功能,节约大量写重复代码的时间。
eclipse调试功能中检查变量也是极其方便,一些没有的变量但却想知道数据,可以在表达式中输入也能得出结果。
一些快捷创建类创建方法的功能也可以帮助程序员节约编写重复必要代码的时间。
而且代码的错误eclipse也提供了一些快捷解决方法,想要快速找到方法只需要按住ctrl然后单击方法名便可快速跳转到该方法所在位置。
而且eclipse的功能框都是可以独立操控,位置和大小均可随自己调动。
2.pta上测试点有大量的边界值测试,在我们写代码时需要多考虑到一些有可能超过边界的情况。在测试时避免出现数组越界等类似问题。然后就是输出格式,在pta上即使计算正确,但是正确的输出输入格式也是经常令我头疼的地方。特别是判定输入格式是否正确方面,需要对数据进行初步检测,提取有用数据,然后对这些数据合法性进行检测,这一步也占有许多测试点。在进行数据合法性检测时,并未考虑到输入负数的情况出现。老师给的类图也在我编写程序的时候给了我极大启发,比如数据加一减一用方法实现。跨年,跨月时数据的改动也可以使用方法实现。由于方法名字有其本身意义,也让的程序的行为变得一眼可见,在检查程序的是后可以省下大量时间的重新思考。月份对应最大值也可以不分平年闰年,因为在检测年月日的分支结构中,2月是被特殊考虑的,也就不存在争议。有少数情况会出现eclipse上可以运行的代码,转到PTA上却无法输出,也许时编译器包容性比较强,这时可以在cmd指令窗口运行代码,有时候也能报出错误,虽然不理解原理,但这一招屡试不爽。而cmd上可以运行的代码基本上pta也能运行。
3.在对类间关系的设计上经过此次练习也得到了启发。MCV模式是被大多数程序员认可的类间关系设计。也就是以后设计一大功能的时候,输入数据的检测判定和初步提取可以写作一个类,对提取好的数据进行计算可以写成一个类,输出展示也可写为一个类,最后写一个类负责和这几种类“交涉”,这样可以大大降低类间耦合性,代码的也就可复用易维护。而我之所以认为三种类间关系第三种比较合理,是因为年月日各自的方法都是通过DateUtil来管理的,当然我还认为可以专门写一个类Intermediary(中介),让DateUtil这个处理数据的类与年月日解耦,Displaly(展示)也可以写成一个类,降低主函数的繁杂从程度,最后main函数只要执行中介类的功能。这是我目前能想到更为合理的解决方案。
4.学习到了许多实用的方法,例如:
// 包装类的Integer中parseInt(String str)(double等同理)可以把字符串转换为对应的基本类型数字,valueOf也有相似功能,不过返回的是对应基本类型包装类的对象。在使用该方法的时候,经常报错,原因是在对数据处理的时候总是无法保证输入的字符串是对应的基本类型的样子。
// String类中(以下的String表示一个String类的对象)
(1) String.substring()方法可以截取字符串,返回一个String对象。并且有不同用法输入一个,在保证不越界的情况下,输入一个(int index),表示截取该索引值之后的字符串,而(int index1,int index2)则是截取index1 到index2的字符串,左开右闭。
(2) String.trim()方法可以删除字符串内的空格,返回一个字符串类型对象。
(3) String.valueOf();该方法可以将基本数据类型转化为String型对象。
(4) String.length().返回字符串长度,返回值为整形
(5) String.charAt(int index).可以返回索引值为index的字符,类型为char,一般用此方法遍历字符串。
(6) String.indexOf(String str);可以找到字符串str在String中出现的第一个索引值String.indexOf(String str,int index),表示从index开始搜寻。
最后我强烈建议!!!在pta作业结束后可以公开测试点!因为有时候直到最后都没办法通过一些测试点,但又不知道是哪里的问题。这样可以起到类似数学作业中错题的作用,以后便可最大程度规避该类错误!
好啦!这大概就是这个月通过三次作业的学习成果了,寥寥几千字,确实n夜抓耳挠腮换来的,秃了呀!而到现在为止也可能只是登堂入室把,后期想要进步还需不断努力呀!