对近期相关作业,实验以及考试的总结
一.前言
- 知识点:
(1)第四次题目集:对字符串的截取,转化,分析。对非法输入的判定。对四边形相关性质的分析,理解,计算。
(2)对把大象装进冰箱:对象的创建。父类的表达,以及运用。多文件运用。相关代码复用性的提高。对ArrayList,及相关函数的熟悉运用。
(3)对线段相关性质的分析以及算法的运用。
- 题量和难度:
对PTA的第四次题目集题量较小,但是在第二题的难度很高。我经过分析,有以下几个难点。
首先,对要求的相关算法的熟练度不高,导致在解决的时候需要花很多的时间来分析和解决算法问题。其次是对对象的创建及函数的运用的不够熟练导致代码重复利用率很高,使其看起来十分冗杂,是自己没有继续写下去的耐心。
对把大象装进冰箱而言,代码的量很多,但是相关难度较为简单,思维难度很低。我认为这个题目主要锻炼我们的对对象创建及父类运用的熟练度。
对在期中考试中的相关题目,我认为难度并不高,这些题目同样的代码量较多,并且考到了最新学习的知识点,着重考察我们对课堂知识的掌握程度及复习程度。
二.设计和分析
1题目集四第二题:
用户输入一组选项和数据,进行与四边形有关的计算。
以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
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)源代码:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); String str = ""; int x1 = 0, y1 = 0, x2 = 0, y2 = 0, x3 = 0, y3 = 0, x4 = 0, y4 = 0, x5 = 0, y5 = 0, x6 = 0, y6 = 0; str = input.nextLine(); String[] S = str.split(":"); if (S.length == 2) { int index = Integer.parseInt(S[0]); String[] s = S[1].split(" "); if (s.length == 4) { String[] s1 = s[0].split(","); String[] s2 = s[1].split(","); String[] s3 = s[2].split(","); String[] s4 = s[3].split(","); x1 = Integer.parseInt(s1[0]); y1 = Integer.parseInt(s1[1]); x2 = Integer.parseInt(s2[0]); y2 = Integer.parseInt(s2[1]); x3 = Integer.parseInt(s3[0]); y3 = Integer.parseInt(s3[1]); x4 = Integer.parseInt(s4[0]); y4 = Integer.parseInt(s4[1]); } if (s.length == 5) { String[] s1 = s[0].split(","); String[] s2 = s[1].split(","); String[] s3 = s[2].split(","); String[] s4 = s[3].split(","); String[] s5 = s[4].split(","); x1 = Integer.parseInt(s1[0]); y1 = Integer.parseInt(s1[1]); x2 = Integer.parseInt(s2[0]); y2 = Integer.parseInt(s2[1]); x3 = Integer.parseInt(s3[0]); y3 = Integer.parseInt(s3[1]); x4 = Integer.parseInt(s4[0]); y4 = Integer.parseInt(s4[1]); x5 = Integer.parseInt(s5[0]); y5 = Integer.parseInt(s5[1]); } if (((index == 1 || index == 2 || index == 3) && s.length == 4) && (index == 5 && s.length == 5) && (index == 4) && s.length == 6) { rectangle rec = new rectangle(x1, y1, x2, y2, x3, y3, x4, y4); switch (index) { case 1: if ((x1 == x2 && y1 == y2) || (x2 == x3 && y2 == y3) || (x3 == x4 && y3 == y4) || (x1 == x3 && y1 == y3) || (x1 == x4 && y1 == y4) || (x2 == x4 && y2 == y4)) { System.out.println("points coincide"); } else { System.out.println(rec.judgeQuadrilateral(x1, y1, x2, y2, x3, y3, x4, y4) + " " + rec.judgeParallelogram(x1, y1, x2, y2, x3, y3, x4, y4)); } break; case 2: if ((x1 == x2 && y1 == y2) || (x2 == x3 && y2 == y3) || (x3 == x4 && y3 == y4) || (x1 == x3 && y1 == y3) || (x1 == x4 && y1 == y4) || (x2 == x4 && y2 == y4)) { System.out.println("points coincide"); } else { if (rec.judgeQuadrilateral(x1, y1, x2, y2, x3, y3, x4, y4)) { System.out.println(rec.judgeRhombus(x1, y1, x2, y2, x3, y3, x4, y4) + " " + rec.judgeRectangles(x1, y1, x2, y2, x3, y3, x4, y4) + " " + rec.judgeSquare(x1, y1, x2, y2, x3, y3, x4, y4)); } else { System.out.println("not a quadrilateral"); } } break; case 3: if ((x1 == x2 && y1 == y2) || (x2 == x3 && y2 == y3) || (x3 == x4 && y3 == y4) || (x1 == x3 && y1 == y3) || (x1 == x4 && y1 == y4) || (x2 == x4 && y2 == y4)) { System.out.println("points coincide"); } else { if (rec.judgeQuadrilateral(x1, y1, x2, y2, x3, y3, x4, y4)) { System.out.println(rec.judgeCONVEXquadrilateral(x1, y1, x2, y2, x3, y3, x4, y4) + " " + rec.girth() + " " + rec.area()); } else { System.out.println("not a quadrilateral"); } } break; case 4: break; case 5: if (rec.judgeQuadrilateral(x2, y2, x3, y3, x4, y4, x5, y5)) { int a = (x3 - x2) * (y1 - y2) - (y3 - y3) * (x1 - x2); int b = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3); int c = (x5 - x4) * (y1 - y4) - (y5 - y4) * (x1 - x4); int d = (x2 - x5) * (y1 - y5) - (y2 - y5) * (x1 - x3); if ((a > 0 && b > 0 && c > 0 && d > 0) || (a < 0 && b < 0 && c < 0 && d < 0)) { System.out.println("in the quadrilateral"); } else if (a == 0 && b == 0 && c == 0 && d == 0) { System.out.println("on the quadrilateral"); } else { System.out.println("out of the quadrilateral"); } } else if (rec.judgeTriangles(x2, y2, x3, y3, x4, y4, x5, y5)) { if (rec.judgeParallel(x2, y2, x3, y3, x4, y5)) { int a = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3); int b = (x5 - x4) * (y1 - y4) - (y5 - y4) * (x1 - x4); int c = (x3 - x5) * (y1 - y5) - (y3 - y5) * (x1 - x5); if ((a > 0 && b > 0 && c > 0) || (a < 0 && b < 0 && c < 0)) { System.out.println("in the triangle"); } else if (a == 0 && b == 0 && c == 0) { System.out.println("on the triangle"); } else { System.out.println("out of the triangle"); } } else if (rec.judgeParallel(x3, y3, x2, y2, x4, y4)) { int a = (x4 - x2) * (y1 - y2) - (y4 - y2) * (x1 - x2); int b = (x5 - x4) * (y1 - y4) - (y5 - y4) * (x1 - x4); int c = (x2 - x5) * (y1 - y5) - (y2 - y5) * (x1 - x5); if ((a > 0 && b > 0 && c > 0) || (a < 0 && b < 0 && c < 0)) { System.out.println("in the triangle"); } else if (a == 0 && b == 0 && c == 0) { System.out.println("on the triangle"); } else { System.out.println("out of the triangle"); } } else if (rec.judgeParallel(x4, y4, x3, y3, x5, y5)) { int a = (x3 - x2) * (y1 - y2) - (y3 - y2) * (x1 - x2); int b = (x5 - x3) * (y1 - y3) - (y5 - y3) * (x1 - x3); int c = (x2 - x5) * (y1 - y5) - (y2 - y5) * (x1 - x5); if ((a > 0 && b > 0 && c > 0) || (a < 0 && b < 0 && c < 0)) { System.out.println("in the triangle"); } else if (a == 0 && b == 0 && c == 0) { System.out.println("on the triangle"); } else { System.out.println("out of the triangle"); } } else if (rec.judgeParallel(x5, y5, x2, y2, x4, y4)) { int a = (x3 - x2) * (y1 - y2) - (y3 - y2) * (x1 - x2); int b = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3); int c = (x2 - x4) * (y1 - y4) - (y2 - y4) * (x1 - x4); if ((a > 0 && b > 0 && c > 0) || (a < 0 && b < 0 && c < 0)) { System.out.println("in the triangle"); } else if (a == 0 && b == 0 && c == 0) { System.out.println("on the triangle"); } else { System.out.println("out of the triangle"); } } } else { System.out.println("not a quadrilateral or triangle"); } break; } } else { System.out.println("wrong number of points"); } } else { System.out.println("Wrong Format"); } } } class rectangle { private double edge; private double edge1; private double edge2; private double edge3; private double edge4; public rectangle(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) { edge1 = Math.sqrt(Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2)); edge2 = Math.sqrt(Math.pow((x3 - x2), 2) + Math.pow((y3 - y2), 2)); edge3 = Math.sqrt(Math.pow((x3 - x4), 2) + Math.pow((y3 - y4), 2)); edge4 = Math.sqrt(Math.pow((x1 - x4), 2) + Math.pow((y1 - y4), 2)); } public double getEdge(int x1, int y1, int x2, int y2) {// 得1条边长 double edge = Math.sqrt(Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2)); return edge; } public double area() {// 求面积 return edge1 * edge2; } public double girth() {// 判断周长 return edge1 + edge2 + edge3 + edge4; } public boolean judgeParallelogram(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {// 判断平行四边形 if ((((x2 - x1) * (y3 - y3) - (x3 - x4) * (y2 - y1)) == 0) && ((x1 - x4) * (y1 - y4) - (x2 - x3) * (y2 - y3) == 0)) { return true; } else { return false; } } public boolean judgeQuadrilateral(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {// 判断四边形 if (!judgeParallel(x1, y1, x2, y2, x4, y4) && !judgeParallel(x4, y4, x3, y3, x1, y1) && !judgeParallel(x2, y2, x3, y3, x1, y1) && !judgeParallel(x3, y3, x2, y2, x4, y4)) { return true; } else { return false; } } public boolean judgeParallel(int x1, int y1, int x2, int y2, int x3, int y3) {// 判断两条线平行 if ((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1) == 0) { return true; } else { return false; } } public boolean judgeRhombus(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {// 判断菱形 if (judgeParallelogram(x1, y1, x2, y2, x3, y3, x4, y4) && (edge1 == edge2)) { return true; } else { return false; } } public boolean judgeRectangles(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {// 判断矩形 edge = getEdge(x1, y1, x3, y3); if ((Math.pow(edge1, 2) + Math.pow(edge2, 2) == Math.pow(edge, 2)) && judgeParallelogram(x1, y1, x2, y2, x3, y3, x4, y4)) { return true; } else { return false; } } public boolean judgeSquare(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {// 判断正方形 if (judgeRectangles(x1, y1, x2, y2, x3, y3, x4, y4) && judgeRhombus(x1, y1, x2, y2, x3, y3, x4, y4)) { return true; } else { return false; } } public boolean judgeCONVEXquadrilateral(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {// 判断凸还是凹 double z1, z2, z3, z4; if (judgeQuadrilateral(x1, y1, x2, y2, x3, y3, x4, y4)) { z1 = (x2 - x1) * (y4 - y1) - (x4 - x1) * (y2 - y1); z2 = (x4 - x1) * (y3 - y1) - (x3 - x1) * (y4 - y1); z3 = (x4 - x2) * (y3 - y2) - (x3 - x2) * (y4 - y2); z4 = (x3 - x2) * (y1 - y2) - (x1 - x2) * (y3 - y2); if ((z1 * z2 > 0) && (z3 * z4 > 0)) { return true; } else { return false; } } else { return false; } } public boolean judgeTriangles(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) { if (judgeParallel(x1, y1, x2, y2, x4, y4) && !judgeParallel(x2, y2, x1, y1, x3, y3) && !judgeParallel(x3, y3, x2, y2, x4, y4) && !judgeParallel(x4, y4, x1, y1, x3, y3)) { return true; } else if (!judgeParallel(x1, y1, x2, y2, x4, y4) && judgeParallel(x2, y2, x1, y1, x3, y3) && !judgeParallel(x3, y3, x2, y2, x4, y4) && !judgeParallel(x4, y4, x1, y1, x3, y3)) { return true; } else if (!judgeParallel(x1, y1, x2, y2, x4, y4) && !judgeParallel(x2, y2, x1, y1, x3, y3) && judgeParallel(x3, y3, x2, y2, x4, y4) && !judgeParallel(x4, y4, x1, y1, x3, y3)) { return true; } else if (!judgeParallel(x1, y1, x2, y2, x4, y4) && !judgeParallel(x2, y2, x1, y1, x3, y3) && !judgeParallel(x3, y3, x2, y2, x4, y4) && judgeParallel(x4, y4, x1, y1, x3, y3)) { return true; } else { return false; } } public static void main(String[] args) { } }
(2)输入与输出实例:
A.输入:1:-1,-1 -1,-1 1,2 1,-2
输出:points coincide
B.输入:1:-1,-1 1,2 -1,1 ++1,0
输出:Wrong Format
C.输入:1:-1,-1 -1,2
输出:wrong number of points
D.输入:1:-1,-1 -1,1 1,2 1,-2
输出:true false
E.输入:2:10,10 1,1 0,0 1,20
输出:not a quadrilateral
F.输入:2:0,0 0,80 80,80 80,0
输出:2:0,0 0,80 80,80 80,0
G.输入:2:0,0 -10,80 0,160 -10,80
输出:not a quadrilateral
H.输入:3:-1,-1 -1,1 1,2 1,-2
输出:true 10.472 6.0
I.输入:3:0,0 -10,100 0,99 10,100
输出:false 221.097 990.0
(3)思路与分析
- 有与输入的是一个字符串,而我们要用相关的数字进行运算,这就是我们要对字符串进行处理,我用String.sqilt(”String”);来进行字符串的分割。
- 在分割完后用Integer的相关函数对其整形转化。
- 在得到相关的数据后多于第一小问。我事先写一个四边形类,将相关的函数全写入里面,然后在主文件里创建相关对象,于是第一问我直接有求四个边的函数和三点不共线和两对边平行相等判断。
- 在第二小问时,因为已经判断为四边形,求四边,以及判断两边是否相等(x1*x2+y1*y2==0)
两边垂直,(x1*y2-x2*y1==0)两边平行,用(Math.pow(a,2)+Math.pow(b,2)=Math.pow(c,2))判断矩形
(4)解释与心得
本题考查队字符串的分析和相关算法的掌握,同时也考查对非法输入的分析。
2把大象装进冰箱
在第二次练习(把大象装入冰箱)的基础上,参考如下类图,把任一动物装入任一电器中。
按输入实现把动物Animal【大象Elephant或狮子Lion】装进家用电器Electric【冰箱Refriger或微波炉Microwave】
对象.getClass().getName()可以获取对象的类名字符串
在测试类可以按如下方式定义:
按如下格式从键盘输入一个字符串:
Put the Elephant Tom into the Refriger r1
代码中的regex是个正则表达式,通过正则表达式可以提取到
电气类名:Elephant
电气名:Tom
动物类名:Regriger
动物名:r1
- 输入格式
Put the Elephant Tom into the Refriger r1
Put the Elephant Mike into the MicroWave w1
Put the Line lion1 into the Refriger r1
Put the Line lion2 into the MicroWave w1
quit
输出:我是Refriger电器,我的名字是r1
我有2头动物,它们分别是:
Elephant:Tom
Lion:lion1
我是MicroWave电器,我的名字是w1
我有2头动物,它们分别是:
Elephant:Mike
Lion:lion2
(1)源代码
package work; import java.util.Scanner; import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Text { public static void main(String[]args) { ArrayList<Electric>electriclist = new ArrayList<Electric>(); Scanner input = new Scanner(System.in); String regex = "Put\\s+the\\s+(\\w+)\\s+(\\w+)\\s+into\\s+the\\s+(\\w+)\\s+(\\w+)"; Pattern pattern = Pattern.compile(regex); String str; while(true) { System.out.println("请按格式输入命令串:quit退出"); str = input.nextLine(); if(str.equals("quit")) { break; } Matcher matcher = pattern.matcher(str); if(matcher.find()) { String animaleClass = matcher.group(1); String animaleName = matcher.group(2); String electricClass=matcher.group(3); String electricName=matcher.group(4); Electric electric = getElectric(electriclist,electricClass,electricName); Animale animale=getAniamle(electriclist,animaleClass,animaleName); if(animale!=null) { electric.open(); electric.putAnimal(animale); electric.close(); } } else { System.out.println("格式输入错误!"); } } for(Electric electric:electriclist) { System.out.println(""); electric.showName(); electric.showAnimal(); } } public static Animale getAniamle(ArrayList<Electric> electriclist, String animaleClass, String animaleName) { // TODO Auto-generated method stub for(Electric electric:electriclist) { for(int j=0;j<electric.getsize();j++) { if(electric.getAnimaleClass(j).equals(animaleClass)) { if(electric.getAnimaleName(j).equals(animaleName)) { return null; } } } } Animale animale; if(animaleClass.equals("Elephant")) { animale=new Elephant(animaleClass,animaleName); } else { animale=new Lion(animaleClass,animaleName); } return animale; } public static Electric getElectric(ArrayList<Electric> electriclist, String electricClass, String electricName) { // TODO Auto-generated method stub for(Electric electric:electriclist) { if(electric.getSorts().equals(electricClass)) { if(electric.getName().equals(electricName)) { return electric; } } } Electric electric; if(electricClass.equals("Refriger")) { electric = new Refriger(electricClass,electricName); } else { electric = new MicroWave(electricClass,electricName); } electriclist.add(electric); return electric; } } package work; import java.util.ArrayList; public class Electric { ArrayList<Animale> list = new ArrayList<Animale>(); private String name; private int position = 0;// 1:开门 0:关门 private String animals; private String sorts; public Electric(String name) { this.name = name; } public Electric(String sorts,String name) { this.sorts=sorts; this.name=name; } public String getSorts() { return sorts; } public String getName() { return name; } public String getAnimaleName(int index) { return list.get(index).getName(); } public String getAnimaleClass(int index) { return list.get(index).getSorts(); } public int open() { position = 1; return position; } public int close() { position = 0; return position; } public void putAnimal(Animale animal) { list.add(animal); } public void showName() { System.out.println("我是" + getSorts() + "电器,我的名字是" + getName()); } public int getsize() { return list.size(); } public void showAnimal() { System.out.println("我有" + getsize() + "头动物,它们分别是:"); for (Animale animale : list) { animale.showName(); } } public Animale[] animals() { Animale[] arry = null; int i = 0; for (Animale animale : list) { arry[i] = animale; i++; } return arry; } public void add(Animale animale) { list.add(animale); } } package work; public class Animale { private String sorts; private String name; public Animale(String sorts, String name) { this.sorts = sorts; this.name = name; } public Animale(String name) { this.name=name; } public String getSorts() { return sorts; } public String getName() { return name; } public void showName() { System.out.println(getSorts() + ":" + getName()); } } package work; public class MicroWave extends Electric { public MicroWave(String sorts,String name) { super(sorts,name); // TODO Auto-generated constructor stub } } package work; public class Refriger extends Electric { public Refriger(String sorts,String name) { // TODO Auto-generated constructor stub super(sorts,name); } } package work; public class Elephant extends Animale { public Elephant(String sorts,String name) { super(sorts,name); // TODO Auto-generated constructor stub } public static void main(String[] args) { // TODO Auto-generated method stub } } package work; public class Lion extends Animale { public Lion(String sorts,String name) { super( sorts,name); } public static void main(String[] args) { // TODO Auto-generated method stub } }
(2)输入与输出实例
A.输入:
Put the Elephant Tom into the Refriger r1
Put the Elephant Mike into the MicroWave w1
Put the Line lion1 into the Refriger r1
Put the Line lion2 into the MicroWave w1
quit
输出:我是Refriger电器,我的名字是r1
我有2头动物,它们分别是:
Elephant:Tom
Lion:lion1
我是MicroWave电器,我的名字是w1
我有2头动物,它们分别是:
Elephant:Mike
Lion:lion2
(3)思路与分析
- 根据类图将各个分类先构建完。
- 由已给出的代码依次将各个类的方法写完。
- 难点在于将东西放进冰箱,在冰箱里用ArrayList方法,来存动物
(4)解释与心得
该题的难度主要在于对象的创建以及相关方法的构建,同时要用到继承提高代码的复用率以次来提高代码质量。
- 期中考试题目
设计一个类表示平面直角坐标系上的点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.util.Scanner; public class Main { public static void main(String[]args) { Scanner input = new Scanner(System.in); double x1,y1,x2,y2; String color; Line line=new Line(input.nextDouble(),input.nextDouble(),input.nextDouble(),input.nextDouble(),input.next()); line.display(); } } class Line { private Point point1=new Point(); private Point point2=new Point(); private String color; public Line() { } public Line(double x1, double y1, double x2, double y2, String color) { point1.setX(x1); point1.setY(y1); point2.setX(x2); point2.setY(y2); this.color = color; } public Point getPoint1() { return point1; } public void setPoint1(Point point1) { this.point1 = point1; } public Point getPoint2() { return point2; } public void setPoint2(Point point2) { this.point2 = point2; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getDistance() { double distance = 0; distance = Math.sqrt(Math.pow(point1.getX() - point2.getX(), 2) + Math.pow(point1.getY() - point2.getY(), 2)); return distance; } public void display() { if(point1.judge(point1)==true&&point2.judge(point2)==true) { System.out.printf("The line's color is:"+getColor()); System.out.println(); System.out.printf("The line's begin point's Coordinate is:\n"); System.out.printf("(%.2f,%.2f)\n",point1.getX(), point1.getY()); System.out.printf("The line's end point's Coordinate is:\n"); System.out.printf("(%.2f,%.2f)\n", point2.getX(), point2.getY()); System.out.printf("The line's length is:%.2f\n", getDistance()); } else { System.out.println("Wrong Format"); } } /* * public static void main(String[] args) { Line line = new Line(5, 9.4, 12.3, * 84, "Red"); line.display(); } */ } class Point { private double x; private double y; public Point() { } public Point(double x,double y) { this.x=x; this.y=y; } public double getX() { return x; } public void setX(double x) { this.x=x; } public double getY() { return y; } public void setY(double y) { this.y=y; } public void display() { if((x>0&&x<=200)&&(y>0&&y<=200)) { System.out.printf("(%.2f,%.2f)",x,y); } else { System.out.println("Wrong Format"); } } public boolean judge(Point point) { if((point.getX()>0&&point.getX()<=200)&&(point.getY()>0&&point.getY()<=200)) { return true; } else { return false; } } /* * public static void main(String[]args) { Point point = new Point(80.2356 * ,100.12); point.display(); } */ }
(2)输入与输出实例
A.输入:5
9.4
12.3
84
Red
输出:The line's color is:Red
The line's begin point's Coordinate is:
(5.00,9.40)
The line's end point's Coordinate is:
(12.30,84.00)
The line's length is:74.96
B.输入:80.2356
352.12
24.5
100
Black
输出:Wrong Format
(3)思路分析
- 根据类图创建各个对象,构建大致框架
- 根据类图的各个方法一定严格按照上面的要求,(很多人已经吃过亏了,测试点过了但是最后分数仍然不理想,这就是没有严格按照类图的原因)。
- 在根据相关的需要进行必要的增添。来完善自己的代码。
- 对继承的代码进一步优化提高自己的代码的复用率,提高代码的质量
(4)解释与心得
该题的主要难度在于对新学的知识的掌握。如对abstruct以及相关抽象函数的运用。
abstract class Element {
abstract void display();
}
大大提高代码的复用率。
浙公网安备 33010602011771号