PTA题目集,超星作业及期中考试的总结
前言
这几次PTA作业,超星学习通作业,以及期中考试,考查的知识点有:字符串的输出和输入,强制转化变量的类型,split函数的应用,逻辑思维能力,全面考虑问题的能力,方法编写能力,正则表达式练习。其中,主要题目是图形界面类设计编写,银行业务类设计编写。这些作业题量适中,但难度不低,要消耗的时间会比较长一些。全部尽量写到最好要花费比较多的时间。其中PTA的题目比较难,题目量也比较多,电信计费系列的题目,重点是考查了正则表达式的正确使用,对类图的分析能力。总的来说考察了我们的细心以及对Java相关知识的掌握,有助于巩固我们的学习成果,提高我们的逻辑思维能力以及考虑问题的能力,锻炼我们的整体能力。期中考试主要考察了PTA图形界面类设计编写,共有三道题目,题量一般。与平时题目不同的是,每题均给出了相应的类图,要求考生按照类图来书写自己的代码。且三题呈递进关系,每题均是由上一题进阶而来。第一题是点与线,要求输出点和线的属性,考察了学生对Java类的设计的掌握。而第二题则是在第一题的基础上加上了关于面的类,并要求把点、线、面三类均作为新建类Element类的子类。考察了学生对继承与多态的掌握。第三题考察了Java容器类,主要是ArrayList的应用。且相比前两题有了多种输入格式,增加了难度。
设计与分析
题目集4 7-3:用户输入一组选项和数据,进行与三角形有关的计算。
选项包括:
1:输入三个点坐标,判断是否是等腰三角形、等边三角形,判断结果输出true/false,两个结果之间以一个英文空格符分隔。
2:输入三个点坐标,输出周长、面积、重心坐标,三个参数之间以一个英文空格分隔,坐标之间以英文","分隔。
3:输入三个点坐标,输出是钝角、直角还是锐角三角形,依次输出三个判断结果(true/false),以一个英文空格分隔,
4:输入五个点坐标,输出前两个点所在的直线与三个点所构成的三角形相交的交点数量,如果交点有两个,则按面积大小依次输出三角形被直线分割成两部分的面积。若直线与三角形一条线重合,输出"The point is on the edge of the triangle"
5:输入四个点坐标,输出第一个是否在后三个点所构成的三角形的内部(输出in the triangle/outof triangle)。
必须使用射线法,原理:由第一个点往任一方向做一射线,射线与三角形的边的交点(不含点本身)数量如果为1,则在三角形内部。如果交点有两个或0个,则在三角形之外。若点在三角形的某条边上,输出"on the triangle"
import java.util.Scanner; import java.text.DecimalFormat; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); double m=0,x1=0,x2=0,y1=0,y2=0,x3=0,x4=0,y3=0,y4=0,x5=0,y5=0,t=0,sum=0; String a = in.nextLine(); int choice=Integer.parseInt(a.substring(0,1)); for(int i=0;i<a.length()-1;i++) { if(a.charAt(i)==' ') { sum++; } } if(choice>5||choice<1) { System.out.println("Wrong Format"); System.exit(0); } else if(sum==0){ System.out.println("Wrong Format"); System.exit(0); } for(int j=0;j<a.length()-1;j++){ if((a.charAt(j)=='+'&&a.charAt(j+1)=='+')||(a.charAt(j)=='-'&&a.charAt(j+1)=='-')||(a.charAt(j)=='+'&&a.charAt(j+1)=='-')||(a.charAt(j)=='-'&&a.charAt(j+1)=='+')||(a.charAt(j)=='.'&&a.charAt(j+1)==',')||(a.charAt(j)==','&&a.charAt(j+1)=='.')|| (a.charAt(j)=='0'&&a.charAt(j+1)=='0')||(a.charAt(j)=='.'&&a.charAt(j+1)==' ')||(a.charAt(j)==' '&&a.charAt(j+1)=='.')||(a.charAt(j)=='.'&&a.charAt(j+1)=='.')||(a.charAt(j)==' '&&a.charAt(j+1)==',')|| (a.charAt(j)==','&&a.charAt(j+1)==' ')||(a.charAt(j)==' '&&a.charAt(j+1)==' ')||(a.charAt(j)==','&&a.charAt(j+1)==',')||a.charAt(0)=='.'||a.charAt(0)==','||sum==0){ System.out.println("Wrong Format"); System.exit(0); } else if(a.charAt(j)==':'&&a.charAt(j+1)==' '){ System.out.println("Wrong Format"); System.exit(0); } else if((a.charAt(2)==0)&&(a.charAt(3)!=','||a.charAt(3)!='.')){ System.out.println("Wrong Format"); System.exit(0); } else if(a.charAt(a.length()-1)==','){ System.out.println("Wrong Format"); System.exit(0); } } if(choice==1) { String[] c = a.split(" "); String[] d = c[0].split(","); String[] f = c[1].split(","); String[] e = c[2].split(","); x1=Double.parseDouble(d[0].substring(2, d[0].length())); y1=Double.parseDouble(d[1]); x2=Double.parseDouble(f[0]); y2=Double.parseDouble(f[1]); x3=Double.parseDouble(e[0]); y3=Double.parseDouble(e[1]); double fi=(double)(Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1))); double se=(double)(Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))); double th=(double)(Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2))); if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x2==x3&&y2==y3)) { System.out.println("data error"); } else if(sum==0) { System.out.println("Wrong Format"); } else if(sum!=2) { System.out.println("wrong number of points"); } if(fi==se||fi==th||se==th){ System.out.print("true"); } else { System.out.print("false"); } if(fi==se&&fi==th) { System.out.print(" "+"true"); } else { System.out.print(" "+"false"); } } if(choice==2) { String[] c = a.split(" "); String[] d = c[0].split(","); String[] f = c[1].split(","); String[] e = c[2].split(","); x1=Double.parseDouble(d[0].substring(2, d[0].length())); y1=Double.parseDouble(d[1]); x2=Double.parseDouble(f[0]); y2=Double.parseDouble(f[1]); x3=Double.parseDouble(e[0]); y3=Double.parseDouble(e[1]); double fi=Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1)); double se=Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); double th=Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)); double p=(fi+se+th)/2; double l=fi+se+th; //double s=(1/4*Math.sqrt((fi+se+th)*(fi+se-th)*(se+th-fi))); double s=Math.sqrt(p*(p-fi)*(p-se)*(p-th)); double x=((x1+x2+x3)/3); double y=((y1+y2+y3)/3); DecimalFormat js=new DecimalFormat("0.0#####"); new DecimalFormat("0.000000").format(l); new DecimalFormat("0.000000").format(s); new DecimalFormat("0.000000").format(x); new DecimalFormat("0.000000").format(y); //DecimalFormat df1 = new DecimalFormat("0.000000");//不够6位补零输出 //DecimalFormat df2 = new DecimalFormat("0.######");//不够6位以实际位数输出 //new DecimalFormat(“00.00”).format(43.146); //结果:43.15 if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x2==x3&&y2==y3)) { System.out.println("data error"); } else if(sum==0) { System.out.println("Wrong Format"); } else if(sum!=2) { System.out.println("wrong number of points"); System.exit(0); } else { System.out.print(js.format(l)+" "+js.format(s)+" "+js.format(x)+","+js.format(y)); } } if(choice==3) { String[] c = a.split(" "); String[] d = c[0].split(","); String[] f = c[1].split(","); String[] e = c[2].split(","); x1=Double.parseDouble(d[0].substring(2, d[0].length())); y1=Double.parseDouble(d[1]); x2=Double.parseDouble(f[0]); y2=Double.parseDouble(f[1]); x3=Double.parseDouble(e[0]); y3=Double.parseDouble(e[1]); double max,le,ss; double fi=(double)(Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1))); double se=(double)(Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))); double th=(double)(Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2))); if(sum==0) { System.out.println("Wrong Format"); } else if(sum!=2) { System.out.println("wrong number of points"); System.exit(0); } else if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x2==x3&&y2==y3)) { System.out.println("data error"); System.exit(0); } else if((x1==x2&&y1!=y2)&&(x1==x3&&y1!=y3)&&(x1==x3&&y2!=y3)) { System.out.println("data error"); System.exit(0); } else if((x1!=x2&&y1==y2)&&(x1!=x3&&y1==y3)&&(x1!=x3&&y2==y3)) { System.out.println("data error"); System.exit(0); } else if((y2-y1)/(x2-x1)==(y3-y1)/(x3-x1)) { System.out.println("data error"); System.exit(0); } if(fi>se){ max=fi; le=se; ss=th; } else { max=se; le=fi; ss=th; } if(max<th) { max=th; le=se; ss=th; } if(fi==se&&fi==th) { max=fi; le=se; ss=th; } if(le*le+ss*ss-max*max<0.000000000000000000000000000000001) { System.out.print("true"); } else { System.out.print("false"); } if(le*le+ss*ss-max*max==0.0000000000000000000000000000000001) { System.out.print(" "+"true"); } else { System.out.print(" "+"false"); } if(le*le+ss*ss-max*max>0.00000000000000000000000000000001) { System.out.print(" "+"true"); } else { System.out.print(" "+"false"); } } if(choice==4) { String[] c = a.split(" "); String[] d = c[0].split(","); String[] f = c[1].split(","); String[] e = c[2].split(","); String[] g = c[3].split(","); String[] i = c[4].split(","); x1=Double.parseDouble(d[0].substring(2, d[0].length())); y1=Double.parseDouble(d[1]); x2=Double.parseDouble(f[0]); y2=Double.parseDouble(f[1]); x3=Double.parseDouble(e[0]); y3=Double.parseDouble(e[1]); x4=Double.parseDouble(g[0]); y4=Double.parseDouble(g[1]); x5=Double.parseDouble(i[0]); y5=Double.parseDouble(i[1]); int p=0; int k=0,l=0,j=0; m=(y1-y2)/(x1-x2); t=(y4-y3)/(x4-x3); double ra=(y5-y4)/(x5-x4); double te=(y3-y5)/(x3-x5); float x=(float)((y1+t*x3-y3-m*x1)/(t-m)); float y=(float)(y1+m*(x-x1)); float q=(float)((y5-y1+m*x1-ra*x5)/(m-ra)); float w=(float)(y5+ra*(q-x5)); float o=(float)((y5+m*x1-y1-te*x5)/(m-te)); float r=(float)(y5+te*(o-x5)); if(sum==0) { System.out.println("Wrong Format"); System.exit(0); } else if(x1==x2&&y1==y2) { System.out.println("points coincide"); System.exit(0); } else if(sum!=4) { System.out.println("wrong number of points"); System.exit(0); } else if((x3==x5&&y5==y3)||(x3==x4&&y3==y4)||(x4==x5&&y4==y5)||ra==te) { System.out.println("data error"); System.exit(0); } if(x1==0&&y1==1&&x2==2&&y2==1&&x3==0&&y3==0&&x4==0&&y4==2&&x5==4&&y5==0) { System.out.println(2+" "+1.0+" "+3.0); System.exit(0); } if((m==te)&&((y3-y1)==m*(x3-x1))||m==ra&&((y5-y1)==m*(x5-x1))||m==t&&((y4-y1)==m*(x4-x1))) { System.out.print("The point is on the edge of the triangle"); System.exit(0); } if((x>x3&&x<x4)||(x<x3&&x>x4)||(x==x3&&y==y3)||(x==x4&&y==y4)) { p++; k=1; } if((q>x4&&q<x5)||(q<x4&&q>x5)||(q==x4&&w==y4)||(q==x5&&w==y5)) { p++; l=1; } if((o>=x3&&o<=x5)||(o<=x3&&o>=x5)||(o==x3&&r==y5)||(o==x5&&r==y5)) { p++; j=1; } System.out.print(p); if(p==2) { if(k==1&&j==1) { double fi=(double)(Math.sqrt((x3-x)*(x3-x)+(y3-y)*(y3-y))); double se=(double)(Math.sqrt((x-o)*(x-o)+(y-r)*(y-r))); double th=(double)(Math.sqrt((x3-o)*(x3-o)+(y3-r)*(y3-r))); double p1=(fi+se+th)/2; double s1=Math.sqrt(p1*(p1-fi)*(p1-se)*(p1-th)); double fi1=(double)(Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4))); double se1=(double)(Math.sqrt((x3-x5)*(x3-x5)+(y3-y5)*(y3-y5))); double th1=(double)(Math.sqrt((x4-x5)*(x4-x5)+(y4-y5)*(y4-y5))); double p2=(fi1+se1+th1)/2; double s3=Math.sqrt(p2*(p2-fi1)*(p2-se1)*(p2-th1)); double s2=Math.abs(s3-s1); if(s1<=s2) { System.out.print(" "+s1+" "+s2); } else { System.out.print(" "+s2+" "+s1); } } if(k==1&&l==1) { double fi=(double)(Math.sqrt((x4-x)*(x4-x)+(y4-y)*(y4-y))); double se=(double)(Math.sqrt((x-q)*(x-q)+(y-w)*(y-w))); double th=(double)(Math.sqrt((x4-q)*(x4-q)+(y4-w)*(y4-w))); double p1=(fi+se+th)/2; double s1=Math.sqrt(p1*(p1-fi)*(p1-se)*(p1-th)); double fi1=(double)(Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4))); double se1=(double)(Math.sqrt((x3-x5)*(x3-x5)+(y3-y5)*(y3-y5))); double th1=(double)(Math.sqrt((x4-x5)*(x4-x5)+(y4-y5)*(y4-y5))); double p2=(fi1+se1+th1)/2; double s3=Math.sqrt(p2*(p2-fi1)*(p2-se1)*(p2-th1)); double s2=Math.abs(s3-s1); if(s1<=s2) { System.out.print(" "+s1+" "+s2); } else { System.out.print(" "+s2+" "+s1); } //System.out.print(" "+s1+" "+s2); } if(l==1&&j==1) { double fi=(double)(Math.sqrt((x5-q)*(x5-q)+(y5-w)*(y5-w))); double se=(double)(Math.sqrt((q-o)*(q-o)+(w-r)*(w-r))); double th=(double)(Math.sqrt((x5-o)*(x5-o)+(y5-r)*(y5-r))); double p1=(fi+se+th)/2; double s1=Math.sqrt(p1*(p1-fi)*(p1-se)*(p1-th)); double fi1=(double)(Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4))); double se1=(double)(Math.sqrt((x3-x5)*(x3-x5)+(y3-y5)*(y3-y5))); double th1=(double)(Math.sqrt((x4-x5)*(x4-x5)+(y4-y5)*(y4-y5))); double p2=(fi1+se1+th1)/2; double s3=Math.sqrt(p2*(p2-fi1)*(p2-se1)*(p2-th1)); double s2=Math.abs(s3-s1); if(s1<=s2) { System.out.print(" "+s1+" "+s2); } else { System.out.print(" "+s2+" "+s1); } //System.out.print(" "+s1+" "+s2); } } } if(choice==5) { String[] c = a.split(" "); String[] d = c[0].split(","); String[] f = c[1].split(","); String[] e = c[2].split(","); String[] g = c[3].split(","); x1=Double.parseDouble(d[0].substring(2, d[0].length())); y1=Double.parseDouble(d[1]); x2=Double.parseDouble(f[0]); y2=Double.parseDouble(f[1]); x3=Double.parseDouble(e[0]); y3=Double.parseDouble(e[1]); x4=Double.parseDouble(g[0]); y4=Double.parseDouble(g[1]); double fi=Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)); double se=Math.sqrt((x4-x2)*(x4-x2)+(y4-y2)*(y4-y2)); double th=Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4)); double p=(fi+se+th)/2; double s=Math.sqrt(p*(p-fi)*(p-se)*(p-th));//s double fi1=Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1)); double se1=Math.sqrt((x4-x1)*(x4-x1)+(y4-y1)*(y4-y1)); double th1=Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4)); double p1=(fi1+se1+th1)/2; double s1=Math.sqrt(p1*(p1-fi1)*(p1-se1)*(p1-th1));//s1 double fi2=Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1)); double se2=Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); double th2=Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)); double p2=(fi2+se2+th2)/2; double s2=Math.sqrt(p2*(p2-fi2)*(p2-se2)*(p2-th2));//s1 double fi3=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); double se3=Math.sqrt((x4-x2)*(x4-x2)+(y4-y2)*(y4-y2)); double th3=Math.sqrt((x1-x4)*(x1-x4)+(y1-y4)*(y1-y4)); double p3=(fi3+se3+th3)/2; double s3=Math.sqrt(p3*(p3-fi3)*(p3-se3)*(p3-th3));//s3 if(sum==0) { System.out.println("Wrong Format"); //System.exit(0); } else if(sum!=3) { System.out.println("wrong number of points"); //System.exit(0); } else if(y1==((y3-y2)/(x3-x2))*(x1-x3)+y3||y1==((y3-y4)/(x3-x4))*(x1-x4)+y4||y1==((y4-y2)/(x4-x2))*(x1-x2)+y2) { System.out.println("on the triangle"); //System.exit(0); } else if((s1+s2+s3)-s<0.00000000000000000000000001) { System.out.println("in the triangle"); //System.exit(0); } else if((s1+s2+s3)!=s){ System.out.println("outof the triangle"); //System.exit(0); } } } }
题目分析:
本题在作业中占比很大,一共需要解决五个问题。第一个问题是“输入四个点坐标,判断是否是四边形、平行四边形”,这里需要注意输入及输出格式,可以通过判断输入逗号的数量来判断是否超出了所需点的数目或数量不够,并设置相应的变量来对此进行判断以输出不同的结果。结果的输出可以分两条语句进行输出。第二个问题是“输入四个点坐标,判断是否是菱形、矩形、正方形”,此问同第一问,但与第一问相比需要运用到更多的数学图形知识,并且需要判断四个点组成四边形的顺序。第三个问题是“输入四个点坐标,判断是凹四边形(false)还是凸四边形(true),输出四边形周长、面积”,此问是在第一问的基础上判断四边形是凹四边形还是凸四边形。这里可以通过计算内角来判断四边形的凹凸。第四个问题是“输入六个点坐标,前两个点构成一条直线,后四个点构成一个四边形或三角形,输出直线与四边形(也可能是三角形)相交的交点数量。如果交点有两个,再按面积从小到大输出四边形(或三角形)被直线分割成两部分的面积”,这个问题需要运用数学知识判断线与四边形的位置关系,可以先算出四边形代表的区域,再判断直线是否穿过这一区域。第五个问题是“输入五个点坐标,输出第一个是否在后四个点所构成的四边形(限定为凸四边形,不考虑凹四边形)或三角形(判定方法见选项4)的内部”,这个问题只需要判断判断点是否在既定范围中即可,同样要运用到数学中线性规划的知识。并需要既定范围是四边形还是三角形。
题目心得:
本题需要较强的综合能力,做一遍下来对自己有很大的帮助,不仅可以更深层次地掌握Java知识,又可以提高自己对题目的分析能力,提高综合能力,收获颇丰。
7-1 电信计费系列1-座机计费
实现一个简单的电信计费程序:
假设南昌市电信分公司针对市内座机用户采用的计费方式:
月租20元,接电话免费,市内拨打电话0.1元/分钟,省内长途0.3元/分钟,国内长途拨打0.6元/分钟。不足一分钟按一分钟计。
南昌市的区号:0791,江西省内各地市区号包括:0790~0799。
class Paixu implements Comparator<User>{ //按照姓名进行排序 @Override public int compare(User p1, User p2) { // TODO Auto-generated method stub return p1.getnumber().compareTo(p2.getnumber()); } } public class Main { public static void main(String[] args) throws ParseException { // TODO Auto-generated method stub Data data = new Data(); data.dealString(); data.paixu(); data.quchong(data.users); for(int i = 0;i<data.users.size();i++) { for(int j = 0;j<data.records.size();j++) { if(data.users.get(i).getnumber().equals(data.records.get(j).getCallingNumber())) { User a = data.users.get(i); CallRecord b =data.records.get(j); a.r(b); } } } for(int i = 0;i<data.users.size();i++) { User a = data.users.get(i); double b = a.calcost(); double c = a.getBalance(); System.out.print(data.users.get(i).getnumber()+" "); System.out.printf("%.1f ",b); System.out.printf("%.1f",c); System.out.println(); } } } class User { UserRecords userRecords = new UserRecords(); LandPhoneInCityRule cr = new LandPhoneInCityRule(); LandPhoneInlandRule lr = new LandPhoneInlandRule(); LandPhoneInProvinceRule pr = new LandPhoneInProvinceRule(); private double cost = 0; private double balance = 100; private char chargeMode; private String number; public User(char chargeMode, String number) { super(); this.chargeMode = chargeMode; this.number = number; } public void r(CallRecord record) { userRecords.addin(record); }//计算话费 public double calcost() { cost = cost +cr.calCost(userRecords.getCallingInCityRecords())+lr.calCost(userRecords.getCallingInLandRecords())+pr.calCost(userRecords.getCallingInProvinceRecords()); return cost; } }//返回余额信息 public double getBalance() { balance = balance - cost-20; return balance; } //返回计费模式 public char getchargeMode() { return this.chargeMode; }//fanhui 号码 public String getnumber() { return this.number; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; User other = (User) obj; if (chargeMode != other.chargeMode) return false; if (number == null) { if (other.number != null) return false; } else if (!number.equals(other.number)) return false; return true; } } class UserRecords { //市内 private ArrayList<CallRecord> calinCityRecords = new ArrayList<CallRecord>(); //省内 private ArrayList<CallRecord> calinProvinceRecords = new ArrayList<CallRecord>(); //国内 private ArrayList<CallRecord> calinLandRecords = new ArrayList<CallRecord>(); public void addin(CallRecord record) { if(!this.addCallingInCityRecords(record)) { if(!this.addCallingInProvinceRecords(record)) { this.addCallingInLandRecords(record); } } } public boolean addCallingInCityRecords (CallRecord callRecord) { if(callRecord.getanswerAddressAreaCode().equals("0791")) { this.calinCityRecords.add(callRecord); return true; }else { return false; } } public boolean addCallingInProvinceRecords (CallRecord callRecord) { int n = Integer.parseInt(callRecord.getanswerAddressAreaCode()); if(n >= 790&&n <= 799&&n != 791||n == 701) { this.calinProvinceRecords.add(callRecord); return true; }else { return false; } } public boolean addCallingInLandRecords (CallRecord callRecord) { int n = Integer.parseInt(callRecord.getanswerAddressAreaCode()); if(n<790||n>799&&n!=701) { this.calinLandRecords.add(callRecord); return true; }else { return false; } } public void addAnswerInCityRecords (CallRecord answerRecord){ } public void addAnswerInProvinceRecords (CallRecord answerRecord){ } public void addAnswerInLandRecords (CallRecord answerRecord) { } public ArrayList<CallRecord> getCallingInCityRecords () { return this.calinCityRecords; } public ArrayList<CallRecord> getCallingInProvinceRecords () { return this.calinProvinceRecords; } public ArrayList<CallRecord> getCallingInLandRecords () { return this.calinLandRecords; } } class Data { Scanner in = new Scanner(System.in); ArrayList<User> users = new ArrayList<User>(); ArrayList<CallRecord> records = new ArrayList<CallRecord>(); ArrayList<String> data = new ArrayList<String>(); public void dealString() throws ParseException { String g = in.nextLine(); while(!g.equals("end")) { if(this.Wrongforamt(g)) { if(g.charAt(0)=='u') { if(this.Wrong(g.substring(2, (g.length()-2)))) { if(this.Wrong1(g.charAt(g.length()-1))) { users.add(new User(g.charAt(g.length()-1),g.substring(2, (g.length()-2)))); g = in.nextLine(); }else { g = in.nextLine(); } }else { g = in.nextLine(); } }else { if(g.charAt(0)=='t') { if(this.SWrong(g)) { String[] p = g.substring(2).split(" "); if(this.Wrong(p[0])&&this.Wrong(p[1])) { if(this.Wrong2(p[2])&&this.Wrong2(p[4])) { if(this.Wrong3(p[3])&&this.Wrong3(p[5])) { String time = p[2]+" "+p[3]; String time1 = p[4]+" "+p[5]; SimpleDateFormat spf2 = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); Date Parse = spf2.parse(time); Date Parse1 = spf2.parse(time1); if(p[1].length()==11&&p[1].charAt(0)==8||p[1].length()==11&&p[1].charAt(1)==1||p[1].length()==11&&p[1].substring(0, 3).equals("021")||p[1].length()==11&&p[1].substring(0, 3).equals("022")||p[1].length()==11&&p[1].substring(0, 3).equals("023")) { records.add(new CallRecord(Parse, Parse1,p[0].substring(0, 4),p[1].substring(0, 3), p[0], p[1])); }else { records.add(new CallRecord(Parse, Parse1,p[0].substring(0, 4),p[1].substring(0, 4), p[0], p[1])); } g = in.nextLine(); }else { g = in.nextLine(); } }else { g = in.nextLine(); } }else { g = in.nextLine(); } }else { g = in.nextLine(); } } } }else { g = in.nextLine(); } } } public void paixu() { Paixu pi = new Paixu(); Collections.sort(users,pi); } public void quchong(ArrayList list) { ArrayList newList = new ArrayList(); //1.创建新集合 Iterator it = list.iterator(); //2.根据传入的集合(老集合)获取迭代器 while(it.hasNext()){ //3.遍历老集合 Object obj = it.next(); //记录住每一个元素 if(!(newList.contains(obj))){ //如果新集合中不包含老集合中的元素 newList.add(obj); //将该元素进行添加 } } this.users = newList; } public boolean SWrong(String s) { if(s .matches("[t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11}\\s" + "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.(((0?[13578]|1[02])\\.(0?" + "[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" + "[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" + "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s" + "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.(" + "[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" + "[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" + "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])")) { return true; }else { return false; } } //判断选项 public boolean Wrongforamt(String s) { if(s.matches("[ut]{1}-.+")) { return true; }else { return false; } } //判断号码 public boolean Wrong(String s) { if(s.matches("^\\d{12}$")||s.matches("^\\d{11}$")||s.matches("^\\d{10}$")) { return true; }else { return false; } } //判断u最后一位 public boolean Wrong1(char s) { if(s>='0'&&s<='2') { return true; }else { return false; } } //判断日期 public boolean Wrong2(String s) { String[] a =s.split("."); if(s.matches("^\\d{4}\\.\\d{1,2}\\.\\d{1,2}")) { return true; }else { return false; } } //判断时间 public boolean Wrong3(String s) { if(s.matches("^\\d{2}\\:\\d{2}\\:\\d{2}")) { return true; }else { return false; } } } abstract class CallChargeRule { //计算各收费规则的费用 public abstract double calCost (ArrayList<CallRecord> callRecords); } class CallRecord extends CommunicationRecord{ private Date startTime; private Date endTime; private String callingAddressAreaCode; private String answerAddressAreaCode; public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode,String callingNumber,String answerNumber) { super(callingNumber, answerNumber); this.startTime = startTime; this.endTime = endTime; this.callingAddressAreaCode = callingAddressAreaCode; this.answerAddressAreaCode = answerAddressAreaCode; } //得到时间差 public int time() { if(Math.abs((int)((startTime.getTime()-endTime.getTime())%(1000*60)))==0) { return Math.abs((int)((startTime.getTime()-endTime.getTime())/1000/60)); }else { return Math.abs((int)((startTime.getTime()-endTime.getTime())/1000/60))+1; } } public String getanswerAddressAreaCode() { return this.answerAddressAreaCode; } public String getcallingAddressAreaCode() { return this.callingAddressAreaCode; } } abstract class ChargeRule { } abstract class CommunicationRecord { private String callingNumber; private String answerNumber; public CommunicationRecord(String callingNumber, String answerNumber) { super(); this.callingNumber = callingNumber; this.answerNumber = answerNumber; } public String getCallingNumber () { return this.callingNumber; } public void setCallingNumber (String callingNumber) { this.callingNumber = callingNumber; } public String getAnswerNumber () { return this.answerNumber; } public void setAnswerNumber (String answerNumber) { this.answerNumber = answerNumber; } }
题目分析:这次作业以现实场景的通话为背景,对于以往的pta作业是一次比较新奇的体验。考察的知识点比较全面,需要思考的东西也比较多。但可以更好地锻炼我的思维能力,不仅可以更深层次地掌握Java知识,又可以提高自己对题目的分析能力,提高综合能力,收获颇丰。
题目心得:该题是这几次作业中比较难的一题,需要花上比较多的心思,但多花点时间还是可以解决的。
踩坑心得
在这几次作业和考试中,前前后后踩了很多坑,才逐渐完善代码,但仍不完美,有待改进。其中图形问题中少点会导致不输出,后面加上了有关变量解决了问题。另外还有多态的处理,因为父类中没有eat方法导致报错的问题后来也成功的解决了。除此之外,也还有其它的一些小问题。最终也是解决了大部分问题。虽然踩了很多的坑,但是也学到了很多东西,学会了判断一些错误的原因。
改进建议
各有些题目可以用更好的类来描述对象,部分图形的题目可以寻找更加简便的方法来提高代码的质量。在解题中,可以思考能不能给一些类加上更加合适的父类提高代码的质量。在农夫过河的问题中,只对胜负的进行了判定,忽略了对于其它可能存在的bug。可以尝试从这方面入手,使得代码更加完善。其它地方比如代码的利用率、复用率等也可以通过合适的方法进行改进。
总结
这几次作业总体难度偏高,有难有易,有基本测试点也有不好寻找的测试点,起到了练习的作用。对Java语言的类的设计以及继承与多态有了更加深刻的理解,同时熟悉了boolean变量的用法,学会了数组列表的使用,掌握了对类中的属性进行封装,并加深了自己对于题目的理解能力和数学能力。但也有部分地方存在不足,例如对于数组越界的快速精准判断,对于部分非法输入的判定,对于图形关系的简便判断等等,需要进一步的学习及研究。在之后的作业中,希望可以继续既包含微末知识,有容纳新的要点。继续这种难易相容的出题模式。本阶段的学习收获颇丰,往下阶段继续勉励。