2021年第二次blog总结

一、前言

经过第二个月学习,已经了进一步了解到面向对象的程序设计的一些特点与方法。下面将总结三次习题集的一些要点。

 1.习题集四 总共3题:

  第一题考查正则表达式对各个字符串处理;第二题为日期类的类间关系的组合;第三题为图形类的继承。

 2.习题集五 总共5题:

  第一题为字符串处理;第二,三题为数组数据处理;第四题考查正则表达式对一段字符串源码的处理;第五题为上次日期类新类间关系的设计——聚合。

 3.习题集六 总共6题:

   1-4题考查正则表达式对字符串处理;第五题为图形继承与多态 ;第六题为实现图形接口及多态性。

二、设计与分析

1.聚合与组合

      组合的类图

 

       聚合的类图

 

 

二者复杂度分析对比如下:

由于二者输出格式不同,main类中Max Complexity 的细微差距可以不考虑。

二者Day类差距在于;前者组合结构中,Day类需要获取Month的对象,后者不需要并且不需要在Day类写数据校验的方法。

组合Day类源码

 1 public class Day {
 2 
 3     private int value = 0;
 4     private Month month ;
 5     public int []mon_maxmun = {31,28,31,30,31,30,31,31,30,31,30,31};
 6     public Day() {
 7         super();
 8         // TODO Auto-generated constructor stub
 9     }
10     public Day(int yearValue,int monthValue,int dayValue) {
11         super();
12         this.value = dayValue;
13         month = new Month(yearValue,monthValue);
14         
15     }
16     public int getValue() {
17         return value;
18     }
19     public void setValue(int value) {
20         this.value = value;
21     }
22     public Month getMonth() {
23         return month;
24     }
25     public void setMonth(Month value) {
26         this.month = value;
27     }
28     
29     public  void resetMin() {
30         setValue(1);
31     }
32     
33     public  void resetMax() {
34         setValue(mon_maxmun[getMonth().getValue()-2]);
35     }
36     
37     public boolean validate() {
38         boolean x=true;
39         if(month.getYear().isLeapYear(month.getYear().getValue())) {
40             mon_maxmun[1] = 29;
41         }
42         if(getValue()>mon_maxmun[getMonth().getValue()-1]||getValue()<1) {
43             x=false;
44         }
45         return x;
46     }
47     
48     public void dayIncrement() {
49          setValue(getValue()+1);
50     }
51     
52     public void dayReduction() {
53          setValue(getValue()-1);
54     }
55     
56     
57 }

聚合Day类源码

 1 public class Day {
 2 
 3     private int value = 0;
 4     
 5     public Day() {
 6         super();
 7         // TODO Auto-generated constructor stub
 8     }
 9     
10     
11     public Day(int value) {
12         super();
13         this.value = value;
14     }
15 
16 
17     public int getValue() {
18         return value;
19     }
20     public void setValue(int value) {
21         this.value = value;
22     }
23     
24     public void dayIncrement() {
25          setValue(getValue()+1);
26     }
27     
28     public void dayReduction() {
29          setValue(getValue()-1);
30     }
31     
32     
33 }

Month类和Year类基本同理

组合的优点

  组合使得类更易于使用,因为他们是自成一体的。使用这些类的代码不必担心会将其传递给其他需要它的对象,在复杂的应用程序中,这经常会变得非常棘手以至于需要我们开发一个设计方案。组合的另一个好处是,查看使用它的代码时,可以准确地知道哪个类能够访问已组合的对象。

聚合的优点

聚合与组合相比具有管理负担较低的优势,因为一个对象可以被其他多个对象所共享。当各个类数据量大时,组合将各个类直接连接在一起,对数据承载量加大。

2.图形继承设计的思路与技术运用(封装、继承、多态、接口等)

本系列都是关于图形设计。

首先为继承的训练

shape类中有一个求面积的抽象方法,两个基本二维图形(圆形,矩形)作为子类继承shape的求面积方法,紧接着两个三维立体图形(立方体,球)分别作为矩形与圆形的子类并具有获得体积的新方法

 

其次进行多态的迭代

shape类中的求面积和校验数据抽象方法在矩形,三角形,圆中方法都不同,在各个子类中对其进行Override;

 

通过GetArea的接口来实现求面积的功能,后续矩形类和圆类实现了这个接口,实现了这个接口代表的求面积的功能。

3.正则表达式技术

(1)校验键盘输入的 QQ 号是否合格,判定合格的条件如下:

  • 要求必须是 5-15 位;
  • 0 不能开头;
  • 必须都是数字; 
 1 import java.util.Scanner;
 2 import java.util.regex.Matcher;
 3 import java.util.regex.Pattern;
 4 
 5 public class Main {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         String QQnumber = new String();
10         Scanner sc = new Scanner(System.in);
11         QQnumber = sc.nextLine();
12         Pattern p = Pattern.compile("^\\d{5,15}$");
13         Matcher m = p.matcher(QQnumber);
14         boolean b = m.matches();
15         if(QQnumber.charAt(0)=='0'||b==false) {
16             System.out.println("你输入的QQ号验证失败");
17             System.exit(0);
18         }
19         System.out.println("你输入的QQ号验证成功");
20         
21 
22     }
23 
24 }

    "^\\d{5,15}$"通过其匹配5-15位数字,最后再检验首位数字

(2)接受给定的字符串,判断该字符串是否属于验证码。验证码是由四位数字或者字母(包含大小写)组成的字符串。

 1 import java.util.Scanner;
 2 import java.util.regex.Matcher;
 3 import java.util.regex.Pattern;
 4 
 5 public class Main {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         String YZM = new String();
10         Scanner sc = new Scanner(System.in);
11         YZM = sc.nextLine();
12         Pattern p = Pattern.compile("^[0-9A-Za-z]{4}$");
13         Matcher m = p.matcher(YZM);
14         boolean b = m.matches();
15         if(b==false) {
16             System.out.println(YZM+"不属于验证码");
17             System.exit(0);
18         }
19         System.out.println(YZM+"属于验证码");
20         
21 
22     }
23 
24 }
[0-9A-Za-z]先通过它确定每位位数字或字母,
{4}在确定位4位数

(3)

对软件学院2020级同学学号进行校验,学号共八位,规则如下:

  • 1、2位:入学年份后两位,例如20年
  • 3、4位:学院代码,软件学院代码为20
  • 5位:方向代码,例如1为软件工程,7为物联网
  • 6位:班级序号
  • 7、8位:学号(序号)

要求如下:

  • 只针对2020级
  • 其中软件工程专业班级分别为:202011~17、61,物联网工程专业班级为202071~202073,数据科学与大数据专业班级为202081~82
  • 每个班级学号后两位为01~40
 1 import java.util.Scanner;
 2 import java.util.regex.Matcher;
 3 import java.util.regex.Pattern;
 4 
 5 public class Main {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         String XH = new String();
10         Scanner sc = new Scanner(System.in);
11         XH = sc.nextLine();
12          Pattern p = Pattern.compile("^\\d{5,15}$");
13         Matcher t = p.matcher(XH);
14          boolean r = t.matches();
15         if(XH.length()!=8||r==false){
16            System.out.println("错误");
17             System.exit(0); 
18         }
19         String direction = XH.substring(4, 6);
20         String classNumber = XH.substring(6, 8);
21         int number =Integer.parseInt(classNumber);
22        
23         Pattern a = Pattern.compile("[1][1-7]");
24         Pattern b = Pattern.compile("[6][1]");
25         Pattern c = Pattern.compile("[78][12]");
26         Matcher x = a.matcher(direction);
27         Matcher y = b.matcher(direction);
28         Matcher z = c.matcher(direction);
29         
30         boolean q = x.matches();
31         boolean w = y.matches();
32         boolean e = z.matches();
33        
34         boolean m = (q==false&&w==false&&e==false);
35         
36         if(Integer.parseInt(XH.substring(0,4))!=2020||number>40||number<1||m) {
37             System.out.println("错误");
38 
39             System.exit(0);
40         }
41         
42         System.out.println("正确");
43 
44     }
45 
46 }

将学号字符串进行分割,前四位规定,后两位转化成整形数据进行判断,在创建a,b,c三个Pattern对象分别匹配各个方向的各个八级,只要三者都不匹配就算错误。

(4)

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

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

 

这里主要处理字符串及注释

源码中的字符串

1 replaceAll("\\\".*?\\\"", "")

首先匹配两端的引号,其次中间位任意个数的任意字符

 

源码中的两种注释

//类型

1 replaceAll("//.*", "")

/* */类型

 1  for(int i = 0;i<theList.size();i++) {
 2              String temp = theList.get(i);
 3               if(temp.indexOf("/*")!=-1) {
 4                  top.add(i);
 5              }
 6               if(temp.indexOf("*/")!=-1) {
 7                   low.add(i);
 8               }
 9          }
10      for(int i = 0;i<top.size();i++) {//把每次/* */这样的注释删除
11              theList.set(top.get(i), theList.get(top.get(i)).replaceAll("/\\*.*", ""));//把/*及后面本行东西删除
12              for(int j =top.get(i)+1;j<=low.get(i);j++) {
13                  theList.set(j,"");//将/*这一行直到*/这一行删除
14              }
15          }

第一个循环确定/* 和*/的位置

第二个循环进行删除

 

4.Java集合框架应用

此次在源码关键字查找中运用了List中的LinkedList来存储源码中的每一行。

1 List<String> list = new LinkedList<String>();
2         String value = sc.nextLine();
3         while(value.equals(exit)==false){
4             list.add(value);
5             value = sc.nextLine();
6         }

这里不使用ArrayList

因为

ArrayList是顺序结构,所以定位很快,但插入,删除数据慢。
LinkedList 是链表结构,定位慢,但插入,删除数据快。

此次要对字符串源码处理,所有选择LinkedList

三、采坑心得

 1.聚合和组合

其二者的优缺点在分析中以指出

组合在设计好后,可以非常方便使用。当相比聚合来说,管理负担较高。就比如在查错时,组合方式需要检查每一个类,聚合不需要;

二者Dateuntil分析图如下:

 

2.  Java集合框架应用

在关键字寻找中,先处理完字符串源码后,原来是先匹配关键字在排序。但是List的链式存储不能够进行如数组一样排序,导致输出不能按格式输出。

最后不得创建一个Keyword类,将关键字先按顺序存储,再反复调用这个类,使排序后的每一个关键字先匹配一遍源码

 1 class Keyword {
 2 
 3     private String[] keyword = {
 4             "abstract", "assert",
 5 
 6             "boolean", "break", "byte",
 7 
 8             "case", "catch", "char", "class", "const", "continue",
 9 
10             "default", "do", "double",
11 
12             "else", "enum", "extends",
13 
14             "false","final", "finally", "float", "for",
15 
16             "goto",
17 
18             "if","implements", "import", "instanceof", "int", "interface",
19 
20             "long",
21 
22             "native", "new","null",
23 
24             "package", "private", "protected", "public",
25 
26             "return",
27 
28             "short", "static", "strictfp", "super", "switch", "synchronized",
29 
30             "this", "throw", "throws", "transient", "true","try",
31 
32             "void", "volatile",
33 
34             "while"
35             
36     };
37 
38     
39 
40     public String[] getValue() {
41         return keyword;
42     }
43 
44     public Keyword() {
45         super();
46         // TODO Auto-generated constructor stub
47     }
48 }

匹配关键字

 1 public void checkSource() {
 2         List<String> endList= separateList();    
 3         int x=0;
 4         for(int i =0;i<53;i++) {
 5             int n = 0;
 6             for(int j =0;j<endList.size();j++) {
 7                 if(getKeyword().getValue()[i].equals(endList.get(j))) {//判断没执行
 8                     n++;
 9                 }
10             }
11             if(n!=0) {
12                 System.out.println(n+"    "+getKeyword().getValue()[i]);
13             }            
14         }
15     }

四、改进建议:

主要是正则表达式

就像学号那一题,在不同方向不同班级的匹配,我是通过分开匹配,如果后续有新方向,可以在创建新Pattern对象匹配。

其次在关键字一题中,我将字符串源码分裂成每一行进行存储,后续可以通过StringBuffer或 StringBuilder来减少代码的最高复杂度

如图:

五、总结:

1.熟悉List的优缺点:定位查找快,但不能排序。

2.熟悉正则表达式

3.了解组合与聚合:部件的生命周期不同,管理负担不同

4.封装的目的:增强安全性和简化编程,使用者不必了解具体的实现细节,而只是要通过外部接口,一特定的访问权限来使用类的成员。

5.继承的目的:实现代码的复用。

6.多态的含义:相同的事物,调用其相同的方法,参数也相同时,但表现的行为却不同。

六、源码附录

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

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

 

图形继承

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

统计Java程序中关键词的出现次数

  1 import java.util.LinkedList;
  2 import java.util.List;
  3 import java.util.Scanner;
  4 import java.util.regex.Matcher;
  5 import java.util.regex.Pattern;
  6 
  7 public class Main {
  8 
  9     public static void main(String[] args) {
 10         // TODO Auto-generated method stub
 11 
 12         Scanner sc = new Scanner(System.in);
 13         String exit = "exit";
 14         List<String> list = new LinkedList<String>();
 15         String value = sc.nextLine();
 16         while(value.equals(exit)==false){
 17             list.add(value);
 18             value = sc.nextLine();
 19 //            System.out.println("ok");
 20         }
 21         List<String> theList = new LinkedList<String>();
 22         Source source = new Source();
 23         
 24         source.setList(list);
 25         if(source.checkTF(list)==false) {
 26             System.out.println("Wrong Format");
 27             System.exit(0);
 28         }
 29         source.checkSource();
 30 //        String pattern ="";
 31 //        Pattern r = Pattern.compile(pattern);
 32 //        Matcher m = r.matcher(list.get(0));
 33 //        Matcher p = r.matcher(list.get(1));
 34 //        boolean a = m.matches();
 35 //        boolean b = p.matches();
 36 //        System.out.println(list.get(0));
 37 //        System.out.println(list.get(1));
 38 //        System.out.println(a);
 39 //        System.out.println(b);
 40         
 41 //        theList=source.clearSource();
 42 //        theList.set(0, theList.get(0).replaceAll("//.*", ""));
 43 //        for(int i = 0;i<theList.size();i++) {
 44 //            System.out.println(theList.get(i));
 45 //        }
 46         
 47 //        list.set(0, list.get(0).replaceAll("//.*", ""));
 48 //        System.out.println(list.get(0));
 49 //        System.out.println(list.size());
 50 //        String s ="213424/*saedfsdfdsf";
 51 //        s=s.replaceAll("/\\*.*", "");
 52 //        System.out.println(s+"ok");
 53     }
 54 
 55 }
 56 
 57 
 58 
 59  class Source {
 60 
 61     private List<String> list = new LinkedList<String>();
 62     private Keyword keyword = new Keyword();
 63 
 64     
 65 
 66     public Source(List<String> list) {
 67         super();
 68         this.list = list;
 69     }
 70 
 71     public Keyword getKeyword() {
 72         return keyword;
 73     }
 74 
 75 
 76     public Source() {
 77         super();
 78         // TODO Auto-generated constructor stub
 79     }
 80 
 81     public List<String> getList() {
 82         return list;
 83     }
 84 
 85     public void setList(List<String> list) {
 86         this.list = list;
 87     }
 88     
 89     public List clearSource() {
 90          List<String> theList = getList();
 91          for(int i = 0;i<theList.size();i++) {
 92              theList.set(i, theList.get(i).replaceAll("//.*", ""));//去除//类型注释
 93              theList.set(i,theList.get(i).replaceAll("\\\".*?\\\"", ""));//去除字符串
 94              
 95          }
 96 //         for(int i = 0;i<theList.size();i++) {
 97 //                System.out.println(theList.get(i));
 98 //            }
 99 //         System.out.println("ok");
100          List <Integer> top = new LinkedList<Integer>();
101          List <Integer> low = new LinkedList<Integer>();
102          for(int i = 0;i<theList.size();i++) {
103              String temp = theList.get(i);
104               if(temp.indexOf("/*")!=-1) {
105                  top.add(i);
106              }
107               if(temp.indexOf("*/")!=-1) {
108                   low.add(i);
109               }
110          }
111 //          for(int i = 0;i<top.size();i++) {//把每次/* */这样的注释删除
112 //              theList.set(top.get(i), theList.get(top.get(i)).replaceAll("/\\*.*", ""));//把/*及后面本行东西删除
113 //              for(int j =top.get(i)+1;j<=low.get(i);j++) {
114 //                  theList.set(j,"");//将/*这一行直到*/这一行删除
115 //              }
116 //          }
117          for(int i = 0;i<theList.size();i++) {//去除特殊符号
118               theList.set(i, theList.get(i).replaceAll("=","a"));
119              theList.set(i, theList.get(i).replaceAll("([^A-Za-z0-9])", " "));
120              theList.set(i, theList.get(i).replaceAll(" +"," "));//去除多余空格
121          }
122          return theList;
123     }
124     public List separateList() {
125         List<String> theList= clearSource();
126         List<String> endList= new LinkedList<String>();
127         int word_sum = 0;
128 //        String [][]split = null;
129         String [][]split = new String[theList.size()][];
130          for(int i = 0;i<theList.size();i++) {
131              split[i]=theList.get(i).split(" ");
132              word_sum+=split[i].length;
133          }
134          String []end = new String[word_sum];
135          
136          for(int i = 0;i<theList.size();i++) {
137              for(int j = 0;j<split[i].length;j++) {
138                   endList.add(split[i][j]) ;
139              }
140          }
141 //         String pattern ="";
142 //         Pattern r = Pattern.compile(pattern);
143 //         Matcher m = r.
144 //         endList.remove(r);
145         
146         return endList;
147      }
148     public void checkSource() {
149         List<String> endList= separateList();    
150         int x=0;
151         for(int i =0;i<53;i++) {
152             int n = 0;
153             for(int j =0;j<endList.size();j++) {
154                 if(getKeyword().getValue()[i].equals(endList.get(j))) {//判断没执行
155                     n++;
156                 }
157             }
158             if(n!=0) {
159                 System.out.println(n+"    "+getKeyword().getValue()[i]);
160             }            
161         }
162     }
163     public boolean checkTF(List theList) {
164         String pattern ="";
165         Pattern r = Pattern.compile(pattern);
166         boolean x = false ;
167         for(int i = 0;i<theList.size();i++) {
168             Matcher m = r.matcher(list.get(i));
169             if(m.matches() == false) {
170                 x =true;
171             }
172         }
173         return x;
174     }
175     }
176     
177     
178 
179 
180  class Keyword {
181 
182     private String[] keyword = {
183             "abstract", "assert",
184 
185             "boolean", "break", "byte",
186 
187             "case", "catch", "char", "class", "const", "continue",
188 
189             "default", "do", "double",
190 
191             "else", "enum", "extends",
192 
193             "false","final", "finally", "float", "for",
194 
195             "goto",
196 
197             "if","implements", "import", "instanceof", "int", "interface",
198 
199             "long",
200 
201             "native", "new","null",
202 
203             "package", "private", "protected", "public",
204 
205             "return",
206 
207             "short", "static", "strictfp", "super", "switch", "synchronized",
208 
209             "this", "throw", "throws", "transient", "true","try",
210 
211             "void", "volatile",
212 
213             "while"
214             
215     };
216 
217     
218 
219     public String[] getValue() {
220         return keyword;
221     }
222 
223     public Keyword() {
224         super();
225         // TODO Auto-generated constructor stub
226     }
227 }
View Code

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

  1 import java.util.Scanner;
  2 
  3 public class Main {
  4 
  5     public static void main(String[] args) {
  6         // TODO Auto-generated method stub
  7         Scanner sc = new Scanner(System.in);
  8         int choice = sc.nextInt();
  9         switch(choice) {
 10         case 1:{
 11             int year1 =sc.nextInt();
 12             int month1 =sc.nextInt();
 13             int day1 =sc.nextInt();
 14             int n = sc.nextInt();
 15             DateUtil date1 = new DateUtil(year1,month1,day1);
 16             
 17             if(date1.checkInputValidity()==false||n<0) {
 18                 System.out.println("Wrong Format");
 19                 System.exit(0);
 20             }
 21             System.out.print(date1.showDate());
 22             DateUtil date2= date2=date1.gerNextDays(n);
 23             System.out.println(" next "+n+" days is:"+date2.showDate());
 24             break;
 25         }
 26 
 27         case 2:{
 28             int year1 =sc.nextInt();
 29             int month1 =sc.nextInt();
 30             int day1 =sc.nextInt();
 31             int n = sc.nextInt();
 32             DateUtil date1 = new DateUtil(year1,month1,day1);
 33              
 34             if(date1.checkInputValidity()==false||n<0) {
 35                 System.out.println("Wrong Format");
 36                 System.exit(0);
 37             }
 38             System.out.print(date1.showDate());
 39             DateUtil date2=date1.gerPreviousDays(n);
 40             System.out.println(" previous "+n+" days is:"+date2.showDate());
 41             break;
 42         }
 43         
 44 case 3:{
 45             int year1 =sc.nextInt();
 46             int month1 =sc.nextInt();
 47             int day1 =sc.nextInt();
 48             int year2 =sc.nextInt();
 49             int month2 =sc.nextInt();
 50             int day2 =sc.nextInt();
 51             DateUtil date1 = new DateUtil(year1,month1,day1);
 52             DateUtil date2 = new DateUtil(year2,month2,day2);
 53             if(date1.checkInputValidity()==false||date2.checkInputValidity()==false) {
 54                 System.out.println("Wrong Format");
 55                 System.exit(0);
 56             }
 57             System.out.println("The days between "+date1.showDate()+" and "+date2.showDate()+" are:"+date1.getDaysofDates(date2));
 58             break;
 59         }
 60         
 61         default :{
 62             System.out.println("Wrong Format");
 63         }
 64         
 65         }
 66 
 67     }
 68 
 69 }
 70 
 71 
 72  class DateUtil {
 73 
 74     private Day day = new Day();
 75     private Month month = new Month();
 76     private Year year = new Year();
 77     private int []mon_maxmun = {31,28,31,30,31,30,31,31,30,31,30,31};
 78     public DateUtil() {
 79         super();
 80         // TODO Auto-generated constructor stub
 81     }
 82     public DateUtil(int y ,int m,int d) {
 83         super();
 84         day.setValue(d);
 85         month.setValue(m);
 86         year.setValue(y);
 87     }
 88     public Day getDay() {
 89         return day;
 90     }
 91     public void setDay(Day day) {
 92         this.day = day;
 93     }
 94     public Month getMonth() {
 95         return month;
 96     }
 97     public void setMonth(Month month) {
 98         this.month = month;
 99     }
100     public Year getYear() {
101         return year;
102     }
103     public void setYear(Year year) {
104         this.year = year;
105     }
106     
107     public boolean checkInputValidity() {
108         boolean x=false;
109         if(getYear().isLeapYear(getYear().getValue())) {
110             mon_maxmun[1] = 29;
111         }
112         if(getYear().validate()) {
113             if(getMonth().validate()) {
114                 if(getDay().getValue()<=mon_maxmun[getMonth().getValue()-1]&&getMonth().getValue()>=1) {
115                     x=true;
116                 }
117             }
118         }
119         return x;
120     }
121     
122     public String showDate() {
123          return getYear().getValue() + "-" + getMonth().getValue() + "-" + getDay().getValue();
124     }
125     
126     public boolean compareDates(DateUtil date) {//date大于day则true
127         boolean x=false;
128         if(date.getYear().getValue() > getYear().getValue()) {
129             x=true;
130         }
131         else if(date.getYear().getValue() == getYear().getValue()){
132             if(date.getMonth().getValue() > getMonth().getValue()) {
133                 x=true;
134             }
135             else if(date.getMonth().getValue() == getMonth().getValue()) {
136                 if(date.getDay().getValue() > getDay().getValue()) {
137                     x=true;
138                 }
139             }
140                 
141         }
142         return x;
143     }
144     public boolean equalTwoDates(DateUtil date) {
145         boolean x=false;
146         if(date.getDay().getValue() == getDay().getValue()&&date.getMonth().getValue() == getMonth().getValue()&&date.getYear().getValue() == getYear().getValue()) {
147             x=true;
148         }
149         return x;
150     }
151     
152     public void setDayMin() {
153         getDay().setValue(1);
154     }
155     public void setDayMax() {
156         if(getYear().isLeapYear(getYear().getValue())) {
157             mon_maxmun[1] = 29;
158         }
159         else {
160             mon_maxmun[1] = 28;
161         }
162         
163         getDay().setValue(mon_maxmun[getMonth().getValue()-2]);
164     }
165     
166     public DateUtil gerNextDays(int n) {
167          int year = 0;
168         
169         year += n / 146097 * 400;
170         n %= 146097;
171        
172         year += n / 36524 * 100;
173         n %= 36524;
174        
175 //        year += n / 1461 * 4;
176 //        n %= 1461;
177         
178        
179         getYear().setValue(getYear().getValue()+year);
180         
181        
182         for(int i=0;i<n;i++) {
183             if(getYear().isLeapYear(getYear().getValue())) {
184                 mon_maxmun[1]=29;
185             }
186             else {
187                 mon_maxmun[1]=28;
188             }
189             if(getDay().getValue()==mon_maxmun[getMonth().getValue()-1]) {
190                 if(getMonth().getValue()==12) {
191                     setDayMin();
192                     getMonth().reseMin();
193                     getYear().yearIncrement();
194                     
195                 }
196                 else {
197                     setDayMin();
198                     getMonth().monthIncrement();
199                 }
200             }
201             else {
202                 day.dayIncrement();
203             }
204         }
205         return new DateUtil(getYear().getValue(),getMonth().getValue(),getDay().getValue());
206         
207     }
208     
209     public DateUtil gerPreviousDays(int n) {
210         if(n>100000000) {
211         int year = 0;
212         year += n / 146097 * 400;
213         n %= 146097;
214         year += n / 36524 * 100;
215         n %= 36524;
216         year += n / 1461 * 4;
217         n %= 1461;
218         year += n / 365;
219         n %= 365;
220         n--;
221         getYear().setValue(getYear().getValue()-year);
222         }
223         for(int i=0;i<n;i++) {
224             if(getYear().isLeapYear(getYear().getValue())) {
225                 mon_maxmun[1]=29;
226             }
227             else {
228                 mon_maxmun[1]=28;
229             }
230             if(getDay().getValue()==1) {
231                 if(getMonth().getValue()==1) {
232                     getDay().setValue(31);
233                     getMonth().reseMax();
234                     getYear().yearReduction();
235                     
236                 }
237                 else {
238                     setDayMax();
239                     getMonth().monthReduction();
240                 }
241             }
242             else {
243                 getDay().dayReduction();
244             }
245         }
246         return new DateUtil(getYear().getValue(),getMonth().getValue(),getDay().getValue());
247         
248     }
249     
250     public int getDaysofDates(DateUtil date) {
251         int x = 0;
252         int y1d=0,y2d=0,m1d=0,m2d=0,d1d=0,d2d=0;
253         if(getYear().isLeapYear(getYear().getValue())) {
254             mon_maxmun[1]=29;
255         }
256         else {
257             mon_maxmun[1]=28;
258         }
259         if(date.getYear().isLeapYear(date.getYear().getValue())) {
260             date.mon_maxmun[1]=29;
261         }
262         else {
263             date.mon_maxmun[1]=28;
264         }
265         for(int i=1800;i<getYear().getValue();i++) {
266             if(getYear().isLeapYear(i)) {
267                 y1d+=366;
268             }
269             else {
270                 y1d+=365;
271             }
272         }
273         for(int i=1800;i<date.getYear().getValue();i++) {
274             if(getYear().isLeapYear(i)) {
275                 y2d+=366;
276             }
277             else {
278                 y2d+=365;
279             }
280         }
281         for(int i =1 ;i<getMonth().getValue();i++) {
282             m1d+=mon_maxmun[i-1];
283         }
284         for(int i =1 ;i<date.getMonth().getValue();i++) {
285             m2d+=date.mon_maxmun[i-1];
286         }
287         d1d=y1d+m1d+getDay().getValue();
288         d2d=y2d+m2d+date.getDay().getValue();
289         x=Math.abs(d1d-d2d);
290         return x;
291     }
292     
293     
294 }
295 
296 class Day {
297 
298     private int value = 0;
299     
300     public Day() {
301         super();
302         // TODO Auto-generated constructor stub
303     }
304     
305     
306     public Day(int value) {
307         super();
308         this.value = value;
309     }
310 
311 
312     public int getValue() {
313         return value;
314     }
315     public void setValue(int value) {
316         this.value = value;
317     }
318     
319     public void dayIncrement() {
320          setValue(getValue()+1);
321     }
322     
323     public void dayReduction() {
324          setValue(getValue()-1);
325     }
326     
327     
328 }
329 
330 class Month {
331 
332     private int value = 0;
333 
334     public Month() {
335         super();
336         // TODO Auto-generated constructor stub
337     }
338 
339     public Month(int value) {
340         super();
341         this.value = value;
342     }
343     public void reseMin() {
344         setValue(1);
345     }
346     
347     public void reseMax() {
348         setValue(12);
349     }
350     
351     public boolean validate() {
352         boolean x=true;
353         if(getValue()>12||getValue()<1) {
354             x=false;
355         }
356         return x;
357     }
358     
359     public void monthIncrement() {
360          setValue(getValue()+1);
361     }
362     
363     public void monthReduction() {
364          setValue(getValue()-1);
365     }
366 
367     public int getValue() {
368         return value;
369     }
370 
371     public void setValue(int value) {
372         this.value = value;
373     }
374     
375 }
376 
377 class Year {
378 
379     private int value = 0;
380 
381     public Year(int value) {
382         super();
383         this.value = value;
384     }
385 
386     public Year() {
387         super();
388         // TODO Auto-generated constructor stub
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(int year) {
400         // TODO Auto-generated method stub
401         boolean x=false;
402         
403         if (year % 4 == 0){
404             if (year % 100 == 0){
405                 if (year % 400 == 0){
406                     x=true;
407                 }
408                 
409             }
410             else{
411                 x=true;
412             }
413         }
414             
415         return x;
416         }
417     
418     public boolean validate() {
419         boolean x=true;
420         if(getValue()>2020||getValue()<1820) {
421             x=false;
422         }
423         return x;
424     }
425     
426     public void yearIncrement() {
427          setValue(getValue()+1);
428     }
429     
430     public void yearReduction() {
431          setValue(getValue()-1);
432     }
433 }
View Code

正则表达式训练-学号校验

 1 import java.util.Scanner;
 2 import java.util.regex.Matcher;
 3 import java.util.regex.Pattern;
 4 
 5 public class Main {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         String XH = new String();
10         Scanner sc = new Scanner(System.in);
11         XH = sc.nextLine();
12          Pattern p = Pattern.compile("^\\d{5,15}$");
13         Matcher t = p.matcher(XH);
14          boolean r = t.matches();
15         if(XH.length()!=8||r==false){
16            System.out.println("错误");
17             System.exit(0); 
18         }
19         String direction = XH.substring(4, 6);
20         String classNumber = XH.substring(6, 8);
21         int number =Integer.parseInt(classNumber);
22        
23         Pattern a = Pattern.compile("[1][1-7]");
24         Pattern b = Pattern.compile("[6][1]");
25         Pattern c = Pattern.compile("[78][12]");
26         Matcher x = a.matcher(direction);
27         Matcher y = b.matcher(direction);
28         Matcher z = c.matcher(direction);
29         
30         boolean q = x.matches();
31         boolean w = y.matches();
32         boolean e = z.matches();
33        
34         boolean m = (q==false&&w==false&&e==false);
35         
36         if(Integer.parseInt(XH.substring(0,4))!=2020||number>40||number<1||m) {
37             System.out.println("错误");
38 
39             System.exit(0);
40         }
41         
42         System.out.println("正确");
43 
44     }
45 
46 }
View Code

图形继承与多态

  1 import java.util.ArrayList;
  2 import java.util.Scanner;
  3 
  4 public class Main {
  5 
  6     public static void main(String[] args) {
  7         // TODO Auto-generated method stub
  8         Scanner sc = new Scanner(System.in);
  9         ArrayList<Shape> list = new ArrayList<Shape>();
 10         int a = sc.nextInt();
 11         int b = sc.nextInt();
 12         int c = sc.nextInt();
 13         if(a<0||b<0||c<0) {
 14             System.out.println("Wrong Format");
 15             System.exit(0);
 16         }
 17         for(int i = 0;i<a;i++) {
 18             double radius = sc.nextDouble();
 19             Circle circle = new Circle(radius);
 20             list.add(circle);
 21         }
 22         for(int i = 0;i<b;i++) {
 23             double width = sc.nextDouble();
 24             double length = sc.nextDouble();
 25             Rectangle rectangle = new Rectangle(width,length);
 26             list.add(rectangle);
 27         }
 28         for(int i = 0;i<c;i++) {
 29             double side1 = sc.nextDouble();
 30             double side2 = sc.nextDouble();
 31             double side3 = sc.nextDouble();
 32             Triangle triangle = new Triangle(side1,side2,side3);
 33             list.add(triangle);
 34         }
 35         
 36         
 37         for(int i = 0;i<a+b+c;i++) {
 38             if(list.get(i).validate()==false) {
 39                 System.out.println("Wrong Format");
 40                 System.exit(0);
 41             }
 42         }
 43         System.out.println("Original area:");
 44         for(int i = 0;i<a+b+c;i++) {
 45             System.out.print(list.get(i).toString()+" ");
 46         }
 47         System.out.println();
 48         double sum = 0;
 49         for(int i = 0;i<a+b+c;i++) {
 50             sum+=list.get(i).getArea();
 51         }
 52         String result = String .format("%.2f",sum);
 53         System.out.println("Sum of area:"+result);
 54         System.out.println("Sorted area:");
 55         double []area = new double[a+b+c] ;
 56         for(int i = 0;i<a+b+c;i++) {
 57             area[i]=list.get(i).getArea();
 58         }
 59         for (int x = 0; x < area.length - 1; x++) {
 60             for (int y = 0; y < area.length - 1 - x; y++) {
 61                 if (area[y] > area[y + 1]) {
 62                     double temp = area[y];
 63                     area[y] = area[y + 1];
 64                     area[y + 1] = temp;
 65                 }
 66             }
 67         }
 68         for(int i = 0;i<a+b+c;i++) {
 69             String result1 = String .format("%.2f",area[i]);
 70             System.out.print(result1+" ");
 71         }
 72         System.out.println();
 73         System.out.print("Sum of area:"+result);
 74 
 75     }
 76 
 77 }
 78 abstract class Shape {
 79 
 80     public double getArea() {
 81         return 0;
 82         
 83     }
 84     public boolean validate() {
 85         return false;
 86         
 87     }
 88     public String toString() {
 89         String result = String .format("%.2f",getArea());
 90         return result;
 91         
 92     }
 93 }
 94 class Circle extends Shape{
 95 
 96     private double radius;
 97 
 98     public double getRadius() {
 99         return radius;
100     }
101 
102     public void setRadius(double radius) {
103         this.radius = radius;
104     }
105 
106     public Circle(double radius) {
107         super();
108         this.radius = radius;
109     }
110 
111     public Circle() {
112         super();
113         // TODO Auto-generated constructor stub
114     }
115     public double getArea() {
116         return getRadius()*getRadius()*Math.PI;
117         
118     }
119     public boolean validate() {
120         if(getRadius()<=0) {
121         return false;
122         }
123         return true;
124     }
125     
126 }class Rectangle extends Shape {
127     private double width =0;
128     private double length =0;
129     
130 
131     public double getWidth() {
132         return width;
133     }
134 
135 
136     public void setWidth(double width) {
137         this.width = width;
138     }
139 
140 
141     public double getLength() {
142         return length;
143     }
144 
145 
146     public void setLength(double length) {
147         this.length = length;
148     }
149 
150 
151     public Rectangle(double width, double length) {
152         super();
153         this.width = width;
154         this.length = length;
155     }
156 
157 
158     public Rectangle() {
159         super();
160         // TODO Auto-generated constructor stub
161     }
162 
163 
164     public double getArea() {
165         return getLength()*getWidth();
166     }
167     public boolean validate() {
168         if(getWidth()<=0||getLength()<=0) {
169         return false;
170         }
171         return true;
172     }
173     
174 
175 }class Triangle extends Shape{
176     private double side1 =0;
177     private double side2 =0;
178     private double side3 =0;
179     public double getSide1() {
180         return side1;
181     }
182     public void setSide1(double side1) {
183         this.side1 = side1;
184     }
185     public double getSide2() {
186         return side2;
187     }
188     public void setSide2(double side2) {
189         this.side2 = side2;
190     }
191     public double getSide3() {
192         return side3;
193     }
194     public void setSide3(double side3) {
195         this.side3 = side3;
196     }
197     public Triangle(double side1, double side2, double side3) {
198         super();
199         this.side1 = side1;
200         this.side2 = side2;
201         this.side3 = side3;
202     }
203     public Triangle() {
204         super();
205         // TODO Auto-generated constructor stub
206     }
207     public double getArea() {
208         double p = (getSide1()+getSide2()+getSide3())/2;
209         double S = p*(p-getSide1())*(p-getSide2())*(p-getSide3());
210         S = Math.sqrt(S);
211         return S;
212     }
213     public boolean validate() {
214         if(getSide1()>0&&getSide2()>0&&getSide3()>0&&((getSide1()+getSide2())>getSide3()&&(getSide1()+getSide3()>getSide2()&&(getSide2()+getSide3())>getSide1()))){
215         return true;
216         }
217         return false;
218     }
219     
220 }
View Code

实现图形接口及多态性 

  1 import java.util.Scanner;
  2 
  3 public class Main {
  4 
  5     public static void main(String[] args) {
  6         // TODO Auto-generated method stub
  7 
  8         Scanner sc = new Scanner(System.in);
  9         double radius = sc.nextDouble();
 10         double width = sc.nextDouble();
 11         double length = sc.nextDouble();
 12         if(radius<=0||width<=0||length<=0) {
 13             System.out.println("Wrong Format");
 14             System.exit(0);
 15         }
 16         Rectangle rectangle = new Rectangle(width,length);
 17         Circle circle = new Circle(radius);
 18         String resultRectangle = String .format("%.2f",rectangle.getArea());
 19         String resultCircle = String .format("%.2f",circle.getArea());
 20         System.out.println(resultCircle);
 21         System.out.println(resultRectangle);
 22         
 23     }
 24 
 25 }
 26  interface GetArea {
 27 
 28      double getArea();
 29 }
 30 class Circle implements GetArea{
 31 
 32     private double radius;
 33 
 34     public Circle() {
 35         super();
 36         // TODO Auto-generated constructor stub
 37     }
 38 
 39     public Circle(double radius) {
 40         super();
 41         this.radius = radius;
 42     }
 43 
 44     public double getRadius() {
 45         return radius;
 46     }
 47 
 48     public void setRadius(double radius) {
 49         this.radius = radius;
 50     }
 51 
 52     @Override
 53     public double getArea() {
 54         // TODO Auto-generated method stub
 55         return getRadius()*getRadius()*Math.PI;
 56     }
 57     
 58  
 59 }
 60 class Rectangle implements GetArea {
 61     private double width =0;
 62     private double length =0;
 63     
 64 
 65     public double getWidth() {
 66         return width;
 67     }
 68 
 69 
 70     public void setWidth(double width) {
 71         this.width = width;
 72     }
 73 
 74 
 75     public double getLength() {
 76         return length;
 77     }
 78 
 79 
 80     public void setLength(double length) {
 81         this.length = length;
 82     }
 83 
 84 
 85     public Rectangle(double width, double length) {
 86         super();
 87         this.width = width;
 88         this.length = length;
 89     }
 90 
 91 
 92     public Rectangle() {
 93         super();
 94         // TODO Auto-generated constructor stub
 95     }
 96 
 97 
 98     public double getArea() {
 99         // TODO Auto-generated method stub
100         return getLength()*getWidth();
101     }
102 
103 }
View Code

 

posted @ 2021-05-02 11:36  犬大三  阅读(87)  评论(0)    收藏  举报