Blog总结02(4~6次作业总结)

1.前言

(1)题目集04共有三道题目,第一题难度较大,第二题和第三题难度适中,第一题考察的知识点是 Java 中的字符串处理类以及正则表达式对输入字符串数据进行合法性校验及计算,难度较大。第二题考察对日期的处理,熟悉面向对象的学习,创建多个类,然后将其中一个类的创建的对象党当作另一个对象的属性,考察了在其他类中创建对象和调用其他类的方法,含参数不含参数的构造方法,getset的构造方法的使用,属性私有性的知识点等,第三题是图形继承题目,难度适中,考察了继承的知识点,弄清楚子类父类的关系,子类调用父类的方法,在子类中重写父类的方法等知识点。

(2)题目集05共有五道题目,1~3题较为简单,分别考察对字符串的处理,对数组的处理,对整型数据排序,第四题难度较大,统计Java程序中关键词的出现次数,知识点为利用正则表达式对字符进行匹配以及对字符串的处理,第五题难度适中,考察了在其他类中创建对象和调用其他类的方法,含参数不含参数的构造方法,getset的构造方法的使用,属性私有性的知识点等,和题目集04第二题很相似但有一些区别。

(3)题目集06共有六道题目,其中1~4题较为简单,1,3,4题考察了利用正则表达式去匹配字符串,2题考察了字符串与字符之间的转换,以及排序,第五六题难度适中,第五题考察了类的继承、多态性及其使用方法,让我们了解抽象类和抽象方法,抽像类方法的调用等,还有一些ArrayList类和Collection类的使用,第六题知识点为接口的使用和类的多态性,用接口去调用方法实现功能。

2.设计与分析

(1)题目集04(7-2 )&&题目集05(7-5)

日期问题面向对象设计

应用程序共测试三个功能:

  1. 求下n天
  2. 求前n天
  3. 求两个日期相差的天数

输入格式:

有三种输入方式(以输入的第一个数字划分[1,3]):

  • 1 year month day n //测试输入日期的下n天
  • 2 year month day n //测试输入日期的前n天
  • 3 year1 month1 day1 year2 month2 day2 //测试两个日期之间相差的天数

输出格式:

  • 当输入有误时,输出格式如下: Wrong Format

  • 当第一个数字为1且输入均有效,输出格式如下:

    year-month-day

  • 当第一个数字为2且输入均有效,输出格式如下:

    year-month-day

  • 当第一个数字为3且输入均有效,输出格式如下:

    天数值

题目集04(7-2)源码如下:

  1 import java.util.Scanner;
2
3 public class Main {
4
5 public static void main(String[] args) {
6 Scanner input = new Scanner(System.in);
7 int n = input.nextInt();
8 if(n >= 1 && n <= 3) {
9 int x = input.nextInt();
10 int y = input.nextInt();
11 int z = input.nextInt();
12 DateUtil date = new DateUtil(x,y,z);
13
14 if(y>12||y<1)
15 System.out.println("Wrong Format");
16 else {
17 if(date.checkInputValidity()) {
18 switch(n) {
19 case 1:
20 date.getNextNdate(input.nextInt());
21 System.out.println(date.showDate());
22 break;
23 case 2:
24 date.getPreviousNDays(input.nextInt());
25 System.out.println(date.showDate());
26 break;
27 case 3:
28 int a = input.nextInt();
29 int b = input.nextInt();
30 int c = input.nextInt();
31 DateUtil date2 = new DateUtil(a,b,c);
32 if(b>12||b<1)
33 System.out.println("Wrong Format");
34 else {
35 System.out.println(date.getDaysofDates(date2));
36 date.getDaysofDates(date2);
37 break;}
38 }
39
40 }
41 else
42 System.out.println("Wrong Format");
43 }
44
45 }
46 else
47 System.out.println("Wrong Format");
48 }
49 }
50
51
52
53 class Year{
54 private int value;
55
56 public Year() {
57 super();
58 }
59
60 public Year(int value) {
61 super();
62 this.value = value;
63 }
64
65 public int getValue() {
66 return value;
67 }
68
69 public void setValue(int value) {
70 this.value = value;
71 }
72 public boolean isLeapYear() {
73 if ((this.value % 4 == 0 && this.value % 100 != 0 )|| this.value % 400 == 0)
74 return true;
75 else
76 return false;
77 }
78 public boolean validate() {
79 if(getValue() >= 1900 && getValue() <= 2050 )
80 return true;
81 else
82 return false;
83 }
84 public void yearIncrement() {
85 this.value ++;
86 }
87 public void yearReduction() {
88 this.value --;
89 }
90 }
91
92 class Month{
93 private int value;
94 private Year year = new Year();
95
96 public Month() {
97 super();
98 }
99
100 public Month(int yearValue, int monthValue) {
101 this.year.setValue(yearValue);
102 this.value = monthValue ;
103
104 }
105
106 public int getValue() {
107 return value;
108 }
109
110 public void setValue(int value) {
111 this.value = value;
112 }
113
114 public Year getYear() {
115 return year;
116 }
117
118 public void setYear(Year year) {
119 this.year = year;
120 }
121
122 public void resetMin() {
123 this.value = 1;
124 }
125
126 public void resetMax() {
127 this.value = 12;
128 }
129
130 public boolean validate() {
131 if(year.validate() && this.value >= 1 && this.value <= 12)
132 return true;
133 else
134 return false;
135 }
136
137 public void monthIncrement() {
138 if(this.value == 12) {
139 year.yearIncrement();
140 resetMin();
141 }
142 else
143 this.value ++;
144 }
145
146 public void monthReduction() {
147 if(this.value == 1) {
148 year.yearReduction();
149 resetMax();
150 }
151 else
152 this.value --;
153 }
154
155 }
156
157 class Day{
158 private int value;
159 private Month month = new Month();
160 private int [] mon_maxnum = {31,28,31,30,31,30,31,31,30,31,30,31};
161
162 public Day() {
163
164 }
165
166 public Day(int yearValue,int monthValue,int dayValue) {
167 this.month.getYear().setValue(yearValue);
168 this.month.setValue(monthValue);
169 this.value = dayValue;
170
171 }
172
173 public int getValue() {
174 return value;
175 }
176
177 public void setValue(int value) {
178 this.value = value;
179 }
180
181 public Month getMonth() {
182 return month;
183 }
184
185 public void setMonth(Month month) {
186 this.month = month;
187 }
188
189 public void resetMin() {
190 this.value = 1;
191 }
192 public void resetMax() {
193 this.value = getMaxOfmonth();
194 }
195 public void resetMMax() {
196 switch (month.getValue()) {
197 case 1:this.value = mon_maxnum[0];break;
198 case 2:
199 if(month.getYear().isLeapYear()) {
200 this.value = mon_maxnum[1] + 1;
201 }
202 else
203 this.value = mon_maxnum[1];
204 break;
205 case 3:this.value = mon_maxnum[2];break;
206 case 4:this.value = mon_maxnum[3];break;
207 case 5:this.value = mon_maxnum[4];break;
208 case 6:this.value = mon_maxnum[5];break;
209 case 7:this.value = mon_maxnum[6];break;
210 case 8:this.value = mon_maxnum[7];break;
211 case 9:this.value = mon_maxnum[8];break;
212 case 10:this.value = mon_maxnum[9];break;
213 case 11:this.value = mon_maxnum[10];break;
214 case 12:this.value = mon_maxnum[11];break;
215 default:;break;
216 }
217 }
218 public int getMaxOfmonth() {
219 if(month.getYear().isLeapYear() && month.getValue() == 2)
220 return 29;
221 else
222 return mon_maxnum[month.getValue()-1];
223 }
224 public int returnMonthMax() {
225 switch (month.getValue()) {
226 case 1:return mon_maxnum[0];
227 case 2:
228 if(month.getYear().isLeapYear()) {
229 return mon_maxnum[1];
230 }
231 else
232 return mon_maxnum[1] + 1;
233
234 case 3:return mon_maxnum[2];
235 case 4:return mon_maxnum[3];
236 case 5:return mon_maxnum[4];
237 case 6:return mon_maxnum[5];
238 case 7:return mon_maxnum[6];
239 case 8:return mon_maxnum[7];
240 case 9:return mon_maxnum[8];
241 case 10:return mon_maxnum[9];
242 case 11:return mon_maxnum[10];
243 case 12:return mon_maxnum[11];
244 default:;break;
245 }
246 return 0;
247 }
248 public boolean validate(){
249 if(month.validate()&&this.value <= getMaxOfmonth()&&this.value >= 1 )
250 return true;
251 else
252 return false;
253 }
254
255 public void dayIncrement() {
256 if(this.value == getMaxOfmonth()) {
257 month.monthIncrement();
258 resetMin();
259 }
260 else
261 this.value ++;
262 }
263 public void dayReduction() {
264 if(this.value == 1) {
265 month.monthReduction();
266 resetMax();
267 }
268 else
269 this.value --;
270 }
271 }
272
273
274
275
276 class DateUtil{
277 private Day day = new Day();
278
279 public DateUtil() {
280 super();
281 }
282 public DateUtil(int y,int m, int d) {
283 day.getMonth().getYear().setValue(y);
284 day.getMonth().setValue(m);
285 day.setValue(d);
286 }
287 public Day getDay() {
288 return day;
289 }
290 public void setDay(Day day) {
291 this.day = day;
292 }
293 public boolean checkInputValidity() {
294 if(day.getMonth().getYear().validate() && day.getMonth().validate() && day.validate())
295 return true;
296 else
297 return false;
298 }
299
300 public String showDate() {
301 return this.day.getMonth().getYear().getValue()+"-"+this.day.getMonth().getValue()+"-"+this.day.getValue();
302 }
303
304 public boolean compareDates(DateUtil date) {
305 int a = date.day.getMonth().getYear().getValue()*20000+date.day.getMonth().getValue()*1000+date.day.getValue();
306 int b = this.day.getMonth().getYear().getValue()*20000+this.day.getMonth().getValue()*1000+this.day.getValue();
307 if(a<b)
308 return false;
309 else
310 return true;
311 }
312
313 public boolean equalTwoDates(DateUtil date) {
314 if(date.showDate().equals(showDate()))
315 return true;
316 else
317 return false;
318 }
319
320 public DateUtil getNextNdate(int n) {
321 for(int i = 0; i < n ; i++) {
322 day.dayIncrement();
323 }
324 return new DateUtil (day.getMonth().getYear().getValue(),day.getMonth().getValue(),day.getValue());
325 }
326
327 public DateUtil getPreviousNDays(int n) {
328 for(int i = 0; i < n; i ++) {
329 day.dayReduction();
330 }
331 return new DateUtil (day.getMonth().getYear().getValue(),day.getMonth().getValue(),day.getValue());
332 }
333
334 public int getDaysofDates(DateUtil date){
335 int number = 0;
336 if(compareDates(date)){
337 for(; !equalTwoDates(date); ) {
338 date.day.dayReduction();
339 number ++;
340 }
341 }
342 else if(!compareDates(date)){
343 for(;!equalTwoDates(date);) {
344 date.day.dayIncrement();
345 number ++;
346 }
347 }
348 return number;
349
350 }
351 }

题目集05(7-5)源代码:

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 > 3 || n < 1)
System.out.println("Wrong Format");
else {
int x = input.nextInt();
int y = input.nextInt();
int z = input.nextInt();
DateUtil date = new DateUtil(x,y,z);
if(date.checkInputValidate()) {
switch(n) {
case 1:
int days1 = input.nextInt();
date.getNextNdate(days1);
System.out.println(x+"-"+y+"-"+z+" next "+days1+" days is:"+date.showDate());
break;
case 2:
int days2 = input.nextInt();
date.getPreviousNDays(days2);
System.out.println(x+"-"+y+"-"+z+" previous "+days2+" days is:"+date.showDate());
break;
case 3:
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
DateUtil date2 = new DateUtil(a,b,c);
if(date2.checkInputValidate()) {
System.out.println("The days between "+date.showDate()+" and "+date2.showDate()+" are:"+date.getDaysofDates(date2));
break;
}
} }
else
System.out.println("Wrong Format"); } }
} class Year{
private int value; public Year() {
super();
} public Year(int value) {
super();
this.value = value;
} public int getValue() {
return value;
} public void setValue(int value) {
this.value = value;
}
public boolean isLeapYear() {
if ((this.value % 4 == 0 && this.value % 100 != 0 )|| this.value % 400 == 0)
return true;
else
return false;
}
public boolean validate() {
if(getValue() >= 1820 && getValue() <= 2020 )
return true;
else
return false;
}
public void yearIncrement() {
this.value ++;
}
public void yearReduction() {
this.value --;
}
} class Month{
private int value; public Month() {
super();
} public Month( int Value) {
this.value = Value ; } public int getValue() {
return value;
} public void setValue(int value) {
this.value = value;
} public void resetMin() {
this.value = 1;
} public void resetMax() {
this.value = 12;
} public boolean validate() {
if(this.value >= 1 && this.value <= 12)
return true;
else
return false;
} public void monthIncrement() { this.value ++;
} public void monthReduction() { this.value --;
} } class Day{
private int value;
public Day() { }
public Day(int value) {
super();
this.value = value;
} public int getValue() {
return value;
} public void setValue(int value) {
this.value = value;
} public void dayIncrement() { this.value ++;
}
public void dayReduction() { this.value --;
} } class DateUtil{
private Year year = new Year();
private Month month = new Month();
private Day day = new Day();
private int [] mon_maxnum = {31,28,31,30,31,30,31,31,30,31,30,31};
public DateUtil() {
super(); }
public DateUtil(int y,int m,int d) {
super();
year.setValue(y);
month.setValue(m);
day.setValue(d);
}
public Year getYear() {
return year;
}
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;
}
private void setDayMin() {
day.setValue(1);
}
private void setDayMax() {
day.setValue(getDayMax()); }
public int getDayMax() {
if(year.isLeapYear() && month.getValue() == 2) {
return 29;
}
else {
return mon_maxnum[month.getValue()-1];
}
}
public boolean checkInputValidate() {
if(year.validate() && month.validate()) {
if(day.getValue() >= 1 && day.getValue() <= getDayMax() )
return true;
else
return false;
}
else
return false;
}
public DateUtil getNextNdate(long n) {
for(int i = 0; i < n ; i ++ ) { if(month.getValue() == 12 && day.getValue() == 31) {
month.resetMin();
setDayMin();
year.yearIncrement();
}
else if(day.getValue() == getDayMax()) {
day.setValue(1);
month.monthIncrement();
}
else
day.dayIncrement();
}
return new DateUtil(year.getValue(),month.getValue(),day.getValue());
}
public DateUtil getPreviousNDays(long n) {
for(int i = 0; i < n ; i ++ ) {
if(month.getValue() == 1 && day.getValue() == 1) {
year.yearReduction();
month.resetMax();
setDayMax();
}
else if(day.getValue() == 1) {
month.monthReduction();
setDayMax(); }
else
day.dayReduction(); }
return new DateUtil(year.getValue(),month.getValue(),day.getValue());
} public int getDaysofDates(DateUtil date) {
int num = 0;
if(equalTwoDates( date))
return 0;
if(compareDates( date)) {
for(;!equalTwoDates( date);) {
if(month.getValue() == 12 && day.getValue() == 31) {
month.resetMin();
setDayMin();
year.yearIncrement();
}
else if(day.getValue() == getDayMax()) {
day.setValue(1);
month.monthIncrement();
}
else
day.dayIncrement();
num ++; }
}
else if(!compareDates( date)) {
for(;!equalTwoDates( date);) {
if(month.getValue() == 1 && day.getValue() == 1) {
year.yearReduction();
month.resetMax();
setDayMax();
}
else if(day.getValue() == 1) {
month.monthReduction();
setDayMax(); }
else
day.dayReduction();
num ++; }
}
return num;
} public boolean compareDates(DateUtil date) {
int a = date.year.getValue()*20000+date.month.getValue()*1000+date.day.getValue();
int b = this.year.getValue()*20000+this.month.getValue()*1000+this.day.getValue();
if(a > b)
return true;
else
return false;
}
public boolean equalTwoDates(DateUtil date) {
if(date.showDate().equals(showDate()))
return true;
else
return false;
} public String showDate() {
return year.getValue()+"-"+month.getValue()+"-"+day.getValue();
} }

SourceMonitor的生成报表内容:

题目集04(7-2):

题目集05(7-5):

类图:

题目集04(7-2):

题目集05(7-5):

根据SourceMonitor的生成报表可以看到两个程序的圈复杂程度相差不大,这两道题相比之前的日期类题目,圈复杂度已经大大的降低了,再根据类图来分析两个程序的不同,首先第一个程序是递进式的聚合,一环套一环,year类最先写,然后在month类中创建了一个year类的对象当作month类的一个属性,然后在day类中又创建了一个month对象作为day类中的属性,最后在日期处理类中创建了一个day的对象当作它的属性,整体来说就是一环扣一环,不可分割,第二个程序year,month,day等类是分开的,他们之间并没有联系,由一个日期处理类将他们联系在一起。

优劣分析:

我认为第一个程序的聚合耦合度非常的高,因为环环相扣,他的劣势在于,如果其中某个环节除了错误,很难改正,尤其是到后面才发现错误,就可能要把整个程序都改一下,类与类之间的关系太紧密了,每当要用到year的方法,需要从DateUtil调用day再调用month再调用year,调用起来也比较复杂,但是它也有他的优点,到最后实现前n天或者后n天的时候,它只需要调用一个day的加减就可以了,因为之前做了铺垫,程序的逻辑就比较简单了。

我认为第二个程序总体来说应该是优于第一个程序的,首先它的年月日的类是分开的,它们之间是没有直接的关系的,耦合程度非常的低,就算我其中一个类出错了,和其他的类也没有什么关系,而且出错之后非常的好修改,也知道错在哪里,而他得缺点就是到最后的日期处理类中,我们需要进行一些思考,去完成前n天后n天的实现,我们知道,java类与类之间的耦合程度越低越好,且第二个程序很好的保证了类的单一职责的原则,所以我认为第二个程序总体来说比第一个好一些。

(2)题目集04(7-3)题目集06(7-5、7-6)

题目集04(7-3):

编写程序,实现图形类的继承,并定义相应类对象并进行测试。

  1. 类Shape,无属性,有一个返回0.0的求图形面积的公有方法public double getArea();//求图形面积
  2. 类Circle,继承自Shape,有一个私有实型的属性radius(半径),重写父类继承来的求面积方法,求圆的面积
  3. 类Rectangle,继承自Shape,有两个私有实型属性width和length,重写父类继承来的求面积方法,求矩形的面积
  4. 类Ball,继承自Circle,其属性从父类继承,重写父类求面积方法,求球表面积,此外,定义一求球体积的方法public double getVolume();//求球体积
  5. 类Box,继承自Rectangle,除从父类继承的属性外,再定义一个属性height,重写父类继承来的求面积方法,求立方体表面积,此外,定义一求立方体体积的方法public double getVolume();//求立方体体积
  6. 注意:
  • 每个类均有构造方法,且构造方法内必须输出如下内容:Constructing 类名
  • 每个类属性均为私有,且必须有getter和setter方法(可用Eclipse自动生成)
  • 输出的数值均保留两位小数

主方法内,主要实现四个功能(1-4): 从键盘输入1,则定义圆类,从键盘输入圆的半径后,主要输出圆的面积; 从键盘输入2,则定义矩形类,从键盘输入矩形的宽和长后,主要输出矩形的面积; 从键盘输入3,则定义球类,从键盘输入球的半径后,主要输出球的表面积和体积; 从键盘输入4,则定义立方体类,从键盘输入立方体的宽、长和高度后,主要输出立方体的表面积和体积;

假如数据输入非法(包括圆、矩形、球及立方体对象的属性不大于0和输入选择值非1-4),系统输出Wrong Format

输入格式:

共四种合法输入

  • 1 圆半径
  • 2 矩形宽、长
  • 3 球半径
  • 4 立方体宽、长、高

输出格式:

按照以上需求提示依次输出

源码如下:

  1 import java.util.Scanner;
2
3 public class Main {
4
5 public static void main(String[] args) {
6 Scanner input = new Scanner(System.in);
7 int n = input.nextInt();
8 if(n > 4 || n < 1) {
9 System.out.println("Wrong Format");
10 System.exit(0);
11 }
12
13 switch(n){
14 case 1:
15
16 double r = input.nextDouble();
17 if(r < 0) {
18 System.out.println("Wrong Format");
19 System.exit(0);}
20 else {
21 Circle c = new Circle();
22 c.setRadius(r);
23 System.out.println("Circle's area:"+String.format("%.2f", c.getArea()));
24 }
25 break;
26 case 2:
27
28 double chang = input.nextDouble();
29 double kuan = input.nextDouble();
30 if(chang < 0 ||kuan < 0) {
31 System.out.println("Wrong Format");
32 System.exit(0);
33 }
34 else {
35 Rectangle zf = new Rectangle();
36 zf.setLength(chang);
37 zf.setWidth(kuan);
38 System.out.println("Rectangle's area:"+String.format("%.2f", zf.getArea()));
39 }break;
40 case 3:
41
42 double R = input.nextDouble();
43 if(R < 0) {
44 System.out.println("Wrong Format");
45 System.exit(0);
46 }
47 else {
48 Ball b = new Ball();
49 b.setRadius(R);
50 System.out.println("Ball's surface area:"+String.format("%.2f", b.getArea()));
51 System.out.println("Ball's volume:"+String.format("%.2f",b.getVolume() ));
52 }
53 break;
54 case 4:
55
56 double ch = input.nextDouble();
57 double ku = input.nextDouble();
58 double gao = input.nextDouble();
59 if(ch < 0 || ku < 0 || gao < 0) {
60 System.out.println("Wrong Format");
61 System.exit(0);
62 }
63 else {
64 Box B = new Box();
65 B.setLength(ch);
66 B.setWidth(ku);
67 B.setHeight(gao);
68 System.out.println("Box's surface area:"+String.format("%.2f",B.getArea() ));
69 System.out.println(String.format("Box's volume:"+"%.2f",B.getVolume() ));
70 }
71 break;
72 default:;
73 }
74 }
75
76
77 private static void Switch(int n) {
78 // TODO 自动生成的方法存根
79
80 }
81 }
82 class Shape{
83 public Shape() {
84 super();
85 System.out.println("Constructing Shape");
86 }
87 public double getArea() {
88 return 0.0;
89 }
90 }
91 class Circle extends Shape{
92 private double radius;
93
94 public double getArea() {
95 return Math.PI*Math.pow(this.getRadius(), 2);
96 }
97 public Circle() {
98 super();
99 System.out.println("Constructing Circle");
100 }
101 public double getRadius() {
102 return radius;
103 }
104 public void setRadius(double radius) {
105 this.radius = radius;
106 }
107
108 }
109 class Rectangle extends Shape{
110 private double width ;
111 private double length;
112
113 public double getWidth() {
114 return width;
115 }
116
117 public void setWidth(double width) {
118 this.width = width;
119 }
120
121 public double getLength() {
122 return length;
123 }
124
125 public void setLength(double length) {
126 this.length = length;
127 }
128
129 public double getArea() {
130 return this.length*this.width;
131 }
132
133 public Rectangle() {
134 super();
135 System.out.println("Constructing Rectangle");
136 }
137 }
138 class Ball extends Circle{
139
140 public Ball() {
141 super();
142 System.out.println("Constructing Ball");
143
144 }
145 public double getArea() {
146 return 4*Math.PI*getRadius()*getRadius();
147 }
148
149 public double getVolume() {
150 return 4.0/3*Math.PI*getRadius()*getRadius()*getRadius();
151 }
152 }
153 class Box extends Rectangle{
154 private double height;
155
156 public double getHeight() {
157 return height;
158 }
159 public void setHeight(double height) {
160 this.height = height;
161 }
162 public double getArea() {
163 return 2*(getWidth()*getLength()+getWidth()*this.height+getLength()*this.height);
164 }
165 public double getVolume() {
166 return this.height*getLength()*getWidth();
167 }
168
169 public Box() {
170 super();
171 System.out.println("Constructing Box");
172 }
173
174 }

SourceMonitor的生成报表内容:

类图如下:

题目集06(7-5):

掌握类的继承、多态性及其使用方法。

输入格式:

从键盘首先输入三个整型值(例如a b c),分别代表想要创建的Circle、Rectangle及Triangle对象的数量,然后根据图形数量继续输入各对象的属性值(均为实型数),数与数之间可以用一个或多个空格或回车分隔。

输出格式:

  1. 如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边关系),则输出Wrong Format

  2. 如果输入合法,则正常输出,输出内容如下(输出格式见输入输出示例):

  • 各个图形的面积;

  • 所有图形的面积总和;

  • 排序后的各个图形面积;

  • 再次所有图形的面积总和。

源码如下:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
if(a < 0 || b < 0 || c < 0) System.out.println("Wrong Format");
else {
Circle [] cir = new Circle[a];
Rectangle [] rec = new Rectangle[b];
Triangle [] tri = new Triangle[c];
ArrayList <Shape> list = new ArrayList<Shape>();
for(int i = 0; i < a; i ++) {
cir [i] = new Circle(input.nextDouble());
if(cir[i].validate() == false) {
System.out.println("Wrong Format");
System.exit(0);
}
list.add(cir[i]);
}
for(int i = 0; i < b ; i ++) {
rec[i] = new Rectangle(input.nextDouble(),input.nextDouble());
if(rec[i].validate() == false) {
System.out.println("Wrong Format");
System.exit(0);
}
list.add(rec[i]); }
for(int i = 0; i < c; i ++) {
tri[i] = new Triangle(input.nextDouble(),input.nextDouble(),input.nextDouble());
if(tri[i].validate() == false) {
System.out.println("Wrong Format");
System.exit(0);
}
list.add(tri[i]);
} double sum = 0;
System.out.println("Original area:");
for(int i =0; i < a+b+c; i ++) {
System.out.printf(String.format("%.2f", list.get(i).getArea()).toString()+" ");
sum = sum + list.get(i).getArea();
} System.out.println("\n"+"Sum of area:"+String.format("%.2f", sum).toString());
for(int i = 0; i < a+b+c - 1; i++)
for(int j = 0; j < a + b + c - 1 - i;j ++) {
if(list.get(j).getArea() > list.get(j+1).getArea())
Collections.swap(list, j, j+1);
}
System.out.println("Sorted area:");
for(int i =0; i < a+b+c; i ++) {
System.out.print(String.format("%.2f", list.get(i).getArea()).toString()+" "); }
System.out.print("\n"+"Sum of area:"+String.format("%.2f", sum).toString());
} //System.out.println(String.format("%.2f", r.getArea()).toString());
}
} abstract class Shape {
public abstract double getArea();
public abstract boolean validate();
//@Override
/*public String toString() {
return "Shape [getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString()
+ "]";
} */
} class Circle extends Shape{
private double radius; public Circle() {
super();
} public Circle(double radius) {
super();
this.radius = radius;
}
public double getArea() {
return Math.PI*this.radius * this.radius ;
}
public boolean validate() {
if(this.radius <= 0)
return false;
else
return true;
} } class Rectangle extends Shape{
private double width;
private double length;
public Rectangle() {
super();
// TODO 自动生成的构造函数存根
}
public Rectangle(double width, double length) {
super();
this.width = width;
this.length = length;
}
public double getArea() {
return this.length *this.width;
}
public boolean validate() {
if(this.length > 0 && this.width > 0)
return true;
else
return false;
}
} class Triangle extends Shape{
private double side1;
private double side2;
private double side3;
public Triangle() {
super();
// TODO 自动生成的构造函数存根
}
public Triangle(double side1, double side2, double side3) {
super();
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
} public double getArea() {
double s = (this.side1 + this.side2 + this.side3)/2.0;
return Math.sqrt(s * (s - this.side1) * (s - this.side2) * (s - this.side3));
}
public boolean validate() {
if(this.side1 > 0 && this.side2 > 0 && this.side3 > 0) {
if(this.side1 + this.side2 > this.side3 && this.side1 + this.side3 > this.side2 && this.side2 + this.side3 > this.side1)
return true;
else
return false; }
else
return false;
}
}

SourceMonitor的生成报表内容:

类图如下:

题目集06(7-6):

编写程序,使用接口及类实现多态性。

其中:

  • GetArea为一个接口,无属性,只有一个GetArea(求面积)的抽象方法;
  • Circle及Rectangle分别为圆类及矩形类,分别实现GetArea接口
  • 要求:在Main类的主方法中分别定义一个圆类对象及矩形类对象(其属性值由键盘输入),使用接口的引用分别调用圆类对象及矩形类对象的求面积的方法,直接输出两个图形的面积值。(要求只保留两位小数)

输入格式:

从键盘分别输入圆的半径值及矩形的宽、长的值,用空格分开。

输出格式:

  • 如果输入的圆的半径值及矩形的宽、长的值非法(≤0),则输出Wrong Format
  • 如果输入合法,则分别输出圆的面积和矩形的面积值(各占一行),保留两位小数。

源码如下:

import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double x = input.nextDouble();
Circle c = new Circle(x);
double y = input.nextDouble();
double z = input.nextDouble();
Rectangle r = new Rectangle(y,z);
if(x <= 0 || y <= 0 || z <= 0) {
System.out.println("Wrong Format");
System.exit(0);
} System.out.println(String.format("%.2f", c.getArea()).toString());
System.out.println(String.format("%.2f", r.getArea()).toString());
}
} class Circle implements GetArea{
private double radius; public Circle() {
super(); } public Circle(double radius) {
super();
this.radius = radius;
} public double getRadius() {
return radius;
} public void setRadius(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI*this.radius*this.radius;
} }
class Rectangle implements GetArea{
private double width;
private double length;
public Rectangle() {
super(); }
public Rectangle(double width, double length) {
super();
this.width = width;
this.length = 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 this.length*this.width;
}
}
interface GetArea{
public double getArea();
}

SourceMonitor的生成报表内容:

类图如下:

这三道题都是继承类题目,第一题要求我们求面积等,首先第一题有一个总的Shape类,一个图形类,图形可以包括很多种,它的子类包含了一个Circle类和Rectangle类,这里的类都不是抽象类,是普通类,Shape类有一个getArea的方法,然后在Circle和Rectangle类中重写了求面积的方法,在这个基础上,又写了两个类Ball和Box分别继承了Circle类和Rectangle类,Ball类和Box类中又重写了求面积的方法,又增加了求体积的方法,每个类中都会输出自己的类名,就是一旦跟这个类有关就会输出这个类的类名,我觉得这道题更多是想帮助我们去理解继承这个东西,理解继承的好处,子类可以调用父类的方法,所以从而可以简单的实现共有的一些功能,可以减少代码量,如果子类的计算方法和父类不一样,还可以重写其方法来达到目的,继承是Java中很重要的一个内容,我们想实现一些东西,在不改变自身代码的情况下,我们可以给之前的代码加一个父类,就像老师实验课所讲的例子,如果我想给空调加一个做饭的功能而且不能对源码做出改变,应该怎么做,我想这时候就可以给它加一个父类来实现一些功能。

第二题也是要求我们求图形的面积,根据图形面积对图形对象进行排序并输出,第二题,这道题采用了抽象类定义,实体类构建的一个方式,我们首先有一个Shape类,但这个类与上一题不同的是,这是一个抽象类,拥有抽象的方法,求面积和数据校验,然后它有三个子类分别是,circle类rectangle类和triangle类分别,三个类继承于shape类,重写了求面积和数据校验的方法,我在主函数中分别创建三个类的对象然后将他们放入shape类型的list中,求面积的时候直接调用shape的getArea的方法就可以了,然后再根据面积对list中的对象排序,最后再输出面积。我认为这道题主要是想让我们更深刻的理解继承以及去理解多态,我们求面积只需要调用shape的求面积方法,就可以直接求面积,不需要调用谁谁的求面积去求谁谁的面积,就像老师上课举得交学费的例子,学校说交学费,只需要说一句交学费,人人就都知道自己交多少学费,学校不用去和每个学院的人说你们交多少多少学费,这就是多态的好处,继承和多态提高了代码的可维护性和可延展性。

第三题还是让我们求面积,不过第三题与第一题和第二题都不同的是第三题让我们使用接口来实现多态,首先我们创建了一个求面积的接口,接口中有一个求面积的的方法,然后创建了俩个类一个是circle类一个是rectangle类,这两个类都与接口相连接,这两个类中都有求面积的方法,然后我们在主函数中创建两个对象,声明类型为接口,实际类型还是circle和rectangle,然后调用接口求面积的方法就可以求出面积。这道题主要的目的应该是想让了解接口,然后用接口以及类实现多态。

三道题核心的内容其实基本都相似,都是想让我们理解多态,通过继承通过接口来理解多态,多态就是同一操作作用于不同的对象,可以有不同的解释,产生不同的执行结果,我们知道,封装可以隐藏实现细节,使得代码模块化,继承可以扩展已存在的代码模块,他们都是为了代码的复用,而多态除了代码的复用性外,还可以解决项目中紧偶合的问题,提高程序的可扩展性.。

(3)对三次题目集中用到的正则表达式技术的分析总结

三次题目集中,题目集04(7-1)题目集05(7-4),题目集06(1、2、4)都是正则表达式的练习。

正则表达式常用的知识点总结:

.     匹配除换行符以外的任意字符

\w  匹配字母或数字或下划线

\s  匹配任意的空白符

\d  匹配数字

\b  匹配单词的开始或结束

^  匹配字符串的开始

$  匹配字符串的结束

*  重复零次或更多次

+  重复一次或更多次

?  重复零次或一次

n  重复n次

{n,}  重复n次或更多次

{n,m}  重复n到m次

\W  匹配任意不是字母,数字,下划线,汉字的字符

\S  匹配任意不是空白符的字符

\D  匹配任意非数字的字符

\B  匹配不是单词开头或结束的位置

[^x]  匹配除了x以外的任意字符

[^asdf]  匹配除了asdf这几个字母以外的任意字符

  • Pattern 类:

    pattern 对象是一个正则表达式的编译表示。Pattern 类没有公共构造方法。要创建一个 Pattern 对象,你必须首先调用其公共静态编译方法,它返回一个 Pattern 对象。该方法接受一个正则表达式作为它的第一个参数。

  • Matcher 类:

    Matcher 对象是对输入字符串进行解释和匹配操作的引擎。与Pattern 类一样,Matcher 也没有公共构造方法。你需要调用 Pattern 对象的 matcher 方法来获得一个 Matcher 对象。

public boolean lookingAt()
 尝试将从区域开头开始的输入序列与该模式匹配。

public boolean find()
尝试查找与该模式匹配的输入序列的下一个子序列。

public boolean find(int start)
重置此匹配器,然后尝试查找匹配该模式、从指定索引开始的输入序列的下一个子序列。

public boolean matches()
尝试将整个区域与模式匹配。

public String replaceAll(String replacement)
 替换模式与给定替换字符串相匹配的输入序列的每个子序列。

public String replaceFirst(String replacement)
 替换模式与给定替换字符串匹配的输入序列的第一个子序列。

以上是一些最常用的正则表达式的知识点。题目集04(7-1)是水文数据校验及处理,该题让我们使用Java中的字符串处理类以及正则表达式对输入字符串数据进行合法性校验及计算,主要考察的就是我们利用正则表达式去匹配和操作,然后还考察了我们对字符串的处理,题目集05(7-4)统计Java程序中关键词的出现次数,该题考察点和上题考查的内容相似,但是第五题考察的非法情况以及注意事项更多一些,所以我认为难度还略大于上一题,前两道题目比较综合,后面的三道小题就比较简单了,考察的都是一些基础知识点。

总的来说,正则表达式可以使我们的代码简单,我们用起来非常的方便,如果没有正则表达式,我们可能要用for循环一个一个去判断,所以正则表达式是帮助我们去操作字符串的一个好的工具。

(4)题目集05(7-4)中Java集合框架应用的分析总结

题目集05(7-4)统计Java程序中关键词的出现次数

编写程序统计一个输入的Java源码中关键字(区分大小写)出现的次数。说明如下:

  • Java中共有53个关键字(自行百度)
  • 从键盘输入一段源码,统计这段源码中出现的关键字的数量
  • 注释中出现的关键字不用统计
  • 字符串中出现的关键字不用统计
  • 统计出的关键字及数量按照关键字升序进行排序输出
  • 未输入源码则认为输入非法

输入格式:

输入Java源码字符串,可以一行或多行,以exit行作为结束标志

输出格式:

  • 当未输入源码时,程序输出Wrong Format
  • 当没有统计数据时,输出为空
  • 当有统计数据时,关键字按照升序排列,每行输出一个关键字及数量,格式为数量\t关键字

这道题让我们匹配关键字,用到了正则表达式以及对字符串的操作和处理,题目还要求题目必须使用List、Set或Map中一种或多种,如完全未使用如上接口,是一道综合性题目,由于本题难度较大,改题目并没有完全完成。所以对该道题目的分析可能不多。

对于这道题目,刚开始看到这道题的时候感觉难度还行,有一些大致的思路,先输入字符串,然后用正则表达式匹配,统计然后输出,但下手的时候发现不太能下手,由于题目有很多细节的需求,以及我自己正则表达式和对字符串的应用不是很熟,所以这道题目并没有完全做完。

3.踩坑心得

(1)第一道日期题目对于月份最大值的处理一开始用了switch和case,列举了12个月,到最后发现了mon_maxnum这个数组都没有用上,用case的时候不仅容易错,而且非常复杂,在第二个代码中就把返回月份最大值,直接用一个简单的方法返回了,下次做题的时候应该考虑清楚类图中的一些属性都有什么用,该怎么使用,然后再进行写代码的工作,这样可能会节省很多时间。

(2)第一道日期类的题目,在类与类之间传输数据的时候,数据对应错了,在Day这个类中,我们创建了year和month的对象作为day这个类的属性,然后写了一个函数public Day(int yearValue,int monthValue,int dayValue),将数据传到一起,但是写完之后发现检验输入数据是否合法的时候一直输出wrong format,后来才发现对应的日期传错了,把年传到了日,所以导致一开始总是找不到错误在哪里,耽误了很长的时间,其实跟那个类图也有关系,跟自己也有关系,类图那个年月日顺序就错了,也还是自己没有特别仔细地阅读题目。

(3)日期类的第二题和第一题有很多不同,因为年月日的类很简单,所以在日期处理的类中,包括了一些核心算法,而这个核心算法的一些顺序很重要,还是逻辑的问题,month.monthReduction();setDayMax();就比如这两个代码的先后顺序就很重要,如果按照自己的逻辑,它们两个谁先谁后都无所谓,但是如果把这两个代码位置换了的话,就会导致闰年2月到三月或者二月到1月出现问题,日期就会提前引用二月的日期,比如一月31日,但是运行后成了1月29日,就有一些逻辑错误,这个也改了一些时间。

(4)题目集06(7-5),图形继承的题,检测输入数据合法性,一开始不管输入什么数据,输出都是wrong format,但是自己去看自己的代码觉得没有问题,然后到最后最后还是发现了一些逻辑错误,就是检验输入数据合法性,虽然都是判断,但是先正常进行,再输出wrong format 和先输wrong format 也是不一样的,所以要多注意这方面的逻辑,看看什么样的代码适合那种逻辑。

4.改进建议

(1)对于第一个日期题,我认为应该多保证类的单一性原则,所以就不应该每个类都聚合,应该把day,month,year类单独设计出来,之间没有关联,就向着第二道日期题改进即可。

(2)对于题目集04第三题,继承类题目,我认为可以写一个操作类,然后把求面积的方法都放在这一个类中,然后让这个类继承这个操作类,以后还可以加求体积等方法,很方便。

5.总结

(1)首先呢,学到了一些正则表达式的知识点以及应用 还需要多加练习,更熟悉的掌握正则表达式的知识点以及用法。

(2)其次,学习到了类与类之间的关系,我认为这次最重要的就是明白了很多类与类之间的关系,比如关联,聚合,继承。

(3)我觉得这次的题目集,我对于继承有了更深的理解,因为理解了继承的好处,明白了它有什么用处,所以更好了理解了为什么要用继承,继承怎样使用。

(4)对于类的设计,整个代码的设计,以及类与类之间的关系还需要进一步学习,需要通过练习来进一步掌握知识。

(5)对于老师讲课的话没有很多意见,就是希望老师可以多带着我们写一些代码,感觉这样效果会好一些。

Blog总结02(4~6次作业总结)的更多相关文章

  1. Blog总结(前三次作业总结)

    前三次作业总结 1.前言 (1)第一次题目集共有8道题目,难度较为简单,知识点为JAVA的一些编程基础知识点,如输入输出,选择,循环,一维数组等. (2)第二次题目集共有5道题目,难度较第一次题目集有 ...

  2. mac 系统通用快捷键(mac 下的应用多数会往这些标准看齐)(转:http://yang3wei.github.io/blog/2013/02/08/chen-ni-yu-mac-chen-ni-yu-xcode/)

    command + w: 关闭当前窗口 command + q: 退出程序 (Google Chrome 有点奇葩,按下之后还需要hold 那么一小会儿才能退出) command + m: 最小化当前 ...

  3. Objective-C的对象模型 http://foredoomed.org/blog/2014/02/24/object-modeling-of-objective-c/

    Objective-C是一门面向对象,并且在C的基础上加入了Smalltalk式的消息机制而形成的编程语言,它主要被苹果公司用于开发Mac OS X和iOS操作系统.既然Objective-C是面向对 ...

  4. 第三周作业-课本&&视频学习

    <网络攻防技术与实践>第三周作业 Part I 寻找自己留在互联网上的足迹并消除隐私 1.1 google/baidu搜索自己的qq号 搜索结果如图,搜到的有用信息其实就是图上这么几条,能 ...

  5. 集大1513 & 1514班 软件工程第一次作业评分与点评

    谢谢大多数同学按时完成了作业,同学态度都比较端正,没有为了完成作业或者讨好老师而说一些假话空话. 很多同学选择CS之前并没有从兴趣或者擅长出发.这是一个普遍的现象,十年前我们是这样,十年后的孩子们还是 ...

  6. Samsung_tiny4412(驱动笔记02)----ASM with C,MMU,Exception,GIC

    /**************************************************************************** * * ASM with C,MMU,Exc ...

  7. Blog

    http://www.cnblogs.com/digdeep/archive/2015/11/16/4968453.htmlhttp://it.dataguru.cn/article-8406-1.h ...

  8. 20181009-3 选题 Scrum立会报告+燃尽图 02

    Scrum立会报告+燃尽图(02)选题 此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2191 一.小组介绍 组长:刘莹莹 ...

  9. C语言I作业12-学期总结

    一.我学到的内容 二.我的收获 我完成的作业: 第一次作业 C语言I博客作业02 C语言I作业004 C语言I博客作业05 C语言I博客作业06 C语言I博客作业07 C语言I博客作业08 C语言I博 ...

随机推荐

  1. ClickHouse源码笔记4:FilterBlockInputStream, 探寻where,having的实现

    书接上文,本篇继续分享ClickHouse源码中一个重要的流,FilterBlockInputStream的实现,重点在于分析Clickhouse是如何在执行引擎实现向量化的Filter操作符,而利用 ...

  2. 【开源】.net微服务开发引擎Anno 让复杂的事简单点- 日志、链路追踪一目了然 (上)

    1.Anno简介? Anno是一个微服务框架引擎.入门简单.安全.稳定.高可用.全平台可视化监控.依赖第三方框架少.详情请查看<[开源].net微服务开发引擎Anno开源啦> 本章主题:. ...

  3. Microsoft Teams 最新功能发布:协作篇

    正在进行的2021年的Microsoft Ignite大会,发布了一系列跟Microsoft Teams相关的新功能,英文介绍请参考 https://techcommunity.microsoft.c ...

  4. HDFS的上传流程以及windows-idea操作文件上传的注意

    HDFS的上传流程 命令:hdfs dfs -put xxx.wmv /hdfs的文件夹 cd进入到要上传文件的当前目录,再输入hdfs命令上传,注意-put后tab可以自动补全, 最后加上你要上传到 ...

  5. Codeforces Round #546 C. Nastya Is Transposing Matrices

    题面: 传送门 题目描述: 给出两个n x m的矩阵A,B.矩阵A可以把正方子矩阵进行"转置操作",问:可不可以对矩阵A进行多次这样的操作,使矩阵A变为矩阵B?   题目分析: 这 ...

  6. ch2_8_2求解幸运数问题

    思路:f(x)进行十进制每位相加,g(x)进行二进制每位相加,比较是否相等即可. 小明同学学习了不同的进制之后,拿起了一些数字做起了游戏.小明同学知道,在日常生活中我们最常用的是十进制数,而在计算机中 ...

  7. Web实验报告

  8. frp穿透内网使用vsftpd服务

    本篇文章将会介绍如何使用frp穿透内网以及如何在centos8环境下安装和使用vsftpd,最后在公网通过frp穿透内网使用ftp. 一.内网穿透神器frp frp 是一个专注于内网穿透的高性能的反向 ...

  9. WebGPU[1] 三角形

    代码见: https://github.com/onsummer/my-dev-notes/tree/master/webgpu-Notes/01-triangle 如果本篇的代码不能跑了,请联系我或 ...

  10. 手机浏览器通过Scheme跳转APP,兼容各种手机浏览器

    一个比较完整的产品线,必定有APP和网站,另外还有微信公众号网页和小程序.那么有一个比较常见的需求就是在手机浏览器内打开APP,实现起来也比较简单,只要APP配置的有URLScheme即可. 但是因为 ...