前言

  这几次作业主要考察对于继承与多态、正则表达式、Lambda表达式、JavaFX和类设计,尤其是对于类设计的考察程度更加深入。

同时,这几次作业相比前几个星期的难度提升很大,需要投入更多的时间。

设计与分析

ATM机类结构设计

题目描述如下:

类图如下:

 

 

源码如下:

 

  1 import java.util.ArrayList;
  2 import java.util.Scanner;
  3 import javax.xml.parsers.*;
  4 import org.w3c.dom.*;
  5 import java.io.*;
  6  
  7 public class Main {
  8 
  9     public static void main(String[] args) {
 10           ATM atm;
 11           atm = (ATM)XMLUtil.getBean("ATM");
 12           atm.start();
 13   }
 14 
 15 }
 16 
 17 class Account {
 18 private double money;
 19 private String bankID;
 20 private ArrayList<Card> cards = new ArrayList();
 21 
 22 public Account() {
 23 this.money = 10000.00;
 24 }
 25 
 26 public Account(String bankID) {
 27 this.bankID = bankID;
 28 this.money = 10000.00;
 29 }
 30 
 31 public double getMoney() {
 32 return money;
 33 }
 34 
 35 public void setMoney(double money) {
 36 this.money = money;
 37 }
 38 
 39 public String getAccountID() {
 40 return bankID;
 41 }
 42 
 43 public void setAccountID(String accountID) {
 44 this.bankID = accountID;
 45 }
 46 
 47 public ArrayList<Card> getCards() {
 48 return cards;
 49 }
 50 
 51 public void setCards(ArrayList<Card> cards) {
 52 this.cards = cards;
 53 }
 54 
 55 }
 56 
 57 class ATM {
 58     private Control control = new Control01();
 59     
 60     public ATM() {
 61     }
 62 
 63     public void start() {
 64         control.getInput().input();
 65         while(control.getInput().getPoints()[0].equals("#") == false) {
 66             if(control.judge1(control.getInput())) {
 67                 if(control.getInput().getPoints().length == 1) {
 68                     System.out.printf("¥%.2f",getBalance(control.getInput()));
 69                 }else {
 70                     if(control.judge2(control.getInput())) {
 71                         if(control.judge3(control.getInput())) {
 72                             if(control.judge5(control.getInput())) {
 73                                 double money = Double.parseDouble(control.getInput().getPoints()[3]);
 74                                 if(money<0) {
 75                                     change(control.getInput(),money);
 76                                     System.out.printf(getUserName(control.getInput())+"在"+getBankName(control.getInput())+"的"+control.getInput().getPoints()[2]+"号ATM机上存款¥%.2f",-money);
 77                                     System.out.println("");
 78                                     System.out.printf("当前余额为¥%.2f",getBalance(control.getInput()));
 79                                 }else {
 80                                     change(control.getInput(),money);
 81                                     if(control.judge4(getBalance(control.getInput()))) {
 82                                         System.out.printf(getUserName(control.getInput())+"在"+getBankName(control.getInput())+"的"+control.getInput().getPoints()[2]+"号ATM机上取存款¥%.2f",money);
 83                                         System.out.println("");
 84                                         System.out.printf("当前余额为¥%.2f",getBalance(control.getInput()));
 85                                     }else {
 86                                         Output.output(4);
 87                                         change(control.getInput(),-money);
 88                                     }
 89                                 }
 90                             }else {
 91                                 Output.output(5);
 92                             }
 93                         }else {
 94                             Output.output(3);
 95                         }
 96                     } else {
 97                         Output.output(2);
 98                     }
 99                 }
100             }else {
101                 Output.output(1);
102             }
103             control.getInput().input();
104         }
105     }
106     
107     public double getBalance(Input input) {
108         int[] index = control.getIndex(input);
109         double balance = control.getUnionPay().getBanks().get(index[0]).getUsers().get(index[1]).getAccounts().get(index[2]).getMoney();
110         return balance;
111     }
112     
113     public String getUserName(Input input) {
114         int[] index = control.getIndex(input);
115         String useName = control.getUnionPay().getBanks().get(index[0]).getUsers().get(index[1]).getUserName();
116         return useName;
117     }
118     
119     public String getBankName(Input input) {
120         int[] index = control.getIndex(input);
121         String bankName = control.getUnionPay().getBanks().get(index[0]).getBankName();
122         return bankName;
123     }
124     
125     public void change(Input input,double money) {
126         int[] index = control.getIndex(input);
127         control.getUnionPay().getBanks().get(index[0]).getUsers().get(index[1]).getAccounts().get(index[2]).setMoney(getBalance(input) - money);
128     }
129 }
130 
131 class ATM01 extends ATM{
132 
133     public ATM01() {
134         super();
135     }
136 
137 }
138 
139 class Bank {
140     private String bankName;
141     private ArrayList<User> users = new ArrayList();
142     private ArrayList<String> IDsOfATM = new ArrayList();
143     
144     public Bank() {
145     }
146 
147     public Bank(String bankName) {
148         this.bankName = bankName;
149     }
150     
151     public String getBankName() {
152         return bankName;
153     }
154 
155     public void setBankName(String bankName) {
156         this.bankName = bankName;
157     }
158 
159     public ArrayList<User> getUsers() {
160         return users;
161     }
162 
163     public void setUsers(ArrayList<User> users) {
164         this.users = users;
165     }
166 
167     public ArrayList<String> getIDsOfATM() {
168         return IDsOfATM;
169     }
170 
171     public void setIDsOfATM(ArrayList<String> iDsOfATM) {
172         IDsOfATM = iDsOfATM;
173     }
174     
175     public void addIDsOfATM(String[] ID) {
176         for(int i = 0;i < ID.length;i++) {
177             IDsOfATM.add(ID[i]);
178         }
179     }
180 }
181 
182 class Bank01 extends Bank{
183 
184     public Bank01() {
185         super();
186     }
187     
188     public Bank01(String bankName) {
189         super(bankName);
190     }
191 
192 }
193 
194 class Card {
195     private String classOfCard;
196     private String cardID;
197     private String password;
198     
199     public Card() {
200     }
201     
202     public Card(String classOfCard,String cardID) {
203         this.classOfCard = classOfCard;
204         this.cardID = cardID;
205         this.password = "88888888";
206     }
207 
208     public String getCardID() {
209         return cardID;
210     }
211 
212     public void setCardID(String cardID) {
213         this.cardID = cardID;
214     }
215 
216     public String getClassOfCard() {
217         return classOfCard;
218     }
219 
220     public void setClassOfCard(String classOfCard) {
221         this.classOfCard = classOfCard;
222     }
223 
224     public String getPassword() {
225         return password;
226     }
227 
228     public void setPassword(String password) {
229         this.password = password;
230     }
231     
232 }
233 
234 class Control {
235     private UnionPay unionPay = new UnionPay();
236     private Input input = new Input();
237     
238     public Control() {
239         intialize();
240     }
241     
242     public UnionPay getUnionPay() {
243         return unionPay;
244     }
245 
246     public void setUnionPay(UnionPay unionPay) {
247         this.unionPay = unionPay;
248     }
249     
250     public Input getInput() {
251         return input;
252     }
253 
254     public void setInput(Input input) {
255         this.input = input;
256     }
257 
258     public void intialize() {
259         unionPay.getBanks().add(new Bank01("中国建设银行"));
260         unionPay.getBanks().add(new Bank01("中国工商银行"));
261         //ATM序号
262         String[] ATM1 = {"01","02","03","04"};
263         unionPay.getBanks().get(0).addIDsOfATM(ATM1);
264         String[] ATM2 = {"05","06"};
265         unionPay.getBanks().get(1).addIDsOfATM(ATM2);
266         //建设
267         //杨过
268         unionPay.getBanks().get(0).getUsers().add(new User01("杨过"));
269         unionPay.getBanks().get(0).getUsers().get(0).getAccounts().add(new Account("3217000010041315709"));
270         unionPay.getBanks().get(0).getUsers().get(0).getAccounts().get(0).getCards().add(new DebitCard("6217000010041315709"));
271         unionPay.getBanks().get(0).getUsers().get(0).getAccounts().get(0).getCards().add(new DebitCard("6217000010041315715"));
272         unionPay.getBanks().get(0).getUsers().get(0).getAccounts().add(new Account("3217000010041315709"));
273         unionPay.getBanks().get(0).getUsers().get(0).getAccounts().get(1).getCards().add(new DebitCard("6217000010041315718"));
274         //郭靖
275         unionPay.getBanks().get(0).getUsers().add(new User01("郭靖"));
276         unionPay.getBanks().get(0).getUsers().get(1).getAccounts().add(new Account("3217000010051320007"));
277         unionPay.getBanks().get(0).getUsers().get(1).getAccounts().get(0).getCards().add(new DebitCard("6217000010051320007"));
278         //工商
279         //张无忌
280         unionPay.getBanks().get(1).getUsers().add(new User01("张无忌"));
281         unionPay.getBanks().get(1).getUsers().get(0).getAccounts().add(new Account("3222081502001312389"));
282         unionPay.getBanks().get(1).getUsers().get(0).getAccounts().get(0).getCards().add(new DebitCard("6222081502001312389"));
283         unionPay.getBanks().get(1).getUsers().get(0).getAccounts().add(new Account("3222081502001312390"));
284         unionPay.getBanks().get(1).getUsers().get(0).getAccounts().get(1).getCards().add(new DebitCard("6222081502001312390"));
285         unionPay.getBanks().get(1).getUsers().get(0).getAccounts().add(new Account("3222081502001312399"));
286         unionPay.getBanks().get(1).getUsers().get(0).getAccounts().get(2).getCards().add(new DebitCard("6222081502001312399"));
287         unionPay.getBanks().get(1).getUsers().get(0).getAccounts().get(2).getCards().add(new DebitCard("6222081502001312400"));
288         //韦小宝
289         unionPay.getBanks().get(1).getUsers().add(new User01("韦小宝"));
290         unionPay.getBanks().get(1).getUsers().get(1).getAccounts().add(new Account("3222081502051320785"));
291         unionPay.getBanks().get(1).getUsers().get(1).getAccounts().get(0).getCards().add(new DebitCard("6222081502051320785"));
292         unionPay.getBanks().get(1).getUsers().get(1).getAccounts().add(new Account("3222081502051320786"));
293         unionPay.getBanks().get(1).getUsers().get(1).getAccounts().get(1).getCards().add(new DebitCard("6222081502051320786"));
294     }
295     public int[] getIndex(Input input) {
296         int[] index = new int[4];
297         for(int i = 0;i < unionPay.getBanks().size(); i++) {
298             for(int j = 0;j < unionPay.getBanks().get(i).getUsers().size(); j++) {
299                 for(int k = 0;k < unionPay.getBanks().get(i).getUsers().get(j).getAccounts().size(); k++) {
300                     for(int l = 0;l < unionPay.getBanks().get(i).getUsers().get(j).getAccounts().get(k).getCards().size(); l++) {
301                         String cardID = unionPay.getBanks().get(i).getUsers().get(j).getAccounts().get(k).getCards().get(l).getCardID();
302                         if(input.getPoints()[0].equals(cardID)) {
303                             index[0] = i;
304                             index[1] = j;
305                             index[2] = k;
306                             index[3] = l;
307                         }
308                     }
309                 }
310             }
311         }
312         return index;
313     }
314     
315     public boolean judge1(Input input) {
316         boolean result = false;
317         for(int i = 0;i < unionPay.getBanks().size(); i++) {
318             for(int j = 0;j < unionPay.getBanks().get(i).getUsers().size(); j++) {
319                 for(int k = 0;k < unionPay.getBanks().get(i).getUsers().get(j).getAccounts().size(); k++) {
320                     for(int l = 0;l < unionPay.getBanks().get(i).getUsers().get(j).getAccounts().get(k).getCards().size(); l++) {
321                         String cardID = unionPay.getBanks().get(i).getUsers().get(j).getAccounts().get(k).getCards().get(l).getCardID();
322                         if(input.getPoints()[0].equals(cardID)) {
323                             result = true;
324                         }
325                     }
326                 }
327             }
328         }
329         return result;
330     }
331     
332     public boolean judge2(Input input) {
333         boolean result = false;
334         
335         if(unionPay.getBanks().get(0).getIDsOfATM().contains(input.getPoints()[2])||unionPay.getBanks().get(1).getIDsOfATM().contains(input.getPoints()[2])) {
336             result = true;
337         }
338         return result;
339     }
340     
341     public boolean judge3(Input input) {
342         boolean result = false;
343         int[] index = getIndex(input);
344         String password = unionPay.getBanks().get(index[0]).getUsers().get(index[1]).getAccounts().get(index[2]).getCards().get(index[3]).getPassword();
345         if(input.getPoints()[1].equals(password)) {
346             result = true;
347         }
348         return result;
349     }
350     
351     public boolean judge4(double balance) {
352         boolean result = true;
353         if(balance < 0) {
354             result = false;
355         }
356         return result;
357     }
358     
359     public boolean judge5(Input input) {
360         boolean result = false;
361         int[] index = getIndex(input);
362         if(unionPay.getBanks().get(index[0]).getIDsOfATM().contains(input.getPoints()[2])) {
363             result = true;
364         }
365         return result;
366     }
367 }
368 
369 class Control01 extends Control{
370 
371     public Control01() {
372         super();
373     }
374 
375 }
376 
377 class DebitCard extends Card{
378 
379     public DebitCard(String cardID) {
380         super("借记卡",cardID);
381     }
382 
383 }
384 
385 class Input {
386     private String[] points;
387 
388     public Input() {
389     }
390 
391     public String[] getPoints() {
392         return points;
393     }
394 
395     public void setPoints(String[] points) {
396         this.points = points;
397     }
398     
399     public void input() {
400         Scanner in = new Scanner(System.in);
401         String allPoints = in.nextLine();
402         this.points = allPoints.split("\\s+");
403     }
404 }
405 
406 class Output {
407 
408     public Output() {
409     }
410 
411     public static void output(int choice) {
412         switch(choice) {
413         case 1:
414             System.out.println("Sorry,this card does not exist.");
415             break;
416         case 2:
417             System.out.println("Sorry,the ATM's id is wrong.");
418             break;
419         case 3:
420             System.out.println("Sorry,your password is wrong.");
421             break;
422         case 4:
423             System.out.println("Sorry,your account balance is insufficient.");
424             break;
425         case 5:
426             System.out.println("Sorry,cross-bank withdrawal is not supported.");
427             break;
428         }
429     }
430 }
431 
432 class UnionPay {
433     private ArrayList<Bank> banks = new ArrayList();
434     
435     public UnionPay() {
436     }
437 
438     public ArrayList<Bank> getBanks() {
439         return banks;
440     }
441 
442     public void setBanks(ArrayList<Bank> banks) {
443         this.banks = banks;
444     }
445     
446 }
447 
448 class User {
449     private String userName;
450     private ArrayList<Account> accounts = new ArrayList();
451     
452     public User() {
453     }
454     
455     public User(String userName) {
456         this.userName = userName;
457     }
458     
459     public User(String userName,String bankID,String cardID) {
460         this.userName = userName;
461         
462     }
463 
464     public String getUserName() {
465         return userName;
466     }
467 
468     public void setUserName(String userName) {
469         this.userName = userName;
470     }
471 
472     public ArrayList<Account> getAccounts() {
473         return accounts;
474     }
475 
476     public void setAccounts(ArrayList<Account> accounts) {
477         this.accounts = accounts;
478     }
479     
480 }
481 
482 class User01 extends User{
483 
484     public User01() {
485         // TODO Auto-generated constructor stub
486     }
487     
488     public User01(String userName) {
489         super(userName);
490     }
491 
492 }
493 
494 class XMLUtil {
495     //该方法用于从XML配置文件中提取具体类类名,并返回一个实例对象
496     public static Object getBean(String args) {
497         try {
498             //创建文档对象
499             DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
500             DocumentBuilder builder = dFactory.newDocumentBuilder();
501             Document doc;                            
502             doc = builder.parse(new File("src//test13//config.xml")); 
503             NodeList nl=null;
504             Node classNode=null;
505             String cName=null;
506             nl = doc.getElementsByTagName("className");
507             
508             //获取第一个包含类名的结点
509             if(args.equals("ATM")) {
510                 classNode=nl.item(0).getFirstChild();
511                 
512             }
513             
514             cName=classNode.getNodeValue();
515             //通过类名生成实例对象并将其返回
516             Class c=Class.forName(cName);
517               Object obj=c.newInstance();
518             return obj;        
519         }   
520         catch(Exception e) {
521             e.printStackTrace();
522             return null;
523         }
524     }
525 }

 

分析如下:此为我之后改进过的,在改进中我用到了继承和修改配置文件更改变量的方法以便于以后的迭代。在改进中我投入了很多时间,这也让我收获很多。

 

链表练习

题目说明如下:

类图如下:

源码如下:

  1 public class Main {
  2 
  3     public static void main(String[] args) {//下标从0开始
  4         Llist<String> list = new Llist<String>();
  5         //测试addisEmpty
  6         System.out.println("测试isEmpty:");
  7         if(list.isEmpty()) {
  8             System.out.println("...");
  9         }
 10         list.add("a");
 11         if(list.isEmpty()) {
 12             System.out.println("...");
 13         }
 14         //测试add
 15         System.out.println("测试add:");
 16         list.printList();
 17         list.add("b");
 18         list.add("c");
 19         list.printList();
 20         System.out.println(list.getData(1));
 21         //测试add(插入)
 22         System.out.println("测试add(插入):");
 23         list.printList();
 24         list.add(0,"d");
 25         System.out.println(list.getData(0));
 26         list.printList();
 27         list.add(3,"e");
 28         list.printList();
 29         //测试remove
 30         System.out.println("测试remove:");
 31         list.printList();
 32         list.remove(1);
 33         list.printList();
 34         //测试getFirst和getLast
 35         System.out.println("测试getFirst和getLast:");
 36         list.printList();
 37         System.out.println(list.getFirst());
 38         System.out.println(list.getLast());
 39         //测试getSize
 40         System.out.println("测试getSize:");
 41         list.printList();
 42         System.out.println(list.getSize());
 43         list.add("f");
 44         list.printList();
 45         System.out.println(list.getSize());
 46     }
 47 }
 48 
 49 class Node<E>{
 50     private E o;
 51     private Node<E> next;
 52     private Node<E> previous;
 53     
 54     public Node() {
 55     }
 56     
 57     public Node(E o,Node<E> previous,Node<E> next) {
 58         this.o = o;
 59         this.previous  =previous;
 60         this.next  =next;
 61     }
 62     
 63     public E getO() {
 64         return o;
 65     }
 66     public void setO(E o) {
 67         this.o = o;
 68     }
 69     public Node<E> getNext() {
 70         return next;
 71     }
 72     public void setNext(Node<E> next) {
 73         this.next = next;
 74     }
 75 
 76     public Node<E> getPrevious() {
 77         return previous;
 78     }
 79 
 80     public void setPrevious(Node<E> previous) {
 81         this.previous = previous;
 82     }
 83      
 84 }
 85 
 86 interface LinearListInterface<E>{
 87 
 88     public boolean isEmpty();
 89 
 90     public int getSize();
 91 
 92     public E getData(int index);
 93 
 94     public void remove(int index);
 95 
 96     public void add(int index, E theElement);
 97 
 98     public void add(E  element);
 99 
100     public void printList();    
101 
102     public E getFirst(); 
103 
104     public E getLast();
105 }
106 
107 class Llist<E> implements LinearListInterface<E>{
108     private Node<E> head = new Node();
109     private Node<E> curr;
110     private Node<E> tail;
111     private int size = 0;
112     
113     public Llist() {    
114     }
115     
116     public boolean isEmpty() {
117         return size == 0;
118     }
119 
120     public int getSize() {
121         return size;
122     }
123 
124     public E getData(int index) {
125         if(index >= size) {
126             System.out.println("Wrong Index");
127             System.exit(0);
128         }
129         curr = head;
130         for(int i = 0;i < index + 1; i++) {
131             curr = curr.getNext();
132         }
133         return curr.getO();
134     }
135 
136     public void remove(int index) {
137         if(index >= size) {
138             System.out.println("Wrong Index");
139             System.exit(0);
140         }
141         curr = head;
142         for(int i = 0;i < index ; i++) {
143             curr = curr.getNext();
144         }
145         curr.setNext(curr.getNext().getNext());
146         size--;
147     }
148 
149     public void add(int index, E theElement) {
150         if(index >= size) {
151             System.out.println("Wrong Index");
152             System.exit(0);
153         }
154         curr = head;
155         for(int i = 0;i < index; i++) {
156             curr = curr.getNext();
157         }
158         Node<E> newnode = new Node<E>(theElement,curr,curr.getNext());
159         curr = head;
160         for(int i = 0;i < index; i++) {
161             curr = curr.getNext();
162         }
163         curr.setNext(newnode);
164         size++;
165     }
166 
167     public void add(E  element) {
168         if(this.isEmpty()) {
169             curr = new Node<E>(element,null,null);
170             head.setNext(curr);
171             tail = curr;
172             size ++;
173         }else {
174             curr = tail;
175             tail = new Node<E>(element,curr.getPrevious(),null);
176             curr.setNext(tail);
177             size ++;
178         }
179         
180     }
181 
182     public void printList() {
183         curr = head.getNext();
184         while(curr != null) {
185             System.out.print("[" + curr.getO() + "]");
186             curr = curr.getNext();
187         }
188         System.out.println(" ");
189     }
190     
191     public E getFirst(){
192         return head.getNext().getO();
193     }
194 
195     public E getLast() {
196         return tail.getO();
197     }
198 }

源码如下:本题主要考察对于链表的理解以及接口的灵活使用。而对于链表的理解尤为重要,否则根本无从下手。同时把头指针设为空更好一点(我此处没有),因为这样便于实现在index为0前插入。

点线形系列

题目说明如下:

类图如下:

源码如下:

 

  1 import java.util.Scanner;
  2 import java.util.regex.Matcher;
  3 import java.util.regex.Pattern;
  4 
  5 public class Main {
  6 
  7     public static void main(String[] args) {
  8         Scanner in = new Scanner(System.in);;
  9         Judge judge = new Judge();
 10         String allPoints = in.nextLine();
 11         String[] half = allPoints.split(":");
 12         int choice = Integer.parseInt(half[0]);
 13         String[] points = half[1].split("\\s+");
 14         Calculate calculate = new Calculate();
 15         if(judge.formatValidate(points)) {
 16             switch(choice) {
 17             case 1 :
 18                 if(judge.numValidate(2,points)) {
 19                     calculate.choose1(points);
 20                 } else {
 21                     System.out.println("wrong number of points");
 22                 }
 23                     break;
 24             case 2 :
 25                 if(judge.numValidate(3,points)) {
 26                     calculate.choose2(points);
 27                 } else {
 28                     System.out.println("wrong number of points");
 29                 }
 30                     break;
 31             case 3 :
 32                 if(judge.numValidate(3,points)) {
 33                     calculate.choose3(points);
 34                 } else {
 35                     System.out.println("wrong number of points");
 36                 }
 37                     break;
 38             case 4 :
 39                 if(judge.numValidate(4,points)) {
 40                     calculate.choose4(points);
 41                 } else {
 42                     System.out.println("wrong number of points");
 43                 }
 44                     break;
 45             case 5 :
 46                 if(judge.numValidate(4,points)) {
 47                     calculate.choose5(points);
 48                 } else {
 49                     System.out.println("wrong number of points");
 50                 }
 51                     break;
 52             default:
 53                 System.out.println("Wrong Format");
 54             }
 55         }else {
 56             System.out.println("Wrong Format");
 57         }
 58 
 59     }
 60 
 61 }
 62 
 63 class Judge {
 64     
 65     public Judge() {
 66     }
 67 
 68     public boolean formatValidate(String[] points) {
 69         Pattern format=Pattern.compile("^([+-]?([0-9]|([1-9][0-9]*)))(\\.\\d+)?,([+-]?([0-9]|([1-9][0-9]*)))(\\.\\d+)?$");
 70         Matcher matcher;
 71         boolean validate = true;
 72         for(int i = 0; i < points.length; i++) {
 73             matcher = format.matcher(points[i]);
 74             if(!matcher.matches()) {
 75                 validate = false;
 76             }
 77         }
 78         return validate;
 79     }
 80     
 81     public boolean numValidate(int num, String[] points) {
 82         boolean validate = true;
 83         if(points.length > num) {
 84             validate = false;
 85         }
 86         return validate;
 87     }
 88 }
 89 
 90 class Calculate {
 91 
 92     public Calculate() {
 93     }
 94     
 95     public void choose1(String[] points) {
 96         double x1, y1, x2, y2, k;
 97         String[] points1 = points[0].split(",");
 98         String[] points2 = points[1].split(",");
 99         x1 = Double.parseDouble(points1[0]);
100         y1 = Double.parseDouble(points1[1]);
101         x2 = Double.parseDouble(points2[0]);
102         y2 = Double.parseDouble(points2[1]);
103         if(x1 == x2 && y1 == y2) {
104             System.out.println("points coincide");
105             System.exit(0);
106         }
107         if(x1 == x2) {
108             System.out.println("Slope does not exist");
109         }else {
110             k = (y2 - y1) / (x2 - x1);
111             System.out.println(k);
112         }
113     }
114     
115     public void choose2(String[] points) {
116         double x1, y1, x2, y2, x3, y3 , l;
117         String[] points1 = points[0].split(",");
118         String[] points2 = points[1].split(",");
119         String[] points3 = points[2].split(",");
120         x1 = Double.parseDouble(points1[0]);
121         y1 = Double.parseDouble(points1[1]);
122         x2 = Double.parseDouble(points2[0]);
123         y2 = Double.parseDouble(points2[1]);
124         x3 = Double.parseDouble(points3[0]);
125         y3 = Double.parseDouble(points3[1]);
126         if((x1 == x2 && y1 == y2) || (x1 == x3 && y1 == y3) || (x2 == x3 && y2 == y3)) {
127             System.out.println("points coincide");
128             System.exit(0);
129         }
130         if(x2 == x3 && y2 == y3) {
131             System.out.println("points coincide");
132             System.exit(0);
133         }
134         l = Math.abs((y1-y2)*x3+(x2-x1)*y3+x1*y2-y1*x2)/Math.sqrt((y1-y2)*(y1-y2) +(x1-x2)*(x1-x2));
135         System.out.println(l);
136     }
137     
138     public void choose3(String[] points) {
139         double x1, y1, x2, y2, x3, y3;
140         String[] points1 = points[0].split(",");
141         String[] points2 = points[1].split(",");
142         String[] points3 = points[2].split(",");
143         x1 = Double.parseDouble(points1[0]);
144         y1 = Double.parseDouble(points1[1]);
145         x2 = Double.parseDouble(points2[0]);
146         y2 = Double.parseDouble(points2[1]);
147         x3 = Double.parseDouble(points3[0]);
148         y3 = Double.parseDouble(points3[1]);
149         if(((y3 - y1 ) * (x2 - x1) - (y2 - y1) * (x3 - x1)) == 0) {
150             System.out.println("true");
151         }else {
152             System.out.println("false");
153         }
154     }
155     
156     public void choose4(String[] points) {
157         double x1, y1, x2, y2, x3, y3, x4, y4;
158         String[] points1 = points[0].split(",");
159         String[] points2 = points[1].split(",");
160         String[] points3 = points[2].split(",");
161         String[] points4 = points[3].split(",");
162         x1 = Double.parseDouble(points1[0]);
163         y1 = Double.parseDouble(points1[1]);
164         x2 = Double.parseDouble(points2[0]);
165         y2 = Double.parseDouble(points2[1]);
166         x3 = Double.parseDouble(points3[0]);
167         y3 = Double.parseDouble(points3[1]);
168         x4 = Double.parseDouble(points4[0]);
169         y4 = Double.parseDouble(points4[1]);
170         if((x1 == x2 && y1 == y2) || (x3 == x4 && y3 == y4)) {
171             System.out.println("points coincide");
172             System.exit(0);
173         }
174         if((x1 == x2 && x3 == x4) || (y2 - y1) * (x4 - x3) == (y4 - y3) * (x2 - x1)) {
175             System.out.println("true");
176         }else {
177             System.out.println("false");
178         }
179     }
180     
181     public void choose5(String[] points) {
182         double x1, y1, x2, y2, x3, y3, x4, y4;
183         String[] points1 = points[0].split(",");
184         String[] points2 = points[1].split(",");
185         String[] points3 = points[2].split(",");
186         String[] points4 = points[3].split(",");
187         x1 = Double.parseDouble(points1[0]);
188         y1 = Double.parseDouble(points1[1]);
189         x2 = Double.parseDouble(points2[0]);
190         y2 = Double.parseDouble(points2[1]);
191         x3 = Double.parseDouble(points3[0]);
192         y3 = Double.parseDouble(points3[1]);
193         x4 = Double.parseDouble(points4[0]);
194         y4 = Double.parseDouble(points4[1]);
195         if((x1 == x2 && y1 == y2) || (x3 == x4 && y3 == y4)) {
196             System.out.println("points coincide");
197             System.exit(0);
198         }
199         if((x1 == x2 && x3 == x4) || (y2 - y1) * (x4 - x3) == (y4 - y3) * (x2 - x1)) {
200             System.out.println("is parallel lines,have no intersection point");
201         }else {
202                 System.out.println("false");
203         }
204     }
205 }

 

分析如下:本题我设计了三个类Main、Judge、Calculate。Judge主要是用来判断是否符合要求,Calculate则主要用来运行各个选项。后面我在回过头来看自己的代码,我发现自己的代码可扩展性不高,如果提高扩展性会更好。

 

 

点线形系列迭代

题目说明如下:

类图如下:

源码如下:

  1 import java.util.Scanner;
  2 import java.util.regex.Matcher;
  3 import java.util.regex.Pattern;
  4 import java.text.DecimalFormat;
  5 public class Main {
  6     public static void main(String[] args) {
  7         Control control = new Control();
  8         control.start();
  9     }
 10 }
 11 class Control {
 12     Input input = new Input();
 13     Judge judge = new Judge();
 14     Calculate calculate = new Calculate();
 15     String[] points;
 16     int choice;
 17     public Control() {
 18     }
 19     public void split() {
 20         String in = input.input();
 21         String[] half = in.split(":");
 22         choice = Integer.valueOf(half[0]);
 23         points = half[1].split("\\s+");
 24     }
 25     public double[][] Points4(String[] points) {
 26         double x1, y1, x2, y2, x3, y3, x4, y4;
 27         String[] points1 = points[0].split(",");
 28         String[] points2 = points[1].split(",");
 29         String[] points3 = points[2].split(",");
 30         String[] points4 = points[3].split(",");
 31         x1 = Double.parseDouble(points1[0]);
 32         y1 = Double.parseDouble(points1[1]);
 33         x2 = Double.parseDouble(points2[0]);
 34         y2 = Double.parseDouble(points2[1]);
 35         x3 = Double.parseDouble(points3[0]);
 36         y3 = Double.parseDouble(points3[1]);
 37         x4 = Double.parseDouble(points4[0]);
 38         y4 = Double.parseDouble(points4[1]);
 39         double[][] point = {{x1, y1}, {x2, y2}, {x3, y3}, {x4, y4}};
 40         return point;
 41     }
 42     public double[][] Points5(String[] points) {
 43         double x1, y1, x2, y2, x3, y3, x4, y4, x5, y5;
 44         String[] points1 = points[0].split(",");
 45         String[] points2 = points[1].split(",");
 46         String[] points3 = points[2].split(",");
 47         String[] points4 = points[3].split(",");
 48         String[] points5 = points[4].split(",");
 49         x1 = Double.parseDouble(points1[0]);
 50         y1 = Double.parseDouble(points1[1]);
 51         x2 = Double.parseDouble(points2[0]);
 52         y2 = Double.parseDouble(points2[1]);
 53         x3 = Double.parseDouble(points3[0]);
 54         y3 = Double.parseDouble(points3[1]);
 55         x4 = Double.parseDouble(points4[0]);
 56         y4 = Double.parseDouble(points4[1]);
 57         x5 = Double.parseDouble(points5[0]);
 58         y5 = Double.parseDouble(points5[1]);
 59         double[][] point = {{x1, y1}, {x2, y2}, {x3, y3}, {x4, y4},{x5, y5}};
 60         return point;
 61     }
 62     public double[][] Points6(String[] points) {
 63         double x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6;
 64         String[] points1 = points[0].split(",");
 65         String[] points2 = points[1].split(",");
 66         String[] points3 = points[2].split(",");
 67         String[] points4 = points[3].split(",");
 68         String[] points5 = points[4].split(",");
 69         String[] points6 = points[5].split(",");
 70         x1 = Double.parseDouble(points1[0]);
 71         y1 = Double.parseDouble(points1[1]);
 72         x2 = Double.parseDouble(points2[0]);
 73         y2 = Double.parseDouble(points2[1]);
 74         x3 = Double.parseDouble(points3[0]);
 75         y3 = Double.parseDouble(points3[1]);
 76         x4 = Double.parseDouble(points4[0]);
 77         y4 = Double.parseDouble(points4[1]);
 78         x5 = Double.parseDouble(points5[0]);
 79         y5 = Double.parseDouble(points5[1]);
 80         x6 = Double.parseDouble(points6[0]);
 81         y6 = Double.parseDouble(points6[1]);
 82         double[][] point = {{x1, y1}, {x2, y2}, {x3, y3}, {x4, y4},{x5, y5},{x6, y6}};
 83         return point;
 84     }
 85     public String rounding(double num) {
 86         String result;
 87         DecimalFormat df;
 88         switch(judge.places(num)) {
 89         case 1 :
 90             df = new DecimalFormat(".0");
 91             result = df.format(num);
 92             break;
 93         case 2 :
 94             df = new DecimalFormat(".00");
 95             result = df.format(num);
 96             break;
 97         default :
 98             df = new DecimalFormat(".000");
 99             result = df.format(num);
100             break;
101             }
102         return result;
103     }
104     public void choose1() {
105         double[][] point = Points4(points);
106         boolean result1 = false,result2 = false;
107         judge.coincideValidate(point);
108         if(judge.quadrilateral(point)) {
109             result1 = true;
110             if(judge.parallelogram(point)) {
111                 result2 = true;
112             }
113         }
114         System.out.println(result1+" "+result2);
115     }
116     public void choose2() {
117         double[][] point = Points4(points);
118         boolean result1 = false,result2 = false,result3 = false;
119         if(judge.quadrilateral(point)) {
120             judge.coincideValidate(point);
121             if(judge.diamond(point)) {
122                 result1 = true;
123             }
124             if(judge.rectangle(point)) {
125                 result2 = true;
126                 if(judge.square(point)) {
127                     result3 = true;
128                 }
129             }
130             System.out.println(result1+" "+result2+" "+result3);
131         }else {
132             System.out.println("not a quadrilateral");
133         }
134     }
135     public void choose3() {
136         double[][] point = Points4(points);
137         boolean result = judge.concavity(point);
138         double perimeter,area;
139         if(judge.quadrilateral(point)) {
140             judge.coincideValidate(point);
141             perimeter = Calculate.perimeter(point);
142             area = Calculate.area(point[0],point[1],point[2]) + Calculate.area(point[0],point[2],point[3]);
143             System.out.println(result+" "+rounding(perimeter)+" "+rounding(area));
144         }else {
145             System.out.println("not a quadrilateral");
146         }
147     }
148     public void choose4() {
149         double[][] point = Points6(points);
150         if(judge.joints(point[0],point[1])) {
151             System.out.println("points coincide");
152             System.exit(0);
153         }
154         double[][] point1 = {point[0],point[1]};
155         double[][] point2 = {point[2],point[3],point[4],point[5]};
156         double area1,area2;
157         int choice = judge.quadrilateralOrTriangle(point2);
158         switch(choice) {
159         case 1 :
160             double[][] point3 = judge.getAfterPoint(point2);
161             if(judge.triangleLinesCoincide(point1, point3)) {
162                 System.out.println("The line is coincide with one of the lines");
163             }
164             else if(judge.triangleOneIntersection(point1, point3)) {
165                 System.out.println("1");
166             }else {
167                 double[][] point4 = judge.triangleIntersection(point1, point3);
168                 if(point4 == null) {
169                     System.out.println("0");
170                 }else {
171                     area1 = Calculate.area(point4[0], point4[1], point4[2]);
172                     area2 =  Calculate.area(point3[0], point3[1], point3[2]) - area1;
173                     if(area1 < area2) {
174                         System.out.println("2 "+rounding(area1)+" "+rounding(area2));
175                     }else {
176                         System.out.println("2 "+rounding(area2)+" "+rounding(area1));
177                     }
178                 }
179             }
180             break;
181         case 2 :
182             if(judge.quadrilateralLinesCoincide(point1, point2)) {
183                 System.out.println("The line is coincide with one of the lines");
184             }
185             else if(judge.quadrilateralOneIntersection(point1, point2)) {
186                 System.out.println("1");
187             }else {
188                 double[][] point5 = judge.quadrilateralIntersection(point1, point2);
189                 if(point5 == null) {
190                     System.out.println("0");
191                 }else {
192                     double area3 = Calculate.area(point2[0], point2[1], point2[2]) + Calculate.area(point2[1], point2[2], point2[3]);
193                     if(point5[3] == null) {
194                         area1 = Calculate.area(point5[0], point5[1], point5[2]);
195                     }else {
196                         area1 = Calculate.area(point5[0], point5[1], point5[2]) + Calculate.area(point5[1], point5[2], point5[3]);
197                     }
198                     area2 =  area3 - area1;
199                     if(area1 < area2) {
200                         System.out.println("2 "+rounding(area1)+" "+rounding(area2));
201                     }else {
202                         System.out.println("2 "+rounding(area2)+" "+rounding(area1));
203                     }
204                 }
205             }
206             break;
207         default:
208             System.out.println("not a quadrilateral or triangle");
209         }
210     }
211     public void choose5() {
212         double[][] point = Points5(points);
213         double[] point1 = point[0];
214         double[][] point2 = {point[1],point[2],point[3],point[4]};
215         switch(judge.quadrilateralOrTriangle(point2)) {
216         case 1 :
217             double[][] point3 = judge.getAfterPoint(point2);
218             judge.placeOfPoint(point1,point3,"triangle");
219             break;
220         case 2 :
221             judge.placeOfPoint(point1,point2,"quadrilateral");
222             break;
223         default:
224             System.out.println("not a quadrilateral or triangle");
225         }
226     }
227     public void start() {
228         split();
229         if(judge.formatValidate(points)) {
230             switch(choice) {
231             case 1 :
232                 if(judge.numValidate(4,points)) {
233                     choose1();
234                 } else {
235                     System.out.println("wrong number of points");
236                 }
237                     break;
238             case 2 :
239                 if(judge.numValidate(4,points)) {
240                     choose2();
241                 } else {
242                     System.out.println("wrong number of points");
243                 }
244                     break;
245             case 3 :
246                 if(judge.numValidate(4,points)) {
247                     choose3();
248                 } else {
249                     System.out.println("wrong number of points");
250                 }
251                     break;
252             case 4 :
253                 if(judge.numValidate(6,points)) {
254                     System.out.println("not a quadrilateral or triangle");
255                 } else {
256                     System.out.println("wrong number of points");
257                 }
258                     break;
259             case 5 :
260                 if(judge.numValidate(5,points)) {
261                     choose5();
262                 } else {
263                     System.out.println("wrong number of points");
264                 }
265                     break;
266             }
267         }else {
268             System.out.println("Wrong Format");
269         }
270     }
271 }
272 class Judge {
273     public Judge() {
274     }
275     public boolean formatValidate(String[] points) {
276         Pattern format=Pattern.compile("^([+-]?([0-9]|([1-9][0-9]*)))(\\.\\d+)?,([+-]?([0-9]|([1-9][0-9]*)))(\\.\\d+)?$");
277         Matcher matcher;
278         boolean validate = true;
279         for(int i = 0; i < points.length; i++) {
280             matcher = format.matcher(points[i]);
281             if(!matcher.matches()) {
282                 validate = false;
283             }
284         }
285         return validate;
286     }
287     public boolean numValidate(int num, String[] points) {
288         return points.length == num;
289     }
290     public boolean joints(double[] p1,double[] p2) {
291         return p1[0] == p2[0] && p1[1] == p2[1];
292     }
293     public void coincideValidate(double[][] point) {
294         if(joints(point[0],point[1]) || joints(point[0],point[2]) || joints(point[0],point[3]) || joints(point[1],point[2]) || joints(point[1],point[3]) || joints(point[2],point[3]) ) {
295             System.out.println("points coincide");
296             System.exit(0);
297         }
298     }
299     public int coincideValidatePlus(double[][] p) {
300         int abandonedPoint = -1;
301         if(joints(p[0],p[1]) && pointOnLine(p[1],p[2],p[3])) {
302             abandonedPoint = 0;
303         }
304         else if(joints(p[1],p[2]) && pointOnLine(p[2],p[3],p[0])) {
305             abandonedPoint = 1;
306         }
307         else if(joints(p[2],p[3]) && pointOnLine(p[3],p[0],p[1])) {
308             abandonedPoint = 2;
309         }
310         else if(joints(p[3],p[0]) && pointOnLine(p[0],p[1],p[2])) {
311             abandonedPoint = 3;
312         }
313         return abandonedPoint;
314     }
315     public boolean pointOnLine(double[] p1,double[] p2,double[] p3) {
316         double k1 = Calculate.getK(p1,p2);
317         double k2 = Calculate.getK(p1,p3);
318         return (k1 == k2||((k1 == Double.NEGATIVE_INFINITY && k2 == Double.POSITIVE_INFINITY)||(k2 == Double.NEGATIVE_INFINITY && k1 == Double.POSITIVE_INFINITY)));
319     }
320     public boolean pointInLine(double[] p1,double[] p2,double[] p3) {
321         return pointOnLine(p1,p2,p3)&&((p2[0] >= p1[0] && p2[0] <= p3[0])||(p2[0] <= p1[0] && p2[0] >= p3[0]))&&((p2[1] >= p1[1] && p2[1] <= p3[1])||(p2[1] <= p1[1] && p2[1] >= p3[1]));
322     }
323     public boolean quadrilateral(double[][] point) { //四边形
324         return !pointOnLine(point[0],point[1],point[2])&&!pointOnLine(point[0],point[1],point[3])&&!pointOnLine(point[0],point[2],point[3])&&!pointOnLine(point[1],point[2],point[3]);
325     }
326     public boolean parallelogram(double[][] point) {
327         return (point[0][0]+point[2][0]==point[1][0]+point[3][0] && point[0][1]+point[2][1]==point[1][1]+point[3][1]) ;
328     }
329     public boolean diamond(double[][] point) {
330         return parallelogram(point)&&(Calculate.distance(point[0], point[1]) == Calculate.distance(point[0], point[3]));
331     }
332     public boolean rectangle(double[][] point) {
333         return parallelogram(point)&&(Calculate.distance(point[0], point[2]) == Calculate.distance(point[1], point[3]));
334     }
335     public boolean square(double[][] point) {
336         return rectangle(point)&&(Calculate.distance(point[0], point[1]) == Calculate.distance(point[0], point[3]));
337     }
338     public boolean concavity(double[][] point) {
339         boolean result = false;//
340         if(Calculate.area(point[0],point[1],point[2]) + Calculate.area(point[0],point[2],point[3]) == Calculate.area(point[1],point[2],point[3]) + Calculate.area(point[0],point[1],point[3])) {
341             result = true;//
342         }
343         return result;
344     }
345     public int places(double num) { //小数位数
346         int result = 3;
347         DecimalFormat df = new DecimalFormat(".000");
348         String a = df.format(num);
349         String[] b = a.split("\\.");
350         if(b[1].charAt(2) == '0') {
351             result = 2;
352             if(b[1].charAt(1) == '0') {
353                 result = 1;
354             }
355         }
356         return result;
357     }
358     public int quadrilateralOrTriangle(double[][] point) {
359         int choice;
360         int abandonedPoint = getAbandonedPoint(point);
361         if(abandonedPoint != -1) {
362             choice = 1;
363         }
364         else if(quadrilateral(point)) {
365             choice = 2;
366         }
367         else {
368             choice = 0;
369         }
370         return choice;
371     }
372     public int getAbandonedPoint(double[][] point) {
373         int abandonedPoint = -1;
374         switch(coincideValidatePlus(point)) {
375         case -1 :
376             boolean j1 = pointOnLine(point[0],point[1],point[2]);
377             boolean j2 = pointOnLine(point[1],point[2],point[3]);
378             if(!(j1&&j2)) {//四点不共线
379                 if(pointInLine(point[0],point[1],point[2])) {
380                     abandonedPoint = 1;
381                     break;
382                 }
383                 else if(pointInLine(point[1],point[2],point[3])) {
384                     abandonedPoint = 2;
385                     break;
386                 }
387                 else if(pointInLine(point[2],point[3],point[0])) {
388                     abandonedPoint = 3;
389                     break;
390                 }
391                 else if(pointInLine(point[3],point[0],point[1])) {
392                     abandonedPoint = 0;
393                     break;
394                 }
395             }
396             break;
397         case 0 :
398             abandonedPoint = 0;
399             break;
400         case 1 :
401             abandonedPoint = 1;
402             break;
403         case 2 :
404             abandonedPoint = 2;
405             break;
406         case 3 :
407             abandonedPoint = 3;
408             break;
409         }
410         return abandonedPoint;
411     }
412     public double[][] getAfterPoint(double[][] point){
413         int abandonedPoint = getAbandonedPoint(point);
414         if (abandonedPoint == 0) {
415             double[][] afterPoint  = {point[1],point[2],point[3]};
416             return afterPoint;
417         } 
418         else if (abandonedPoint == 1) {
419             double[][] afterPoint  = {point[0],point[2],point[3]};
420             return afterPoint;
421         }
422         else if(abandonedPoint == 2) {
423             double[][] afterPoint  = {point[0],point[1],point[3]};
424             return afterPoint;
425         }
426         else if(abandonedPoint == 3) {
427             double[][] afterPoint  = {point[0],point[1],point[2]};
428             return afterPoint;
429         }
430         else {
431             return null;
432         }
433     }
434     public boolean kIsExit(double[] p1,double[] p2) {
435         boolean result = true;
436         if(p1[0] == p2[0]) {
437             result = false;
438         }
439         return result;
440     }
441     public int getSide(double[] p1,double[] p2,double[] p3) { //点在直线上侧或下侧或在直线上
442         int result = 0;
443         if(kIsExit(p1,p2)) {
444             if(Calculate.getK(p1, p2)*p3[0] + Calculate.getB(p1, p2) - p3[1] > 0) { //表达式kx + b - y = 0
445                 result = 1;//上侧
446             }
447             else if(Calculate.getK(p1, p2)*p3[0] + Calculate.getB(p1, p2) - p3[1] < 0) { //表达式kx + b - y = 0
448                 result = 2;//下侧
449             }
450         } else {
451             if(p3[0] < p1[0]) { //表达式kx + b - y = 0
452                 result = 1;//左侧
453             }
454             else if(p3[0] > p1[0]) { //表达式kx + b - y = 0
455                 result = 2;//右侧
456             }
457         }
458         return result;
459     }
460     public double[] getIntersectPoint(double[] p1, double[] p2, double[] p3, double[] p4){
461         double A1=p1[1]-p2[1];
462         double B1=p2[0]-p1[0];
463         double C1=A1*p1[0]+B1*p1[1];
464         double A2=p3[1]-p4[1];
465         double B2=p4[0]-p3[0];
466         double C2=A2*p3[0]+B2*p3[1];
467         double det_k=A1*B2-A2*B1;
468         if(Math.abs(det_k)<0.00001){
469             return null;
470         }
471         double a=B2/det_k;
472         double b=-1*B1/det_k;
473         double c=-1*A2/det_k;
474         double d=A1/det_k;
475         double x=a*C1+b*C2;
476         double y=c*C1+d*C2;
477         double[] intersection = {x,y};
478         return  intersection;
479     }
480     public boolean  triangleLinesCoincide(double[][] p1,double[][] p2) {
481         return (getSide(p1[0],p1[1],p2[0]) ==0 && getSide(p1[0],p1[1],p2[1]) ==0)||(getSide(p1[0],p1[1],p2[1]) ==0 && getSide(p1[0],p1[1],p2[2]) ==0)||(getSide(p1[0],p1[1],p2[2]) ==0 && getSide(p1[0],p1[1],p2[0]) ==0);
482     }
483     public boolean  triangleOneIntersection(double[][] p1,double[][] p2) {
484         boolean result = false;
485         if(getSide(p1[0],p1[1],p2[0]) ==0 && getSide(p1[0],p1[1],p2[1]) == getSide(p1[0],p1[1],p2[2]) && getSide(p1[0],p1[1],p2[1]) !=0){
486             result = true;//过点0
487         }
488         else if(getSide(p1[0],p1[1],p2[1]) ==0 && getSide(p1[0],p1[1],p2[2]) == getSide(p1[0],p1[1],p2[0]) && getSide(p1[0],p1[1],p2[2]) !=0){
489             result = true;//过点1
490         }
491         else if(getSide(p1[0],p1[1],p2[2]) ==0 && getSide(p1[0],p1[1],p2[0]) == getSide(p1[0],p1[1],p2[1]) && getSide(p1[0],p1[1],p2[0]) !=0){
492             result = true;//过点2
493         }
494         return result;
495     }
496     public double[][] triangleIntersection(double[][] p1,double[][] p2) {
497         double[][] intersection = new double[3][2];
498         boolean judge = true;
499         boolean a1 = false,a2 = false,a3 = false;
500         boolean b1 = false,b2 =false,b3 = false;
501         if(getSide(p1[0],p1[1],p2[0]) != getSide(p1[0],p1[1],p2[1]) && getSide(p1[0],p1[1],p2[0]) !=0 && getSide(p1[0],p1[1],p2[1]) !=0){
502             a1 = true;//与a1交
503         }
504         if(getSide(p1[0],p1[1],p2[1]) != getSide(p1[0],p1[1],p2[2]) && getSide(p1[0],p1[1],p2[1]) !=0 && getSide(p1[0],p1[1],p2[2]) !=0){
505             a2 = true;//与a2交
506         }
507         if(getSide(p1[0],p1[1],p2[2]) != getSide(p1[0],p1[1],p2[0]) && getSide(p1[0],p1[1],p2[2]) !=0 && getSide(p1[0],p1[1],p2[0]) !=0){
508             a3 = true;//与a3交
509         }
510         if(getSide(p1[0],p1[1],p2[2]) ==0){
511             b1 = true;//过点2
512         }
513         if(getSide(p1[0],p1[1],p2[0]) ==0){
514             b2 = true;//过点0
515         }
516         if(getSide(p1[0],p1[1],p2[1]) ==0){
517             b3 = true;//过点1
518         }
519         if(a1&&b1) {
520             intersection[0] = p2[2];
521             intersection[1] = getIntersectPoint(p1[0],p1[1],p2[0],p2[1]);
522             intersection[2] = p2[0];
523         }
524         else if(a2&&b2) {
525             intersection[0] = p2[0];
526             intersection[1] = getIntersectPoint(p1[0],p1[1],p2[1],p2[2]);
527             intersection[2] = p2[1];
528         }
529         else if(a3&&b3) {
530             intersection[0] = p2[1];
531             intersection[1] = getIntersectPoint(p1[0],p1[1],p2[2],p2[0]);
532             intersection[2] = p2[2];
533         }
534         else if(a1&&a2) {
535             intersection[0] = getIntersectPoint(p1[0],p1[1],p2[0],p2[1]);
536             intersection[1] = getIntersectPoint(p1[0],p1[1],p2[1],p2[2]);
537             intersection[2] = p2[1];
538         }
539         else if(a1&&a3) {
540             intersection[0] = getIntersectPoint(p1[0],p1[1],p2[0],p2[1]);
541             intersection[1] = getIntersectPoint(p1[0],p1[1],p2[2],p2[0]);
542             intersection[2] = p2[0];
543         }
544         else if(a2&&a3) {
545             intersection[0] = getIntersectPoint(p1[0],p1[1],p2[1],p2[2]);
546             intersection[1] = getIntersectPoint(p1[0],p1[1],p2[2],p2[0]);
547             intersection[2] = p2[2];
548         }
549         else {
550             judge = false;
551         }
552         if(judge) {
553             return intersection;
554         }else {
555             return null;
556         }
557     }
558     public boolean quadrilateralLinesCoincide(double[][] p1,double[][] p2) {
559         return (getSide(p1[0],p1[1],p2[0]) ==0 && getSide(p1[0],p1[1],p2[1]) ==0)||(getSide(p1[0],p1[1],p2[1]) ==0 && getSide(p1[0],p1[1],p2[2]) ==0)||(getSide(p1[0],p1[1],p2[2]) ==0 && getSide(p1[0],p1[1],p2[3]) ==0)||(getSide(p1[0],p1[1],p2[3]) ==0 && getSide(p1[0],p1[1],p2[0])==0);
560     }
561     public boolean  quadrilateralOneIntersection(double[][] p1,double[][] p2) {
562         boolean result = false;
563         if(getSide(p1[0],p1[1],p2[0]) ==0 && getSide(p1[0],p1[1],p2[1]) == getSide(p1[0],p1[1],p2[2]) && getSide(p1[0],p1[1],p2[2]) == getSide(p1[0],p1[1],p2[3]) && getSide(p1[0],p1[1],p2[1]) !=0){
564             result = true;//过点0
565         }
566         else if(getSide(p1[0],p1[1],p2[1]) ==0 && getSide(p1[0],p1[1],p2[2]) == getSide(p1[0],p1[1],p2[3]) && getSide(p1[0],p1[1],p2[3]) == getSide(p1[0],p1[1],p2[0])&& getSide(p1[0],p1[1],p2[2]) !=0){
567             result = true;//过点1
568         }
569         else if(getSide(p1[0],p1[1],p2[2]) ==0 && getSide(p1[0],p1[1],p2[3]) == getSide(p1[0],p1[1],p2[0]) && getSide(p1[0],p1[1],p2[0]) == getSide(p1[0],p1[1],p2[1])&& getSide(p1[0],p1[1],p2[3]) !=0){
570             result = true;//过点2
571         }
572         else if(getSide(p1[0],p1[1],p2[2]) ==0 && getSide(p1[0],p1[1],p2[0]) == getSide(p1[0],p1[1],p2[1]) && getSide(p1[0],p1[1],p2[1]) == getSide(p1[0],p1[1],p2[2])&& getSide(p1[0],p1[1],p2[0]) !=0){
573             result = true;//过点3
574         }
575         return result;
576     }
577     public double[][] quadrilateralIntersection(double[][] p1,double[][] p2) {
578         double[][] intersection = new double[4][2];
579         boolean judge = true;
580         boolean a1 = false,a2 = false,a3 = false,a4 = false;
581         boolean b1 = false,b2 =false,b3 = false,b4 = false;
582         if(getSide(p1[0],p1[1],p2[0]) != getSide(p1[0],p1[1],p2[1]) && getSide(p1[0],p1[1],p2[0]) !=0 && getSide(p1[0],p1[1],p2[1]) !=0){
583             a1 = true;//与a1交
584         }
585         if(getSide(p1[0],p1[1],p2[1]) != getSide(p1[0],p1[1],p2[2]) && getSide(p1[0],p1[1],p2[1]) !=0 && getSide(p1[0],p1[1],p2[2]) !=0){
586             a2 = true;//与a2交
587         }
588         if(getSide(p1[0],p1[1],p2[2]) != getSide(p1[0],p1[1],p2[3]) && getSide(p1[0],p1[1],p2[2]) !=0 && getSide(p1[0],p1[1],p2[3]) !=0){
589             a3 = true;//与a3交
590         }
591         if(getSide(p1[0],p1[1],p2[3]) != getSide(p1[0],p1[1],p2[0]) && getSide(p1[0],p1[1],p2[3]) !=0 && getSide(p1[0],p1[1],p2[0]) !=0){
592             a4 = true;//与a4交
593         }
594         if(getSide(p1[0],p1[1],p2[0]) ==0){
595             b1 = true;//过点0
596         }
597         if(getSide(p1[0],p1[1],p2[1]) ==0){
598             b2 = true;//过点1
599         }
600         if(getSide(p1[0],p1[1],p2[2]) ==0){
601             b3 = true;//过点2
602         }
603         if(getSide(p1[0],p1[1],p2[3]) ==0){
604             b4 = true;//过点3
605         }
606         if(b1&&b3) {//点点
607             intersection[0] = p2[0];
608             intersection[1] = p2[1];
609             intersection[2] = p2[2];
610             intersection[3] = null;
611         }
612         else if(b2&&b4) {
613             intersection[0] = p2[1];
614             intersection[1] = p2[2];
615             intersection[2] = p2[3];
616             intersection[3] = null;
617         }
618         else if(b1&&a2) {//点线
619             intersection[0] = p2[0];
620             intersection[1] = p2[1];
621             intersection[2] = getIntersectPoint(p1[0],p1[1],p2[1],p2[2]);
622             intersection[3] = null;
623         }
624         else if(b1&&a3) {
625             intersection[0] = p2[0];
626             intersection[1] = getIntersectPoint(p1[0],p1[1],p2[2],p2[3]);
627             intersection[2] = p2[3];
628             intersection[3] = null;
629         }
630         else if(b2&&a3) {
631             intersection[0] = p2[1];
632             intersection[1] = p2[2];
633             intersection[2] = getIntersectPoint(p1[0],p1[1],p2[2],p2[3]);
634             intersection[3] = null;
635         }
636         else if(b2&&a4) {
637             intersection[0] = p2[1];
638             intersection[1] = getIntersectPoint(p1[0],p1[1],p2[3],p2[0]);
639             intersection[2] = p2[0];
640             intersection[3] = null;
641         }
642         else if(b3&&a4) {
643             intersection[0] = p2[2];
644             intersection[1] = p2[3];
645             intersection[2] = getIntersectPoint(p1[0],p1[1],p2[3],p2[0]);
646             intersection[3] = null;
647         }
648         else if(b3&&a1) {
649             intersection[0] = p2[2];
650             intersection[1] = getIntersectPoint(p1[0],p1[1],p2[0],p2[1]);
651             intersection[2] = p2[1];
652             intersection[3] = null;
653         }
654         else if(b4&&a1) {
655             intersection[0] = p2[3];
656             intersection[1] = p2[0];
657             intersection[2] = getIntersectPoint(p1[0],p1[1],p2[0],p2[1]);
658             intersection[3] = null;
659         }
660         else if(b4&&a2) {
661             intersection[0] = p2[3];
662             intersection[1] = getIntersectPoint(p1[0],p1[1],p2[1],p2[2]);
663             intersection[2] = p2[2];
664             intersection[3] = null;
665         }
666         else if(a1&&a3) {//线线
667             intersection[0] = p2[0];
668             intersection[1] = getIntersectPoint(p1[0],p1[1],p2[0],p2[1]);
669             intersection[2] = getIntersectPoint(p1[0],p1[1],p2[2],p2[3]);
670             intersection[3] = p2[3];
671         }
672         else if(a2&&a4) {
673             intersection[0] = p2[0];
674             intersection[1] = p2[1];
675             intersection[2] = getIntersectPoint(p1[0],p1[1],p2[1],p2[2]);
676             intersection[3] = getIntersectPoint(p1[0],p1[1],p2[3],p2[0]);
677         }
678         else if(a1&&a2) {
679             intersection[0] = getIntersectPoint(p1[0],p1[1],p2[0],p2[1]);
680             intersection[1] = getIntersectPoint(p1[0],p1[1],p2[1],p2[2]);
681             intersection[0] = p2[1];
682             intersection[3] = null;
683         }
684         else if(a2&&a3) {
685             intersection[0] = getIntersectPoint(p1[0],p1[1],p2[1],p2[2]);
686             intersection[1] = getIntersectPoint(p1[0],p1[1],p2[2],p2[3]);
687             intersection[0] = p2[2];
688             intersection[3] = null;
689         }
690         else if(a3&&a4) {
691             intersection[0] = getIntersectPoint(p1[0],p1[1],p2[2],p2[3]);
692             intersection[1] = getIntersectPoint(p1[0],p1[1],p2[3],p2[0]);
693             intersection[0] = p2[3];
694             intersection[3] = null;
695         }
696         else if(a4&&a1) {
697             intersection[0] = getIntersectPoint(p1[0],p1[1],p2[3],p2[0]);
698             intersection[1] = getIntersectPoint(p1[0],p1[1],p2[0],p2[1]);
699             intersection[0] = p2[3];
700             intersection[3] = null;
701         }
702         else {
703             judge = false;
704         }
705         if(judge) {
706             return intersection;
707         }else {
708             return null;
709         }
710     }
711     public boolean pointAndLine(double[] p1, double[] p2,double[] p3,String name,boolean result) {
712         if(pointInLine(p1, p2, p3)){
713             System.out.println("on the "+name);
714             System.exit(0);
715         }
716         double v = (p3[1] - p1[1]) * (p2[0] - p1[0]) / (p2[1] - p1[1]);
717         if (p1[1] < p3[1] && p2[1] >= p3[1]) {
718             double x = p1[0] + v;
719             if (x > p3[0]) {
720                 result = !result;
721             }
722         } else if (p1[1] >= p3[1] && p2[1] < p3[1]) {
723             double x = p1[0] + v;
724             if (x > p3[0]) {
725                 result = !result;
726             }
727         }
728         return result;
729     }
730     public void placeOfPoint(double[] p1, double[][] p2,String name) {
731         boolean result = false;
732         if(name.equals("triangle")) {
733             result = pointAndLine(p2[0],p2[1],p1,name,result);
734             result = pointAndLine(p2[1],p2[2],p1,name,result);
735             result = pointAndLine(p2[2],p2[0],p1,name,result);
736         }else {
737             result = pointAndLine(p2[0],p2[1],p1,name,result);
738             result = pointAndLine(p2[1],p2[2],p1,name,result);
739             result = pointAndLine(p2[2],p2[3],p1,name,result);
740             result = pointAndLine(p2[3],p2[0],p1,name,result);
741         }
742         if (result) {
743             System.out.println("in the " + name);
744         } else {
745             System.out.println("outof the " + name);
746         }
747     }
748 }
749 class Calculate {
750     public Calculate() {
751     }
752     public static double getK(double[] p1,double[] p2) {
753         return (p2[1] - p1[1]) / (p2[0] - p1[0]);
754     }
755     public static double getB(double[] p1,double[] p2) {
756         return p1[1] - (p2[1] - p1[1]) / (p2[0] - p1[0]) * p1[0];
757     }
758     public static double distance(double[] p1,double[] p2) {
759         return Math.sqrt((p2[1]-p1[1])*(p2[1]-p1[1]) + (p2[0]-p1[0])*(p2[0]-p1[0]));
760     }
761     public static double area(double[] p1,double[] p2,double[] p3) {
762         double a = distance(p1,p2);
763         double b = distance(p1,p3);
764         double c = distance(p2,p3);
765         double p = (a + b + c) /2;
766         return Math.sqrt(p*(p-a)*(p-b)*(p-c));
767     }
768     public static double perimeter(double[][] p) {
769         double a = distance(p[0],p[1]);
770         double b = distance(p[1],p[2]);
771         double c = distance(p[2],p[3]);
772         double d = distance(p[3],p[0]);
773         return a+b+c+d;
774     }
775 }
776 class Input {
777     public Input() {
778     }
779     public String input() {
780         Scanner in = new Scanner(System.in);
781         String input = in.nextLine();
782         return input;
783     }
784 }

分析如下:这题有些难度,我花费了很多时间最后也只得了82分。虽然题目看起来这里只有简单的五个选项,但后面两个选项非常复杂,需要写很多代码,进行很多判断和分情况。前面三个选项还好,但是有几个我一直过不了,应该是一些细节没有设计好。我此处只设计五个类Main、Judge、Control、Calculate、Input。Judge主要用来判断和分类各种情况以及不符合规范的输入,Control则用来执行五个选项,Calculate用来为Judge和Control提供一些设计好的计算方法,Input则用来接收用户的输入。

OOP期中考试分析

第一题:

题目说明如下:

 

类图如下:

源码如下:

 

  1 import java.util.Scanner;
  2 
  3 public class Main {
  4 
  5     public static void main(String[] args) {
  6         Scanner in = new Scanner(System.in);
  7         double[] points =new double[4];
  8         String color;
  9         for(int i = 0;i < 4;i++) {
 10             points[i] = in.nextDouble();
 11             if(points[i]<=0||points[i]>200) {
 12                 System.out.println("Wrong Format");
 13                 System.exit(0);
 14             }
 15         }
 16         color = in.next();
 17         Point point1 = new Point(points[0],points[1]);
 18         Point point2 = new Point(points[2],points[3]);
 19         Line line = new Line(point1,point2,color);
 20         line.display();
 21     }
 22 
 23 }
 24 
 25 class Point {
 26     private double x;
 27     private double y;
 28     
 29     public Point() {
 30     }
 31 
 32     public Point(double x, double y) {
 33         this.x = x;
 34         this.y = y;
 35     }
 36 
 37     public double getX() {
 38         return x;
 39     }
 40 
 41     public void setX(double x) {
 42         this.x = x;
 43     }
 44 
 45     public double getY() {
 46         return y;
 47     }
 48 
 49     public void setY(double y) {
 50         this.y = y;
 51     }
 52     
 53     public void display() {
 54         System.out.println("("+String.format("%.2f", x)+","+String.format("%.2f", y)+")");
 55     }
 56 }
 57 
 58 class Line {
 59     private Point point1 = new Point();
 60     private Point point2 = new Point();
 61     private String color;
 62     
 63     public Line() {
 64     }
 65 
 66     public Line(Point p1, Point p2, String color) {
 67         this.point1 = p1;
 68         this.point2 = p2;
 69         this.color = color;
 70     }
 71 
 72     public Point getPoint1() {
 73         return point1;
 74     }
 75 
 76     public void setPoint1(Point point1) {
 77         this.point1 = point1;
 78     }
 79 
 80     public Point getPoint2() {
 81         return point2;
 82     }
 83 
 84     public void setPoint2(Point point2) {
 85         this.point2 = point2;
 86     }
 87 
 88     public String getColor() {
 89         return color;
 90     }
 91 
 92     public void setColor(String color) {
 93         this.color = color;
 94     }
 95     
 96     public double getDistance() {
 97         double d1 = point2.getY() - point1.getY();
 98         double d2 = point2.getX() - point1.getX();
 99         return Math.sqrt(d1*d1+d2*d2);
100     }
101     
102     public void display() {
103         System.out.println("The line's color is:"+color);
104         System.out.println("The line's begin point's Coordinate is:");
105         point1.display();
106         System.out.println("The line's end point's Coordinate is:");
107         point2.display();
108         System.out.println("The line's length is:"+String.format("%.2f", getDistance()));
109     }
110 }

 

分析如下:本题主要考察对于类间关系的理解以及String.format("%.2f", data)保留小数输出的方法

第二题:

题目说明如下:

 

类图如下:

 

 源码如下:

 

  1 import java.util.Scanner;
  2 
  3 public class Main {
  4 
  5     public static void main(String[] args) {
  6         Scanner in = new Scanner(System.in);
  7         double[] points =new double[4];
  8         String color;
  9         for(int i = 0;i < 4;i++) {
 10             points[i] = in.nextDouble();
 11             if(points[i]<=0||points[i]>200) {
 12                 System.out.println("Wrong Format");
 13                 System.exit(0);
 14             }
 15         }
 16         color = in.next();
 17         Element plane = new Plane(color);
 18         Point point1 = new Point(points[0],points[1]);
 19         Point point2 = new Point(points[2],points[3]);
 20         Element line = new Line(point1,point2,color);
 21         point1.display();
 22         point2.display();
 23         line.display();
 24         plane.display();
 25     }
 26 
 27 }
 28 
 29 abstract class Element {
 30 
 31     public Element() {
 32     }
 33 
 34     public abstract void display();
 35     
 36 }
 37 
 38 class Point extends Element{
 39     private double x;
 40     private double y;
 41     
 42     public Point() {
 43         super();
 44     }
 45 
 46     public Point(double x, double y) {
 47         this.x = x;
 48         this.y = y;
 49     }
 50 
 51     public double getX() {
 52         return x;
 53     }
 54 
 55     public void setX(double x) {
 56         this.x = x;
 57     }
 58 
 59     public double getY() {
 60         return y;
 61     }
 62 
 63     public void setY(double y) {
 64         this.y = y;
 65     }
 66     
 67     @Override
 68     public void display() {
 69         System.out.println("("+String.format("%.2f", x)+","+String.format("%.2f", y)+")");
 70     }
 71 }
 72 
 73 class Line extends Element{
 74     private Point point1 = new Point();
 75     private Point point2 = new Point();
 76     private String color;
 77     
 78     public Line() {
 79         super();
 80     }
 81 
 82     public Line(Point p1, Point p2, String color) {
 83         super();
 84         this.point1 = p1;
 85         this.point2 = p2;
 86         this.color = color;
 87     }
 88 
 89     public Point getPoint1() {
 90         return point1;
 91     }
 92 
 93     public void setPoint1(Point point1) {
 94         this.point1 = point1;
 95     }
 96 
 97     public Point getPoint2() {
 98         return point2;
 99     }
100 
101     public void setPoint2(Point point2) {
102         this.point2 = point2;
103     }
104 
105     public String getColor() {
106         return color;
107     }
108 
109     public void setColor(String color) {
110         this.color = color;
111     }
112     
113     public double getDistance() {
114         double d1 = point2.getY() - point1.getY();
115         double d2 = point2.getX() - point1.getX();
116         return Math.sqrt(d1*d1+d2*d2);
117     }
118     
119     @Override
120     public void display() {
121         System.out.println("The line's color is:"+color);
122         System.out.println("The line's begin point's Coordinate is:");
123         point1.display();
124         System.out.println("The line's end point's Coordinate is:");
125         point2.display();
126         System.out.println("The line's length is:"+String.format("%.2f", getDistance()));
127     }
128 }
129 
130 class Plane extends Element{
131     private String color;
132 
133     public Plane() {
134         super();
135     }
136     
137     public Plane(String color) {
138         super();
139         this.color = color;
140     }
141 
142     @Override
143     public void display() {
144         System.out.println("The Plane's color is:"+color);
145     }
146 }

 

分析如下:本题主要考察继承与多态的运用以及对抽象类的理解,但同时要记住别忘了对各个类的属性进行封装。

第三题:

题目说明如下:

类图如下:

 

 

 源码如下:

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

分析如下:本题主要考察对于ArrayList的运用和对象创建的理解。对象创建时不积极也要定义,也要new一个新的对象。而ArrayList中remove操作主要要注意不能超过其长度。

踩坑心得

  在这次几次作业中,我深刻得认识到——自己的思维仍然是c语言的思维而不是OOP的思维,每次拿到一个题目就急着去做而不是一开始就进行类设计。这样做的后果是我没有深刻理解用户需求和题目含义,导致本想节省时间,结果却会花更多的时间。同时,在编写程序时也会顾此失彼,找不到解决和前进的方向。

内容总结:

   从这几次的题目中,很明显能看出难度想较前几个星期有很大地提升,不像以前仅仅是对于语法的考察,尤其是蔡柯老师的题目。这就需要花费很多时间,而不是像以前一样在后几天才开始分析。同时,掌握OOP思维才是解题的关键,而不是痴迷于对语法的研究。语法固然重要,但设计思维更为重要。

 

posted on 2022-05-01 22:40  不知明月照何人  阅读(30)  评论(0)    收藏  举报