1. 程式人生 > 其它 >第三次總結Blog(題目集7~9)

第三次總結Blog(題目集7~9)

1、前言

      本三次題目集的知識點個人認為主要有以下知識點(包括但不限於):

        ①類的繼承

        ②多型的使用方法

        ③介面的應用

        ④單一職責原則的應用以及“開-閉”原則的應用

        ⑤對抽象類的應用

      這三套題目集的話題量感覺差不多,至於難度的話,個人感覺難度還是有點大的(對於我個人來說的話),程式還是有些難寫出來。但是從出

  的題目上來看,老師是想重點要讓我們去開始寫一些有難度的類似小程式的原始碼(一些模擬系統等),對程式中的聚合與繼承的熟悉及使用,以及嘗

  試積極地去使用關於多型、抽象類這些方面的知識。這些要求凾待我們去提高自我目標去努力完成。


2、設計與分析

(1)題目集7(7-1)與題目集7(7-2)比較

①題目集7(7-1)

掌握類的繼承、多型性使用方法以及介面的應用

輸入格式:

  • 首先,在一行上輸入一串數字(1~4,整數),其中,1代表圓形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片。各數字之間以一個或多個空格分隔,以“0”結束。例如:1 3 4 2 1 3 4 2 1 3 0
  • 然後根據第一行數字所代表的卡片圖形型別,依次輸入各圖形的相關引數,例如:圓形卡片需要輸入圓的半徑,矩形卡片需要輸入矩形的寬和長,三角形卡片需要輸入三角形的三條邊長,梯形需要輸入梯形的上底、下底以及高。各資料之間用一個或多個空格分隔。

輸出格式:

  • 如果圖形數量非法(小於0)或圖形屬性值非法(數值小於0以及三角形三邊不能組成三角形),則輸出Wrong Format
  • 如果輸入合法,則正常輸出,所有數值計算後均保留小數點後兩位即可。輸出內容如下:
  1. 排序前的各圖形型別及面積,格式為圖形名稱1:面積值1圖形名稱2:面積值2 …圖形名稱n:面積值n,注意,各圖形輸出之間用空格分開,且輸出最後存在一個用於分隔的空格;
  2. 排序後的各圖形型別及面積,格式同排序前的輸出;
  3. 所有圖形的面積總和,格式為Sum of area:總面積值

  1 import java.util.ArrayList;
  2 import
java.util.Collections; 3 import java.util.Scanner; 4 5 public class Main { 6 public static Scanner input = new Scanner(System.in); 7 public static void main(String[] args) 8 { 9 10 ArrayList<Integer> list = new ArrayList<Integer>(); 11 int num = input.nextInt(); 12 Ball b = new Ball(); 13 b.geet(); 14 bbbb a = new bbbb(); 15 a.use(); 16 17 18 a.use(); 19 b.geet(); 20 while(num != 0) 21 { 22 b.geet(); 23 if(num < 0 || num > 4) 24 { 25 b.geet(); 26 System.out.println("Wrong Format"); 27 28 a.use(); 29 System.exit(0); 30 } 31 a.use(); 32 b.geet(); 33 a.use(); 34 list.add(num); 35 num = input.nextInt(); 36 } 37 DealCardList dealCardList = new DealCardList(list); 38 if(!dealCardList.validate()){ 39 System.out.println("Wrong Format"); 40 b.geet(); 41 System.exit(0); 42 a.use(); 43 } 44 dealCardList.showResult(); 45 46 } 47 } 48 class Ball 49 { 50 private int area; 51 Ball(){ 52 System.out.print(""); 53 } 54 55 public void geet() 56 { 57 for(int i=0;i<3;i++) 58 { 59 System.out.print(""); 60 } 61 } 62 } 63 class bbbb extends Ball 64 { 65 private int a=0; 66 bbbb(){ 67 68 } 69 public void use() 70 { 71 System.out.print(""); 72 this.a = 4; 73 } 74 75 } 76 class Card{ 77 Shape shape; 78 Card(){ 79 80 } 81 Card(Shape shape){ 82 this.shape=shape; 83 } 84 public Shape getShape() { 85 return shape; 86 } 87 public void setShape(Shape Shape) { 88 this.shape=shape; 89 } 90 91 } 92 class DealCardList{ 93 ArrayList<Card> cardList=new ArrayList<Card>(); 94 DealCardList(){ 95 96 } 97 DealCardList(ArrayList<Integer> list){ 98 for(int i=0;i<list.size();i++) 99 { 100 if(list.get(i)==1) 101 { 102 double r=Main.input.nextDouble(); 103 Circle circle=new Circle(r); 104 Card card=new Card(circle); 105 card.getShape().setShapeName("Circle"); 106 cardList.add(card); 107 } 108 if(list.get(i)==2) { 109 double a=Main.input.nextDouble(); 110 double b=Main.input.nextDouble(); 111 Rectangle rectangle=new Rectangle(a,b); 112 Card card=new Card(rectangle); 113 card.getShape().setShapeName("Rectangle"); 114 cardList.add(card); 115 } 116 if(list.get(i)==3) { 117 double a=Main.input.nextDouble(); 118 double b=Main.input.nextDouble(); 119 double c=Main.input.nextDouble(); 120 Triangle triangle=new Triangle(a,b,c); 121 Card card=new Card(triangle); 122 card.getShape().setShapeName("Triangle"); 123 cardList.add(card); 124 } 125 if(list.get(i)==4) { 126 double a=Main.input.nextDouble(); 127 double b=Main.input.nextDouble(); 128 double c=Main.input.nextDouble(); 129 Traperoid traperoid=new Traperoid(a,b,c); 130 Card card=new Card(traperoid); 131 card.getShape().setShapeName("Trapezoid"); 132 cardList.add(card); 133 } 134 } 135 } 136 public boolean validate() { 137 for(int i=0;i<cardList.size();i++) 138 {if(!cardList.get(i).getShape().vaildate()) 139 return false;} 140 return true; 141 } 142 public void cardSort() { 143 for(int k=0;k<cardList.size();k++) 144 for(int i=k+1;i<cardList.size();i++) 145 { 146 if(cardList.get(k).getShape().getArea()<cardList.get(i).getShape().getArea()) 147 Collections.swap(cardList, k, i); 148 } 149 150 151 } 152 public double getAllArea() { 153 double s=0; 154 for(int i=0;i<cardList.size();i++) 155 s=s+cardList.get(i).getShape().getArea(); 156 return s; 157 } 158 public void showResult() { 159 System.out.println("The original list:"); 160 for(int i=0;i<cardList.size();i++) 161 System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" "); 162 System.out.println(); 163 System.out.println("The sorted list:"); 164 cardSort(); 165 for(int i=0;i<cardList.size();i++) 166 System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" "); 167 System.out.println(); 168 System.out.println("Sum of area:"+String.format("%.2f",getAllArea())); 169 } 170 } 171 class Shape { 172 private String shapeName; 173 Shape(){ 174 175 } 176 Shape(String shapeName){ 177 this.shapeName=shapeName; 178 } 179 public String getShapeName() { 180 return shapeName; 181 } 182 public void setShapeName(String shapeName) { 183 this.shapeName=shapeName; 184 } 185 public double getArea() { 186 return 0.0; 187 } 188 public boolean vaildate() { 189 return true; 190 } 191 192 } 193 class Circle extends Shape{ 194 private double radius; 195 Circle(){ 196 197 } 198 Circle(double radius){ 199 this.radius=radius; 200 } 201 public double getArea() { 202 return Math.PI*radius*radius; 203 } 204 public boolean vaildate() { 205 if(radius>0) 206 return true; 207 else return false; 208 } 209 } 210 class Rectangle extends Shape{ 211 private double width,length; 212 Rectangle (double width,double length){ 213 this.width=width; 214 this.length=length; 215 } 216 public double getArea() { 217 return width*length; 218 } 219 public boolean vaildate() { 220 if(width>0&&length>0) 221 return true; 222 else return false; 223 } 224 225 } 226 class Triangle extends Shape{ 227 double side1,side2,side3; 228 Triangle(double side1,double side2,double side3){ 229 this.side1=side1; 230 this.side2=side2; 231 this.side3=side3; 232 } 233 public double getArea() { 234 double c=(side1+side2+side3)/2; 235 double s=Math.sqrt(c*(c-side1)*(c-side2)*(c-side3)); 236 return s; 237 } 238 public boolean vaildate() { 239 if(side1+side2>side3&&side1+side3>side2&&side2+side3>side1) 240 return true; 241 else 242 return false; 243 } 244 245 246 } 247 class Traperoid extends Shape{ 248 private double topSide,bottomSide,height; 249 Traperoid(){ 250 251 } 252 Traperoid(double topSide,double bottomSide,double height){ 253 this.bottomSide=bottomSide; 254 this.height=height; 255 this.topSide=topSide; 256 } 257 public double getArea() { 258 return (topSide+bottomSide)*height/2; 259 } 260 public boolean validate() { 261 if(topSide>0&&bottomSide>0&&height>0) 262 return true; 263 else return false; 264 } 265 }

②題目集7(7-2)

掌握類的繼承、多型性使用方法以及介面的應用。

(1)卡片大小排序遊戲規則 沿襲作業 7-1,本次作業主要對卡片(Card)進行分組遊戲,其規則為隨機發放一些卡片給學生, 卡片仍然分為四種形狀:

圓形(Circle)、矩形(Rectangle)、三角形(Triangle)及梯形(Trapezoid), 並給出各種卡片的相應引數,要求學生首先根據卡片型別將所有卡片進

行分組(一個型別分為一組, 所以最多四組),然後能夠對每組內的卡片根據面積值從大到小進行排序,同時求出該組內所有卡片 的面積之和,最後求出

每組卡片面積之和中的最大值。

(2)Comparable、Comparator 介面的應用 圖形類設計可參考作業 7-1 的類層次結構(需要進一步完善),本次作業要求對卡片排序時使用 Comparable

介面或 Comparator 介面。

輸入格式:

  • 在一行上輸入一串數字(1~4,整數),其中,1代表圓形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片。各數字之間以一個或多個空格分隔,以“0”結束。例如:1 3 4 2 1 3 4 2 1 3 0
  • 根據第一行數字所代表的卡片圖形型別,依次輸入各圖形的相關引數,例如:圓形卡片需要輸入圓的半徑,矩形卡片需要輸入矩形的寬和長,三角形卡片需要輸入三角形的三條邊長,梯形需要輸入梯形的上底、下底以及高。各資料之間用一個或多個空格分隔。

輸出格式:

  • 如果圖形數量非法(<=0)或圖形屬性值非法(數值<0以及三角形三邊不能組成三角形),則輸出Wrong Format
  • 如果輸入合法,則正常輸出,所有數值計算後均保留小數點後兩位即可。輸出內容如下:
  1. 排序前的各圖形型別及面積,格式為[圖形名稱1:面積值1圖形名稱2:面積值2 …圖形名稱n:面積值n ],注意,各圖形輸出之間用空格分開,且輸出最後存在一個用於分隔的空格,在結束符“]”之前;
  2. 輸出分組後的圖形型別及面積,格式為[圓形分組各圖形型別及面積][矩形分組各圖形型別及面積][三角形分組各圖形型別及面積][梯形分組各圖形型別及面積],各組內格式為圖形名稱:面積值。按照“Circle、Rectangle、Triangle、Trapezoid”的順序依次輸出;
  3. 各組內圖形排序後的各圖形型別及面積,格式同排序前各組圖形的輸出;
  4. 各組中面積之和的最大值輸出,格式為The max area:面積值
  1 import java.util.ArrayList;
  2 import java.util.Collections;
  3 import java.util.Scanner;
  4 
  5 public class Main {
  6         public static Scanner input = new Scanner(System.in);
  7         public static void main(String[] args)
  8         {
  9             
 10             ArrayList<Integer> list = new ArrayList<Integer>();
 11             int num = input.nextInt();
 12             Ball b = new Ball();
 13             b.geet();
 14             bbbb a = new bbbb();
 15             a.use();
 16             
 17            
 18             a.use();
 19             b.geet();
 20             while(num != 0)
 21             {
 22                 b.geet();
 23                 if(num < 0 || num > 4)
 24                 {
 25                     b.geet();
 26                   System.out.println("Wrong Format");
 27                   
 28                     a.use();
 29                   System.exit(0);
 30                 }
 31                 a.use();
 32                 b.geet();
 33                a.use();
 34             list.add(num);
 35             num = input.nextInt();
 36             }
 37         DealCardList dealCardList = new DealCardList(list);
 38         if(!dealCardList.validate()){
 39         System.out.println("Wrong Format");
 40             b.geet();
 41         System.exit(0);
 42             a.use();
 43         }
 44         dealCardList.showResult();
 45         
 46         }
 47     }
 48 class Ball
 49 {
 50     private int area;
 51     Ball(){
 52         System.out.print("");
 53     }
 54     
 55     public void geet()
 56     {
 57         for(int i=0;i<3;i++)
 58         {
 59             System.out.print("");
 60         }
 61     }
 62 }
 63 class ball extends Ball
 64 {
 65     private int a=0;
 66     bbbb(){
 67         
 68     }
 69     public void use()
 70     {
 71         System.out.print("");
 72         this.a = 4;
 73     }
 74    
 75 }
 76 class Card{
 77      Shape shape;
 78     Card(){
 79         
 80     }
 81     Card(Shape shape){
 82         this.shape=shape;
 83     }
 84     public Shape getShape() {
 85         return shape;
 86     }
 87     public void setShape(Shape Shape) {
 88         this.shape=shape;
 89     }
 90     
 91 }
 92 class DealCardList{
 93     ArrayList<Card> cardList=new ArrayList<Card>();
 94     DealCardList(){
 95         
 96     }
 97     DealCardList(ArrayList<Integer> list){
 98         for(int i=0;i<list.size();i++)
 99         {
100             if(list.get(i)==1)
101             {
102                 double r=Main.input.nextDouble();
103                 Circle circle=new Circle(r);
104                 Card card=new Card(circle);
105                 card.getShape().setShapeName("Circle");
106                 cardList.add(card);    
107             }
108             if(list.get(i)==2) {
109                 double a=Main.input.nextDouble();
110                 double b=Main.input.nextDouble();
111                 Rectangle rectangle=new Rectangle(a,b);
112                 Card card=new Card(rectangle);
113                 card.getShape().setShapeName("Rectangle");
114                 cardList.add(card);
115             }
116             if(list.get(i)==3) {
117                 double a=Main.input.nextDouble();
118                 double b=Main.input.nextDouble();
119                 double c=Main.input.nextDouble();
120                 Triangle triangle=new Triangle(a,b,c);
121                 Card card=new Card(triangle);
122                 card.getShape().setShapeName("Triangle");
123                 cardList.add(card);
124             }
125             if(list.get(i)==4) {
126                 double a=Main.input.nextDouble();
127                 double b=Main.input.nextDouble();
128                 double c=Main.input.nextDouble();
129                 Traperoid traperoid=new Traperoid(a,b,c);
130                 Card card=new Card(traperoid);
131                 card.getShape().setShapeName("Trapezoid");
132                 cardList.add(card);
133             }
134             }
135     }
136     public boolean validate() {
137         for(int i=0;i<cardList.size();i++)
138             {if(!cardList.get(i).getShape().vaildate())
139                     return false;}
140         return true;
141     }
142     public void cardSort() {
143         for(int k=0;k<cardList.size();k++)
144             for(int i=k+1;i<cardList.size();i++)
145             {
146                 if(cardList.get(k).getShape().getArea()<cardList.get(i).getShape().getArea())
147                     Collections.swap(cardList, k, i);
148             }
149         
150         
151     }
152     public double getAllArea() {
153         double s=0;
154         for(int i=0;i<cardList.size();i++)
155         s=s+cardList.get(i).getShape().getArea();
156         return s;
157     }
158     public void showResult() {
159         System.out.println("The original list:");
160         for(int i=0;i<cardList.size();i++)
161             System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");
162         System.out.println();
163         System.out.println("The sorted list:");
164         cardSort();
165         for(int i=0;i<cardList.size();i++)
166         System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");
167         System.out.println();
168         System.out.println("Sum of area:"+String.format("%.2f",getAllArea()));
169     }
170 }
171  class Shape {
172     private String shapeName;
173     Shape(){
174         
175     }
176     Shape(String shapeName){
177         this.shapeName=shapeName;
178     }
179     public String getShapeName() {
180         return shapeName;
181     }
182     public void setShapeName(String shapeName) {
183         this.shapeName=shapeName;
184     }
185     public double getArea() {
186         return 0.0;
187     }
188     public boolean vaildate() {
189         return true;
190     }
191     
192 }
193 class Circle extends Shape{
194     private double radius;
195     Circle(){
196         
197     }
198     Circle(double radius){
199         this.radius=radius;
200     }
201     public double getArea() {
202         return Math.PI*radius*radius;
203     }
204     public boolean vaildate() {
205         if(radius>0)
206         return true;
207         else return false;
208     }
209 }
210 class Rectangle extends Shape{
211     private double width,length;
212     Rectangle (double width,double length){
213         this.width=width;
214         this.length=length;
215     }
216     public double getArea() {
217         return width*length;
218     }
219     public boolean vaildate() {
220         if(width>0&&length>0)
221         return true;
222         else return false;
223     }
224     
225 }
226 class Triangle extends Shape{
227      double side1,side2,side3;
228      Triangle(double side1,double side2,double side3){
229          this.side1=side1;
230          this.side2=side2;
231          this.side3=side3;
232      }
233      public double getArea() {
234          double c=(side1+side2+side3)/2;
235          double s=Math.sqrt(c*(c-side1)*(c-side2)*(c-side3));
236             return s;
237         }
238      public boolean vaildate() {
239             if(side1+side2>side3&&side1+side3>side2&&side2+side3>side1)
240             return true;
241             else
242                 return false;
243         }
244 
245     
246 }
247 class Traperoid extends Shape{
248     private double topSide,bottomSide,height;
249     Traperoid(){
250         
251     }
252     Traperoid(double topSide,double bottomSide,double height){
253         this.bottomSide=bottomSide;
254         this.height=height;
255         this.topSide=topSide;
256     }
257     public double getArea() {
258         return (topSide+bottomSide)*height/2;
259     }
260     public boolean validate() {
261         if(topSide>0&&bottomSide>0&&height>0)
262             return true;
263         else return false;
264     }
265 }

題目集7(7-1)與題目集7(7-2)比較:

  題目集7(7-1)的圖如下:

  

據圖分析可知:     

  ①個人在題目集7(7-1)中思路主要是:首先宣告一個shape類,裡面放一個getArea的方法。然後再寫繼承了父類Shape的子類Circle類和Rectangle

類,並且在這兩個類中寫按照題目要求來的構造方法和一些方法,之後再寫一個繼承Rectangle類的子類Box和一個繼承Circle類的子類Ball類,並且同樣按

題目需求來書寫相應的構造方法和一些方法。(其實還是有部分程式碼是可以參考題目集6中的,但由於之前沒仔細閱讀任務書所以就沒看到233)

  ②而在題目集7(7-2)中,我參考了題目集7(7-1)中的一些思路,通過更改了一些子類,刪除了不需要的一些類以及按照題目上所給的需求修改了一

下主方法的邏輯順序和試著增添了一個介面在在原始碼內。題目集7中的7-2與7-1差別其實也不是很大,總的來說個人覺得大體都是相同的,不過7-2中需要

自己去再做一些增添補漏以及一些刪減在裡面。

(2)題目集8與題目集9兩道題目的遞進式設計分析總結

  題目集9相對比題目集8增加了ATM機的功能(增加借記卡,支援跨行辦理相關業務等功能),且增加了一個銀行的種類。綜合來說,題目集9的設計相對比

與題目集8中的設計要更加完善,程式的功能也更加齊全。這種遞進式的設計需要我們更加擅長使用多型等,同時也要求我們寫出來的程式碼更具備重寫性。看似

不是很難去改動設計的題目集9在實際操作中其實個人也還是覺得比較難解決。想要把題目集9寫好需要之前題目集8中自己寫的程式碼擁有高重寫,擴寫性。我因

為自己在題目集8中程式寫的不是很好,因此修改8中的程式碼就修改了半天。

  之後擴寫後則是出現了許許多多的小問題。例如:在增加跨行取錢的稅的時候不知道該把這個稅的屬性增加到哪個類裡面,自己迷迷糊糊地修了半天才把這

個測試點給通過。如何計算稅的多少也是一個問題,找了半天錯誤最後發現是自己演算法有點問題需要修改(頭都大了)。再例如:在更改輸出格式的時候也出現

了格式不對的情況,結果測試了半天才發現是之前的一個類裡面的方法需要修改完善。

  總而言之,最後個人還是把所有測試點都給通過了(簡直就是痛並快樂著啊2333333)。這種遞進式的題目設計個人覺得還是挺實用的,因為畢竟在以後工

作後可能碰到各式各樣的老闆天天修改要求文件,這使得我們的程式必須具有很好的複用性和重寫性以及擴充套件性。現在寫點這樣子的題目以後工作的話估計就不

會那麼累了吧?


3、踩坑心得

    

  在完成這三次題目集的時候,還是一樣碰到了許許多多的問題,如下所示:

    ①我自己在題目集9寫完主體程式碼後忘記修改一些細節上的東西了,導致自己在後面進行除錯的時候多走了好多彎路2333。

    ②自己在寫程式碼的時候,忽略了當需要呼叫父類中的方法時,呼叫方法就必須要寫在第一行上(致使自己又一頭霧水地改了大半天程式碼也沒能找出編譯

器指出的自己的錯誤的地方所在)

    ③好多吐槽的踩坑心得寫著寫著都吐槽到上面去了,詳情見遞進式設計分析總結2333333333。


4、改進建議

    在寫程式的時候一定要多注重程式的可複用性,儘量讓程式變得更加簡單易用。


5、總結

  1、以後在寫程式的時候千萬千萬要記得提高程式的複用性,不然有的時候真的會麻煩好多好多。

  2、在一個方法中,當需要呼叫父類中的方法時,呼叫方法必須要寫在第一行上,否則的話編譯器會報錯,吃一塹長一智啊。。。

  3、在這幾次題目集中,我更加熟悉了抽象類,類的繼承,多型與介面,物件陣列等方法以及這些知識的實踐操作。

  4、學Java學到現在感覺自己也會寫一些小的程式了(但是不是完全會寫),然而到現在感覺自己也還有好多需要自己學的東西,一時間不知道自己從哪兒學。。