·前言:第二阶段的提交已经结束,在此对第二阶段题目集的知识点、题量、难度进行总结,并发表自己的看法。

(1)第四次大作业考察了面向对象的多个方面,有正则表达式、设计一个银行业务类以及点于四边形的关系问题,用到了包装类,regex以及对类与类之间关系的考察;题量为三道题,三道题的难度为中高中的难度。

  从提交通过率来看,7-1和7-3相较于7-2来说是高的,这说明对于难的题目还存在很多问题,如:计算问题、考虑不全、判断条件有细小错误等问题导致通过率不高;这也反应了自己对所学知识的不够全面,以及对解决数学问题的错误方法。

(2)期中考试知识点侧重于对对象、类、抽象类、继承、重载、重写、以及多态之间关系的理解和熟练使用,需要一定的逻辑能力,且三道题一题套一题,只有完成前面的才可以继续完成后面的题目。难度适中,需要对给定函数了解并能够使用。

  不能通过的主要问题是不清楚相关知识点或对继承、多态、容器类的不理解,导致不会使用,这反映了我对于知识点的理解不够到位,之后还需要多加练习,达到掌握的程度。

 

    ·各题目集题目 设计与分析 采坑心得 改进建议

(1)题目集 PTA大作业四:7-1 sdut-String-2 识蛟龙号载人深潜,立科技报国志(正则表达式)设计与分析

 1 import java.util.Scanner;
 2 import java.util.ArrayList;
 3 public class Main{
 4     public static void main(String[] args) {
 5         Scanner input = new Scanner(System.in);
 6         ArrayList<String> title=new ArrayList<>();
 7         ArrayList<Integer> sumlist=new ArrayList<>();
 8         String str="end";
 9         boolean isresult=true;
10         int i=-1,sum=0;
11         while(isresult) {
12             title.add(input.nextLine());
13             if(title.get(++i).equals(str)) {
14                 break;
15             }
16             else {
17                 sum=0;
18                 String regex="[^0-9]";        //规则        
19                 String result=title.get(i).replaceAll(regex," ");
20                 String[] arr=result.split(" ");
21                 for(int j=0;j<arr.length;j++) {
22                     if(arr[j].length()!=0)//判断是否为空
23                         sum+=parseint(arr[j]);
24                 }
25                 sumlist.add(sum);
26             }
27         }        
28         for(int j=0;j<sumlist.size();j++) {
29             System.out.print(sumlist.get(j));
30             if(j+1<sumlist.size())
31                 System.out.println();
32         }
33     }
34     public static int parseint(String arr) {
35         int sum=0,temp=arr.length()-1;
36         char []str=arr.toCharArray();
37         for(int i=0;i<str.length;i++) {
38             sum+=(str[i]-'0')*Math.pow(10,temp);
39             temp--;
40         }        
41         return sum;
42     }
43 }

分析:用title来存储输入的多行字符串,通过title.get(++i).equals(str)来判断是否到达结束语end,用sum来记录所有字符串中的整数之和。

采坑心得
刚开始我使用的是数组装输入的数据,但发现用数组时,长度固定,于是使用动态数组来存储不固定的数据,如:ArrayList<String> title=new ArrayList<>();并用其title.add(input.nextLine())来添加数据;用title.get(++i)来获取数据;

改进建议
通过不同的正则表达式可以简化代码长度,使其更好的维护。

 

(2)题目集 PTA大作业四:7-2 点线形系列4-凸四边形的计算 设计与分析

  1 import java.util.Scanner;
  2 public class Main{
  3     public static void main(String[] args) {
  4         Scanner input=new Scanner(System.in);
  5         String firststring=input.nextLine();
  6         char []selections=firststring.toCharArray();//转换为数组
  7             switch(selections[0]) {
  8             case '1':
  9                 if(isBaseform(selections,firststring,4)) {
 10                     method1(selections,firststring);                    
 11                 }
 12                     break;
 13             case '2':
 14                 if(isBaseform(selections,firststring,4)) {
 15                     method2(selections,firststring);                    
 16                 }
 17                 break;
 18             case '3':
 19                 if(isBaseform(selections,firststring,4)) {
 20                     method3(selections,firststring);                    
 21                 }
 22                 break;
 23             case '4':
 24                 if(isBaseform(selections,firststring,6)) {
 25                     method4(firststring);                    
 26                 }
 27                 break;
 28             case '5':
 29                 if(isBaseform(selections,firststring,5)) {
 30                     method5(firststring);                    
 31                 }
 32                 break;
 33             default:System.out.print("Wrong Format");
 34                     break;
 35             }            
 36     }
 37     //判断是否符合基本格式
 38     private static boolean isBaseform(char selections[],String firststring,int number) {
 39             boolean isForm=true;
 40             int number1=0,number2=0,number3=0;
 41             for(int i=0;i<selections.length;i++){//判断,与空格
 42                 if(selections[i]==','){
 43                     number1++;
 44                     number2++;
 45                     number3++;
 46                     if(i-1>0&&i+1<=selections.length-1&&(selections[i-1]>='0'&&selections[i-1]<='9')&&((selections[i+1]>='0'&&selections[i+1]<='9')||selections[i+1]=='+'||selections[i+1]=='-')){                        
 47                     }
 48                     else{
 49                         System.out.print("Wrong Format");    
 50                         return false;
 51                     }   
 52                 }
 53                 if(selections[i]==' '&&i!=selections.length-1){
 54                    if(number1<=0||number1>2){             
 55                     System.out.print("Wrong Format");    
 56                     return false;
 57                     }
 58                     number1=0;
 59                 }
 60             }  
 61             if(number2==0){
 62                 System.out.print("Wrong Format");    
 63                 return false;
 64             }
 65             String []sixstring=firststring.split(" ");
 66             for(int i=0;i<sixstring.length;i++){
 67                 char qt[]=sixstring[i].toCharArray();
 68                 int ji=0;
 69                 for(int j=0;j<qt.length;j++){
 70                     if(qt[j]==','){
 71                         ji++;
 72                     }                   
 73                 }
 74                 if(ji!=1){
 75                     System.out.print("Wrong Format");    
 76                     return false;
 77                 }
 78             }
 79             String regex="[0-9.+-]";        //规则        
 80             String secondstring=firststring.replaceAll(regex,"");
 81             char []arr=secondstring.toCharArray();                                    
 82             if(arr[0]==':') {//判断第一个字符是否符合标准
 83                 for(int i=1;i<arr.length;i+=2) {
 84                     if(i==arr.length-1&&arr[i]==',') {//最后一位少个空格所以需要判断一下
 85                     }
 86                     else {
 87                         if(arr[i]==','&&arr[i+1]==' ') {                    
 88                         }
 89                         else {
 90                             System.out.print("Wrong Format");    
 91                             return false;
 92                         }
 93                     }                    
 94                 }    
 95             }
 96             else {
 97                 System.out.print("Wrong Format");    
 98                 return false;
 99             }    
100             String []fourstring=firststring.split(":");
101             secondstring=fourstring[1].replaceAll(",", " ");
102             char []arr2=secondstring.toCharArray();
103             for(int i=0;i<arr2.length;i++) {
104                 //System.out.print(arr2[i]);
105                 if(arr2[i]=='+'||arr2[i]=='-') {//判断+-号前后
106                     if((i-1)>=0&&arr2[i-1]!=' ') {
107                         isForm=false;
108                     }
109                     if(i<(arr2.length-1)&&arr2[i+1]>='0'&&arr2[i]<='9') {
110                     }
111                     else isForm=false;
112                 }
113                 if(arr2[i]=='.') {//判断.号前后
114                     if((i-1)>=1&&(i+1)<=(arr2.length-1)&&(arr2[i-1]>='0'&&arr2[i]<='9')&&(arr2[i+1]>='0'&&arr2[i+1]<='9')) {                        
115                     }
116                     else isForm=false;
117                 }                
118             }
119             String []fivestring=firststring.split("[:, ]");
120             for(int i=0,summer=0;i<fivestring.length;i++) {
121                 arr2=fivestring[i].toCharArray();
122                 if(arr2.length==1&&arr2[0]=='0') {                    
123                 }
124                 else {
125                     for(int j=0;j<arr2.length-1;j++) {//问题1?选项5时124行会出现错误,只有改为arr2.length-1才可以,以前是arr2.length
126                         if(arr2[j]=='0') {
127                             if(j-1>=0) {
128                                 if(arr2[j-1]=='+'||arr2[j-1]=='-') {
129                                         if(arr2[j+1]!='.') {
130                                             isForm=false;
131                                             break;                                            
132                                         }
133                                 }
134                             }
135                             else if(arr2[j+1]!='.') {
136                                 isForm=false;
137                                 break;
138                             }
139                         }
140                         if(arr2[j]=='.') {
141                         summer++;
142                         for(int k=0;k<j;k++) {
143                             if(arr2[k]=='0') {
144                                 if(k-1>=0) {
145                                     if(arr2[k-1]=='+'||arr2[k-1]=='-') {
146                                         if(arr2[k+1]!='.') {
147                                             isForm=false;
148                                             break;
149                                         }
150                                     }
151                                 }
152                                 else {
153                                     if(arr2[k+1]!='.') {
154                                         isForm=false;
155                                         break;
156                                     }
157                                 }
158                             }
159                         }
160                         }
161                     }
162                 }
163                 if(summer>=2) {
164                     isForm=false;
165                     break;
166                 }
167                 summer=0;
168             }
169             if(isForm==false) {
170                 System.out.print("Wrong Format");    
171                 return false;
172             }
173             //判断点的数量是否一致
174             if(number==4) {
175                 if(number3!=number) {
176                     System.out.print("wrong number of points");
177                     return false;
178                 }
179             }
180             return true;
181     }    
182     //判断输入的四个点是否为四边形
183     private  double[] isQuadrilateral(String firststring,int select) {
184         boolean judge=false;//选项1和选项2他输出的点的重合和不能构成四边形顺序不一样
185         String []secondString=firststring.split("[:, ]");
186         double arr[]=new double[secondString.length-1];//记录那四个点
187         int str[]=new int[8];//统计那四个点出现的次数
188         int transverse=0;//统计横坐标重复出现
189         int ordinate=0;//统计纵坐标重复出现
190         for(int i=1;i<secondString.length;i++) {
191             arr[i-1]=Double.parseDouble(secondString[i]);//将字符串转换为数字
192         }                
193         for(int i=0;i<arr.length;i+=2) {//检查是否有重合的点
194             transverse=1;
195             ordinate=1;
196             for(int j=0;j<arr.length;j+=2) {
197                 if(i!=j) {
198                     if(arr[i]==arr[j]&&arr[i+1]==arr[j+1]) {
199                         judge=true;//两点重合
200                         if(select==1) {
201                             System.out.print("points coincide");
202                            // System.out.print("not a quadrilateral");
203                             return null;                        
204                         }                
205                     }
206                     if(arr[i]==arr[j]) {
207                         transverse++;
208                     }
209                     else if(arr[i+1]==arr[j+1]) {
210                         ordinate++;
211                     }
212                 }
213             }
214             str[i]=transverse;
215             str[i+1]=ordinate;
216         }
217         for(int i=0;i<8;i++) {
218             if(str[i]>2) {
219                 if(select==1)System.out.print(false+" "+false);
220                 if(select==2)System.out.print(false+" "+false+" "+false);
221                 return null;
222             }
223         }
224         //判断是否能构成四边形
225             //判断是否为四边形时要去掉两线段平行这一条件        
226             if(((arr[5]-arr[1])*(arr[2]-arr[0])!=(arr[3]-arr[1])*(arr[4]-arr[0]))&&((arr[5]-arr[1])*(arr[6]-arr[0])!=(arr[7]-arr[1])*(arr[4]-arr[0]))&&((arr[5]-arr[3])*(arr[6]-arr[2])!=(arr[7]-arr[3])*(arr[4]-arr[2]))&&((arr[5]-arr[1])*(arr[6]-arr[2])!=(arr[7]-arr[3])*(arr[4]-arr[0]))) {
227                 if(select==1) {
228                     System.out.print(true+" ");
229                     return arr;
230                 }
231                 if(select==2&&judge) {
232                     System.out.print("not a quadrilateral");
233                     return null;
234                 }
235                 if(select==3&&judge) {
236                     System.out.print("not a quadrilateral");
237                     return null;
238                 }
239             }
240             else {
241                 if(select==1) System.out.print(false+" "+false);
242                 if(select==2) System.out.print("not a quadrilateral");
243                 if(select==3) System.out.print("not a quadrilateral");
244                 return null;
245             }
246             return arr;
247         }
248     //选项1
249     private static void method1(char str[],String firststring) {//选项1
250         Main m=new Main();
251         double []arr=m.isQuadrilateral(firststring,1);//获取四点的坐标
252         if(arr!=null) {//判断是否为为空,如果为空那就不用判断是否为平行四边形
253             if((Math.pow(arr[0]-arr[6],2)+Math.pow(arr[1]-arr[7],2)==Math.pow(arr[4]-arr[2],2)+Math.pow(arr[5]-arr[3],2))&&(Math.pow(arr[0]-arr[2],2)+Math.pow(arr[1]-arr[3],2)==Math.pow(arr[4]-arr[6],2)+Math.pow(arr[5]-arr[7],2))) {
254                 System.out.print(true);    
255             }
256             else {
257                 System.out.print(false);        
258             }
259         }
260     }
261     //选项2
262     private static void method2(char str[],String firststring) {
263         boolean a1=true;
264         boolean a2=true;
265         boolean a3=true;
266         Main m=new Main();
267         double []arr=m.isQuadrilateral(firststring,2);//获取四点的坐标
268         if(arr!=null) {//判断是否为为空,如果为空那就不用判断是否为平行四边形
269             //判断是否为平行四边形
270             if((Math.sqrt(Math.pow(arr[0]-arr[6],2)+Math.pow(arr[1]-arr[7],2))==Math.sqrt(Math.pow(arr[4]-arr[2],2)+Math.pow(arr[5]-arr[3],2)))&&(Math.sqrt(Math.pow(arr[0]-arr[2],2)+Math.pow(arr[1]-arr[3],2))==Math.sqrt(Math.pow(arr[4]-arr[6],2)+Math.pow(arr[5]-arr[7],2)))) {
271                 //判断是否为菱形
272                 if(((arr[1]-arr[5])*(arr[3]-arr[7]))==-1*((arr[0]-arr[4])*(arr[2]-arr[6]))) {
273                     a1=true;                        
274                 }
275                 else {
276                     a1=false;                       
277                 }               
278                 //判断是否为距形
279                     if(Math.pow(arr[0]-arr[4],2)+Math.pow(arr[1]-arr[5],2)==Math.pow(arr[2]-arr[6],2)+Math.pow(arr[3]-arr[7],2)) {
280                         a2=true;
281                     }
282                     else a2=false;
283                 //判断你是否为正方形
284                     if(((arr[1]-arr[5])*(arr[3]-arr[7]))==-1*((arr[0]-arr[4])*(arr[2]-arr[6]))&&((arr[1]-arr[3])*(arr[1]-arr[7]))==-1*((arr[0]-arr[6])*(arr[0]-arr[2]))) {
285                         a3=true;
286                     }
287                     else {
288                         a3=false;                           
289                     }
290                 System.out.print(a1+" "+a2+" "+a3);
291             }
292             else {
293                 System.out.print(false+" "+false+" "+false);
294             }
295         }    
296         
297     }
298     //选项3
299     private static void method3(char str[],String firststring) {//选项1
300         Main m=new Main();
301         boolean judge=true;
302         double []arr=m.isQuadrilateral(firststring,3);//获取四点的坐标
303         if(arr!=null) {//判断是否为为空,如果不为空那就是凸凹四边形
304             //求周长
305             double a=Math.sqrt(Math.pow(arr[0]-arr[2], 2)+Math.pow(arr[1]-arr[3], 2));
306             double b=Math.sqrt(Math.pow(arr[4]-arr[2], 2)+Math.pow(arr[5]-arr[3], 2));
307             double c=Math.sqrt(Math.pow(arr[4]-arr[6], 2)+Math.pow(arr[5]-arr[7], 2));
308             double d=Math.sqrt(Math.pow(arr[0]-arr[6], 2)+Math.pow(arr[1]-arr[7], 2));
309             double perimeter=a+b+c+d;//周长
310             perimeter=(double)Math.round(perimeter*1000)/1000;//后三位
311             double area;//面积
312             //叉乘来判断凹凸四边形,判断三组,只要出现叉乘小于0,则为凹
313             if((((arr[2]-arr[0])*(arr[5]-arr[3])-(arr[3]-arr[1])*(arr[4]-arr[2]))>0&&((arr[4]-arr[2])*(arr[7]-arr[5])-(arr[5]-arr[3])*(arr[6]-arr[4]))>0&&((arr[6]-arr[4])*(arr[1]-arr[7])-(arr[7]-arr[5])*(arr[0]-arr[6]))>0&&((arr[0]-arr[6])*(arr[3]-arr[1])-(arr[1]-arr[7])*(arr[2]-arr[0]))>0)||(((arr[2]-arr[0])*(arr[5]-arr[3])-(arr[3]-arr[1])*(arr[4]-arr[2]))<0&&((arr[4]-arr[2])*(arr[7]-arr[5])-(arr[5]-arr[3])*(arr[6]-arr[4]))<0&&((arr[6]-arr[4])*(arr[1]-arr[7])-(arr[7]-arr[5])*(arr[0]-arr[6]))<0&&((arr[0]-arr[6])*(arr[3]-arr[1])-(arr[1]-arr[7])*(arr[2]-arr[0]))<0)) {
314                 judge=true;//凸四边形
315                 double e=Math.sqrt(Math.pow(arr[0]-arr[4], 2)+Math.pow(arr[1]-arr[5], 2));                
316                 double p1=(a+b+e)/2;
317                 double p2=(a+c+e)/2;
318                 area=Math.sqrt(p1*(p1-a)*(p1-b)*(p1-e))+Math.sqrt(p2*(p2-a)*(p2-c)*(p2-e));
319                 area=(double)Math.round(area*1)/1;//整数位
320                 System.out.print(judge+" "+perimeter+" "+area);
321             }
322             else {
323                 judge=false;//凹四边形
324                 //假设第一个点为凹点
325                 if((((arr[2]-arr[0])*(arr[5]-arr[3])-(arr[3]-arr[1])*(arr[4]-arr[2]))<0&&((arr[4]-arr[2])*(arr[7]-arr[5])-(arr[5]-arr[3])*(arr[6]-arr[4]))>0&&((arr[6]-arr[4])*(arr[1]-arr[7])-(arr[7]-arr[5])*(arr[0]-arr[6]))>0&&((arr[0]-arr[6])*(arr[3]-arr[1])-(arr[1]-arr[7])*(arr[2]-arr[0]))>0)||(((arr[2]-arr[0])*(arr[5]-arr[3])-(arr[3]-arr[1])*(arr[4]-arr[2]))>0&&((arr[4]-arr[2])*(arr[7]-arr[5])-(arr[5]-arr[3])*(arr[6]-arr[4]))<0&&((arr[6]-arr[4])*(arr[1]-arr[7])-(arr[7]-arr[5])*(arr[0]-arr[6]))<0&&((arr[0]-arr[6])*(arr[3]-arr[1])-(arr[1]-arr[7])*(arr[2]-arr[0]))<0)) {
326                     double    e1=Math.sqrt(Math.pow(arr[6]-arr[2], 2)+Math.pow(arr[7]-arr[3], 2));        
327                     double p1=(a+d+e1)/2;
328                     double p2=(b+c+e1)/2;
329                     area=Math.sqrt(p1*(p1-a)*(p1-d)*(p1-e1))+Math.sqrt(p2*(p2-b)*(p2-c)*(p2-e1));
330                     area=(double)Math.round(area*1)/1;//整数位
331                     System.out.print(judge+" "+perimeter+" "+area);
332                 }
333                 if((((arr[2]-arr[0])*(arr[5]-arr[3])-(arr[3]-arr[1])*(arr[4]-arr[2]))>0&&((arr[4]-arr[2])*(arr[7]-arr[5])-(arr[5]-arr[3])*(arr[6]-arr[4]))<0&&((arr[6]-arr[4])*(arr[1]-arr[7])-(arr[7]-arr[5])*(arr[0]-arr[6]))>0&&((arr[0]-arr[6])*(arr[3]-arr[1])-(arr[1]-arr[7])*(arr[2]-arr[0]))>0)||(((arr[2]-arr[0])*(arr[5]-arr[3])-(arr[3]-arr[1])*(arr[4]-arr[2]))<0&&((arr[4]-arr[2])*(arr[7]-arr[5])-(arr[5]-arr[3])*(arr[6]-arr[4]))>0&&((arr[6]-arr[4])*(arr[1]-arr[7])-(arr[7]-arr[5])*(arr[0]-arr[6]))<0&&((arr[0]-arr[6])*(arr[3]-arr[1])-(arr[1]-arr[7])*(arr[2]-arr[0]))<0)) {
334                     double    e1=Math.sqrt(Math.pow(arr[0]-arr[4], 2)+Math.pow(arr[1]-arr[5], 2));        
335                     double p1=(a+b+e1)/2;
336                     double p2=(a+c+e1)/2;
337                     area=Math.sqrt(p1*(p1-a)*(p1-b)*(p1-e1))+Math.sqrt(p2*(p2-a)*(p2-c)*(p2-e1));
338                     area=(double)Math.round(area*1)/1;//整数位
339                     System.out.print(judge+" "+perimeter+" "+area);
340                 }
341                 if((((arr[2]-arr[0])*(arr[5]-arr[3])-(arr[3]-arr[1])*(arr[4]-arr[2]))>0&&((arr[4]-arr[2])*(arr[7]-arr[5])-(arr[5]-arr[3])*(arr[6]-arr[4]))>0&&((arr[6]-arr[4])*(arr[1]-arr[7])-(arr[7]-arr[5])*(arr[0]-arr[6]))<0&&((arr[0]-arr[6])*(arr[3]-arr[1])-(arr[1]-arr[7])*(arr[2]-arr[0]))>0)||(((arr[2]-arr[0])*(arr[5]-arr[3])-(arr[3]-arr[1])*(arr[4]-arr[2]))<0&&((arr[4]-arr[2])*(arr[7]-arr[5])-(arr[5]-arr[3])*(arr[6]-arr[4]))<0&&((arr[6]-arr[4])*(arr[1]-arr[7])-(arr[7]-arr[5])*(arr[0]-arr[6]))>0&&((arr[0]-arr[6])*(arr[3]-arr[1])-(arr[1]-arr[7])*(arr[2]-arr[0]))<0)) {
342                     double    e1=Math.sqrt(Math.pow(arr[6]-arr[2], 2)+Math.pow(arr[7]-arr[3], 2));        
343                     double p1=(a+d+e1)/2;
344                     double p2=(b+c+e1)/2;
345                     area=Math.sqrt(p1*(p1-a)*(p1-d)*(p1-e1))+Math.sqrt(p2*(p2-b)*(p2-c)*(p2-e1));
346                     area=(double)Math.round(area*1)/1;//整数位
347                     System.out.print(judge+" "+perimeter+" "+area);
348                 }
349                 if((((arr[2]-arr[0])*(arr[5]-arr[3])-(arr[3]-arr[1])*(arr[4]-arr[2]))>0&&((arr[4]-arr[2])*(arr[7]-arr[5])-(arr[5]-arr[3])*(arr[6]-arr[4]))>0&&((arr[6]-arr[4])*(arr[1]-arr[7])-(arr[7]-arr[5])*(arr[0]-arr[6]))>0&&((arr[0]-arr[6])*(arr[3]-arr[1])-(arr[1]-arr[7])*(arr[2]-arr[0]))<0)||(((arr[2]-arr[0])*(arr[5]-arr[3])-(arr[3]-arr[1])*(arr[4]-arr[2]))<0&&((arr[4]-arr[2])*(arr[7]-arr[5])-(arr[5]-arr[3])*(arr[6]-arr[4]))<0&&((arr[6]-arr[4])*(arr[1]-arr[7])-(arr[7]-arr[5])*(arr[0]-arr[6]))<0&&((arr[0]-arr[6])*(arr[3]-arr[1])-(arr[1]-arr[7])*(arr[2]-arr[0]))>0)) {
350                     double    e1=Math.sqrt(Math.pow(arr[0]-arr[4], 2)+Math.pow(arr[1]-arr[5], 2));        
351                     double p1=(a+b+e1)/2;
352                     double p2=(a+c+e1)/2;
353                     area=Math.sqrt(p1*(p1-a)*(p1-b)*(p1-e1))+Math.sqrt(p2*(p2-a)*(p2-c)*(p2-e1));
354                     area=(double)Math.round(area*1)/1;//整数位
355                     System.out.print(judge+" "+perimeter+" "+area);
356                 }
357             }
358         }
359     }
360     //选项4
361     private static void method4(String firststring) {
362                 System.out.print("not a quadrilateral or triangle");        
363     }
364     //选项5
365     private static void method5(String firststring) {
366         String []secondString=firststring.split("[:, ]");
367         double arr[]=new double[secondString.length-1];//记录那四个点
368         for(int i=1;i<secondString.length;i++) {
369             arr[i-1]=Double.parseDouble(secondString[i]);//将字符串转换为数字
370         }
371         if((arr[2]==arr[4]&&arr[4]==arr[6]&&arr[8]==arr[6]&&arr[3]==arr[5]&&arr[5]==arr[7]&&arr[7]==arr[9])
372             ||( ( (arr[9]-arr[3])*(arr[4]-arr[2])==(arr[5]-arr[3])*(arr[8]-arr[2]) )&&( (arr[7]-arr[3])*(arr[4]-arr[2])==(arr[5]-arr[3])*(arr[6]-arr[2]) ) )    
373             ||( (arr[9]-arr[5])*(arr[6]-arr[2])==(arr[8]-arr[4])*arr[7]-arr[3] ) ) {
374                 System.out.print("not a quadrilateral or triangle");
375         }
376         else {//是三角形或是四边形
377             //判断是否为四边形
378             if(((arr[9]-arr[3])*(arr[4]-arr[2])!=(arr[5]-arr[3])*(arr[8]-arr[2]))&&((arr[9]-arr[5])*(arr[6]-arr[4])!=(arr[7]-arr[5])*(arr[8]-arr[4]))&&((arr[9]-arr[3])*(arr[6]-arr[2])!=(arr[7]-arr[3])*(arr[8]-arr[2]))&&( (arr[9]-arr[5])*(arr[6]-arr[2])!=(arr[8]-arr[4])*arr[7]-arr[3] )) {
379                 //判断点是否在边上,注意,点可能在延长线上
380                 double a=Math.sqrt(Math.pow(arr[4]-arr[2], 2)+Math.pow(arr[5]-arr[3], 2));
381                 double b=Math.sqrt(Math.pow(arr[4]-arr[6], 2)+Math.pow(arr[5]-arr[7], 2));
382                 double c=Math.sqrt(Math.pow(arr[8]-arr[6], 2)+Math.pow(arr[9]-arr[7], 2));
383                 double d=Math.sqrt(Math.pow(arr[8]-arr[2], 2)+Math.pow(arr[9]-arr[3], 2));
384                 if( ( (arr[1]-arr[3])*(arr[4]-arr[2])==(arr[5]-arr[3])*(arr[0]-arr[2])&&( Math.sqrt(Math.pow(arr[0]-arr[2], 2)+Math.pow(arr[1]-arr[3], 2))<a && Math.sqrt(Math.pow(arr[0]-arr[4], 2)+Math.pow(arr[1]-arr[5], 2))<a ) )
385                     || ( (arr[1]-arr[5])*(arr[6]-arr[4])==(arr[7]-arr[5])*(arr[0]-arr[4]) &&( Math.sqrt(Math.pow(arr[0]-arr[6], 2)+Math.pow(arr[1]-arr[7], 2))<b && Math.sqrt(Math.pow(arr[0]-arr[4], 2)+Math.pow(arr[1]-arr[5], 2))<b ) )
386                     || ( (arr[1]-arr[7])*(arr[8]-arr[6])==(arr[9]-arr[7])*(arr[0]-arr[6]) &&( Math.sqrt(Math.pow(arr[0]-arr[6], 2)+Math.pow(arr[1]-arr[7], 2))<c && Math.sqrt(Math.pow(arr[0]-arr[8], 2)+Math.pow(arr[1]-arr[9], 2))<c ) )
387                     || ( (arr[1]-arr[3])*(arr[8]-arr[2])==(arr[9]-arr[3])*(arr[0]-arr[2]) &&( Math.sqrt(Math.pow(arr[0]-arr[2], 2)+Math.pow(arr[1]-arr[3], 2))<d && Math.sqrt(Math.pow(arr[0]-arr[8], 2)+Math.pow(arr[1]-arr[9], 2))<d ) )    
388                         ) {
389                     System.out.print("on the quadrilateral");
390                 }
391                 else {
392                     //一个面积
393                     double a1=Math.sqrt(Math.pow(arr[0]-arr[2], 2)+Math.pow(arr[1]-arr[3], 2));
394                     double a2=Math.sqrt(Math.pow(arr[0]-arr[4], 2)+Math.pow(arr[1]-arr[5], 2));
395                     double a3=Math.sqrt(Math.pow(arr[0]-arr[6], 2)+Math.pow(arr[1]-arr[7], 2));
396                     double a4=Math.sqrt(Math.pow(arr[0]-arr[8], 2)+Math.pow(arr[1]-arr[9], 2));            
397                     double p1=(d+a1+a4)/2;
398                     double p2=(a+a1+a2)/2;
399                     double p3=(b+a2+a3)/2;
400                     double p4=(c+a3+a4)/2;
401                     double area1=Math.sqrt(p1*(p1-d)*(p1-a1)*(p1-a4))+Math.sqrt(p2*(p2-a)*(p2-a1)*(p2-a2))+Math.sqrt(p3*(p3-b)*(p3-a2)*(p3-a3))+Math.sqrt(p4*(p4-c)*(p4-a3)*(p4-a4));
402                     area1=(double)Math.round(area1*1)/1;
403                     //四边形面积
404                     double e=Math.sqrt(Math.pow(arr[6]-arr[2], 2)+Math.pow(arr[7]-arr[3], 2));
405                     double p5=(d+c+e)/2;
406                     double p6=(a+b+e)/2;
407                     double area2=Math.sqrt(p5*(p5-d)*(p5-c)*(p5-e))+Math.sqrt(p6*(p6-a)*(p6-b)*(p6-e));
408                     area2=(double)Math.round(area2*1)/1;//整数位
409                     //判断点是否在四边形里
410                     if(area1==area2) {//点在里面
411                         System.out.print("in the quadrilateral");
412                     }
413                     else {
414                         System.out.print("outof the quadrilateral");
415                     }
416                 }                
417             }
418             double h=Math.sqrt(Math.pow(arr[4]-arr[8], 2)+Math.pow(arr[5]-arr[9], 2));
419             double i=Math.sqrt(Math.pow(arr[2]-arr[6], 2)+Math.pow(arr[3]-arr[7], 2));
420                 //判断三角形
421                 if( (arr[2]==arr[4]&&arr[3]==arr[5])||(arr[2]==arr[8]&&arr[3]==arr[9]) 
422                     ||(arr[4]==arr[6]&&arr[5]==arr[7]) 
423                     ||(arr[6]==arr[8]&&arr[7]==arr[9]) 
424                     ||( (arr[3]-arr[5])*(arr[8]-arr[4])==(arr[9]-arr[5])*(arr[2]-arr[4]) &&( Math.sqrt(Math.pow(arr[2]-arr[4], 2)+Math.pow(arr[3]-arr[5], 2))<h && Math.sqrt(Math.pow(arr[2]-arr[8], 2)+Math.pow(arr[3]-arr[9], 2))<h ))
425                     || ( (arr[5]-arr[3])*(arr[6]-arr[2])==(arr[7]-arr[3])*(arr[4]-arr[2]) &&( Math.sqrt(Math.pow(arr[2]-arr[4], 2)+Math.pow(arr[3]-arr[5], 2))<i && Math.sqrt(Math.pow(arr[4]-arr[6], 2)+Math.pow(arr[5]-arr[7], 2))<i ) )
426                     || ( (arr[9]-arr[3])*(arr[6]-arr[2])==(arr[7]-arr[3])*(arr[8]-arr[2]) &&( Math.sqrt(Math.pow(arr[8]-arr[6], 2)+Math.pow(arr[9]-arr[7], 2))<h && Math.sqrt(Math.pow(arr[4]-arr[6], 2)+Math.pow(arr[5]-arr[7], 2))<h ) )
427                     || ( (arr[7]-arr[5])*(arr[8]-arr[4])==(arr[9]-arr[5])*(arr[6]-arr[4]) &&( Math.sqrt(Math.pow(arr[8]-arr[2], 2)+Math.pow(arr[9]-arr[3], 2))<i && Math.sqrt(Math.pow(arr[8]-arr[6], 2)+Math.pow(arr[9]-arr[7], 2))<i ) )    
428                     ) {
429                       System.out.print("in the triangle");
430                 }
431         }
432     }
433     
434 }

分析:本题共有五个选项,可以用switch来进行输入选择选项;五个选项的共同函数是格式与点的数量判断是否正确函数;将五个函数分开实现有助于修改和调用。


采坑心得
格式判断有好多种情况,如:1:-1,-1 -1,1 1,2 1,-2.或1:--1,-1 -1,1 1,2 1,-2或1:-1,-1 -1,1 1,2 1,-01等都是格式错误,需要提前进行判断。


改进建议
使用数学公式(射线法)判断点与四边形位置,及计算面积时可以大大减小错的概率,缩短代码数量。

 

(3)题目集 PTA大作业四:7-3 设计一个银行业务类 设计与分析

 1 import java.util.Scanner;
 2 public class Main{
 3     public static void main(String[] args) {        
 4         Scanner input=new Scanner(System.in);
 5         String name;
 6         int password;
 7         int depositmoney,withdrawmoney;
 8         boolean isscuss;
 9         BankBusiness.welcome();
10         //创号
11         name=input.next();
12         password=input.nextInt();
13         BankBusiness account=new BankBusiness(name,password);
14         //存款
15         do {
16             password=input.nextInt();
17             depositmoney=input.nextInt();
18             isscuss=account.deposit(password,depositmoney);
19         }
20         while(!isscuss);
21         //取款
22         do {
23             password=input.nextInt();
24             withdrawmoney=input.nextInt();
25             isscuss=account.withdraw(password,withdrawmoney);
26         }
27         while(!isscuss);        
28         BankBusiness.welcomeNext();
29     }
30 }
31 class BankBusiness{
32     public static String bankName="中国银行";
33     private String name;//账户名、密码、
34     int password;
35     private double balance=0;//账户余额
36     //创建用户
37     BankBusiness(String name,int password){
38         this.name=name;
39         this.password=password;
40     }    
41     //存款
42     public boolean deposit(int ispassword,int depositmoney) {
43         if(password==ispassword) {//判断密码是否正确
44             balance+=depositmoney;
45             System.out.printf("您的余额有%.1f元。\n",balance);
46             return true;
47         }
48         else {
49             System.out.println("您的密码错误!");
50             return false;
51         }
52     }
53     //取款
54     public boolean withdraw(int ispassword,int withdrawmoney) {
55         if(password==ispassword) {//判断密码是否正确
56             if(withdrawmoney>balance) {
57                 System.out.println("您的余额不足!");
58                 return false;
59             }
60             else {
61                 balance-=withdrawmoney;
62                 System.out.printf("请取走钞票,您的余额还有%.1f元。\n",balance);
63                 return true;
64             }
65         }
66         else {
67             System.out.println("您的密码错误!");
68             return false;
69         }
70     }    
71     //开头语
72     public static void welcome() {
73         System.out.println(bankName+"欢迎您的到来!");
74     }
75     //结束语
76     public static void welcomeNext() {
77         System.out.print("请收好您的证件和物品,欢迎您下次光临!");
78     }
79 }

分析
创建一个银行类bankName,在类中写入用户的存款(deposit)、取款等操作。

采坑心得
取款金额小于余额时不能进行取款。

改进建议
可以使用switch让用户更直观的选择要进行的操作。

 

(4)题目集 期中考试:7-1 点与线(类设计)设计与分析

 1 import java.util.Scanner;
 2 import java.util.ArrayList;
 3 public class Main{
 4     public static void main(String[] args) {
 5         Scanner input=new Scanner(System.in);
 6         double x1=input.nextDouble();
 7         double y1=input.nextDouble();
 8         double x2=input.nextDouble();
 9         double y2=input.nextDouble();
10         String color=input.next();    
11         if((x1>0&&x1<=200&&y1>0&&y1<=200)&&(x2>0&&x2<=200&&y2>0&&y2<=200)) {
12             Point point1=new Point(x1,y1);
13             Point point2=new Point(x2,y2);
14             Line line=new Line(point1,point2,color);
15             line.display();            
16         }
17         else {
18             System.out.print("Wrong Format");
19         }
20     }
21 }
22 class Point{
23     private double x;
24     private double y;
25     Point(){    
26     }
27     Point(double x,double y){
28         this.x=x;
29         this.y=y;
30     }
31     public double getX() {
32         return x;
33     }
34     public void setX(double x) {
35         this.x=x;
36     }
37     public double getY() {
38         return y;
39     }
40     public void setY(double y) {
41         this.y=y;
42     }
43     public void display() {
44         System.out.print("(");
45         System.out.printf("%.2f,", x);
46         System.out.printf("%.2f)\n", y);
47     }
48 }
49 class Line{
50     private Point point1=new Point();
51     private Point point2=new Point();
52     private String color;
53     public Line() {        
54     }
55     public Line(Point point1,Point point2,String color) {
56         this.point1=point1;
57         this.point2=point2;
58         this.color=color;
59     }
60     public Point getPoint1() {
61         return point1;
62     }
63     public void setPoint1(Point point1) {
64         this.point1=point1;
65     }
66     public Point getPoint2() {
67         return point2;
68     }
69     public void setPoint2(Point point2) {
70         this.point2=point2;
71     }
72     public String getColor() {
73         return color;
74     }
75     public void setColor(String color) {
76         this.color=color;
77     }
78     public double getDistance() {
79         return Math.sqrt(Math.pow(point1.getX()-point2.getX(),2)+Math.pow(point1.getY()-point2.getY(),2));
80     }
81     public void display() {
82         System.out.println("The line's color is:"+color);
83         System.out.println("The line's begin point's Coordinate is:");
84         point1.display();
85         System.out.println("The line's end point's Coordinate is:");
86         point2.display();
87         System.out.print("The line's length is:");
88         System.out.printf("%.2f", getDistance());
89     }
90 }

分析:设计Point和Line两个类,在两个类中分别实现各自功能。

采坑心得
在Line的构造方法中传的是Point的对象,小心混淆。

 

(5)题目集 期中考试:7-2 点线面问题重构(继承与多态)设计与分析

  1 import java.util.Scanner;
  2 import java.util.ArrayList;
  3 public class Main{
  4     public static void main(String[] args) {
  5         Scanner input=new Scanner(System.in);
  6         double x1=input.nextDouble();
  7         double y1=input.nextDouble();
  8         double x2=input.nextDouble();
  9         double y2=input.nextDouble();
 10         String color=input.next();    
 11         if((x1>0&&x1<=200&&y1>0&&y1<=200)&&(x2>0&&x2<=200&&y2>0&&y2<=200)) {
 12             Point point1=new Point(x1,y1);
 13             Point point2=new Point(x2,y2);
 14             Plane plane=new Plane(color);
 15             Line line=new Line(point1,point2,color);
 16             Element a= point1;
 17             a.display();
 18             Element b= point2;
 19             b.display();
 20             Element c= line;
 21             c.display();
 22             Element d= plane;
 23             d.display();
 24         }
 25         else {
 26             System.out.print("Wrong Format");
 27         }
 28     }
 29 }
 30 abstract class Element{
 31     
 32     public void display() {
 33         
 34     }
 35 }
 36 
 37 class Plane extends Element{
 38     private String color;
 39     Plane(){    
 40     }
 41     Plane(String color){    
 42         this.color=color;
 43     }
 44     public String getColor() {
 45         return color;
 46     }
 47     public void setColor(String color) {
 48         this.color=color;
 49     }
 50     public void display() {
 51         System.out.print("The Plane's color is:"+color);
 52     }
 53 }
 54 
 55 class Point extends Element{
 56     private double x;
 57     private double y;
 58     Point(){    
 59     }
 60     Point(double x,double y){
 61         this.x=x;
 62         this.y=y;
 63     }
 64     public double getX() {
 65         return x;
 66     }
 67     public void setX(double x) {
 68         this.x=x;
 69     }
 70     public double getY() {
 71         return y;
 72     }
 73     public void setY(double y) {
 74         this.y=y;
 75     }
 76     public void display() {
 77         System.out.print("(");
 78         System.out.printf("%.2f,", x);
 79         System.out.printf("%.2f)\n", y);
 80     }
 81 }
 82 class Line extends Element{
 83     private Point point1=new Point();
 84     private Point point2=new Point();
 85     private String color;
 86     public Line() {        
 87     }
 88     public Line(Point point1,Point point2,String color) {
 89         this.point1=point1;
 90         this.point2=point2;
 91         this.color=color;
 92     }
 93     public Point getPoint1() {
 94         return point1;
 95     }
 96     public void setPoint1(Point point1) {
 97         this.point1=point1;
 98     }
 99     public Point getPoint2() {
100         return point2;
101     }
102     public void setPoint2(Point point2) {
103         this.point2=point2;
104     }
105     public String getColor() {
106         return color;
107     }
108     public void setColor(String color) {
109         this.color=color;
110     }
111     public double getDistance() {
112         return Math.sqrt(Math.pow(point1.getX()-point2.getX(),2)+Math.pow(point1.getY()-point2.getY(),2));
113     }
114     public void display() {
115         System.out.println("The line's color is:"+color);
116         System.out.println("The line's begin point's Coordinate is:");
117         point1.display();
118         System.out.println("The line's end point's Coordinate is:");
119         point2.display();
120         System.out.print("The line's length is:");
121         System.out.printf("%.2f\n", getDistance());
122     }
123 }

分析:在7-1 点与线(类设计)基础上添加Element抽象类和它的子类Plane,并把7-1中的Point类和Line类继承于抽象类Element。

采坑心得
在抽象类Element中需要将public void display() {}改为抽象方法abstract public void display();否则不符合题意;注意如何使用多态,使父类变量引用子类对象及相对应的方法。

 

(6)题目集 期中考试:7-3 点线面问题再重构(容器类)设计与分析

  1 import java.util.Scanner;
  2 import java.util.ArrayList;
  3 public class Main{
  4     public static void main(String[] args) {
  5         Scanner input=new Scanner(System.in);
  6         GeometryObject g =new GeometryObject();
  7          int  choice = input.nextInt();
  8             while(choice != 0) {
  9                 switch(choice) {
 10                 case 1://insert Point object into list 
 11                     double x=input.nextDouble();
 12                     double y=input.nextDouble();                
 13                     Point point=new Point(x,y);
 14                     Element a= point;
 15                     g.add(a);
 16                     break;
 17                 case 2://insert Line object into list
 18                     double x1=input.nextDouble();
 19                     double y1=input.nextDouble();
 20                     double x2=input.nextDouble();
 21                     double y2=input.nextDouble();
 22                     String color=input.next();
 23                     Point point1=new Point(x1,y1);
 24                     Point point2=new Point(x2,y2);
 25                     Line line=new Line(point1,point2,color);
 26                     Element b= line;
 27                     g.add(b);
 28                     break;
 29                 case 3://insert Plane object into list
 30                     String color1=input.next();
 31                     Plane plane=new Plane(color1);
 32                     Element c= plane;
 33                     g.add(c);
 34                     break;
 35                 case 4://delete index - 1 object from list
 36                     int index = input.nextInt();
 37                     if(index>=1) {
 38                         g.remove(index-1);
 39                     }    
 40               
 41                 }
 42                 choice = input.nextInt();
 43             }
 44             ArrayList<Element> list1=new ArrayList<>();
 45             list1=g.getList();
 46             for(int i=0;i<list1.size();i++) {
 47                 list1.get(i).display();
 48             }
 49     }
 50 }
 51 
 52 class GeometryObject{
 53     private ArrayList<Element> list=new ArrayList<>();
 54     GeometryObject(){
 55     }
 56     public void add(Element element) {
 57         list.add(element);
 58     }
 59     public void remove(int index) {
 60         list.remove(index);
 61     }
 62     public ArrayList<Element> getList(){
 63         return list;
 64     }
 65 }
 66 abstract class Element{
 67     
 68     public void display() {
 69         
 70     }
 71 }
 72 
 73 class Plane extends Element{
 74     private String color;
 75     Plane(){    
 76     }
 77     Plane(String color){    
 78         this.color=color;
 79     }
 80     public String getColor() {
 81         return color;
 82     }
 83     public void setColor(String color) {
 84         this.color=color;
 85     }
 86     public void display() {
 87         System.out.print("The Plane's color is:"+color);
 88     }
 89 }
 90 
 91 class Point extends Element{
 92     private double x;
 93     private double y;
 94     Point(){    
 95     }
 96     Point(double x,double y){
 97         this.x=x;
 98         this.y=y;
 99     }
100     public double getX() {
101         return x;
102     }
103     public void setX(double x) {
104         this.x=x;
105     }
106     public double getY() {
107         return y;
108     }
109     public void setY(double y) {
110         this.y=y;
111     }
112     public void display() {
113         if(x>0&&x<=200&&y>0&&y<=200) {
114             System.out.print("(");
115             System.out.printf("%.2f,", x);
116             System.out.printf("%.2f)\n", y);
117         }
118         else System.out.print("Wrong Format");
119     }
120 }
121 class Line extends Element{
122     private Point point1=new Point();
123     private Point point2=new Point();
124     private String color;
125     public Line() {        
126     }
127     public Line(Point point1,Point point2,String color) {
128         this.point1=point1;
129         this.point2=point2;
130         this.color=color;
131     }
132     public Point getPoint1() {
133         return point1;
134     }
135     public void setPoint1(Point point1) {
136         this.point1=point1;
137     }
138     public Point getPoint2() {
139         return point2;
140     }
141     public void setPoint2(Point point2) {
142         this.point2=point2;
143     }
144     public String getColor() {
145         return color;
146     }
147     public void setColor(String color) {
148         this.color=color;
149     }
150     public double getDistance() {
151         return Math.sqrt(Math.pow(point1.getX()-point2.getX(),2)+Math.pow(point1.getY()-point2.getY(),2));
152     }
153     public void display() {
154         System.out.println("The line's color is:"+color);
155         System.out.println("The line's begin point's Coordinate is:");
156         point1.display();
157         System.out.println("The line's end point's Coordinate is:");
158         point2.display();
159         System.out.print("The line's length is:");
160         System.out.printf("%.2f\n", getDistance());
161     }
162 }

分析:在7-2 点线面问题重构(继承与多态)继承上添加容器类GeometryObject,其属性为ArrayList<Element>类型的对象;使其可以通过选项来进行相应的使用,用容器来存储当前所以数据。

采坑心得
首先要掌握ArrayList<Element>类型的对象如何使用,理解之间的关系,梳理逻辑关系,防止写到后面有些懵;删除容器中第index - 1个数据时需要进行判断,要删除的数据不能小于0或大于容器的大小。

 

  总结: 在第二阶段的学习中。对于类以及对象有了更深的了解,并在基础的语法下可以使用继承、多态、抽象类、包装类进行编写代码,对JAVA有了进一步的理解;面向对象的程序设计的重点在于如何创建对象,以及使用对象来做一些事,这使得理解类与对象是尤为的重要,自身不足有以下点:

 

1.当类和对象非常多少,导致我对之间的关系有些混乱,这说明我还需要多加练习,应用需要更加熟练。

 

2.对包装类中的方法不够清楚,导致使用时常常报错,在之后需要多次使用其中的方法来增加印象。

 

3.数学计算能力较差,导致解决问题时不能立刻想出解决问题的办法,解决方案是在其他软件上进行刷题,可以增加自己的解决问题的能力以及对知识点的巩固。

  感谢OOP课程老师的指导与作业的监督,,老师的每节课都是干货满满,同时也希望老师可以发一些的题,使我们能在困难中提升自己编程能力,提升自己对问题的解决能力。

 

posted on 2022-05-09 17:21  霍家少爷  阅读(166)  评论(0)    收藏  举报