Java第二次Blog
- 前言
1. 習題集四前言
本次題集共有三題, 第一題是對正則表達式的考查,要求用正則表達式來完成題目,輸入一段話,將每一行中的數字提取出來并相加,最后輸出每一行數字的總和;第二題仍是對點線坐標的考查,這次主要考的是凸四邊形的相關計算,題目較難,對數學知識有一定的考量,同時也是對情況多樣性的一種考查,完成情況較差,只拿了30分(滿分是70分);第三題則是要求寫一個銀行業務類,實現設置密碼,存款,取款等一系列操作,同時輸出對應的金額,這道題相對來說是簡單的,得到了滿分。
2. 期中考試題集前言
期中考試題集是對點與線(類設計)的一種不斷深入考查,給出相應的類圖,要求寫出對應的代碼,第一題是簡單類的考查,涉及到私有屬性,方法調用和使用的考查;第二題則是加入了抽象類的考查,還有父類子類的關系、抽象方法的考查等;第三題是在原有類設計的基礎上,增加一個GeometryObject容器類,其屬性為ArrayList<Element>類型的對象,主要考查多態等相關問題。三次題目不是很難,但是未在指定時間內完成,技術上仍有很大的不足,知識點運用不熟悉,前兩題耗費較多的時間。
3.農夫過河題集前言
本次題集是對給定代碼的一個改寫,第一次是將程序代碼補充完整,實現農夫過河游戲功能:由用戶選擇角色過河,系統自動判斷游戲的勝負:當出現有生物被吃掉的時候,游戲失敗,所有角色都到了河的另一邊,游戲成功,為了確保結果的正確性,應該進行多次測試;第二次則是增加Boat(船)類,設計船類的相應方法,并修改main方法中相關的代碼,完善農夫帶東西過河的功能,同時將數據進行封裝,涉及到屬性和方法的使用和調用;第三次是繼承和多態的考查,寫入一個父類,通過繼承的方式讓每個子類都可以調用父類的方法,也應該進行多次測試,不僅僅是正確的過河方式,還有生物在另外一岸,農夫無法帶的問題,以及生物被吃游戲結束等問題。
- 習題集的得分情況:
習題集四:60分(滿分100分)
期中考試題集:72分(滿分100分)
農夫過河問題:86分(滿分100分)
- 設計與分析
(1)銀行業務類類圖:

import java.util.Scanner; import java.util.regex.*; import java.util.Arrays; public class Main{ public static void main(String[] args){ Scanner in=new Scanner(System.in); do{ int sum=0; String s=in.nextLine(); if(s.equals("end")){ break; } Pattern a=Pattern.compile("\\d+"); Matcher b=a.matcher(s); while(b.find()) { sum+=Integer.parseInt(b.group()); } System.out.println(sum); }while(true); } }
①設計思路:輸入一行數據時,用空格來間隔,當檢測到空格時轉為下一個數據,將一行輸入的數值存到不同的數據中;
代碼分析:第一個數據用input.next()輸入,第二個則采用imput.nextInt();
String username; int key; double balance = 0.0; username = input1.next(); key = input1.nextInt();
第一題的簡單類圖:

import java.util.Scanner; import java.util.regex.*; import java.util.Arrays; public class Main{ public static void main(String[] args){ Scanner in=new Scanner(System.in); do{ int sum=0; String s=in.nextLine(); if(s.equals("end")){ break; } Pattern a=Pattern.compile("\\d+"); Matcher b=a.matcher(s); while(b.find()) { sum+=Integer.parseInt(b.group()); } System.out.println(sum); }while(true); } }
①設計思路:用正則表達式來判斷一行中的數字,提取出所有的數字;
代碼分析:運用Pattern和Matcher函數將數字分割并從字符串中提取,再利用find()函數找到每個數字,強制類型轉化后得到數字;
Pattern a=Pattern.compile("\\d+");
Matcher b=a.matcher(s);
while(b.find()) {
sum+=Integer.parseInt(b.group());
}
(2)


import java.util.Scanner; public class Main{ public static void main(String[] agrs){ Scanner input = new Scanner(System.in); double x1 = input.nextDouble(); double y1 = input.nextDouble(); double x2= input.nextDouble(); double y2 = input.nextDouble(); String s = input.next(); Point point1 = new Point(x1,y1); Point point2 = new Point(x2,y2); Line b = new Line(point1,point2,s); if(x1>0 && x1<=200 && x2>0 && x2<=200 && y1>0 && y1<=200 && y2>0 && y2<=200) { System.out.println("The line's color is:" + b.getColor()); System.out.println("The line's begin point's Coordinate is:"); point1.display(); System.out.println("The line's end point's Coordinate is:"); point2.display(); System.out.print("The line's length is:"); b.display(); } else { System.out.println("Wrong Format"); } } } class Point{ private double x; private double y; public Point(){ } public Point(double x,double y){ this.x = x; this.y = y; } public double getX(){ return x; } public void setX(double x){ this.x = x; } public double getY(){ return y; } public void setY(double y){ this.y = y; } public void display(){ System.out.printf("(%.2f,%.2f)\n",this.x,this.y); } } class Line{ private Point point1; private Point point2; private String color; public Line(){ this.point1 = point1; this.point2 = point2; this.color = color; } public Line(Point p1,Point p2,String s){ this.point1 = p1; this.point2 = p2; this.color = s; } public Point getPoint1(){ return point1; } public void setPoint1(Point point1){ this.point1 = point1; } public Point getPoint2(){ return point2; } public void setPoint2(Point point2){ this.point2 = point2; } public String getColor(){ return color; } public void setColor(String color){ this.color = color; } public double getDistance(){ return getDistance(); } public void display(){ double distance = Math.sqrt(Math.pow((point1.getX()-point2.getX()),2)+Math.pow((point1.getY()-point2.getY()),2)); System.out.printf("%.2f",distance); } }


import java.util.Scanner; public class Main{ public static void main(String[] agrs){ Scanner input = new Scanner(System.in); double x1 = input.nextDouble(); double y1 = input.nextDouble(); double x2= input.nextDouble(); double y2 = input.nextDouble(); String s = input.next(); Point point1 = new Point(x1,y1); Point point2 = new Point(x2,y2); Line b = new Line(point1,point2,s); Plane colour = new Plane(s); Element element = new Element(){ public void display() }; if(x1>0 && x1<=200 && x2>0 && x2<=200 && y1>0 && y1<=200 && y2>0 && y2<=200) { element = point1; element.display(); element = point2; element.display(); System.out.println("The line's color is:" + b.getColor()); System.out.println("The line's begin point's Coordinate is:"); element = point1; element.display(); System.out.println("The line's end point's Coordinate is:"); element = point2; element.display(); System.out.print("The line's length is:"); element = b; element.display(); System.out.println(""); System.out.print("The Plane's color is:"); element = colour; element.display(); } else { System.out.println("Wrong Format"); } } } abstract class Element{ public abstract void display(); } class Point extends Element{ private double x; private double y; public Point(){ } public Point(double x,double y){ this.x = x; this.y = y; } public double getX(){ return x; } public void setX(double x){ this.x = x; } public double getY(){ return y; } public void setY(double y){ this.y = y; } public void display(){ System.out.printf("(%.2f,%.2f)\n",this.x,this.y); } } class Line extends Element{ private Point point1; private Point point2; private String color; public Line(){ this.point1 = point1; this.point2 = point2; this.color = color; } public Line(Point p1,Point p2,String s){ this.point1 = p1; this.point2 = p2; this.color = s; } public Point getPoint1(){ return point1; } public void setPoint1(Point point1){ this.point1 = point1; } public Point getPoint2(){ return point2; } public void setPoint2(Point point2){ this.point2 = point2; } public String getColor(){ return color; } public void setColor(String color){ this.color = color; } public double getDistance(){ return getDistance(); } public void display(){ double distance = Math.sqrt(Math.pow((point1.getX()-point2.getX()),2)+Math.pow((point1.getY()-point2.getY()),2)); System.out.printf("%.2f",distance); } } class Plane extends Element{ private String color; Plane(){ } Plane(String color){ this.color = color; } public String getColor(){ return color; } public void setColor(String color){ this.color = color; } public void display(){ System.out.print(this.color); } }
(3)農夫過河問題中的部分代碼分析和思路
①設計思路:當使用private時,需要用get()來進行對數據的訪問,用set()來進行數據的使用;
代碼分析:在get()函數中return需要訪問的值,set()對訪問的值進行修改;
class object{ private boolean isAlive = true; private boolean crossRiver = false; private String name; public String getname() { return name; }public void setname(String name) { this.name = name; } public boolean getcrossRiver() { return crossRiver; } public void setcrossRiver() { crossRiver=!crossRiver; } public boolean getisAlive() { return isAlive; } public void setisAlive() { isAlive=!isAlive; } }
② 設計思路:當使用private時,需要用get()來進行對數據的訪問,用set()來進行數據的使用;
代碼分析:在get()函數中return需要訪問的值,set()對訪問的值進行修改;
class Wolf extends object{ public String getname() { return super.getname(); } public boolean getcrossRiver() { return super.getcrossRiver(); } public boolean getisAlive() { return super.getisAlive(); } Wolf(String name){ super.setname(name); System.out.println("啊嗚~~~我" + super.getname() + "狼又回來了"); } public void setcrossRiver() { super.setcrossRiver(); } public void eatSheep(object sheep,object farmer){ if(super.getcrossRiver() == sheep.getcrossRiver() && super.getcrossRiver()!=farmer.getcrossRiver()){ sheep.setisAlive(); } } public void showStatus(){ System.out.printf("Wolf " + super.getname() + " is alive :%s Wolf "+ super.getname() +" has Cross :%s\n",super.getisAlive(),super.getcrossRiver()); } public void setisAlive() { super.setisAlive(); } }
③設計思路:父類與子類的繼承關系,用extends來連接,子類中采用super關鍵字;
代碼分析:super表示通過當前子類的構造方法調用父類的無參數構造方法。所以必須保證父類的無參數構造方法是存在的;
class Sheep extends object{ public String getname() { return super.getname(); } public boolean getcrossRiver() { return super.getcrossRiver(); } public boolean getisAlive() { return super.getisAlive(); } Sheep(String name){ super.setname(name); System.out.println("咩咩,我是可愛的小羊" + super.getname()); } public void eatCabbage(object cabbage,object farmer){ if(super.getcrossRiver() == cabbage.getcrossRiver() && super.getcrossRiver() != farmer.getcrossRiver()) { cabbage.setisAlive(); } } public void showStatus(){ System.out.printf("Sheep " + super.getname() + " is alive :%s Sheep " + super.getname() + " has Cross :%s\n",super.getisAlive(),super.getcrossRiver()); } public void setcrossRiver() { super.setcrossRiver(); } public void setisAlive() { super.setisAlive(); } }



- 踩坑心得
①正則表達式默認進行貪婪匹配,可以用?來實現非貪婪匹配;
②字符串的直接比較可以采用equals連接前后兩者,沒必要依次比較每一個字符;
③Find函數用來對原始數據中某個字符串進行定位,以確定其位置,可以簡化程序代碼,使程序更加簡潔明了;
④不應該空定義抽象方法,切記切記切記?。。”敬纹谥锌荚囶}集第二題因此導致0分;
⑤Java中想要在一行輸入兩個數據時,可以采用input.next()方式來進行輸入。
⑥不斷優化實現功能的代碼算法,實現對代碼的凝練和升華,不斷熟悉代碼和知識,加快做題速度。
- 改進建議
①代碼盡可能融入新的知識,一個功能一個類,避免一個類復用,導致代碼的容錯率降低,后期應逐步加入繼承等新知識。
②順序結構不夠簡潔,邏輯較為混亂,應在設計代碼時提前用流程圖進行規劃,使程序清晰易讀。
③不斷考慮可能會出現的情況,不僅僅局限于測試點的通過。
④正則表達式的學習還不到位,仍然需要繼續在網上查找資料來學習。
⑤在一些代碼的關鍵節點,我應該加上一些注釋,方便之后的查閱和修改。
⑥在進行繼承關系描述時,需要注意格式問題,用super關鍵字來調用父類中的方法。
- 總結
①深入了解了正則表達式后學會了簡單運用正則表達式。
②這幾周學會了類圖和流程圖的制作。
③學會了繼承和多態等知識點,對代碼之間的聯系理解進一步加深。
④對知識點的運用不熟悉,導致期中考試的成績很低,最后一道題甚至沒有做完,值得反思,要在題目中不斷加入新學到的知識,加快做題速度。
浙公網安備 33010602011771號