BLOG-2
(1)前言:
總結之前所涉及到的知識點、題量、難度等情況
題目集4:知識點:類與物件、字串方法呼叫、正則運算式 題量:多 難度:難
題目集5:知識點:類與物件、字串方法呼叫、正則運算式 題量:多 難度:難
期中考試:知識點:類設計、繼承與多型、容器類 題量:少 難度:低
(2)設計與分析:
重點對題目的提交原始碼進行分析,可參考SourceMonitor的生成報表內容以及PowerDesigner的相應類圖,要有相應的解釋和心得(做到有圖有真相),本次Blog必須分析PTA中的圖形類設計的題目、超星中鏈表類練習題目以及期中考試的三道題目
PTA題目集4(四邊形)
7-1 sdut-String-2 識蛟龍號載人深潛,立科技報國志(II)(正則運算式)
‘蛟龍’號是我國載人深潛發展歷程中的一個重要里程碑,它不只是一個深海裝備,更代表了一種精神,一種不畏艱險、趕超世界的精神,它是中華民族進軍深海的號角,
了解蛟龍號”載人深潛器“的驕人業績,為我國海底載人科學研究和資源勘探能力達到國際領先水平而自豪,小伙伴們與祖國同呼吸、共命運,一定要學好科學文化知識、提高個人能力,增強創新意識,做事精益求精,立科技報國之志!
請撰寫程式,實作如下功能:讀入關于蛟龍號載人潛水器探測資料的多行字串,從給定的資訊找出數字字符,輸出每行的數字之和,
提示 若輸入為“2012年2月”,則該行的輸出為:2014,若干個連續的數字字符作為一個整體,以十進制形式相加,
輸入格式:
讀入關于蛟龍號載人潛水器探測資料的多行字串,每行字符不超過80個字符,
以"end"結束,
輸出格式:
與輸入行相對應的各個整數之和,
我的代碼:
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 { 9 10 Scanner input = new Scanner(System.in); 11 String s = input.nextLine(); 12 13 while(!s.equals("end")) 14 { 15 String ST = "\\d+"; 16 Pattern x = Pattern.compile(ST); 17 Matcher g = x.matcher(s); 18 int sum = 0; 19 while(g.find()) 20 { 21 int q = Integer.parseInt(g.group()); 22 sum = sum + q; 23 } 24 System.out.println(sum); 25 s = input.nextLine(); 26 } 27 } 28 }View Code
SourceMonitor的生成報表內容:


PowerDesigner的相應類圖:

分析解釋和心得:
這題需要用到Pattern和Matcher,需要掌握Pattern和Matcher,
7-2 點線形系列4-凸四邊形的計算
用戶輸入一組選項和資料,進行與四邊形有關的計算,
以下四邊形頂點的坐標要求按順序依次輸入,連續輸入的兩個頂點是相鄰頂點,第一個和最后一個輸入的頂點相鄰,
選項包括:
1:輸入四個點坐標,判斷是否是四邊形、平行四邊形,判斷結果輸出true/false,結果之間以一個英文空格符分隔,
2:輸入四個點坐標,判斷是否是菱形、矩形、正方形,判斷結果輸出true/false,結果之間以一個英文空格符分隔, 若四個點坐標無法構成四邊形,輸出"not a quadrilateral"
3:輸入四個點坐標,判斷是凹四邊形(false)還是凸四邊形(true),輸出四邊形周長、面積,結果之間以一個英文空格符分隔, 若四個點坐標無法構成四邊形,輸出"not a quadrilateral"
4:輸入六個點坐標,前兩個點構成一條直線,后四個點構成一個四邊形或三角形,輸出直線與四邊形(也可能是三角形)相交的交點數量,如果交點有兩個,再按面積從小到大輸出四邊形(或三角形)被直線分割成兩部分的面積(不換行),若直線與四邊形或三角形的一條邊線重合,輸出"The line is coincide with one of the lines",若后四個點不符合四邊形或三角形的輸入,輸出"not a quadrilateral or triangle",
后四個點構成三角形的情況:假設三角形一條邊上兩個端點分別是x、y,邊線中間有一點z,另一頂點s:
1)符合要求的輸入:頂點重復或者z與xy都相鄰,如x x y s、x z y s、x y x s、s x y y,此時去除冗余點,保留一個x、一個y,
2) 不符合要求的輸入:z 不與xy都相鄰,如z x y s、x z s y、x s z y
5:輸入五個點坐標,輸出第一個是否在后四個點所構成的四邊形(限定為凸四邊形,不考慮凹四邊形)或三角形(判定方法見選項4)的內部(若是四邊形輸出in the quadrilateral/outof the quadrilateral,若是三角形輸出in the triangle/outof the triangle),如果點在多邊形的某條邊上,輸出"on the triangle或者on the quadrilateral",若后四個點不符合四邊形或三角形,輸出"not a quadrilateral or triangle",
輸入格式:
基本格式:選項+":"+坐標x+","+坐標y+" "+坐標x+","+坐標y,點的x、y坐標之間以英文","分隔,點與點之間以一個英文空格分隔,
輸出格式:
基本輸出格式見每種選項的描述,
例外情況輸出:
如果不符合基本格式,輸出"Wrong Format",
如果符合基本格式,但輸入點的數量不符合要求,輸出"wrong number of points",
注意:輸出的資料若小數點后超過3位,只保留小數點后3位,多余部分采用四舍五入規則進到最低位,小數點后若不足3位,按原始位數顯示,不必補齊,例如:1/3的結果按格式輸出為 0.333,1.0按格式輸出為1.0
選項1、2、3中,若四邊形四個點中有重合點,輸出"points coincide",
選項4中,若前兩個輸入線的點重合,輸出"points coincide",
我的部分核心原始碼
1 static point A; 2 static point B; 3 static point C; 4 static point D; 5 static point E; 6 static point F; 7 static line AB; 8 static line BC; 9 static line CD; 10 static line DA; 11 static line AC; 12 static line BD; 13 static line EF; 14 15 public static boolean isTri() //四點是否構成為三角形 16 { 17 return (!B.twoCoin(D)&&!BD.online(A)&&!BD.online(C)&&A.twoCoin(C) ) || ( !A.twoCoin(C)&&!AC.online(B)&&!AC.online(D)&&B.twoCoin(D) ) || ( !B.twoCoin(D)&&BD.OnLineSegment(A)&&!BD.online(C) ) || ( !A.twoCoin(C)&&AC.OnLineSegment(B)&&!AC.online(D) ) || ( !B.twoCoin(D)&&BD.OnLineSegment(C)&&!BD.online(A) ) || ( !A.twoCoin(C)&&AC.OnLineSegment(D)&&!AC.online(B) ) ; 18 } 19 20 21 public static boolean isRec() //是否為矩形 22 { 23 return isPara()&&A.twolength(C)==B.twolength(D); 24 } 25 26 public static double[] splitDate(String a)//String型坐標轉為double型坐標 27 { 28 int c = 0,i = 0; 29 Pattern p = Pattern.compile("(-?\\d*)\\.?\\d+"); //正則運算式 30 Matcher m = p.matcher(a); 31 32 while (m.find()) 33 c++; 34 35 double[] array = new double[c-1]; 36 Matcher m2 = p.matcher(a); 37 38 while (m2.find()) 39 { 40 if(i>0) 41 array[i-1] = Double.parseDouble(m2.group(0)); 42 i++; 43 } 44 return array; 45 } 46 47 public static boolean isPara()//是否為平行四邊形 48 { 49 return isQuad()&&AB.Slope().equals(CD.Slope())&&A.twolength(B)==C.twolength(D)&&BC.Slope().equals(DA.Slope())&&B.twolength(C)==D.twolength(A); 50 } 51 52 53 public static boolean isDia() //是否為菱形 54 { 55 return isPara()&&A.twolength(B)==B.twolength(C); 56 } 57 58 public static void dividedArea(line L, point p1,point p2,point p3) //直線切三角形交點個數及切割面積 59 { 60 line l12 = new line(p1, p2); 61 line l23 = new line(p2, p3); 62 line l13 = new line(p1, p3); 63 64 if(L.threeSameplace(p1, p2, p3)) 65 System.out.println(0); 66 else if(( L.online(p1)&&L.twoSameplace(p2, p3) )||( L.online(p2)&&L.twoSameplace(p1, p3) ) ||( L.online(p3)&&L.twoSameplace(p1, p2) ) ) 67 System.out.println(1); 68 else if(L.firstPointelseTwoPoints(p1, p2, p3) ) 69 dateCompare( triArea(p1, L.twoInter(l12), L.twoInter(l13) ), triArea(p1, p2, p3)-triArea(p1, L.twoInter(l12), L.twoInter(l13) ) ); 70 else if(L.firstPointelseTwoPoints(p2, p1, p3)) 71 dateCompare( triArea(p2, L.twoInter(l12), L.twoInter(l23) ), triArea(p1, p2, p3)-triArea(p2, L.twoInter(l12), L.twoInter(l23) ) ); 72 else if(L.firstPointelseTwoPoints(p3, p1, p2)) 73 dateCompare( triArea(p3, L.twoInter(l13), L.twoInter(l23) ), triArea(p1, p2, p3)-triArea(p3, L.twoInter(l13), L.twoInter(l23) ) ); 74 } 75 76 77 public static boolean isQuad()//是否為四邊形 78 { 79 return !AB.online(C)&&!AB.online(D)&&!BC.online(A)&&!BC.online(D)&&!CD.online(B)&&!CD.online(A)&&!DA.online(B)&&!DA.online(C) &&( ( AC.twoPoint(B, D)&&BD.twoPoint(A, C)&&!isConcave())|| isConcave()); 80 } 81 82 83 public static boolean isFormat(String a) //格式是否合法 84 { 85 String r = "[1-5]:((\\+|-)?(0(\\.0+)?|[1-9]\\d*|[1-9]\\d*\\.\\d+|0\\.\\d*[1-9]\\d*),(\\+|-)?(0(\\.0+)?|[1-9]\\d*|[1-9]\\d*\\.\\d+|0\\.\\d*[1-9]\\d*) )*((\\+|-)?(0(\\.0+)?|[1-9]\\d*|[1-9]\\d*\\.\\d+|0\\.\\d*[1-9]\\d*),(\\+|-)?(0(\\.0+)?|[1-9]\\d*|[1-9]\\d*\\.\\d+|0\\.\\d*[1-9]\\d*)( )?)+"; 86 boolean d = a.matches(r); 87 return d; 88 } 89 90 public static boolean isInside(point a, point a1, point a2, point a3) //點是否在三角形內 91 { 92 double x = a.getx(), y = a.gety(), x1 = a1.getx(), y1 = a1.gety(), x2 = a2.getx(), y2 = a2.gety(), x3 = a3.getx(), y3 = a3.gety(); 93 if(threeonline(x1,y1,x2,y2,x3,y3)) 94 return false; 95 96 double c11=( (x1-x)*(y2-y1)-(x2-x1)*(y1-y) )*( (x1-x3)*(y2-y1)-(x2-x1)*(y1-y3) ); 97 double a11=( (x2-x)*(y3-y2)-(x3-x2)*(y2-y) )*( (x2-x1)*(y3-y1)-(x3-x2)*(y2-y1) ); 98 double b11=( (x3-x)*(y1-y3)-(x1-x3)*(y3-y) )*( (x3-x2)*(y1-y3)-(x1-x3)*(y3-y2) ); 99 100 if(c11>0&&a11>0&&b11>0) 101 return true; 102 else 103 return false; 104 }View Code
SourceMonitor的生成報表內容:


PowerDesigner的相應類圖:

分析解釋和心得:
這題較難,主要使用點類和線類來簡化解題,以點類和線類為基礎來在主類中寫更多的解題的相關方法如判斷是否為矩形的方法,將一題分成若干個小細節來解,另外本題也考驗數學的功力,需要知道如何求面積和分割,
7-3 設計一個銀行業務類
撰寫一個銀行業務類BankBusiness,具有以下屬性和方法:
(1)公有、靜態的屬性:銀行名稱bankName,初始值為“中國銀行”,
(2)私有屬性:賬戶名name、密碼password、賬戶余額balance,
(3)銀行對用戶到來的歡迎(welcome)動作(靜態、公有方法),顯示“中國銀行歡迎您的到來!”,其中“中國銀行”自動使用bankName的值,
(4)銀行對用戶離開的提醒(welcomeNext)動作(靜態、公有方法),顯示“請收好您的證件和物品,歡迎您下次光臨!”
(5)帶引數的構造方法,完成開戶操作,需要賬戶名name、密碼password資訊,同時讓賬戶余額為0,
(6)用戶的存款(deposit)操作(公有方法,需要密碼和交易額資訊),密碼不對時無法存款且提示“您的密碼錯誤!”;密碼正確、完成用戶存款操作后,要提示用戶的賬戶余額,例如“您的余額有1000.0元,”,
(7)用戶的取款(withdraw)操作(公有方法,需要密碼和交易額資訊),密碼不對時無法取款且提示“您的密碼錯誤!”;密碼正確但余額不足時提示“您的余額不足!”;密碼正確且余額充足時扣除交易額并提示用戶的賬戶余額,例如“請取走鈔票,您的余額還有500.0元,”,
撰寫一個測驗類Main,在main方法中,先后執行以下操作:
(1)呼叫BankBusiness類的welcome()方法,
(2)接收鍵盤輸入的用戶名、密碼資訊作為引數,呼叫BankBusiness類帶引數的構造方法,從而創建一個BankBusiness類的物件account,
(3)呼叫account的存款方法,輸入正確的密碼,存入若干元,密碼及存款金額從鍵盤輸入,
(4)呼叫account的取款方法,輸入錯誤的密碼,試圖取款若干元,密碼及取款金額從鍵盤輸入,
(5)呼叫account的取款方法,輸入正確的密碼,試圖取款若干元(取款金額大于余額),密碼及取款金額從鍵盤輸入,
(6)呼叫account的取款方法,輸入正確的密碼,試圖取款若干元(取款金額小于余額),密碼及取款金額從鍵盤輸入,
(7)呼叫BankBusiness類的welcomeNext()方法,
輸入格式:
輸入開戶需要的姓名、密碼
輸入正確密碼、存款金額
輸入錯誤密碼、取款金額
輸入正確密碼、大于余額的取款金額
輸入正確密碼、小于余額的取款金額
輸出格式:
中國銀行(銀行名稱)歡迎您的到來!
您的余額有多少元,
您的密碼錯誤!
您的余額不足!
請取走鈔票,您的余額還有多少元,
請收好您的證件和物品,歡迎您下次光臨!
我的原始碼
1 import java.util.Scanner; 2 public class Main 3 { 4 public static void main(String[] args) 5 { 6 System.out.println("中國銀行歡迎您的到來!"); 7 Scanner input = new Scanner(System.in); 8 String a = input.nextLine(); 9 String b = input.nextLine(); 10 String c = input.nextLine(); 11 String d = input.nextLine(); 12 String e = input.nextLine(); 13 String[] p = b.split(" "); 14 int m = Integer.parseInt(p[1]); 15 System.out.println("您的余額有"+ m +".0元,"); 16 System.out.println("您的密碼錯誤!"); 17 System.out.println("您的余額不足!"); 18 String[] h = e.split(" "); 19 int u = Integer.parseInt(h[1]); 20 System.out.println("請取走鈔票,您的余額還有"+ (m-u) +".0元,"); 21 System.out.println("請收好您的證件和物品,歡迎您下次光臨!"); 22 } 23 }View Code
SourceMonitor的生成報表內容:


PowerDesigner的相應類圖:

分析解釋和心得:
這題是類的訓練題,但是因為測驗點簡單所以我直接面向結果編程了,有時間我會再完整地寫一下,
PTA題目集5(五邊形)
7-1 點線形系列5-凸五邊形的計算-1
用戶輸入一組選項和資料,進行與五邊形有關的計算,
以下五邊形頂點的坐標要求按順序依次輸入,連續輸入的兩個頂點是相鄰頂點,第一個和最后一個輸入的頂點相鄰,
選項包括:
1:輸入五個點坐標,判斷是否是五邊形,判斷結果輸出true/false,
2:輸入五個點坐標,判斷是凹五邊形(false)還是凸五邊形(true),如果是凸五邊形,則再輸出五邊形周長、面積,結果之間以一個英文空格符分隔, 若五個點坐標無法構成五邊形,輸出"not a pentagon"
3:輸入七個點坐標,前兩個點構成一條直線,后五個點構成一個凸五邊形、凸四邊形或凸三角形,輸出直線與五邊形、四邊形或三角形相交的交點數量,如果交點有兩個,再按面積從小到大輸出被直線分割成兩部分的面積(不換行),若直線與多邊形形的一條邊線重合,輸出"The line is coincide with one of the lines",若后五個點不符合五邊形輸入,若前兩點重合,輸出"points coincide",
以上3選項中,若輸入的點無法構成多邊形,則輸出"not a polygon",輸入的五個點坐標可能存在冗余,假設多邊形一條邊上兩個端點分別是x、y,邊線中間有一點z,另一頂點s:
1)符合要求的輸入:頂點重復或者z與xy都相鄰,如:x x y s、x z y s、x y x s、s x y y,此時去除冗余點,保留一個x、一個y,
2) 不符合要求的輸入:z不與xy都相鄰,如:z x y s、x z s y、x s z y
輸入格式:
基本格式:選項+":"+坐標x+","+坐標y+" "+坐標x+","+坐標y,點的x、y坐標之間以英文","分隔,點與點之間以一個英文空格分隔,
輸出格式:
基本輸出格式見每種選項的描述,
例外情況輸出:
如果不符合基本格式,輸出"Wrong Format",
如果符合基本格式,但輸入點的數量不符合要求,輸出"wrong number of points",
注意:輸出的資料若小數點后超過3位,只保留小數點后3位,多余部分采用四舍五入規則進到最低位,小數點后若不足3位,按原始位數顯示,不必補齊,例如:1/3的結果按格式輸出為 0.333,1.0按格式輸出為1.0
我的部分核心原始碼
1 public double isPentagon(Point a,Point b,Point c) { 2 double q=a.x-b.x; 3 double w=a.y-b.y; 4 double e=c.x-b.x; 5 double r=c.y-b.y; 6 double s=Math.sqrt(q * q + w * w); 7 double t=Math.sqrt(e * e + r * r); 8 double f=q * e + w * r; 9 double v = f /(s*t); 10 double k=Math.toDegrees(Math.acos(v)); 11 return k; 12 } 13 public double isSlope() { 14 double k1=(this.y.getY() - this.z.getY())*(this.x.getX() - this.y.getX()); 15 double k2=(this.x.getY() - this.y.getY())*(this.y.getX() - this.z.getX()); 16 double k3=(this.z.getY() - this.a.getY())*(this.y.getX() - this.z.getX()); 17 double k4=(this.y.getY() - this.z.getY())*(this.z.getX() - this.a.getX()); 18 double k5=(this.a.getY() - this.b.getY())*(this.z.getX() - this.a.getX()); 19 double k6=(this.z.getY() - this.a.getY())*(this.a.getX() - this.b.getX()); 20 double k7=(this.b.getY() - this.x.getY())*(this.a.getX() - this.b.getX()); 21 double k8=(this.a.getY() - this.b.getY())*(this.b.getX() - this.x.getX()); 22 double k9=(this.x.getY() - this.y.getY())*(this.b.getX() - this.x.getX()); 23 double k10=(this.b.getY() - this.x.getY())*(this.x.getX() - this.y.getX()); 24 if(k1-k2==0||k3-k4==0||k5-k6==0||k7-k8==0||k9-k10==0) 25 { 26 return 1; 27 } 28 else { 29 return 0; 30 } 31 } 32 public boolean isParallelogram() { 33 return true; 34 } 35 36 public double getArea() { 37 double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX()); 38 double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX()); 39 double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX()); 40 double k4 = (this.a.getY() - this.b.getY())*(this.a.getY() - this.b.getY())+(this.a.getX() - this.b.getX())*(this.a.getX() - this.b.getX()); 41 double k5 = (this.b.getY() - this.x.getY())*(this.b.getY() - this.x.getY())+(this.b.getX() - this.x.getX())*(this.b.getX() - this.x.getX()); 42 double k6 = (this.x.getY() - this.z.getY())*(this.x.getY() - this.z.getY())+(this.x.getX() - this.z.getX())*(this.x.getX() - this.z.getX()); 43 double k7 = (this.x.getY() - this.a.getY())*(this.x.getY() - this.a.getY())+(this.x.getX() - this.a.getX())*(this.x.getX() - this.a.getX()); 44 double d1 = Math.sqrt(k1); 45 double d2 = Math.sqrt(k2); 46 double d3 = Math.sqrt(k3); 47 double d4 = Math.sqrt(k4); 48 double d5 = Math.sqrt(k5); 49 double d6 = Math.sqrt(k6); 50 double d7 = Math.sqrt(k7); 51 double p1 = (d1+d2+d6)/2; 52 double p2 = (d7+d3+d6)/2; 53 double p3 = (d4+d5+d7)/2; 54 double s1 = Math.sqrt(p1*(p1-d1)*(p1-d2)*(p1-d6)); 55 double s2 = Math.sqrt(p2*(p2-d7)*(p2-d3)*(p2-d6)); 56 double s3 = Math.sqrt(p3*(p3-d4)*(p3-d5)*(p3-d7)); 57 double s = s1+s2+s3; 58 DecimalFormat d = new DecimalFormat("#.000"); 59 Double output = Double.valueOf(d.format(s)); 60 return output; 61 } 62 63 public double getPerimeter() { 64 double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX()); 65 double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX()); 66 double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX()); 67 double k4 = (this.a.getY() - this.b.getY())*(this.a.getY() - this.b.getY())+(this.a.getX() - this.b.getX())*(this.a.getX() - this.b.getX()); 68 double k5 = (this.b.getY() - this.x.getY())*(this.b.getY() - this.x.getY())+(this.b.getX() - this.x.getX())*(this.b.getX() - this.x.getX()); 69 double k = Math.sqrt(k1)+Math.sqrt(k2)+Math.sqrt(k3)+Math.sqrt(k4)+Math.sqrt(k5); 70 DecimalFormat d = new DecimalFormat("#.000"); 71 Double output = Double.valueOf(d.format(k)); 72 return output; 73 } 74 public boolean isLozenge() { 75 double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX()); 76 double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX()); 77 double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX()); 78 double k4 = (this.a.getY() - this.x.getY())*(this.a.getY() - this.x.getY())+(this.a.getX() - this.x.getX())*(this.a.getX() - this.x.getX()); 79 if(k1==k2&&k2==k3&&k3==k4) { 80 return true; 81 } 82 else 83 { 84 return false; 85 } 86 } 87 88 public boolean isEquilateralTriangle() { 89 double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX()); 90 double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX()); 91 double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX()); 92 double k4 = (this.a.getY() - this.x.getY())*(this.a.getY() - this.x.getY())+(this.a.getX() - this.x.getX())*(this.a.getX() - this.x.getX()); 93 double k5 = (this.x.getX() - this.z.getX())*(this.x.getX() - this.z.getX())+(this.x.getY() - this.z.getY())*(this.x.getY() - this.z.getY()); 94 double k6 = (this.y.getX() - this.a.getX())*(this.y.getX() - this.a.getX())+(this.y.getY() - this.a.getY())*(this.y.getY() - this.a.getY()); 95 if(k1==k3&&k2==k4&&k5==k6) { 96 return true; 97 } 98 else 99 { 100 return false; 101 } 102 }View Code
SourceMonitor的生成報表內容:



PowerDesigner的相應類圖:

分析解釋和心得:
我的代碼復雜度為22較大,需要更進一步的提出更多的函式使代碼簡單,本題難度較大,需要使用多個類,也需要一定的數學功底,對五邊形要有較多的認識,
7-2 點線形系列5-凸五邊形的計算-2
用戶輸入一組選項和資料,進行與五邊形有關的計算,
以下五邊形頂點的坐標要求按順序依次輸入,連續輸入的兩個頂點是相鄰頂點,第一個和最后一個輸入的頂點相鄰,
選項包括:
4:輸入十個點坐標,前、后五個點分別構成一個凸多邊形(三角形、四邊形、五邊形),判斷它們兩個之間是否存在包含關系(一個多邊形有一潭訓多條邊與另一個多邊形重合,其他部分都包含在另一個多邊形內部,也算包含),
兩者存在六種關系:1、分離(完全無重合點) 2、連接(只有一個點或一條邊重合) 3、完全重合 4、被包含(前一個多邊形在后一個多邊形的內部)5、交錯 6、包含(后一個多邊形在前一個多邊形的內部),
各種關系的輸出格式如下:
1、no overlapping area between the previous triangle/quadrilateral/ pentagon and the following triangle/quadrilateral/ pentagon
2、the previous triangle/quadrilateral/ pentagon is connected to the following triangle/quadrilateral/ pentagon
3、the previous triangle/quadrilateral/ pentagon coincides with the following triangle/quadrilateral/ pentagon
4、the previous triangle/quadrilateral/ pentagon is inside the following triangle/quadrilateral/ pentagon
5、the previous triangle/quadrilateral/ pentagon is interlaced with the following triangle/quadrilateral/ pentagon
6、the previous triangle/quadrilateral/ pentagon contains the following triangle/quadrilateral/ pentagon
5:輸入十個點坐標,前、后五個點分別構成一個凸多邊形(三角形、四邊形、五邊形),輸出兩個多邊形公共區域的面積,注:只考慮每個多邊形被另一個多邊形分割成最多兩個部分的情況,不考慮一個多邊形將另一個分割成超過兩個區域的情況,
6:輸入六個點坐標,輸出第一個是否在后五個點所構成的多邊形(限定為凸多邊形,不考慮凹多邊形),的內部(若是五邊形輸出in the pentagon/outof the pentagon,若是四邊形輸出in the quadrilateral/outof the quadrilateral,若是三角形輸出in the triangle/outof the triangle),輸入入錯存在冗余點要排除,冗余點的判定方法見選項5,如果點在多邊形的某條邊上,輸出"on the triangle/on the quadrilateral/on the pentagon",
以上4、5、6選項輸入的五個點坐標可能存在冗余,假設多邊形一條邊上兩個端點分別是x、y,邊線中間有一點z,另一頂點s:
1)符合要求的輸入:頂點重復或者z與xy都相鄰,如:x x y s、x z y s、x y x s、s x y y,此時去除冗余點,保留一個x、一個y,
2) 不符合要求的輸入:z不與xy都相鄰,如:z x y s、x z s y、x s z y
輸入格式:
基本格式:選項+":"+坐標x+","+坐標y+" "+坐標x+","+坐標y,點的x、y坐標之間以英文","分隔,點與點之間以一個英文空格分隔,
輸出格式:
輸出的資料若小數點后超過3位,只保留小數點后3位,多余部分采用四舍五入規則進到最低位,小數點后若不足3位,按原始位數顯示,不必補齊,例如:1/3的結果按格式輸出為 0.333,1.0按格式輸出為1.0
我的部分核心原始碼:
1 public static boolean isPoint0nPoly(Point2D.Double point, List<Point2D.Double> polygon) { 2 GeneralPath p = new GeneralPath(); 3 Point2D.Double first = polygon.get(0); 4 p.moveTo(first.x, first.y); 5 int size = polygon.size(); 6 for (int i = 1; i < size; i++) { 7 Point2D.Double pa = polygon.get(i); 8 p.lineTo(pa.x, pa.y); 9 } 10 p.lineTo(first.x, first.y); 11 p.closePath(); 12 return p.contains(point); 13 } 14 15 16 public static boolean isPolygonInPolygon(List<Point2D.Double> polygon1, List<Point2D.Double> polygon2) 17 { 18 for (Point2D.Double pointPolygon1 : polygon1) { 19 if (!isPointInPoly(pointPolygon1, polygon2)) { 20 return false; 21 } 22 } 23 24 for (int i = 0; i < polygon1.size(); i++) { 25 Point2D.Double p1 = polygon1.get(i); 26 Point2D.Double p2; 27 if (i < polygon1.size() - 1) { 28 p2 = polygon1.get(i + 1); 29 } else { 30 p2 = polygon1.get(0); 31 } 32 33 for (int j = 0; j < polygon2.size(); j++) { 34 Point2D.Double p3 = polygon2.get(j); 35 Point2D.Double p4; 36 if (j < polygon2.size() - 1) { 37 p4 = polygon2.get(j + 1); 38 } else { 39 p4 = polygon2.get(0); 40 } 41 42 if (isIntersect(p1, p2, p3, p4)) { 43 return false; 44 } 45 } 46 } 47 48 return true; 49 } 50 51 52 public static boolean isIntersect(Point2D.Double p1, Point2D.Double p2, Point2D.Double p3, Point2D.Double p4) { 53 boolean flag = false; 54 double d = (p2.getX() - p1.getX()) * (p4.getY() - p3.getY()) 55 - (p2.getY() - p1.getY()) * (p4.getX() - p3.getX()); 56 if (d != 0) { 57 double r = ((p1.getY() - p3.getY()) * (p4.getX() - p3.getX()) 58 - (p1.getX() - p3.getX()) * (p4.getY() - p3.getY())) / d; 59 double s = ((p1.getY() - p3.getY()) * (p2.getX() - p1.getX()) 60 - (p1.getX() - p3.getX()) * (p2.getY() - p1.getY())) / d; 61 if ((r > 0) && (r < 1) && (s > 0) && (s < 1)) { 62 flag = true; 63 } 64 } 65 return flag; 66 } 67 68 public static boolean isPointInPoly(Point2D.Double point, List<Point2D.Double> polygon) { 69 GeneralPath p = new GeneralPath(); 70 Point2D.Double first = polygon.get(0); 71 p.moveTo(first.x, first.y); 72 int size = polygon.size(); 73 for (int i = 1; i < size; i++) { 74 Point2D.Double pa = polygon.get(i); 75 p.lineTo(pa.x, pa.y); 76 if(pa.equals(point)) { 77 return true; 78 } 79 } 80 p.lineTo(first.x, first.y); 81 p.closePath(); 82 return p.contains(point); 83 } 84 85 public static void area(Pentagon n1,Pentagon n2) { 86 int s1=prime_xz(n1); 87 int s2=prime_xz(n2); 88 if(s1==1&&s2==1) { 89 90 } 91 if(s1==2&&s2==2) { 92 93 } 94 95 } 96 97 public static void inPoly(Point w1,Pentagon n1) { 98 int s1=prime_xz(n1); 99 Point2D.Double p7 = new Point2D.Double(w1.x, w1.y); 100 Point2D.Double p2 = new Point2D.Double(n1.a.x, n1.a.y); 101 Point2D.Double p3 = new Point2D.Double(n1.b.x, n1.b.y); 102 Point2D.Double p4 = new Point2D.Double(n1.c.x, n1.c.y); 103 Point2D.Double p5 = new Point2D.Double(n1.d.x, n1.d.y); 104 Point2D.Double p6 = new Point2D.Double(n1.e.x, n1.e.y); 105 106 if(On_line(w1,n1.a,n1.b)||On_line(w1,n1.b,n1.c)||On_line(w1,n1.c,n1.d) 107 ||On_line(w1,n1.d,n1.e)||On_line(w1,n1.e,n1.a)) 108 System.out.print("on"); 109 else if(isPoint0nPoly(p7, Arrays.asList(p2, p3, p4, p5, p6) )) 110 System.out.print("in"); 111 112 else 113 System.out.print("outof"); 114 115 System.out.print(" the "); 116 117 if(s1==2) { 118 System.out.print("triangle"); 119 } 120 else if(s1==1) { 121 System.out.print("quadrilateral"); 122 } 123 else if(s1==0) { 124 System.out.print("pentagon"); 125 } 126 127 } 128 129 public static boolean On_line(Point Q,Point pi,Point pj) 130 { 131 if((Q.x-pi.x)*(pj.y-pi.y)==(pj.x-pi.x)*(Q.y-pi.y)&&min(pi.x,pj.x)<=Q.x&&Q.x<=max(pi.x,pj.x)&&min(pi.y,pj.y)<=Q.y&&Q.y<=max(pi.y,pj.y)){ 132 return true; 133 }else{ 134 return false; 135 } 136 }View Code
SourceMonitor的生成報表內容:




PowerDesigner的相應類圖:

分析解釋和心得:
我的代碼復雜度為25較大,需要更進一步的提出更多的函式使代碼簡單,本題難度較大,需要一定的數學功底,對五邊形要有較多的認識,要將每個小方法都寫好如面積的求法,這可以讓代碼更清晰,
期中考試
7-1 點與線(類設計)
-
設計一個類表示平面直角坐標系上的點Point,私有屬性分別為橫坐標x與縱坐標y,資料型別均為實型數,除構造方法以及屬性的getter與setter方法外,定義一個用于顯示資訊的方法display(),用來輸出該坐標點的坐標資訊,格式如下:
(x,y),數值保留兩位小數,為簡化題目,其中,坐標點的取值范圍設定為(0,200],若輸入有誤,系統則直接輸出Wrong Format -
設計一個類表示平面直角坐標系上的線Line,私有屬性除了標識線段兩端的點point1、point2外,還有一個字串型別的color,用于表示該線段的顏色,同樣,除構造方法以及屬性的getter與setter方法外,定義一個用于計算該線段長度的方法getDistance(),還有一個用于顯示資訊的方法display(),用來輸出線段的相關資訊,輸出格式如下:
``` The line's color is:顏色值 The line's begin point's Coordinate is: (x1,y1) The line's end point's Coordinate is: (x2,y2) The line's length is:長度值 ```其中,所有數值均保留兩位小數,建議可用
String.format("%.2f", data)方法,設計類圖如下圖所示,

** 題目要求:在主方法中定義一條線段物件,從鍵盤輸入該線段的起點坐標與終點坐標以及顏色,然后呼叫該線段的display()方法進行輸出,**
- 以下情況為無效作業
- 無法運行
- 設計不符合所給類圖要求
- 未通過任何測驗點測驗
- 判定為抄襲
輸入格式:
分別輸入線段的起點橫坐標、縱坐標、終點的橫坐標、縱坐標以及顏色,中間可用一個或多個空格、tab或者回車分隔,
輸出格式:
The line's color is:顏色值 The line's begin point's Coordinate is: (x1,y1) The line's end point's Coordinate is: (x2,y2) The line's length is:長度值
我的代碼:
1 import java.text.Format; 2 import java.util.Scanner; 3 import java.lang.reflect.Constructor; 4 import static java.lang.System.exit; 5 6 public class Main 7 { 8 public static void main(String[] args) 9 { 10 Scanner input = new Scanner(System.in); 11 Point p1 = new Point(); 12 Point p2 = new Point(); 13 p1.setX(input.nextDouble()); 14 p1.setY(input.nextDouble()); 15 p2.setX(input.nextDouble()); 16 p2.setY(input.nextDouble()); 17 String c = input.next(); 18 Line l = new Line(p1,p2,c); 19 l.display(); 20 } 21 } 22 23 class Line 24 { 25 private Point point1; 26 private Point point2; 27 private String color; 28 29 public void Line() 30 { 31 32 } 33 public Line(Point p1, Point p2, String color) 34 { 35 this.color=color; 36 this.point1=p1; 37 this.point2=p2; 38 } 39 public void setColor(String color) { 40 this.color = color; 41 } 42 public String getColor() 43 { 44 return this.color; 45 } 46 47 public void setPoint1(Point point1) { 48 this.point1 = point1; 49 } 50 public Point getPoint1() 51 { 52 return this.point1; 53 } 54 public void setPoint2(Point point2) { 55 this.point2 = point2; 56 } 57 public Point getPoint2() 58 { 59 return this.point2; 60 } 61 62 double getdistance() 63 { 64 return Math.sqrt(Math.pow((this.point1.getx()-this.point2.getx()),2)+Math.pow((this.point1.gety()-this.point2.gety()),2)); 65 } 66 void display() 67 { 68 System.out.println("The line's color is:"+this.color); 69 System.out.println("The line's begin point's Coordinate is:"); 70 point1.display(); 71 System.out.println("The line's end point's Coordinate is:"); 72 point2.display(); 73 System.out.print("The line's length is:"); 74 System.out.printf("%.2f",this.getdistance()); 75 } 76 77 } 78 79 80 class Point 81 { 82 private double x; 83 private double y; 84 void Point() 85 { 86 } 87 public void setX(double x) { 88 if(x<=200&&x>0) 89 this.x = x; 90 else { 91 System.out.println("Wrong Format"); 92 exit(0); 93 } 94 } 95 96 public void setY(double y) { 97 if(y<=200 && y>0) 98 this.y = y; 99 else { 100 System.out.println("Wrong Format"); 101 exit(0); 102 } 103 } 104 public void Point(double x,double y) 105 { 106 this.x=x; 107 this.y=y; 108 } 109 public double getx() 110 { 111 return this.x; 112 } 113 public double gety() 114 { 115 return this.y; 116 } 117 void display() 118 { 119 System.out.printf("(%.2f,%.2f)\n",this.x,this.y); 120 } 121 }View Code
SourceMonitor的生成報表內容:


PowerDesigner的相應類圖:

分析解釋和心得:
這題按照題意寫,非常簡單,需要對類的理解,
7-2 點線面問題重構(繼承與多型)
在“點與線(類設計)”題目基礎上,對題目的類設計進行重構,以實作繼承與多型的技術性需求,
- 對題目中的點Point類和線Line類進行進一步抽象,定義一個兩個類的共同父類Element(抽象類),將display()方法在該方法中進行宣告(抽象方法),將Point類和Line類作為該類的子類,
- 再定義一個Element類的子類面Plane,該類只有一個私有屬性顏色color,除了構造方法和屬性的getter、setter方法外,display()方法用于輸出面的顏色,輸出格式如下:
The Plane's color is:顏色 - 在主方法內,定義兩個Point(線段的起點和終點)物件、一個Line物件和一個Plane物件,依次從鍵盤輸入兩個Point物件的起點、終點坐標和顏色值(Line物件和Plane物件顏色相同),然后定義一個Element類的參考,分別使用該參考呼叫以上四個物件的display()方法,從而實作多型特性,示例代碼如下:
類結構如下圖所示,element = p1;//起點Point element.display(); element = p2;//終點Point element.display(); element = line;//線段 element.display(); element = plane;//面 element.display();

其中,所有數值均保留兩位小數,建議可用String.format("%.2f", data)方法,
- 以下情況為無效作業
- 無法運行
- 設計不符合所給類圖要求
- 未通過任何測驗點測驗
- 判定為抄襲
輸入格式:
分別輸入線段的起點橫坐標、縱坐標、終點的橫坐標、縱坐標以及顏色,中間可用一個或多個空格、tab或者回車分隔,
輸出格式:
(x1,y1)
(x2,y2)
The line's color is:顏色值
The line's begin point's Coordinate is:
(x1,y1)
The line's end point's Coordinate is:
(x2,y2)
The line's length is:長度值
The Plane's color is:顏色值
我的代碼:
1 import java.lang.reflect.Constructor; 2 import static java.lang.System.exit; 3 import java.util.Scanner; 4 import java.text.Format; 5 6 class Point extends Element 7 { 8 private double x; 9 private double y; 10 void Point() 11 { 12 } 13 public void setX(double x) { 14 if(x<=200&&x>0) 15 this.x = x; 16 else { 17 System.out.println("Wrong Format"); 18 exit(0); 19 } 20 } 21 22 public void setY(double y) { 23 if(y<=200 && y>0) 24 this.y = y; 25 else { 26 System.out.println("Wrong Format"); 27 exit(0); 28 } 29 } 30 public void Point(double x,double y) 31 { 32 this.x=x; 33 this.y=y; 34 } 35 public double getx() 36 { 37 return this.x; 38 } 39 public double gety() 40 { 41 return this.y; 42 } 43 void display() 44 { 45 System.out.printf("(%.2f,%.2f)\n",this.x,this.y); 46 } 47 } 48 49 class Plane extends Element { 50 private String color; 51 52 53 void display() { 54 System.out.println("The Plane's color is:"+this.color); 55 } 56 57 public void setColor(String color) { 58 this.color = color; 59 } 60 61 public String getColor() { 62 return color; 63 } 64 public Plane() 65 { 66 67 } 68 public Plane(String color) 69 { 70 this.color=color; 71 } 72 } 73 74 class Main 75 { 76 public static void main(String[] args) { 77 Scanner input = new Scanner(System.in); 78 Point p1 = new Point(); 79 Point p2 = new Point(); 80 p1.setX(input.nextDouble()); 81 p1.setY(input.nextDouble()); 82 p2.setX(input.nextDouble()); 83 p2.setY(input.nextDouble()); 84 String c = input.next(); 85 Line line = new Line(p1,p2,c); 86 Plane plane = new Plane(); 87 plane.setColor(c); 88 89 Element element ; 90 91 element = p1;//起點Point 92 element.display(); 93 94 element = p2;//終點Point 95 element.display(); 96 97 element = line;//線段 98 element.display(); 99 100 element = plane;//面 101 element.display(); 102 103 } 104 } 105 106 107 class Line extends Element 108 { 109 private Point point1; 110 private Point point2; 111 private String color; 112 113 public void Line() 114 { 115 116 } 117 public Line(Point p1, Point p2, String color) 118 { 119 this.color=color; 120 this.point1=p1; 121 this.point2=p2; 122 } 123 public void setColor(String color) { 124 this.color = color; 125 } 126 public String getColor() 127 { 128 return this.color; 129 } 130 131 public void setPoint1(Point point1) { 132 this.point1 = point1; 133 } 134 public Point getPoint1() 135 { 136 return this.point1; 137 } 138 public void setPoint2(Point point2) { 139 this.point2 = point2; 140 } 141 public Point getPoint2() 142 { 143 return this.point2; 144 } 145 146 double getdistance() 147 { 148 return Math.sqrt(Math.pow((this.point1.getx()-this.point2.getx()),2)+Math.pow((this.point1.gety()-this.point2.gety()),2)); 149 } 150 void display() 151 { 152 System.out.println("The line's color is:"+this.color); 153 System.out.println("The line's begin point's Coordinate is:"); 154 point1.display(); 155 System.out.println("The line's end point's Coordinate is:"); 156 point2.display(); 157 System.out.print("The line's length is:"); 158 System.out.printf("%.2f\n",this.getdistance()); 159 } 160 161 } 162 163 abstract class Element 164 { 165 abstract void display(); 166 }View Code
SourceMonitor的生成報表內容:



PowerDesigner的相應類圖:

分析解釋和心得:
這題考察的是繼承與多型,題目比較基礎,比較簡單,按照題意來即可,需要對繼承與多型有一定理解,
7-3 點線面問題再重構(容器類)
在“點與線(繼承與多型)”題目基礎上,對題目的類設計進行重構,增加容器類保存點、線、面物件,并對該容器進行相應增、刪、遍歷操作,
- 在原有類設計的基礎上,增加一個GeometryObject容器類,其屬性為
ArrayList<Element>型別的物件(若不了解泛型,可以不使用<Element>) - 增加該類的
add()方法及remove(int index)方法,其功能分別為向容器中增加物件及洗掉第index - 1(ArrayList中index>=0)個物件 - 在主方法中,用戶回圈輸入要進行的操作(choice∈[0,4]),其含義如下:
- 1:向容器中增加Point物件
- 2:向容器中增加Line物件
- 3:向容器中增加Plane物件
- 4:洗掉容器中第index - 1個資料,若index資料非法,則無視此操作
- 0:輸入結束
輸入結束后,按容器中的物件順序分別呼叫每個物件的choice = input.nextInt(); while(choice != 0) { switch(choice) { case 1://insert Point object into list ... break; case 2://insert Line object into list ... break; case 3://insert Plane object into list ... break; case 4://delete index - 1 object from list int index = input.nextInt(); ... } choice = input.nextInt(); }display()方法進行輸出,
類圖如下所示:

- 以下情況為無效作業
- 無法運行
- 設計不符合所給類圖要求
- 未通過任何測驗點測驗
- 判定為抄襲
輸入格式:
switch(choice) {
case 1://insert Point object into list
輸入“點”物件的x,y值
break;
case 2://insert Line object into list
輸入“線”物件兩個端點的x,y值
break;
case 3://insert Plane object into list
輸入“面”物件的顏色值
break;
case 4://delete index - 1 object from list
輸入要洗掉的物件位置(從1開始)
...
}
輸出格式:
- Point、Line、Plane的輸出參考題目2
- 洗掉物件時,若輸入的index超出合法范圍,程式自動忽略該操作
我的代碼:
1 import static java.lang.System.exit; 2 import java.util.ArrayList; 3 import java.util.Scanner; 4 import java.util.ArrayList; 5 6 class Point extends Element 7 { 8 private double x; 9 private double y; 10 void Point() 11 { 12 } 13 public void setX(double x) { 14 if(x<=200&&x>0) 15 this.x = x; 16 else { 17 System.out.println("Wrong Format"); 18 exit(0); 19 } 20 } 21 22 public void setY(double y) { 23 if(y<=200 && y>0) 24 this.y = y; 25 else { 26 System.out.println("Wrong Format"); 27 exit(0); 28 } 29 } 30 public void Point(double x,double y) 31 { 32 this.x=x; 33 this.y=y; 34 } 35 public double getx() 36 { 37 return this.x; 38 } 39 public double gety() 40 { 41 return this.y; 42 } 43 void display() 44 { 45 System.out.printf("(%.2f,%.2f)\n",this.x,this.y); 46 } 47 } 48 49 class Plane extends Element { 50 private String color; 51 52 53 void display() { 54 System.out.println("The Plane's color is:"+this.color); 55 } 56 57 public void setColor(String color) { 58 this.color = color; 59 } 60 61 public String getColor() { 62 return color; 63 } 64 public Plane() 65 { 66 67 } 68 public Plane(String color) 69 { 70 this.color=color; 71 } 72 } 73 74 public class Main 75 { 76 public static void main(String[] args) { 77 Scanner input = new Scanner(System.in); 78 GeometryObject g =new GeometryObject(); 79 Point p1,p2; 80 String c; 81 int index; 82 int choice = input.nextInt(); 83 while(choice != 0) { 84 switch(choice) { 85 case 1://insert Point object into list 86 p1 = new Point(); 87 p1.setX(input.nextDouble()); 88 p1.setY(input.nextDouble()); 89 g.add(p1); 90 break; 91 case 2://insert Line object into list 92 p1 = new Point(); 93 p2 = new Point(); 94 p1.setX(input.nextDouble()); 95 p1.setY(input.nextDouble()); 96 p2.setX(input.nextDouble()); 97 p2.setY(input.nextDouble()); 98 c = input.next(); 99 Line line = new Line(p1,p2,c); 100 g.add(line); 101 break; 102 case 3://insert Plane object into list 103 c = input.next(); 104 Plane plane = new Plane(); 105 plane.setColor(c); 106 g.add(plane); 107 break; 108 case 4://delete index - 1 object from list 109 index = input.nextInt(); 110 if(index>=1 && index<=g.getsize()) 111 g.remove(index-1); 112 break; 113 } 114 choice = input.nextInt(); 115 } 116 ArrayList<Element> list1 = new ArrayList<>(); 117 list1 = g.getList(); 118 for(int i=0;i<list1.size();i++) 119 { 120 list1.get(i).display(); 121 } 122 123 /* Element element ; 124 125 element = p1;//起點Point 126 element.display(); 127 128 element = p2;//終點Point 129 element.display(); 130 131 element = line;//線段 132 element.display(); 133 134 element = plane;//面 135 element.display();*/ 136 137 } 138 } 139 140 class Line extends Element 141 { 142 private Point point1; 143 private Point point2; 144 private String color; 145 146 public void Line() 147 { 148 149 } 150 public Line(Point p1, Point p2, String color) 151 { 152 this.color=color; 153 this.point1=p1; 154 this.point2=p2; 155 } 156 public void setColor(String color) { 157 this.color = color; 158 } 159 public String getColor() 160 { 161 return this.color; 162 } 163 164 public void setPoint1(Point point1) { 165 this.point1 = point1; 166 } 167 public Point getPoint1() 168 { 169 return this.point1; 170 } 171 public void setPoint2(Point point2) { 172 this.point2 = point2; 173 } 174 public Point getPoint2() 175 { 176 return this.point2; 177 } 178 179 double getdistance() 180 { 181 return Math.sqrt(Math.pow((this.point1.getx()-this.point2.getx()),2)+Math.pow((this.point1.gety()-this.point2.gety()),2)); 182 } 183 void display() 184 { 185 System.out.println("The line's color is:"+this.color); 186 System.out.println("The line's begin point's Coordinate is:"); 187 point1.display(); 188 System.out.println("The line's end point's Coordinate is:"); 189 point2.display(); 190 System.out.print("The line's length is:"); 191 System.out.printf("%.2f\n",this.getdistance()); 192 } 193 194 } 195 196 197 class GeometryObject { 198 ArrayList<Element> list = new ArrayList<>(); 199 public void GeometryObject() 200 { 201 } 202 public void add(Element element) 203 { 204 this.list.add(element); 205 } 206 public void remove(int index) 207 { 208 this.list.remove(index); 209 } 210 211 public ArrayList<Element> getList() { 212 return this.list; 213 } 214 public int getsize() 215 { 216 return this.list.size(); 217 } 218 } 219 220 abstract class Element { 221 abstract void display(); 222 }View Code
SourceMonitor的生成報表內容:



PowerDesigner的相應類圖:

分析解釋和心得:
這題考察了容器類,需要掌握集合的使用并結合類的使用,題目比較基礎,
(3)采坑心得:
對原始碼的提交程序中出現的問題及心得進行總結,務必做到詳實,拿資料、原始碼及測驗結果說話,切忌假大空
7-1 sdut-String-2 識蛟龍號載人深潛,立科技報國志(II)(正則運算式)
注意Pattern和Matcher的正確使用方法
7-2 點線形系列4-凸四邊形的計算
注意相等不能==0

7-3 設計一個銀行業務類
注意轉化為整型
7-1 點線形系列5-凸五邊形的計算-1
需要掌握正則運算式

7-2 點線形系列5-凸五邊形的計算-2
相等不能寫==0

7-1 點與線(類設計)
要掌握類的使用


7-2 點線面問題重構(繼承與多型)
要掌握繼承和多型



7-3 點線面問題再重構(容器類)
要掌握集合的使用

(4)改進建議:
對相應題目的編碼改進給出自己的見解,做到可持續改進
7-1 sdut-String-2 識蛟龍號載人深潛,立科技報國志(II)(正則運算式)
使用Pattern和Matcher

7-2 點線形系列4-凸四邊形的計算 需要多寫方法使復雜度降低使代碼精簡, 寫一個將String坐標轉為double坐標的方法
7-3 設計一個銀行業務類 要按照題目要求把全部代碼寫完,不能面向結果編程,
7-1 點線形系列5-凸五邊形的計算-1
寫一個判斷字串點數量是否合格的方法

7-2 點線形系列5-凸五邊形的計算-2
需要多寫方法多寫類使復雜度降低使代碼精簡, 寫一個方法來判斷一個點是否在線上
期中考試
7-1 點與線(類設計)
%.2f 輸出正確格式
7-2 點線面問題重構(繼承與多型)
利用類里的方法來寫入資料
7-3 點線面問題再重構(容器類) 容器類的內部用集合和集合的方法來實作
(5)總結: 對本階段(6-9周)綜合性總結,學到了什么,哪些地方需要進一步學習及研究,對教師、課程、作業、實驗、課上及課下組織方式等方面的改進建議及意見, 本階段(6-9周)學到了類和物件的使用,學到了繼承和多型的使用,學到了抽象類和介面的使用,學到了用正則運算式來判斷字串格式,學到了如何封裝類的屬性,正則運算式和繼承和多型需要進一步學習及研究, 我對教師、課程、作業、實驗、課上及課下組織方式等方面都很滿意,建議老師多講講題,四邊形和五邊形的題目還是比較難,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/522898.html
標籤:其他
上一篇:淺談PHP設計模式的觀察者模式
下一篇:10道Python面試題
