oop課程第二階段 Blog-2
·前言:第二階段的提交已經結束,在此對第二階段題目集的知識點、題量、難度進行總結,並發表自己的看法。
(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課程老師的指導與作業的監督,,老師的每節課都是乾貨滿滿,同時也希望老師可以發一些的題,使我們能在困難中提升自己程式設計能力,提升自己對問題的解決能力。