目錄
- pta第二次博客
- 1.前言
- 2.設計與分析
- 第四次作業第一題
- 第四次作業第二題
- 第四次作業第三題
- 第五次作業第一題
- 第五次作業第二題
- 期中考試第一題
- 期中考試第二題
- 期中考試第三題
- 3.踩坑心得:
- 4.改進建議
- 5.總結
pta第二次博客
1.前言
2.設計與分析
第四次作業第一題
1.題目:
“蛟龍號”載人深潛器是我國首臺自主設計、自主集成研制的作業型深海載人潛水器,設計最大下潛深度為7000米級,也是目前世界上下潛能力最強的作業型載人潛水器,“蛟龍號”可在占世界海洋面積99.8%的廣闊海域中使用,對于我國開發利用深海的資源有著重要的意義,
中國是繼美、法、俄、日之后世界上第五個掌握大深度載人深潛技術的國家,在全球載人潛水器中,“蛟龍號”屬于第一梯隊,目前全世界投入使用的各類載人潛水器約90艘,其中下潛深度超過1000米的僅有12艘,更深的潛水器數量更少,目前擁有6000米以上深度載人潛水器的國家包括中國、美國、日本、法國和俄羅斯,除中國外,其他4國的作業型載人潛水器最大作業深度為日本深潛器的6527米,因此“蛟龍號”載人潛水器在西太平洋的馬里亞納海溝海試成功到達7020米海底,創造了作業類載人潛水器新的世界紀錄,
從2009年至2012年,蛟龍號接連取得1000米級、3000米級、5000米級和7000米級海試成功,下潛至7000米,說明蛟龍號載人潛水器集成技術的成熟,標志著我國深海潛水器成為海洋科學考察的前沿與制高點之一,
2012年6月27日11時47分,中國“蛟龍”再次重繪“中國深度”——下潛7062米,6月3日,“蛟龍”出征以來,已經連續書寫了5個“中國深度”新紀錄:6月15日,6671米;6月19日,6965米;6月22日,6963米;6月24日,7020米;6月27日,7062米,下潛至7000米,標志著我國具備了載人到達全球99%以上海洋深處進行作業的能力,標志著“蛟龍”載人潛水器集成技術的成熟,標志著我國深海潛水器成為海洋科學考察的前沿與制高點之一,標志著中國海底載人科學研究和資源勘探能力達到國際領先水平,
‘蛟龍’號是我國載人深潛發展歷程中的一個重要里程碑,它不只是一個深海裝備,更代表了一種精神,一種不畏艱險、趕超世界的精神,它是中華民族進軍深海的號角,
了解蛟龍號”載人深潛器“的驕人業績,為我國海底載人科學研究和資源勘探能力達到國際領先水平而自豪,小伙伴們與祖國同呼吸、共命運,一定要學好科學文化知識、提高個人能力,增強創新意識,做事精益求精,立科技報國之志!
請撰寫程式,實作如下功能:讀入關于蛟龍號載人潛水器探測資料的多行字串,從給定的資訊找出數字字符,輸出每行的數字之和,
提示 若輸入為“2012年2月”,則該行的輸出為:2014,若干個連續的數字字符作為一個整體,以十進制形式相加,
輸入格式:
讀入關于蛟龍號載人潛水器探測資料的多行字串,每行字符不超過80個字符,
以"end"結束,
輸出格式:
與輸入行相對應的各個整數之和,
試題分析
本題的難點在于如何如何將數字從段落中提取出來,只要對pattern和Matcher有所了解就能解決,
源代碼展示:
查看代碼
package pta41;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String S = in.nextLine();
int sum=0;
Pattern x=Pattern.compile("[0-9]+");
while(!S.equals("end"))
{
sum=0;
Matcher matcher=x.matcher(S);
while(matcher.find())
{
zd n=new zd();
sum+=n.zh(matcher.group());
}
System.out.println(sum);
S=in.nextLine();
}
}
}
class zd {
int str;
public int zh(String x)
{
str=Integer.valueOf(x);
return str;
}
}
SourceMonitor生成的報表內容:

類圖

代碼分析總結:
本次作業比較簡單,代碼也不多,不做分析,
第四次作業第二題
題目:
點線形系列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",
試題分析
本題主要考察大量的點與線的判斷以及點與面的判斷
源代碼展示:
查看代碼
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String S = in.nextLine();
if (S.charAt(1) != ':'||S.contains(", ")||S.charAt(S.length()-1)==',') {
System.out.println("Wrong Format");
return;
}
String[] str = S.split(":");
if ("1".equals(str[0])) {
point4 p1=new point4();
p1.quadrilateral(str[1],1);
System.out.println(p1.sbx +" "+p1.pxsbx);
}
if ("2".equals(str[0])) {
point4 p1=new point4();
p1.quadrilateral(str[1],1);
if(p1.sbx)
System.out.println(p1.lx +" "+p1.jx+" "+p1.zfx);
else
System.out.println("not a quadrilateral");
}
if ("3".equals(str[0])) {
point4 p1=new point4();
p1.quadrilateral(str[1],1);
if(p1.t|| p1.a)
p1.S=p1.s1+p1.s2;
else
p1.S= p1.s1-p1.s2;
String c1=String.format("%."+3+"f",p1.c);String s2=String.format("%."+3+"f", p1.S);
p1.c= Double.parseDouble(c1);p1.S= Double.parseDouble(s2);
if(p1.sbx)
System.out.println(p1.t+" "+p1.c+" "+p1.S);
else
System.out.println("not a quadrilateral");
}
if("4".equals(str[0]))
{
case4 p2=new case4();
p2.point6(str[1]);
System.out.print("p2.j");
}
if("5".equals(str[0]))
{
case5 p2=new case5();
p2.point5(str[1]);
}
}
}
class point4{
String[] l=new String[20];
String[] s=new String[20];
boolean sbx,pxsbx,jx,lx,zfx,t,a;
double x1, y1, x2, y2, x3, y3, x4, y4,k4,k5,k6,l4,l5,l6,c,S,s1,s2;
public void quadrilateral(String str,int b){
l=str.split(" ");
zz x=new zz();
samepoint p=new samepoint();
point3 triangle=new point3();
sbx=false;pxsbx=false;jx=false;lx=false;zfx=false;t=false;a=false;
int i,j;
for(i=0;i<l.length;i++)
{
s=l[i].split(",");
if (s.length != 2) {
System.out.println("Wrong Format");
System.exit(0);
}
for(j=0;j<s.length;j++)
x.z(s[j]);
}
if (l.length != 4) {
System.out.println("wrong number of points");
System.exit(0);
}
s = l[0].split(",");x1 = Double.valueOf(s[0]);y1 = Double.valueOf(s[1]);
s = l[1].split(",");x2 = Double.valueOf(s[0]);y2 = Double.valueOf(s[1]);
s = l[2].split(",");x3 = Double.valueOf(s[0]);y3 = Double.valueOf(s[1]);
s = l[3].split(",");x4 = Double.valueOf(s[0]);y4 = Double.valueOf(s[1]);
triangle.sjx(x1,y1,x2,y2,x3,y3);
s1= triangle.s;
k4=(y4-y1)/(x4-x1);k5=(y4-y2)/(x4-x2);k6=(y4-y3)/(x4-x3);
l4=Math.sqrt(Math.pow(x1-x4,2)+Math.pow(y1-y4,2));
l5=Math.sqrt(Math.pow(x4-x2,2)+Math.pow(y4-y2,2));
l6=Math.sqrt(Math.pow(x3-x4,2)+Math.pow(y3-y4,2));
if(b==1)
{
if(triangle.inline(x4,y4,x1,y1,x2,y2)||triangle.inline(x4,y4,x1,y1,x3,y3)||triangle.inline(x4,y4,x3,y3,x2,y2)||triangle.inline(x1,y1,x2,y2,x3,y3))
return;
if(p.point(x1,x2,y1,y2)||p.point(x1,x3,y1,y3)||p.point(x1,x4,y1,y4)||p.point(x3,x2,y3,y2)||p.point(x4,x2,y4,y2)||p.point(x3,x4,y3,y4))
{
System.out.println("points coincide");
System.exit(0);
}
if((!xj(x1,y1,x4,y4,x2,y2,x3,y3)&&!xj(x1,y1,x2,y2,x3,y3,x4,y4))||(!xj(x2,y2,x3,y3,x1,y1,x4,y4)&&!xj(x1,y1,x2,y2,x3,y3,x4,y4)))
{
sbx=true;t=true;
}
if((xj(x1,y1,x4,y4,x2,y2,x3,y3)&&xj(x1,y1,x2,y2,x3,y3,x4,y4))||(xj(x2,y2,x3,y3,x1,y1,x4,y4)&&xj(x3,y3,x4,y4,x1,y1,x2,y2))||(xj(x3,y3,x4,y4,x1,y1,x2,y2)&&xj(x1,y1,x3,y3,x2,y2,x4,y4)))
{
sbx=true;a=true;t=false;
}
if(xj(x2,y2,x3,y3,x1,y1,x4,y4)&&xj(x1,y1,x2,y2,x3,y3,x4,y4))
{
sbx=true;a=false;t=false;
}
if(((l5== triangle.l2&&l4==triangle.l3&&k4== triangle.k3)||(l4==triangle.l3&&l6==triangle.l1&&k4== triangle.k3)||(k5== triangle.k2&&l5== triangle.l2&&l6==triangle.l1))&&sbx)
pxsbx=true;
if(pxsbx&&triangle.zj)
jx=true;
if(triangle.l1==l4&&l4==l6&&l6==triangle.l3&&sbx)
lx=true;
if(jx&&lx)
zfx=true;
}
c= triangle.l1+ triangle.l3+l4+l6;
triangle.sjx(x1,y1,x3,y3,x4,y4);
s2= triangle.s;
S=s1+s2;
if(b==2)
{
if(x1==x3)
{
triangle.sjx(x2,y2,x3,y3,x4,y4);
s2= triangle.s;
S=s1+s2;
}
}
if(b==3)
{
if(pd(x1,y1,x2,y2,x3,y3,x4,y4))
{
if((!xj(x1,y1,x4,y4,x2,y2,x3,y3)&&!xj(x1,y1,x2,y2,x3,y3,x4,y4))||(!xj(x2,y2,x3,y3,x1,y1,x4,y4)&&!xj(x1,y1,x2,y2,x3,y3,x4,y4)))
{
sbx=true;t=true;
}
if((xj(x1,y1,x4,y4,x2,y2,x3,y3)&&xj(x1,y1,x2,y2,x3,y3,x4,y4))||(xj(x2,y2,x3,y3,x1,y1,x4,y4)&&xj(x3,y3,x4,y4,x1,y1,x2,y2))||(xj(x3,y3,x4,y4,x1,y1,x2,y2)&&xj(x1,y1,x3,y3,x2,y2,x4,y4)))
{
sbx=true;a=true;t=false;
}
if(xj(x2,y2,x3,y3,x1,y1,x4,y4)&&xj(x1,y1,x2,y2,x3,y3,x4,y4))
{
sbx=true;a=false;t=false;
}
if(t||a)
S=s1+s2;
else
S= s1-s2;
}
else {
if(triangle.inline2(x2,y2,x1,y1,x3,y3))
triangle.sjx(x1,y1,x3,y3,x4,y4);
if(triangle.inline2(x3,y3,x2,y2,x4,y4))
triangle.sjx(x1,y1,x2,y2,x4,y4);
if(triangle.inline2(x4,y4,x1,y1,x3,y3))
triangle.sjx(x1,y1,x2,y2,x3,y3);
}
}
}
public boolean xj(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)
{
double A1,B1,C1,A2,B2,C2,x,y;
A1=y1-y2;B1=x2-x1;C1=y2*x1-y1*x2;
A2=y3-y4;B2=x4-x3;C2=y4*x3-y3*x4;
if(A1*B2==A2*B1)
return false;
x=(B2*C1-B1*C2)/(B1*A2-B2*A1);
y=(A2*C1-A1*C2)/(A1*B2-A2*B1);
if((x>x1&&x<x2||x<x1&&x>x2)&&(y>x1&&y<y2||y>y2&&y<y1))
return true;
else
return false;
}
public void jd(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4,double x,double y) {
double A1, B1, C1, A2, B2, C2;
A1 = y1 - y2;B1 = x2 - x1;C1 = y2 * x1 - y1 * x2;
A2 = y3 - y4;B2 = x4 - x3;C2 = y4 * x3 - y3 * x4;
x = (B2 * C1 - B1 * C2) / (B1 * A2 - B2 * A1);
y = (A2 * C1 - A1 * C2) / (A1 * B2 - A2 * B1);
}
public boolean pd(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){
//判斷四邊形還是三角形
point3 s=new point3();
samepoint p=new samepoint();
if((x1==x2&&x2==x3&&y1==y2&&y2==y3)||(x1==x2&&x2==x4&&y1==y2&&y2==y4)||(x1==x3&&x3==x4&&y1==y3&&y3==y4)||(x2==x3&&x3==x4&&y2==y3&&y3==y4))
{
System.out.println("not a quadrilateral or triangle");
System.exit(0);
}
if((p.point(x1,x2,y1,y2)||p.point(x1,x3,y1,y3)||p.point(x3,x4,y3,y4)||s.inline(x3,y3,x1,y1,x2,y2))&&(s.pdsjx(x1,y1,x3,y3,x4,y4)||s.pdsjx(x1,y1,x2,y2,x4,y4)||s.pdsjx(x1,y1,x2,y2,x3,y3)))
return false;
else if((!xj(x1,y1,x4,y4,x2,y2,x3,y3)&&!xj(x1,y1,x2,y2,x3,y3,x4,y4))||(!xj(x2,y2,x3,y3,x1,y1,x4,y4)&&!xj(x1,y1,x2,y2,x3,y3,x4,y4)))
return true;
else{
System.out.println("not a quadrilateral or triangle");
System.exit(0);
return false;
}
}
}
class case4{
String[] l=new String[50];
String[] s=new String[50];
double x1, y1, x2, y2,S,s1,s2,x5,x6,x7,x8,y5,y6,y7,y8,j;
public void point6(String str)
{
j=0;
l=str.split(" ");
zz x=new zz();
samepoint p=new samepoint();
point3 t=new point3();
point4 q=new point4();
q.quadrilateral(str.substring(l[1].length()+1),3);
s=l[0].split(",");x1 = Double.valueOf(s[0]);y1 = Double.valueOf(s[1]);
s=l[1].split(",");x2 = Double.valueOf(s[0]);y2 = Double.valueOf(s[1]);
if(p.point(x1,x2,y1,y2)){
System.out.println("points coincide");
System.exit(0);
}
q.jd(x1,y1,x2,y2,q.x2,q.y2,q.x3,q.y3,x6,y6);
q.jd(x1,y1,x2,y2,q.x2,q.y2,q.x1,q.y1,x5,y5);
q.jd(x1,y1,x2,y2,q.x4,q.y4,q.x3,q.y3,x7,y7);
q.jd(x1,y1,x2,y2,q.x1,q.y1,q.x4,q.y4,x8,y8);
if(t.inline2(q.x1,q.y1,x1,y1,x2,y2)||t.inline2(q.x2,q.y2,x1,y1,x2,y2)||t.inline2(q.x3,q.y3,x1,y1,x2,y2)||t.inline2(q.x4,q.y4,x1,y1,x2,y2))
j+=1;
if(!q.sbx)
{
if(t.inline2(q.x1,q.y1,x1,y1,x2,y2)){
if(t.inline2(q.x2,q.y2,x1,y1,x2,y2)||t.inline2(q.x3,q.y3,x1,y1,x2,y2)){
System.out.println("The line is coincide with one of the lines");
System.exit(0);
}
S=t.s;
if(q.xj(x1,y1,x2,y2,q.x2,q.y2,q.x3,q.y3)){
j+=1;
t.sjx(x1,y1,x2,y2,x6,y6);
s1=t.s;
s2=S-t.s;
}
}
}
}
}
class case5{
String[] l=new String[50];
String[] s=new String[50];
double x1, y1;
public void point5(String str){
l=str.split(" ");
zz x=new zz();
samepoint p=new samepoint();
point3 t=new point3();
point4 q=new point4();
q.quadrilateral(str.substring(l[0].length()+1),2);
s=l[0].split(",");
x1 = Double.valueOf(s[0]);y1 = Double.valueOf(s[1]);
if(q.pd(q.x1,q.y1,q.x2,q.y2,q.x3,q.y3,q.x4,q.y4))
{
if(t.inline2(x1,y1,q.x1,q.y1,q.x2,q.y2)||t.inline2(x1,y1,q.x2,q.y2,q.x3,q.y3)||t.inline2(x1,y1,q.x3,q.y3,q.x4,q.y4)||t.inline2(x1,y1,q.x1,q.y1,q.x4,q.y4))
{
System.out.println("on the quadrilateral");
System.exit(0);
}
double S= q.S;
t.sjx(x1,y1,q.x1,q.y1,q.x2,q.y2);double s1=t.s;
t.sjx(x1,y1,q.x2,q.y2,q.x3,q.y3);double s2=t.s;
t.sjx(x1,y1,q.x3,q.y3,q.x4,q.y4);double s3=t.s;
t.sjx(x1,y1,q.x1,q.y1,q.x4,q.y4);double s4=t.s;
if(Math.abs(S-s1-s2-s3-s4)<0.001)
System.out.println("in the quadrilateral");
else
System.out.println("outof the quadrilateral");
}
else
{
if(t.inline2(x1,y1,q.x1,q.y1,q.x2,q.y2)||t.inline2(x1,y1,q.x2,q.y2,q.x3,q.y3)||t.inline2(x1,y1,q.x3,q.y3,q.x4,q.y4)||t.inline2(x1,y1,q.x1,q.y1,q.x4,q.y4))
{
System.out.println("on the triangle");
System.exit(0);
}
if(p.point(x1,q.x1,y1,q.y1)||p.point(x1,q.x2,y1,q.y2)||p.point(x1,q.x3,y1,q.y3)||p.point(x1,q.x4,y1,q.y4))
{
System.out.println("on the triangle");
System.exit(0);
}
double S= q.S;
t.sjx(x1,y1,q.x1,q.y1,q.x2,q.y2);double s1=t.s;
t.sjx(x1,y1,q.x2,q.y2,q.x3,q.y3);double s2=t.s;
t.sjx(x1,y1,q.x3,q.y3,q.x4,q.y4);double s3=t.s;
t.sjx(x1,y1,q.x1,q.y1,q.x4,q.y4);double s4=t.s;
if(Math.abs(S-s1-s2-s3-s4)<0.001)
System.out.println("in the triangle");
else
System.out.println("outof the triangle");
}
}
}
class samepoint{
public boolean point(double x1,double x2,double y1,double y2)
{
if(x1==x2&&y1==y2)
return true;
else
return false;
}
}
class zz {
public void z(String s){
if (!s.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)")||s.matches("[+-]?")||s.length()>6) {
System.out.println("Wrong Format");
System.exit(0);
}
}
}
class point3{
boolean zj;
double l1,l2,l3,k1,k2,k3,s,c;
public void sjx(double x1,double y1,double x2,double y2,double x3,double y3)
{
zj=false;
l1=Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2));
l2=Math.sqrt(Math.pow(x1-x3,2)+Math.pow(y1-y3,2));
l3=Math.sqrt(Math.pow(x3-x2,2)+Math.pow(y3-y2,2));
if(Math.abs(l1*l1+l3*l3-l2*l2)<0.000001)
zj=true;
k1=(y2-y1)/(x2-x1);
k2=(y3-y1)/(x3-x1);
k3=(y3-y2)/(x3-x2);
c=l1+l2+l3;
s=Math.sqrt((c/2)*(c/2-l1)*(c/2-l2)*(c/2-l3));
}
public boolean pdsjx(double x1,double y1,double x2,double y2,double x3,double y3)
{
if(inline1(x1,y1,x2,y2,x3,y3))
return false;
else
return true;
}
public boolean inline(double x1,double y1,double x2,double y2,double x3,double y3)
{
double l=((y2-y3)*x1-(x2-x3)*y1-y2*x3+y3*x2)/(Math.sqrt((y2-y3)*(y2-y3)+(x2-x3)*(x2-x3)));
if(Math.abs(l)<0.00001)
return true;
else
return false;
}
public boolean in(double x,double y,double x1,double y1,double x2,double y2)
{
if((x>x1&&x<x2||x<x1&&x>x2)&&(y>x1&&y<y2||y>y2&&y<y1))
return true;
else
return false;
}
public boolean inline1(double x1,double y1,double x2,double y2,double x3,double y3)
{
double l=((y2-y3)*x1-(x2-x3)*y1-y2*x3+y3*x2)/(Math.sqrt((y2-y3)*(y2-y3)+(x2-x3)*(x2-x3)));
if(Math.abs(l)<0.00001&&in(x1,y1,x2,y2,x3,y3))
return true;
else
return false;
}
public boolean inline2(double x1,double y1,double x2,double y2,double x3,double y3)
{
double l=((y2-y3)*x1-(x2-x3)*y1-y2*x3+y3*x2)/(Math.sqrt((y2-y3)*(y2-y3)+(x2-x3)*(x2-x3)));
if(Math.abs(l)<0.00001&&(in(x1,y1,x2,y2,x3,y3)||(x1==x2&&y1==y2)||x1==x3&&y1==y3))
return true;
else
return false;
}
}
SourceMonitor生成的報表內容:

類圖

代碼分析總結:
本次作業如果前面的三角形代碼復用性較高的話難度相對三角形提升不高,但我三角形的代碼復用性太低了,所以又需要從頭開始,但也是與三角形的代碼一樣沒有學會面向物件,導致代碼復用性不行,而且因為沒有面向物件導致沒有拿滿分,最后一個選項沒有實作,
第四次作業第三題
題目:
設計一個銀行業務類
撰寫一個銀行業務類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()方法,
輸入格式:
輸入開戶需要的姓名、密碼
輸入正確密碼、存款金額
輸入錯誤密碼、取款金額
輸入正確密碼、大于余額的取款金額
輸入正確密碼、小于余額的取款金額
輸出格式:
中國銀行(銀行名稱)歡迎您的到來!
您的余額有多少元,
您的密碼錯誤!
您的余額不足!
請取走鈔票,您的余額還有多少元,
請收好您的證件和物品,歡迎您下次光臨!
試題分析
本題主要考驗對私有屬性的使用,難度不高
源代碼展示:
查看代碼
import java.util.Scanner;
public class Main {
public static void welcome()
{
String s="中國銀行";
System.out.println(s+"歡迎您的到來!");
}
public static void welcomeNext()
{
System.out.println("請收好您的證件和物品,歡迎您下次光臨!");
}
public static void main(String[] args)
{
int i=3;
zz l=new zz();
welcome();
account people=new account();
Scanner in = new Scanner(System.in);
String s=in.nextLine();
l.z(s);
String n = l.S[0];
String p=l.S[1];
people.setName(n);
people.setPassword(p);
// System.out.println("注冊成功");
s=in.nextLine();
l.z(s);
p=l.S[0];
Double b=Double.valueOf(l.S[1]);
people.input(p,b);
while(i>0)
{
s=in.nextLine();
l.z(s);
p=l.S[0];
b=Double.valueOf(l.S[1]);
people.output(p,b);
i--;
}
welcomeNext();
}
}
class zz {
public String[] S= new String[1];
public void z(String s){
S=s.split(" ");
}
}
class account {
private String name;
private String password;
private double balance;
public void input(String p,double b)
{
if(!p.equals(getPassword()))
{
System.out.println("您的密碼錯誤!");
}
else
{
this.balance+=b;
System.out.println("您的余額有"+getBalance()+"元,");
}
}
public void output(String p,double b)
{
if(!p.equals(getPassword()))
{
System.out.println("您的密碼錯誤!");
}
else if(b>this.balance)
{
System.out.println("您的余額不足!");
}
else
{
this.balance-=b;
System.out.println("請取走鈔票,您的余額還有"+getBalance()+"元,");
}
}
public String getPassword() {
return password;
}
public double getBalance() {
return balance;
}
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
SourceMonitor生成的報表內容:

類圖

代碼分析總結:
本次作業因為題目要求寫的比較詳細,所以對類的定義和呼叫操作比較簡單,代碼的復用性較高,
第五次作業第一題
題目:
點線形系列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
試題分析
本次作業是上一次四邊形的延申,在四邊形的基礎上再加一條邊變成五邊形,但考核的仍然是基于三角形的點與線,點與面的操作,
源代碼展示:
查看代碼
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Case case1=new Case(),case2=new Case(),case3=new Case();
Scanner in = new Scanner(System.in);
String S = in.nextLine();
if(!S.matches("^[1-3]:((([+-]?((0|[1-9][0-9]*)(\\.[0-9]+)?)),([+-]?(0|[1-9][0-9]*)(\\.[0-9]+)?))\\s?)+")){
System.out.println("Wrong Format");
return;
}
String[] str = S.split(":");
if ("1".equals(str[0])) {
case1.is(str[1]);
}
if ("2".equals(str[0])) {
case2.dented(str[1]);
}
if ("3".equals(str[0])) {
case3.Cut(str[1]);
}
}
}
class Case{
public void is(String str){
point5 p5=new point5();
p5.pentagon(str);
System.out.println(p5.wbx);
}
public void dented(String str){
point5 p5=new point5();
point3 triangle=new point3();
p5.pentagon(str);
reduce r=new reduce();
if(!p5.wbx){
System.out.println("not a pentagon");
return;
}
triangle.sjx(p5.p[0],p5.p[1],p5.p[2]);
double s1= triangle.s;
triangle.sjx(p5.p[0],p5.p[2],p5.p[3]);
double s2= triangle.s;
triangle.sjx(p5.p[0],p5.p[3],p5.p[4]);
double s3= triangle.s;
p5.S= s1+s2+s3;
int i;
for(i=0;i<p5.p.length;i++){
int a=i+1;if(a>4) a=a-5;
int b=i+2;if(b>4) b=b-5;
int c=i+3;if(c>4) c=c-5;
int d=i+4;if(d>4) d=d-5;
triangle.sjx(p5.p[i],p5.p[a],p5.p[b]);
s1= triangle.s;
triangle.sjx(p5.p[i],p5.p[b],p5.p[c]);
s2= triangle.s;
triangle.sjx(p5.p[i],p5.p[c],p5.p[d]);
s3= triangle.s;
if(Math.abs(p5.S-s1-s2-s3)>0.0001){
System.out.println("false");
return;
}
}
p5.S=r.exchange(p5.S);p5.c=r.exchange(p5.c);
System.out.println("true"+" "+p5.c+" "+p5.S);
}
public void Cut(String str){
point7 p7=new point7();
p7.cut(str);
System.out.println(p7.jd+" "+p7.s1+" "+p7.s2);
}
}
class point7{
String[] input = new String[30];
String[] s = new String[30];
point1[] p=new point1[7];
point2 l=new point2();
double S,s1,s2;
int jd=0;
public void cut(String str){
int i, j;
point3 p3=new point3();
input = str.split(" ");
for (i = 0; i < input.length; i++)
s = input[i].split(",");
if (input.length !=7) {
System.out.println("wrong number of points");
System.exit(0);
}
for(i=0;i<input.length;i++){
p[i]=new point1();
p[i].point(input[i]);
}
l.get(p[0],p[1]);
ArrayList line1=new ArrayList();ArrayList line2=new ArrayList();ArrayList line3=new ArrayList();
line2.add(input[2]);
for(i=2;i<input.length;i++){
line1.add(input[i]);
}
for(i=1;i<line1.size();i++){
if(line1.get(i)!=line2.get(i-1<0? i+4:i-1)){
continue;
}
if(i==line1.size()-1){
line2.add(line1.get(i));
}
}
for(i=0;i<line2.size();i++){
point2 l1=new point2();
l1.get(p[(i-1<0? i+4:i-1)+2],p[(i+1>4? i-4:i+1)+2]);
if(p3.in(p[i+2],l1)){
line3.add(line2.get(i));
}
}
if(line3.size()<3){
System.out.println("not a polygon");
return;
}
if(line3.size()==3){
point3 triangle = new point3();
point1[] p=new point1[3];
for(i=0;i<line3.size();i++){
p[i].point(String.valueOf(line3.get(i)));
}
triangle.sjx(p[0],p[1],p[2]);
S=triangle.s;
}
if(line3.size()==4){
point4 p4=new point4();
point1[] p=new point1[4];
for(i=0;i<line3.size();i++){
p[i].point(String.valueOf(line3.get(i)));
}
p4.quadrilateral(p[0],p[1],p[2],p[3]);
S=p4.S;
}
if(line3.size()==5){
point5 p5=new point5();
String line=""+input[2]+input[3]+input[4]+input[5]+input[6];
p5.pentagon(line);
S=p5.S;
}
}
public void cut1(point2 l1,point3 triangle){
}
}
class point5{
String[] input = new String[30];
String[] s = new String[30];
point1[] p=new point1[5];
point2[] l=new point2[5];
boolean wbx=true;
double S,c;
public void pentagon(String str) {
input = str.split(" ");
point1 p1=new point1();//交點
point2 line =new point2();
point3 triangle = new point3();
point4 p4=new point4();
int i, j;
for (i = 0; i < input.length; i++)
s = input[i].split(",");
if (input.length != 5) {
System.out.println("wrong number of points");
System.exit(0);
}
for(i=0;i<input.length;i++){
p[i]=new point1();
p[i].point(input[i]);
}
for(i=0,j=0;i<l.length;i++,j++){
if(j==5) break;
int z=i+1;
if(z>4)z=z-5;
l[i]=new point2();
l[i].get(p[i],p[z]);
}
for(i=0;i<l.length;i++){
for(j=i+1;j<l.length;j++){
if (line.samepoint(p[i], p[j])) {
wbx=false;return;
}
}
}
for(i=0;i<5;i++) {
j = i + 1;
if (j > 4) j = j - 5;
int z;
for(z=0;z<3;z++){
if(triangle.in(p[i],l[j])){
wbx=false;return;
}
}
}
for(i=0;i<5;i++){
j=i+2;j=j%5;
int a=i+1;if(a>4) a=a-5;
int z;
if(p4.parallel(l[i],l[a])){
wbx=false;
return;
}
for(z=0;z<2;z++){
if(p4.xj(l[i],l[j])){
p4.jd(l[i],l[j],p1);
if(triangle.in(p1,l[i])||triangle.in(p1,l[j])){
wbx=false;
return;
}
}
j++;j=j%5;
}
}
for(i=0;i<l.length;i++){
c+=l[i].len;
}
}
}
class point4 {
double S;
point2 l1=new point2();point2 l2=new point2();point2 l3=new point2();point2 l4=new point2();
public void quadrilateral(point1 p1,point1 p2,point1 p3,point1 p4){
l1.get(p1,p2);l2.get(p2,p3);l3.get(p3,p4);l4.get(p4,p1);
point3 triangle1 = new point3();point3 triangle2 = new point3();
triangle1.sjx(p1,p2,p3);triangle2.sjx(p1,p3,p4);
S=triangle1.s+triangle2.s;
}
public boolean xj(point2 l1,point2 l2)
{
double x,y;
if(l1.A*l2.B==l2.A*l1.B)
return false;
x=(l2.B*l1.C-l1.B*l2.C)/(l1.B*l2.A-l2.B*l1.A);
y=(l2.A*l1.C-l1.A*l2.C)/(l1.A*l2.B-l2.A*l1.B);
if((x>l1.p1.x&&x<l1.p2.x||x<l1.p1.x&&x>l1.p2.x)&&(y>l1.p1.y&&y<l1.p2.y||y>l1.p2.y&&y<l1.p1.y))
return true;
else
return false;
}
public void jd(point2 l1,point2 l2,point1 p) {
p.x=(l2.B*l1.C-l1.B*l2.C)/(l1.B*l2.A-l2.B*l1.A);
p.y=(l2.A*l1.C-l1.A*l2.C)/(l1.A*l2.B-l2.A*l1.B);
}
public boolean parallel(point2 l1,point2 l2){//平行
if(l1.k==l2.k)
return true;
else
return false;
}
}
class point1{
double x,y;
public void point(String l){
String[] s=l.split(",");
x = Double.valueOf(s[0]);
y = Double.valueOf(s[1]);
}
}
class point2{
double len,k,A,B,C;
point1 p1,p2;
public boolean samepoint(point1 p1,point1 p2)
{
if(p1.x==p2.x&&p1.y==p2.y)
return true;
else
return false;
}
public void get(point1 p1,point1 p2){
this.p1=p1;this.p2=p2;
len=Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
k=(p2.y-p1.y)/(p2.x-p1.x);
A=p1.y-p2.y;B=p2.x-p1.x;C=p2.y*p1.x-p1.y*p2.x;
}
}
class point3{
point2 l1,l2,l3;
double s,c;
public void sjx(point1 p1,point1 p2,point1 p3)
{
l1=new point2();l2=new point2();l3=new point2();
l1.get(p1,p2);l2.get(p1,p3);l3.get(p2,p3);
c=l1.len+l2.len+l3.len;
s=Math.sqrt((c/2)*(c/2-l1.len)*(c/2-l2.len)*(c/2-l3.len));
}
public double ptol(point1 p1,point2 l2){//點到直線距離
double l=(l2.A*p1.x+l2.B*p1.y+l2.C)/(Math.sqrt(l2.A*l2.A+l2.B*l2.B));
return l;
}
public boolean in(point1 p1,point2 l2)
{
point2 l=new point2();
if(l.samepoint(p1,l2.p1)||l.samepoint(p1,l2.p2))
return true;
if((p1.x-l2.p1.x)*(p1.x-l2.p2.x)<=0&&(p1.y-l2.p1.y)*(p1.y-l2.p2.y)<=0)
return true;
else
return false;
}
}
class reduce{
public double exchange(double a){
String c=String.format("%."+3+"f",a);a= Double.parseDouble(c);
return a;
}
}
SourceMonitor生成的報表內容:

類圖

代碼分析總結:
吸取了前面兩次奪得教訓,這次的代碼我進行了重構,對點線面三個類繼續了詳細的分工,但因為在前一次作業上花費了太多時間,導致我沒有時間做完這題,
第五次作業第二題
題目:
點線形系列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
試題分析
本題是上一題的延申,在構建完五邊形后進行面與面,點與面的判斷,
源代碼展示:
查看代碼
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Case case1=new Case(),case2=new Case(),case3=new Case();
Scanner in = new Scanner(System.in);
String S = in.nextLine();
if(!S.matches("^[1-6]:((([+-]?((0|[1-9][0-9]*)(\\.[0-9]+)?)),([+-]?(0|[1-9][0-9]*)(\\.[0-9]+)?))\\s?)+")){
System.out.println("Wrong Format");
return;
}
String[] str = S.split(":");
if ("6".equals(str[0])) {
case3.Cut(str[1]);
}
}
}
class Case{
public void Cut(String str){
point6 p6=new point6();
p6.cut(str);
if(p6.line3.size()==3)
p6.cut1(p6.p[0],p6.triangle);
}
}
class point6{
String[] input = new String[30];
String[] s = new String[30];
point1[] p=new point1[7];
point3 triangle = new point3();
point4 p4=new point4();
point5 p5=new point5();
double S;
ArrayList line1=new ArrayList();ArrayList line2=new ArrayList();ArrayList line3=new ArrayList();
public void cut(String str){
int i,j;
point3 p3=new point3();
input = str.split(" ");
for (i = 0; i < input.length; i++)
s = input[i].split(",");
if (input.length !=6) {
System.out.println("wrong number of points");
System.exit(0);
}
for(i=0;i<input.length;i++){
p[i]=new point1();
p[i].point(input[i]);
}
line2.add(input[2]);
for(i=2;i<input.length;i++){
line1.add(input[i]);
}
for(i=1;i<line1.size();i++){
boolean same=false;
for(j=0;j<line2.size();j++){
if(line1.get(i)==line2.get(j))
same=true;
}
if(same==false)
line2.add(line1.get(i));
}
for(i=0;i<line2.size();i++){
point2 l1=new point2();
l1.get(p[(i-1<0? i+line2.size():i-1)+1],p[(i>line2.size()-1? i-line2.size()-1:i+1)+1]);
if(!p3.in(p[i+1],l1)){
line3.add(line2.get(i));
}
}
if(line3.size()<3){
System.out.println("not a polygon");
return;
}
if(line3.size()==3){
point1[] p=new point1[3];
for(i=0;i<line3.size();i++){
p[i]=new point1();
p[i].point(line3.get(i).toString());
}
triangle.sjx(p[0],p[1],p[2]);
S=triangle.s;
}
if(line3.size()==4){
point1[] p=new point1[4];
for(i=0;i<line3.size();i++){
p[i].point(String.valueOf(line3.get(i)));
}
p4.quadrilateral(p[0],p[1],p[2],p[3]);
S=p4.S;
}
if(line3.size()==5){
String line=""+input[1]+input[2]+input[3]+input[4]+input[5];
p5.pentagon(line);
S=p5.S;
}
}
public void cut1(point1 p1, point3 triangle){
point3 triangle1=new point3();point3 triangle2=new point3();point3 triangle3=new point3();
point3 p3=new point3();
point2 l=new point2();
triangle1.sjx(p1,triangle.p1,triangle.p2);triangle2.sjx(p1,triangle.p2,triangle.p3);triangle3.sjx(p1, triangle.p1, triangle.p3);
if(l.samepoint(p1,triangle.p1)||l.samepoint(p1,triangle.p2)||l.samepoint(p1,triangle.p3)){
System.out.println("on the triangle");
System.exit(0);
}
if(p3.in(p1,triangle.l1)||p3.in(p1,triangle.l2)||p3.in(p1,triangle.l3)){
System.out.println("on the triangle");
System.exit(0);
}
if(Math.abs(S-triangle1.s-triangle2.s-triangle3.s)<0.001){
System.out.println("in the triangle");
}
else {
System.out.println("outof the triangle");
}
}
}
class point5{
String[] input = new String[30];
String[] s = new String[30];
point1[] p=new point1[5];
point2[] l=new point2[5];
boolean wbx=true;
double S,c;
public void pentagon(String str) {
input = str.split(" ");
point1 p1=new point1();//交點
point2 line =new point2();
point3 triangle = new point3();
point4 p4=new point4();
int i, j;
for (i = 0; i < input.length; i++)
s = input[i].split(",");
if (input.length != 5) {
System.out.println("wrong number of points");
System.exit(0);
}
for(i=0;i<input.length;i++){
p[i]=new point1();
p[i].point(input[i]);
}
for(i=0,j=0;i<l.length;i++,j++){
if(j==5) break;
int z=i+1;
if(z>4)z=z-5;
l[i]=new point2();
l[i].get(p[i],p[z]);
}
for(i=0;i<l.length;i++){
for(j=i+1;j<l.length;j++){
if (line.samepoint(p[i], p[j])) {
wbx=false;return;
}
}
}
for(i=0;i<5;i++) {
j = i + 1;
if (j > 4) j = j - 5;
int z;
for(z=0;z<3;z++){
if(triangle.in(p[i],l[j])){
wbx=false;return;
}
}
}
for(i=0;i<5;i++){
j=i+2;j=j%5;
int a=i+1;if(a>4) a=a-5;
int z;
if(p4.parallel(l[i],l[a])){
wbx=false;
return;
}
for(z=0;z<2;z++){
if(p4.xj(l[i],l[j])){
p4.jd(l[i],l[j],p1);
if(triangle.in(p1,l[i])||triangle.in(p1,l[j])){
wbx=false;
return;
}
}
j++;j=j%5;
}
}
for(i=0;i<l.length;i++){
c+=l[i].len;
}
}
}
class point4 {
double S;
point1 p1=new point1(),p2=new point1(),p3=new point1(),p4=new point1();
point2 l1=new point2();point2 l2=new point2();point2 l3=new point2();point2 l4=new point2();
public void quadrilateral(point1 p1,point1 p2,point1 p3,point1 p4){
this.p1=p1;this.p2=p2;this.p3=p3;this.p4=p4;
l1.get(p1,p2);l2.get(p2,p3);l3.get(p3,p4);l4.get(p4,p1);
point3 triangle1 = new point3();point3 triangle2 = new point3();
triangle1.sjx(p1,p2,p3);triangle2.sjx(p1,p3,p4);
S=triangle1.s+triangle2.s;
}
public boolean xj(point2 l1,point2 l2)
{
double x,y;
if(l1.A*l2.B==l2.A*l1.B)
return false;
x=(l2.B*l1.C-l1.B*l2.C)/(l1.B*l2.A-l2.B*l1.A);
y=(l2.A*l1.C-l1.A*l2.C)/(l1.A*l2.B-l2.A*l1.B);
if((x>l1.p1.x&&x<l1.p2.x||x<l1.p1.x&&x>l1.p2.x)&&(y>l1.p1.y&&y<l1.p2.y||y>l1.p2.y&&y<l1.p1.y))
return true;
else
return false;
}
public void jd(point2 l1,point2 l2,point1 p) {
p.x=(l2.B*l1.C-l1.B*l2.C)/(l1.B*l2.A-l2.B*l1.A);
p.y=(l2.A*l1.C-l1.A*l2.C)/(l1.A*l2.B-l2.A*l1.B);
}
public boolean parallel(point2 l1,point2 l2){//平行
if(l1.k==l2.k)
return true;
else
return false;
}
}
class point1{
double x,y;
public void point(String l){
String[] s=l.split(",");
x = Double.valueOf(s[0]);
y = Double.valueOf(s[1]);
}
}
class point2{
double len,k,A,B,C;
point1 p1,p2;
public boolean samepoint(point1 p1,point1 p2)
{
if(p1.x==p2.x&&p1.y==p2.y)
return true;
else
return false;
}
public void get(point1 p1,point1 p2){
this.p1=p1;this.p2=p2;
len=Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
k=(p2.y-p1.y)/(p2.x-p1.x);
A=p1.y-p2.y;B=p2.x-p1.x;C=p2.y*p1.x-p1.y*p2.x;
}
}
class point3{
point1 p1=new point1(),p2=new point1(),p3=new point1();
point2 l1,l2,l3;
double s,c;
public void sjx(point1 p1,point1 p2,point1 p3)
{
this.p1=p1;this.p2=p2;this.p3=p3;
l1=new point2();l2=new point2();l3=new point2();
l1.get(p1,p2);l2.get(p1,p3);l3.get(p2,p3);
c=l1.len+l2.len+l3.len;
s=Math.sqrt((c/2)*(c/2-l1.len)*(c/2-l2.len)*(c/2-l3.len));
}
public double ptol(point1 p1,point2 l2){//點到直線距離
double l=(l2.A*p1.x+l2.B*p1.y+l2.C)/(Math.sqrt(l2.A*l2.A+l2.B*l2.B));
return l;
}
public boolean in(point1 p1,point2 l2)
{
point2 l=new point2();
if(l.samepoint(p1,l2.p1)||l.samepoint(p1,l2.p2))
return true;
if((p1.x-l2.p1.x)*(p1.x-l2.p2.x)<=0&&(p1.y-l2.p1.y)*(p1.y-l2.p2.y)<=0)
return true;
else
return false;
}
}
class reduce{
public double exchange(double a){
String c=String.format("%."+3+"f",a);a= Double.parseDouble(c);
return a;
}
}
SourceMonitor生成的報表內容:

類圖

代碼分析總結:
因為時間不夠,所以我只簡略寫了一下選項6,上一題重構代碼后我代碼的復用性明顯提高了不少,
期中考試第一題
題目:
點與線(類設計)
設計一個類表示平面直角坐標系上的點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:長度值
試題分析
本題因為給出了類圖,所以只要對著類圖去定義類和函式就行
源代碼展示:
查看代碼
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double x1 ,x2,y1,y2;
try{
x1 = input.nextDouble(); y1= input.nextDouble();
x2= input.nextDouble();y2= input.nextDouble();
}
catch(Exception InInitializerError){
System.out.println("Wrong Format");return;
}
if(x1<0||x1>200||y1<0||y1>200||x2<0||x2>200||y2<0||y2>200){
System.out.println("Wrong Format");return;
}
String color= input.next();
Point1 point1=new Point1(x1,y1);
Point1 point2=new Point1(x2,y2);
Line line=new Line(point1,point2,color);
line.display();
}
}
class Point1{
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;
}
private double x;
private double y;
Point1(double x,double y){
this.x=x;this.y=y;
}
public void display(){
System.out.println("("+String.format("%.2f", this.getX())+","+String.format("%.2f", this.getY())+")");
}
}
class Line{
public Point1 getPoint1() {
return point1;
}
public void setPoint1(Point1 point1) {
this.point1 = point1;
}
public Point1 getPoint2() {
return point2;
}
public void setPoint2(Point1 point2) {
this.point2 = point2;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
private Point1 point1;
private Point1 point2;
private String color;
Line(Point1 point1,Point1 point2,String color){
this.point1=point1;this.point2=point2;this.color=color;
}
public void display(){
System.out.println("The line's color is:" +this.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:");
System.out.println(String.format("%.2f", getDistance()));
}
public double getDistance(){
double len=Math.sqrt(Math.pow(this.point2.getX()-this.point1.getX(),2)+Math.pow(this.point2.getY()-this.point1.getY(),2));
return len;
}
}
SourceMonitor生成的報表內容:
(因為我在電腦上寫代碼時是在上一題的代碼中修改的,所以電腦上只保存了第三題的代碼檔案故三題的報表內容會一致)

類圖
略
代碼分析總結:
因為給出了類圖,所以難度不高,代碼的復用性也較高,復雜性也較低,
期中考試第二題
題目:
點線面問題重構(繼承與多型)
在“點與線(類設計)”題目基礎上,對題目的類設計進行重構,以實作繼承與多型的技術性需求,
對題目中的點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:顏色值
試題分析
本題只是在上一題的基礎上增加了一個父類,按照給的類圖寫就行
源代碼展示:
查看代碼
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double x1 ,x2,y1,y2;
try{
x1 = input.nextDouble(); y1= input.nextDouble();
x2= input.nextDouble();y2= input.nextDouble();
}
catch(Exception InInitializerError){
System.out.println("Wrong Format");return;
}
if(x1<0||x1>200||y1<0||y1>200||x2<0||x2>200||y2<0||y2>200){
System.out.println("Wrong Format");return;
}
String color= input.next();
Point1 point1=new Point1(x1,y1);
Point1 point2=new Point1(x2,y2);
Line line=new Line(point1,point2,color);
Plane plane=new Plane(color);
Element element;
element = point1;//起點Point
element.display();
element = point2;//終點Point
element.display();
element = line;//線段
element.display();
element = plane;//面
element.display();
}
}
class Point1 extends Element{
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;
}
private double x;
private double y;
Point1(double x,double y){
this.x=x;this.y=y;
}
void display(){
System.out.println("("+String.format("%.2f", this.getX())+","+String.format("%.2f", this.getY())+")");
}
}
class Line extends Element{
public Point1 getPoint1() {
return point1;
}
public void setPoint1(Point1 point1) {
this.point1 = point1;
}
public Point1 getPoint2() {
return point2;
}
public void setPoint2(Point1 point2) {
this.point2 = point2;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
private Point1 point1;
private Point1 point2;
private String color;
Line(Point1 point1,Point1 point2,String color){
this.point1=point1;this.point2=point2;this.color=color;
}
public double getDistance(){
double len=Math.sqrt(Math.pow(this.point2.getX()-this.point1.getX(),2)+Math.pow(this.point2.getY()-this.point1.getY(),2));
return len;
}
@Override
void display() {
System.out.println("The line's color is:" +this.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:");
System.out.println(String.format("%.2f", getDistance()));
}
}
abstract class Element{
abstract void display();
}
class Plane extends Element{
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
private String color;
Plane(String color){
this.color=color;
}
@Override
void display() {
System.out.println("The Plane's color is:"+this.getColor());
}
}
SourceMonitor生成的報表內容:

類圖
略
代碼分析總結:
本題只是在上一題的基礎上增加一個面類以及增加一個父類,讓點線面類繼承父類,對上一題稍作修改就行,
期中考試第三題
題目:
點線面問題再重構(容器類)
在“點與線(繼承與多型)”題目基礎上,對題目的類設計進行重構,增加容器類保存點、線、面物件,并對該容器進行相應增、刪、遍歷操作,
在原有類設計的基礎上,增加一個GeometryObject容器類,其屬性為ArrayList
增加該類的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超出合法范圍,程式自動忽略該操作
試題分析
本題引入了ArrayList,還增加了洗掉和增加的操作要求,
源代碼展示:
查看代碼
package 期中考試;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double x1 ,x2,y1,y2;
String color;
GeometryObject g=new GeometryObject();
try{
x1 = input.nextDouble(); y1= input.nextDouble();
x2= input.nextDouble();y2= input.nextDouble();
}
catch(Exception InInitializerError){
System.out.println("Wrong Format");return;
}
if(x1<0||x1>200||y1<0||y1>200||x2<0||x2>200||y2<0||y2>200){
System.out.println("Wrong Format");return;
}
int choice = input.nextInt();
while(choice != 0) {
switch(choice) {
case 1://insert Point object into list
try{
x1 = input.nextDouble(); y1= input.nextDouble();
}
catch(Exception InInitializerError){
System.out.println("Wrong Format");return;
}
if(x1<0||x1>200||y1<0||y1>200){
System.out.println("Wrong Format");return;
}
Element point=new Point1(x1,y1);
g.add(point);
break;
case 2://insert Line object into list
try{
x1 = input.nextDouble(); y1= input.nextDouble();
x2= input.nextDouble();y2= input.nextDouble();
}
catch(Exception InInitializerError){
System.out.println("Wrong Format");return;
}
if(x1<0||x1>200||y1<0||y1>200||x2<0||x2>200||y2<0||y2>200){
System.out.println("Wrong Format");return;
}
color= input.next();
Point1 point1=new Point1(x1,y1);
Point1 point2=new Point1(x2,y2);
Element line=new Line(point1,point2,color);
g.add(line);
break;
case 3://insert Plane object into list
color= input.next();
Element plane=new Plane(color);
g.add(plane);
break;
case 4://delete index - 1 object from list
int index = input.nextInt();
g.remove(index);
}
choice = input.nextInt();
}
for(Element element:g.list){
element.display();
}
}
}
class Point1 extends Element{
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;
}
private double x;
private double y;
Point1(double x,double y){
this.x=x;this.y=y;
}
void display(){
System.out.println("("+String.format("%.2f", this.getX())+","+String.format("%.2f", this.getY())+")");
}
}
class Line extends Element{
public Point1 getPoint1() {
return point1;
}
public void setPoint1(Point1 point1) {
this.point1 = point1;
}
public Point1 getPoint2() {
return point2;
}
public void setPoint2(Point1 point2) {
this.point2 = point2;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
private Point1 point1;
private Point1 point2;
private String color;
Line(Point1 point1,Point1 point2,String color){
this.point1=point1;this.point2=point2;this.color=color;
}
public double getDistance(){
double len=Math.sqrt(Math.pow(this.point2.getX()-this.point1.getX(),2)+Math.pow(this.point2.getY()-this.point1.getY(),2));
return len;
}
@Override
void display() {
System.out.println("The line's color is:" +this.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:");
System.out.println(String.format("%.2f", getDistance()));
}
}
abstract class Element{
abstract void display();
}
class Plane extends Element{
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
private String color;
Plane(String color){
this.color=color;
}
@Override
void display() {
System.out.println("The Plane's color is:"+this.getColor());
}
}
class GeometryObject{
GeometryObject(){
}
ArrayList<Element> list=new ArrayList<Element>();
public void add(Element element){
list.add(element);
}
public void remove(int index){
list.remove(index-1);
}
public ArrayList<Element> getList() {
return list;
}
}
SourceMonitor生成的報表內容:

類圖
略
代碼分析總結:
本題是對上一題按照給予的類圖進行修改,但因為時間有限,在考試時沒能寫完,在考完回寢室之后才寫完代碼,
3.踩坑心得:
在寫期中考試的題目時忘記用try來判斷錯誤輸入了,導致卡了較久,

4.改進建議
在寫題時要先對題目總體進行分析,先畫出類圖的大致框架再去碼代碼,這樣寫題速度會提升較多,
5.總結
通過這幾次作業,我對類的分工越來越熟練,提升也比較明顯,也認識到了細分類的重要性,代碼的復雜度在不斷降低,復用性在不斷上升,
建議:今后也要重視問題分析,仔細分類,也多去了解多型的使用,
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/523135.html
標籤:其他
上一篇:斯威夫特:(如何)我可以在一個視圖中使用兩個不同的資料模型結構嗎?
下一篇:pta第二次博客
