题目集总结:

前言:

一、 知识点运用:

①    Java入门的基础语法(循环,判断,字符串,数组等等),Java的基础类运用,类与对象关系调用,类间关系(聚合)。

②    引(类与对象):

  对象:是实体,需要被创建,可以为我们做事情。

  类:是规范,根据类的定义来创建对象。

   {

          对象 = 属性 + 服务

          设计原则:单一职责原则——一个类做一件事情

    类/方法:职责单一,根据实际场景只创造需要的属性,明确类间关系,做到简而易。(具体场景,具体功能)

    }

二、题量:

  • Java入门语法:题目集一:9题;题目集二:3题;
  • 面向对象程序设计:题目集三:4题
  • 难度:题目集随着学习内容的推进逐渐契合,难度逐渐上升(题目集三后面两题对我来说已经可以说是无从下手)。

设计与分析:

(这里主要挑一些本人卡了很久的题目进行过程分析。)

基础型:(基础语法测试)

题目集二——7-2:

串口字符解析:

题目源码如下:

  1 import java.util.Scanner;
2 public class Main {
3
4 public static void main(String[] args) {
5 // TODO Auto-generated method stub
6 Scanner in = new Scanner(System.in);
7 String s = in.nextLine();
8 int[] m = new int[1000];              
9
10
11 if(s.length()<11)                  //先判断输入字符串长度是否合法
12 {
13 System.out.print("null data");
14 }
15
16 else if(s.length() == 11)              //字符串长度恰好等于11的情形
17 {
18 if(s.charAt(0) != '0')
19 {
20 System.out.print("null data");
21 }
22
23 else
24 {
25 int num = 0;
26 for(int i=1;i<10;i++)
27 {
28 if(s.charAt(i)=='1')
29 {
30 num++;
31 }
32 }
33
34 if(num%2==0&&s.charAt(10)!='1')
35 {
36 System.out.print("validate error");
37 }
38
39 else if(num%2==0)
40 {
41 System.out.print("parity check error");
42 }
43
44 else if(s.charAt(10)!='1')
45 {
46 System.out.print("validate error");
47 }
48
49 else
50 {
51 System.out.print("1:"+s.substring(1,9));     //substring(a,b):得到字符串a到b之前的所有内容
52 }
53 }
54 }
55
56 else                              //输入字符串长度大于11的情形
57 {
58 int i;
59 int index = 0;
60 int sign = 0;
61
62 for(i=0;i<s.length();i++)
63 {
64 if(s.charAt(i)!='1')
65 {
66 sign = 1;
67 m[index++] = i;
68 i += 10;
69 }
70 }
71
72 m[index] = -1;
73 if(sign == 0)
74 {
75 System.out.print("null data");
76 }
77
78 else                            //截取有效数据后长度不够10位的清晰
79 {
80 if(s.length()-m[0]<10)
81 {
82 System.out.print("null data");
83 }
84
85 else
86 {
87 int No = 1;
88 int sig;
89 index = 0;
90 i = m[index];
91
92 while(i!=-1)
93 {
94 sig = 0;
95 for (int y=i+1;y<i+10; y++)
96 {
97 if(s.charAt(y)=='1')
98 {
99 sig++;
100 }
101 }
102 //根据各自的判断条件返回相应的结果
103 if(sig%2==0&&s.charAt(i+10)!='1')
104 {
105 System.out.println(No+":validate error");
106 No++;
107 }
108
109 else if(sig%2==0)
110 {
111 System.out.println(No+":parity check error");
112 No++;
113 }
114
115 else if(s.charAt(i+10)!='1')
116 {
117 System.out.println(No+":validate error");
118 No++;
119 }
120
121 else
122 {
123 System.out.print(No+":"+s.substring(i+1, i+9));
124 System.out.println();
125 No++;
126 }
127
128 i=m[++index];
129 }
130 }
131 }
132 }
133
134
135 }
136
137 }

解释:这题虽然没有第三题思路那么复杂,但条件可以说是题目集二中最繁琐的一,分有很多种情形。并且题目意思比较难以理解,如果没有好好分析和思考,可能就会导致无从下手。

    

首先,单从题目角度看就有两点需要非常注意。

  • 题目的意思是什么?题目让你判断的信息是什么?

对于这一点本人也是十分的煎熬,对着电脑看了半个小时还是没能够理解这题目到底要让我干嘛。后来实在忍不了了,就拿一张草稿纸跟着题目思路把数据过一遍,

终于发现了玄机(呜呜呜,太难了)

简单来说:题目要求是让你输入一段由0和1组成的二进制数据流(当然,你输入其他的数据肯定是可以的,程序会报错就是了), 而你写的程序则是要根据你输入的

数据进行判断,返回题目要求所希望看到的结果。这里本人也画了一张草稿图(技术不精,望谅解)

如图所示:在输入一串的空闲位1后一出现0就代表你的程序要开始干活了(11位数据),这是判断输入数据有效的一个重要标志(当然如果一直没碰到0,也有对应的结果,这种情

形题目也做了介绍),然后,在零之后的后八位有效数据你就不用管它了(当然,如果它后面的有效数据不满足八位也要有相应的结果输出哦)。接下来的一个重要标志

就是开始位0后的第9位数校验位了,这里在下一点做出有关奇偶校验位的判断标准,总之,校验位也要根据相应的情形返回输出结果。最后就是结束位了,这里特别容易

让人混淆,概括来说,如果你开始位后最后没有结束位1会报错,结束位不是1也会报错,结束位不在检验数据的第11位还会报错。

  • 奇偶校验是什么?奇校验是怎么判断的?

这也是本人刚开始比较懵的,后来经过查找也是得到了结果,在这里简单概括一下,奇校验就是对你输入有效数据奇数位1进行统计,也就是说如果你的八位有效数据中1的出现次数是奇数位

那么你的奇偶校验位就应该是1,不然程序就会报错(这也是题目所给到的情形),反之,如果1出现的位数是偶数位,那么你的奇偶校验位就应该是2,不然程序就会报错。这样题目大体的思路和

需要判断的情形就捋顺了,代码就好写了。

踩坑心得:

①写代码前一定要先捋清思路,先把大概的想法框架设计出来,再动手会大大的节省时间。

就好像本人开始看题目的时候,我心里一直在想怎么办,怎么办,从哪里开始写,但现在回去想想,如果我一开始就冷静地在草稿纸上把题目的思路弄清楚,那样写代码一定会快速很多,我

也不会繁琐到写了100多行代码了。

②一定一定要注意结果对应关系(看清题目)

这里本人没有注意到结束位不为1是输出“validate error”,所以刚开始写的输出“parity check error”,所以在这里一个测试点卡了很久,最后看到题目才恍然大悟,所以一定要看清题目要求

改进建议:

可以将字符串长度恰好等于11的情形放到大于11的情形中,可以少写很多代码。

进阶型:(用类解决问题)

题目集三——7-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 //返回方程的两个根
44 private double a;
45 private double b;
46 private double c;
47
48 public QuadraticEquation() {
49 super();
50 // TODO Auto-generated constructor stub
51 }
52
53 public QuadraticEquation(double a, double b, double c) {
54 super();
55 this.a = a;
56 this.b = b;
57 this.c = c;
58 }
59
60 public double getA() {
61 return a;
62 }
63
64 public double getB() {
65 return b;
66 }
67
68 public double getC() {
69 return c;
70 }
71
72 public double getDiscriminant() {//返回判断值
73 return b*b-4*a*c;
74 }
75
76
77 public double getRoot1() {
78 double dis = getDiscriminant();
79 return ((-b) + Math.sqrt(dis))/(2*a);
80 }
81
82 public double getRoot2() {
83 double dis = getDiscriminant();
84 return ((-b) - Math.sqrt(dis))/(2*a);
85 }
86
87 }

解释:

这一题难度不大,也比较直接(前面做过很多次类似的题目),但也算是本人第一次自己创建一个类去做事情,有很多可以改进的地方在这里分享一下。

踩坑心得:

这题并没有太大的参考价值,主要就是让我学会了可以多用类去做事情,这样就可以让主方法变得精简,易懂,代码也会变得更有条理,其他的就不在这赘述了。

改进建议:

①只构造自己用的到的方法;

像本人刚开始因为贪图便捷,就直接用eclipise提供的source将a,b的get,set方法都构造出来了,但set方法其实是没有必要的,遵循简而精的原则,以后写代码还是

要仔细考虑,养成良好的编码习惯。

②能直接从类中获得便利就不要引进其他东西;

像这里是完全不需要再创造一个新的变量dis的,直接调用getDiscriminant()方法就行。

题目集三——7-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 public static class DateUtil {
85 private int year;
86 private int month;
87 private int day;
88
89 public DateUtil() {
90 super();
91 // TODO Auto-generated constructor stub
92 }
93
94 public DateUtil(int year, int month, int day) {
95 super();
96 this.year = year;
97 this.month = month;
98 this.day = day;
99 }
100
101 public int getYear() {
102 return year;
103 }
104
105 public void setYear(int year) {
106 this.year = year;
107 }
108
109 public int getMonth() {
110 return month;
111 }
112
113 public void setMonth(int month) {
114 this.month = month;
115 }
116
117 public int getDay() {
118 return day;
119 }
120
121 public void setDay(int day) {
122 this.day = day;
123 }
124
125 public boolean checkInputValidity() {
126 //判断输入是否合法
127 int year = getYear();
128 int month = getMonth();
129 int day = getDay();
130 int[] years = {31,28,31,30,31,30,31,31,30,31,30,31};
131
132 if(!(year>=1820&&year<=2020&&month>0&&month<=12&&day>0)) {
133 return false;
134 }
135 if(isLeapYear(year)){
136 if(month==2){
137 if(day>29) {
138 return false;
139 }
140 }else if(day>years[month-1]){
141 return false;
142 }else {
143 return true;
144 }
145 }else if(day>years[month-1]){
146 return false;
147 }else {
148 return true;
149 }
150 return true;
151 }
152
153 public boolean isLeapYear(int year) {
154 //判断year是否为闰年
155 if(year%4==0&&year%100!=0||year%400==0) {
156 return true;
157 }
158 else {
159 return false;
160 }
161 }
162
163 public DateUtil getNextNDays(int n)
164 {
165 //取得year-month-day的下n天
166 DateUtil nextdate = new DateUtil(year,month,day);
167 long[] years= {0,31,28,31,30,31,30,31,31,30,31,30,31};
168 if(isLeapYear(nextdate.year))
169 {
170 years[2] = 29;
171 }
172
173 long ob = (long)nextdate.day;
174 long op = (long)n;
175 ob = ob + op;
176 long sum = ob;
177 while(sum > years[nextdate.month])
178 {
179 if(isLeapYear(nextdate.year))
180 {
181 years[2] = 29;
182 }
183
184 sum = sum - years[nextdate.month];
185 nextdate.month++;
186 if(nextdate.month > 12)
187 {
188 nextdate.year++;
189 nextdate.month = 1;
190 }
191 years[2] = 28;
192 }
193 nextdate.day = (int)sum;
194 return nextdate;
195
196 }
197
198 public DateUtil getPreviousNDays(int n)
199 {
200 //取得year-month-day的前n天日期
201 DateUtil predate = new DateUtil(year,month,day);
202 int[] years= {0,31,28,31,30,31,30,31,31,30,31,30,31};
203 if(isLeapYear(predate.year))
204 {
205 years[2] = 29;
206 }
207
208 predate.day = predate.day - n;
209 while(predate.day <= 0)
210 {
211 if(isLeapYear(predate.year))
212 {
213 years[2] = 29;
214 }
215
216 predate.month--;
217
218 if(predate.month == 0)
219 {
220 predate.year--;
221 predate.month = 12;
222 }
223 predate.day = predate.day + years[predate.month];
224 years[2] = 28;
225 }
226 return predate;
227 }
228
229 public boolean compareDates(DateUtil date)
230 {
231 //比较两个日期的先后
232 int day_1 = getDay();
233 int day_2 = date.getDay();
234 int month_1 = getMonth();
235 int month_2 = date.getMonth();
236 int year_1 = getYear();
237 int year_2 = date.getYear();
238
239 if(year_1 > year_2)
240 {
241 return true;
242 }
243 else if(year_1 == year_2)
244 {
245 if(month_1 > month_2)
246 {
247 return true;
248 }
249 else if(month_1 == month_2)
250 {
251 if(day_1 > day_2)
252 {
253 return true;
254 }
255
256 else
257 {
258 return false;
259 }
260
261 }
262 else
263 {
264 return false;
265 }
266 }
267 else
268 {
269 return false;
270 }
271
272 }
273
274 public boolean equalTwoDates(DateUtil date)
275 {
276 //判断两个日期是否相等
277 int day_1 = getDay();
278 int day_2 = date.getDay();
279 int month_1 = getMonth();
280 int month_2 = date.getMonth();
281 int year_1 = getYear();
282 int year_2 = date.getYear();
283
284 if(year_1 == year_2)
285 {
286 if(month_1 == month_2)
287 {
288 if(day_1 == day_2)
289 {
290 return true;
291 }
292 else
293 {
294 return false;
295 }
296 }
297
298 else
299 {
300 return false;
301 }
302 }
303 else
304 {
305 return false;
306 }
307
308 }
309
310 public int getDaysofDates(DateUtil date)
311 {
312 //求当前日期与date之间相差的天数
313 int[] years= {0,31,28,31,30,31,30,31,31,30,31,30,31};
314 int[] years2= {0,31,29,31,30,31,30,31,31,30,31,30,31};
315
316 int day_1 = getDay();
317 int day_2 = date.getDay();
318 int month_1 = getMonth();
319 int month_2 = date.getMonth();
320 int year_1 = getYear();
321 int year_2 = date.getYear();
322
323 int sum_1 = 0;
324 int sum_2 = 0;
325 int sum = 0;
326
327 int index_1 = year_1 - 1820;
328 for(int i=1820; i<year_1; i++)
329 {
330 if(isLeapYear(i))
331 {
332 sum_1 += 366;
333 }
334 else
335 {
336 sum_1 += 365;
337 }
338 }
339 if(isLeapYear(year_1))
340 {
341 if(month_1 > 2)
342 {
343 for(int i = 0; i<month_1; i++)
344 {
345 sum_1 += years2[i];
346 }
347 sum_1 += day_1;
348 }
349 else if(month_1 == 2)
350 {
351 sum_1 += 31 + day_1;
352 }
353 else
354 {
355 sum_1 += day_1;
356 }
357 }
358 else
359 {
360 if(month_1 > 2)
361 {
362 for(int i = 0; i<month_1; i++)
363 {
364 sum_1 += years[i];
365 }
366 sum_1 += day_1;
367 }
368 else if(month_1 == 2)
369 {
370 sum_1 += 31 + day_1;
371 }
372 else
373 {
374 sum_1 += day_1;
375 }
376 }
377
378 for(int i=1820; i<year_2; i++)
379 {
380 if(isLeapYear(i))
381 {
382 sum_2 += 366;
383 }
384 else
385 {
386 sum_2 += 365;
387 }
388 }
389 if(isLeapYear(year_2))
390 {
391 if(month_2 > 2)
392 {
393 for(int i = 0; i<month_2; i++)
394 {
395 sum_2 += years2[i];
396 }
397 sum_2 += day_2;
398 }
399 else if(month_2 == 2)
400 {
401 sum_2 += 31 + day_2;
402 }
403 else
404 {
405 sum_2 += day_2;
406 }
407 }
408 else
409 {
410 if(month_2 > 2)
411 {
412 for(int i = 0; i<month_2; i++)
413 {
414 sum_2 += years[i];
415 }
416 sum_2 += day_2;
417 }
418 else if(month_2 == 2)
419 {
420 sum_2 += 31 + day_2;
421 }
422 else
423 {
424 sum_2 += day_2;
425 }
426 }
427
428 sum = Math.abs(sum_1-sum_2);
429 return sum;
430
431 }
432
433
434 public String showDate() {
435 //以“year-month-day”格式返回日期值
436 int year = getYear();
437 int month = getMonth();
438 int day = getDay();
439
440 String s = year + "-" + month + "-" + day;
441 return s;
442
443 }
444
445 }
446 }

解释:

这题可以说是一个大工程了(融合了许多前面题目用到的方法,后面两题也是根据这题思路而来),对本人这种编程不怎么好的同志来说,也是几乎花了一天半的时间。

设计思路:明确每个类要做的事情与联系,逐个攻破。

踩坑心得:

①不好区分就大不了再创一个;

想必2月份的问题是很多同学的一大障碍,很多测试点没过大概率就是因为这个2月,因为在闰年和平年只有2月的天数不是固定的,这就会导致很多结果产生一些误差。

本人刚开始也是对每个年份进行判断是闰年还是平年,写了一大串if-else语句,后面实在受不了了,索性声明两个数组然后分别调用,感觉世界都祥和了。

②有时候粗暴一点更能解决问题;

在写求两个日期之间相差的天数的时候,本人刚开始也没什么思路,想着如果直接用这两个日期相减会非常的复杂,又要比谁先谁后,还要分很多种情况讨论,觉得这样搞

太复杂了,于是,决定粗暴一点,索性以一个相同的日期为基准,求这两个日期分别与这个基准相差的天数,再相减求绝对值不就是要求的相差的天数了吗。

③要换就换全一点;

不知道大家有没有碰到这个测试点一直过不去,“求下n天的最大值测试”,刚开始我以为是1820-1-1——2020-12-31之间的天数测试点没过去,可测试一下结果是对的,所以我就在想是不是整形最大值

没有实现,测试一下,果然结果变成负的了,于是决定将所有的int型换成long型。但忘记将数组也变成long型,导致我卡了很久。

题目集三——7-3

日期问题面向对象设计(聚合一):

设计思路:

解释:

说起来很惭愧,本人这题其实没有按照题目的要求做出来,所以在这里就不给出源码了,以免误人子弟,因为对类图

并没有什么接触,也不知道这个类图的含义是什么,所以我基本无从下手,想了很久,也尝试了很久,但感觉还是不太对劲

所以只能放弃,将原先的代码进行修改就交上去了。

心得:因此,在这里告诫大家,一定要按照项目的要求来做,不能任性发挥,这是对未来的工作负责,更是对自己的将来负责。

总结:

  • 这段时间本人通过PTA的训练学到了什么:

    ①:Java入门语法方面:对循环,数组,字符串,判断,函数等基础语法有了更进一步地熟练和运用

    ②:面对对象设计方面:对类与对象之间的关系和运用有了基础的认识和理解,可以对一些简单的问题和实际场景用面向对象

      的思路进行解决。

  • 有待进一步学习和深入改造的地方:

    ①:对类与对象方面的知识还要多加巩固学习;

    ②:每天还是要多练,多学,多动手写代码,以达到熟练运用的效果。

  • 对现在教学方式的改进建议及意见:

    ①:希望老师在讲授知识的过程中还是循序渐进一点的好,不要太乱;

    ②:希望老师在布置作业的时候可以先结合学生的学习课程确定截止期限。

最新文章

  1. 【linux】之相关命令
  2. 转:strcmp函数实现及分析
  3. Linux系统编程——进程间通信:命名管道(FIFO)
  4. 关于PHP导入项目的时候导入不了的情况
  5. JDBC官方用法
  6. 小C的树(Treap节点删除)[70/100]
  7. 学习笔记: JavaScript/JQuery 的cookie操作
  8. background是什么样式?
  9. 201621123040《Java程序设计》第4周学习总结
  10. Deepin下配置JDK8
  11. WebDriver实现网页自动化测试(以python为例说明,ruby用法类似)
  12. ATDD
  13. darknet中的若干问题
  14. AOAPC I: Beginning Algorithm Contests (Rujia Liu) Volume 6. Mathematical Concepts and Methods
  15. WebService 服务端客户端 实例 HTTPRIO (一) SOAP WSDL
  16. iOS下微信语音播放之切换听筒和扬声器的方法解决方案
  17. 【HLSDK系列】groupinfo的基本用法
  18. 5、RabbitMQ - Exchange之 fanout \ 【direct 关键字发送】 \ topic
  19. 小米2S手机 - Charles无法安装证书 因为无法读取证书
  20. code2198 数字三角形WWW

热门文章

  1. Python里的引用与拷贝规律
  2. 面试 | Java 算法的 ACM 模式
  3. ORM中聚合函数、分组查询、Django开启事务、ORM中常用字段及参数、数据库查询优化
  4. 获取ul中li的value值
  5. java连接mysql8.0.28数据库实例
  6. uniapp-app 打开小程序
  7. 深度剖析text-align家族
  8. Dependabot 开始支持 pub package 版本检测
  9. python学习-Day9
  10. XCTF练习题---MISC---倒立屋