近期PTA题目集,超星作业以及期中考试的总结
一.前言
- 知识点
1.正则表达式的运用
2.对象和类
3.多态与继承
4.抽象类以及接口
5.单双链表
- 题量
1.期中考试:3题(类,多态)
2.超星作业:1题(双链表)
3.PTA练习:2题(正则,图形类设计)
- 难度
1.期中考试:中等
2.超星作业:中等
3.PTA练习:正则:中等,图形类设计:偏难
二.设计与分析
- PTA题目集04 7-2 点线形系列2-线的计算
用户输入一组选项和数据,进行与直线有关的计算。选项包括:
1:输入两点坐标,计算斜率,若线条垂直于X轴,输出"Slope does not exist"。
2:输入三个点坐标,输出第一个点与另外两点连线的垂直距离。
3:输入三个点坐标,判断三个点是否在一条线上,输出true或者false。
4:输入四个点坐标,判断前两个点所构成的直线与后两点构成的直线是否平行,输出true或者false.
5:输入四个点坐标,计算输出前两个点所构成的直线与后两点构成的直线的交点坐标,x、y坐标之间以英文分隔",",并输出交叉点是否在两条线段之内(不含四个端点)的判断结果(true/false),判断结果与坐标之间以一个英文空格分隔。若两条线平行,没有交叉点,则输出"is parallel lines,have no intersection point"。
题目源码:
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); String s = input.nextLine(); String [] str= s.split(":"); int n = str.length; if(n==2) { int a = Integer.parseInt(str[0]); if(a==1) { if(count(str[1])==2) { String [] str1= str[1].split(" "); boolean a1 = process(str1[0]); boolean a2 = process(str1[1]); if(a1&&a2) { if(norepeat(str[1])) { String [] str11= str1[0].split(","); String [] str22= str1[1].split(","); double x1 = Double.parseDouble(str11[0]); double y1 = Double.parseDouble(str11[1]); double x2 = Double.parseDouble(str22[0]); double y2 = Double.parseDouble(str22[1]); if(x1==x2) { System.out.println("Slope does not exist"); } else { double m ; if(y1==y2) { m = 0; } else { m = ((y1-y2)/(x1-x2)); } System.out.println(m); } } else { System.out.println("points coincide"); } } else { System.out.println("Wrong Format"); } } else { int l = count(str[1]); String [] str1= str[1].split(" "); boolean b; int z = 0; for(int i=0;i<l;i++) { b = process(str1[i]); if(!b) { z++; break; } } if(z==0) { System.out.println("wrong number of points"); } else { System.out.println("Wrong Format"); } } } else if(a==2) { if(count(str[1])==3) { String [] str1= str[1].split(" "); boolean a1 = process(str1[0]); boolean a2 = process(str1[1]); boolean a3 = process(str1[2]); if(a1&&a2&&a3) { if(norepeat(str[1])) { String [] str11= str1[0].split(","); String [] str22= str1[1].split(","); String [] str33= str1[2].split(","); double x1 = Double.parseDouble(str11[0]); double y1 = Double.parseDouble(str11[1]); double x2 = Double.parseDouble(str22[0]); double y2 = Double.parseDouble(str22[1]); double x3 = Double.parseDouble(str33[0]); double y3 = Double.parseDouble(str33[1]); double distance = (distance(x1,y1,x2,y2,x3,y3)); System.out.println(distance); } else { System.out.println("points coincide"); } } else { System.out.println("Wrong Format"); } } else { int l = count(str[1]); String [] str1= str[1].split(" "); boolean b; int z = 0; for(int i=0;i<l;i++) { b = process(str1[i]); if(!b) { z++; break; } } if(z==0) { System.out.println("wrong number of points"); } else { System.out.println("Wrong Format"); } } } else if(a==3) { if(count(str[1])==3) { String [] str1= str[1].split(" "); boolean a1 = process(str1[0]); boolean a2 = process(str1[1]); boolean a3 = process(str1[2]); if(a1&&a2&&a3) { if(norepeat(str[1])) { String [] str11= str1[0].split(","); String [] str22= str1[1].split(","); String [] str33= str1[2].split(","); double x1 = Double.parseDouble(str11[0]); double y1 = Double.parseDouble(str11[1]); double x2 = Double.parseDouble(str22[0]); double y2 = Double.parseDouble(str22[1]); double x3 = Double.parseDouble(str33[0]); double y3 = Double.parseDouble(str33[1]); double distance = (distance(x1,y1,x2,y2,x3,y3)); if(distance==0.0) { System.out.println("true"); } else { System.out.println("false"); } } else { System.out.println("points coincide"); } } else { System.out.println("Wrong Format"); } } else { int l = count(str[1]); String [] str1= str[1].split(" "); boolean b; int z = 0; for(int i=0;i<l;i++) { b = process(str1[i]); if(!b) { z++; break; } } if(z==0) { System.out.println("wrong number of points"); } else { System.out.println("Wrong Format"); } } } else if(a==4) { if(count(str[1])==4) { String [] str1= str[1].split(" "); boolean a1 = process(str1[0]); boolean a2 = process(str1[1]); boolean a3 = process(str1[2]); boolean a4 = process(str1[3]); if(a1&&a2&&a3&&a4) { if(norepeat(str[1])) { String [] str11= str1[0].split(","); String [] str22= str1[1].split(","); String [] str33= str1[2].split(","); String [] str44= str1[3].split(","); double x1 = Double.parseDouble(str11[0]); double y1 = Double.parseDouble(str11[1]); double x2 = Double.parseDouble(str22[0]); double y2 = Double.parseDouble(str22[1]); double x3 = Double.parseDouble(str33[0]); double y3 = Double.parseDouble(str33[1]); double x4 = Double.parseDouble(str44[0]); double y4 = Double.parseDouble(str44[1]); boolean p = parallel(x1,y1,x2,y2,x3,y3,x4,y4); if(p) { System.out.println("true"); } else { System.out.println("false"); } } else { System.out.println("points coincide"); } } else { System.out.println("Wrong Format"); } } else { int l = count(str[1]); String [] str1= str[1].split(" "); boolean b; int z = 0; for(int i=0;i<l;i++) { b = process(str1[i]); if(!b) { z++; break; } } if(z==0) { System.out.println("wrong number of points"); } else { System.out.println("Wrong Format"); } } } else if(a==5) { if(count(str[1])==4) { String [] str1= str[1].split(" "); boolean a1 = process(str1[0]); boolean a2 = process(str1[1]); boolean a3 = process(str1[2]); boolean a4 = process(str1[3]); if(a1&&a2&&a3&&a4) { if(norepeat(str[1])) { String [] str11= str1[0].split(","); String [] str22= str1[1].split(","); String [] str33= str1[2].split(","); String [] str44= str1[3].split(","); double x1 = Double.parseDouble(str11[0]); double y1 = Double.parseDouble(str11[1]); double x2 = Double.parseDouble(str22[0]); double y2 = Double.parseDouble(str22[1]); double x3 = Double.parseDouble(str33[0]); double y3 = Double.parseDouble(str33[1]); double x4 = Double.parseDouble(str44[0]); double y4 = Double.parseDouble(str44[1]); boolean p = parallel(x1,y1,x2,y2,x3,y3,x4,y4); if(p) { System.out.println("is parallel lines,have no intersection point"); } else { double x; double y; double k1,k2; if(x1==x2&&x3!=x4) { k2 = (y3-y4)/(x3-x4); y = k2*(x1-x3)+y4; x = x1; } else if(x1!=x2&&x3==x4) { k1 = (y1-y2)/(x1-x2); y = k1*(x3-x1)+y1; x = x3; } else { k1 = (y1-y2)/(x1-x2); k2 = (y3-y4)/(x3-x4); x = -(y1-y3+k1*x1-k2*x3)/(k1-k2); y = k1*(x-x1)+y1; } boolean q; if((x>=x1&&x<=x2)||(x<=x1&&x>=x2)||(x>=x3&&x<=x4)||(x<=x3&&x>=x4)) { q = true; } else { q = false; } System.out.println(x+","+y+" "+q); } } else { System.out.println("points coincide"); } } else { System.out.println("Wrong Format"); } } else { int l = count(str[1]); String [] str1= str[1].split(" "); boolean b; int z = 0; for(int i=0;i<l;i++) { b = process(str1[i]); if(!b) { z++; break; } } if(z==0) { System.out.println("wrong number of points"); } else { System.out.println("Wrong Format"); } } } else { System.out.println("Wrong Format"); } } else { System.out.println("Wrong Format"); } } public static boolean parallel(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { double k1; double k2; if((x1==x2&&x3==x4&&x1!=x3)||(y1==y2&&y3==y4&&y1!=y3)) { return true; } else if((x1==x2&&x3!=x4)||(x1!=x2&&x3==x4)||(y1==y2&&y3!=y4)||(y1!=y2&&y3==y4)) { return false; } else if((x1==x2&&x3==x4&&x1==x3)||(y1==y2&&y3==y4&&y1==y3)) { return true; } else { k1 = (y1-y2)/(x1-x2); k2 = (y3-y4)/(x3-x4); if(y3==k1*(x3-x1)+y1&&y4==k1*(x4-x1)+y1) { return false; } else { if(k1==k2) { return true; } else { return false; } } } } public static double distance(double x1,double y1,double x2,double y2,double x3,double y3) { double distance; if(x2==x3) { distance = Math.abs(x1-x2); } else if(y2==y3) { distance = Math.abs(y1-y2); } else { double k = (y2-y3)/(x2-x3); distance = Math.abs((k*(x1-x2)+y2-y1)/(Math.sqrt(k*k+1))); } return distance; } public static boolean process (String s) { String [] str= s.split(","); int n = str.length; if(n==2){ boolean b1 = deepprocess(str[0]); boolean b2 = deepprocess(str[1]); if(b1&&b2) { return true; } else { return false; } } else { return false; } } public static int count (String s) { String [] str= s.split(" "); int b = str.length; return b; } public static boolean deepprocess(String s) { int a=0; char m; int z = 0; for(int i=0;i<s.length();i++) { m = s.charAt(i); if(m=='+'||m=='-') { a++; } } String []str = s.split("\\."); int k = str.length; int n = 0; for(int i=0;i<s.length();i++) { m = s.charAt(i); if(m=='.') { n++; } } if(a==0) { m = s.charAt(0); if(m=='0'&&s.length()>1) { z++; } } if(a==1) { m = s.charAt(1); if(m=='0'&&s.length()>2) { z++; } } if(a<=1&&((n!=0&&k==2)||(n==0&&k==1))) { double x = Double.parseDouble(s); if(z!=0) { if(n!=0&&x!=0) { return true; } return false; } return true; } else { return false; } } public static boolean norepeat(String s) { String [] str= s.split(" "); int count = 0; double x1,y1,x2,y2; for(int i=0;i<str.length;i++) { String [] str1= str[i].split(","); x1 = Double.parseDouble(str1[0]); y1 = Double.parseDouble(str1[1]); for(int j=i+1;j<str.length;j++) { String [] str2= str[j].split(","); x2 = Double.parseDouble(str2[0]); y2 = Double.parseDouble(str2[1]); if(x1==x2&&y1==y2) { count++; } } } if(count==0) { return true; } else { return false; } } }
分析:首先是要对输入的数据的处理,需要用到split函数进行分割点,我认为输入格式的判断也有很多个点,eg:++7,0.00等很多种错误格式,我没想到一种比较好的方法去判断所有种类,只能一个个去,分割出坐标后,对各个选项的判断也有很多细节,比如斜率是否存在的问题就需要另外处理,以及对题目的理解的问题,第五个选项需要自己画图才能理解
心得:不应该一看到题目就直接开始打代码,而是应该确认好各种情况,在纸上写出具体的思路,进行补充,慢慢调整到自己认为可以的时候在进行代码的实现,可以避免很多错误和细节上的报错。
- PTA题目集05 7-3正则表达式训练-验证码校验
接受给定的字符串,判断该字符串是否属于验证码。验证码是由四位数字或者字母(包含大小写)组成的字符串。
题目源码:
1 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 Scanner input = new Scanner(System.in); 8 String s = input.nextLine(); 9 String str = "[A-Za-z0-9]{4}"; 10 if(s.matches(str)) { 11 System.out.println(s+"属于验证码"); 12 } 13 else { 14 System.out.println(s+"不属于验证码"); 15 } 16 } 17 }
分析:因为是没有接触过的知识,所以就查了资料自学,只需要跟正则的格式对应就行了
心得:正则真的很好用,可以方便很多
- 超星中链表类练习题目
1 public class DoubleLinkedList<E> implements DoubleLinkedListImpl<E> { 2 private Node<E> head;// 头结点(非第一个节点),当前节点,尾节点 3 private Node<E> curr; 4 private Node<E> tail; 5 6 private int size = 0; 7 8 public DoubleLinkedList() { 9 super(); 10 // TODO Auto-generated constructor stub 11 head = new Node<E>(); 12 head.setNext(null); 13 head.setPrevious(null); 14 curr = tail = null; 15 this.size = 0; 16 } 17 18 @Override 19 public boolean isEmpty() { 20 // TODO Auto-generated method stub 21 return this.size == 0; 22 } 23 24 @Override 25 public int getSize() { 26 // TODO Auto-generated method stub 27 return this.size; 28 } 29 30 @Override 31 public E get(int index) { 32 // TODO Auto-generated method stub 33 if(index < 1 || index > this.size) { 34 return null; 35 } 36 37 curr = head; 38 for(int i = 0; i < index; i ++) { 39 curr = curr.getNext(); 40 } 41 return curr.getData(); 42 } 43 44 @Override 45 public void remove() { 46 // TODO Auto-generated method stub 47 if(size==0) { 48 return ; 49 } 50 if(size==1) { 51 head = null; 52 } 53 else { 54 tail = tail.getPrevious(); 55 tail.setNext(null); 56 } 57 size--; 58 } 59 60 @Override 61 public void remove(int index) { 62 // TODO Auto-generated method stub 63 if(index < 1 || index > this.size) { 64 return ; 65 } 66 67 curr = head; 68 69 if(index == 1) { 70 curr = head.getNext(); 71 head.setNext(curr.getNext()); 72 curr.getNext().setPrevious(head); 73 }else {//找到第index - 1个节点赋给curr 74 for(int i = 1;i < index; i++) { 75 curr = curr.getNext(); 76 } 77 78 curr.setNext(curr.getNext().getNext()); 79 curr.getNext().setPrevious(curr); 80 } 81 82 if(index == this.size) {//如果删除的是最后一个节点 83 tail = curr; 84 curr.setNext(null); 85 } 86 87 this.size --;//整个链表的节点数量-1 88 89 } 90 91 @Override 92 public void add(int index, E theElement) { 93 // TODO Auto-generated method stub 94 if(index < 1 || index > this.size+1) { 95 return ; 96 } 97 98 Node<E> curr = new Node<>(); 99 curr.setData(theElement); 100 curr.setNext(null); 101 curr.setPrevious(null); 102 if(this.size == 0) { 103 head.setNext(curr); 104 curr.setPrevious(head); 105 tail = curr; 106 }else if(index == this.size + 1) { 107 this.tail.setNext(curr); 108 curr.setPrevious(tail); 109 tail = curr; 110 }else { 111 Node<E> tempNode = head; 112 for(int i = 1; i < index;i++) { 113 tempNode = tempNode.getNext(); 114 } 115 116 tempNode.getNext().setPrevious(curr); 117 curr.setNext(tempNode.getNext()); 118 curr.setPrevious(tempNode); 119 tempNode.setNext(curr); 120 } 121 122 this.size ++; 123 124 } 125 126 @Override 127 public void add(E element) { 128 // TODO Auto-generated method stub 129 this.add(this.size + 1,element); 130 131 } 132 133 @Override 134 public void printList() { 135 // TODO Auto-generated method stub 136 curr = head.getNext(); 137 for(int i = 1; i <= this.size;i ++) { 138 System.out.print(curr.getData() + " "); 139 curr = curr.getNext(); 140 } 141 142 System.out.println(""); 143 144 } 145 146 public E getLast() { 147 if(this.size != 0) { 148 return tail.getData(); 149 } 150 151 return null; 152 } 153 154 @Override 155 public E getFirst() { 156 // TODO Auto-generated method stub 157 if(this.size != 0) { 158 return head.getNext().getData(); 159 } 160 return null; 161 } 162 163 public void oppositeprint() { 164 curr = tail; 165 for(int i = size-1; i >= 0;i --) { 166 System.out.print(curr.getData() + " "); 167 curr = curr.getPrevious(); 168 } 169 } 170 } 171 172 public interface DoubleLinkedListImpl<E> { 173 174 public boolean isEmpty(); 175 176 public int getSize(); 177 178 public E get(int index); 179 180 public void remove(); 181 182 public void remove(int index); 183 184 public void add(int index, E theElement); 185 186 public void add(E element); 187 188 public void printList(); 189 190 public E getFirst(); 191 192 public E getLast(); 193 } 194 195 public class Main { 196 197 public static void main(String[] args) { 198 // TODO Auto-generated method stub 199 DoubleLinkedList<Integer> list = new DoubleLinkedList<>(); 200 201 list.add(2); 202 list.add(4); 203 list.add(6); 204 list.add(8); 205 list.printList(); 206 list.remove(3); 207 System.out.println(list.getFirst()); 208 System.out.println(list.getLast()); 209 list.printList(); 210 list.add(2,100); 211 list.printList(); 212 System.out.println(list.get(4)); 213 list.oppositeprint();//从后到前输出 214 } 215 216 } 217 public class Node<E> { 218 private E data; 219 private Node<E> next; 220 private Node<E> previous; 221 222 public Node() { 223 224 } 225 226 public Node(E data, Node<E> next, Node<E> previous ) { 227 super(); 228 this.data = data; 229 this.next = next; 230 this.previous = previous; 231 } 232 233 public E getData() { 234 return data; 235 } 236 237 public void setData(E data) { 238 this.data = data; 239 } 240 241 public Node<E> getNext() { 242 return next; 243 } 244 245 public Node<E> getPrevious(){ 246 return previous; 247 } 248 249 public void setNext(Node<E> next) { 250 this.next = next; 251 } 252 253 public void setPrevious(Node<E> previous) {//从后到前输出 254 this.previous = previous; 255 } 256 257
分析:双链表在单链表的基础上增加一条链,相当于一个反方向的单链表
心得:双链表可以更快速的更方便的存储寻找数据,以后可以多运用
- PTA期中考试
- 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)方法。设计类图如下图所示。

1 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 Scanner input = new Scanner(System.in); 8 double x1 = input.nextDouble(); 9 double y1 = input.nextDouble(); 10 double x2 = input.nextDouble(); 11 double y2 = input.nextDouble(); 12 input.nextLine(); 13 String s = input.nextLine(); 14 if(x1>0&&x1<=200&&x2>0&&x2<=200&&y1>0&&y1<=200&&y2>0&&y2<=200) { 15 Line line = new Line(); 16 Point point1 = new Point (x1,y1); 17 Point point2 = new Point (x2,y2); 18 line.setPoint1(point1); 19 line.setPoint2(point2); 20 line.setColor(s); 21 line.display(); 22 } 23 else { 24 System.out.println("Wrong Format"); 25 } 26 } 27 28 29 } 30 31 class Point{ 32 private double x; 33 private double y; 34 35 public Point() { 36 37 } 38 39 public Point(double x,double y) { 40 this.x = x; 41 this.y = y; 42 } 43 44 public double getX() { 45 return x; 46 } 47 48 public void setX(double x) { 49 this.x = x; 50 } 51 52 public double getY() { 53 return y; 54 } 55 56 public void setY(double y) { 57 this.y = y; 58 } 59 60 public void display() { 61 String str1 = String.format("%.2f", x); 62 String str2 = String.format("%.2f", y); 63 System.out.println("("+str1+","+str2+")"); 64 } 65 } 66 67 class Line{ 68 private Point point1; 69 private Point point2; 70 private String color; 71 72 public Line() { 73 74 } 75 76 public Line(Point point1,Point point2,String color) { 77 this.point1 = point1; 78 this.point2 = point2; 79 char []arr = color.toCharArray(); 80 this.color = new String(arr); 81 } 82 83 public Point getPoint1() { 84 return point1; 85 } 86 87 public void setPoint1(Point point1) { 88 this.point1 = point1; 89 } 90 91 public Point getPoint2() { 92 return point2; 93 } 94 95 public void setPoint2(Point point2) { 96 this.point2 = point2; 97 } 98 99 public String getColor() { 100 return color; 101 } 102 103 public void setColor(String color) { 104 char []arr = color.toCharArray(); 105 this.color = new String(arr); 106 } 107 108 public double getDistance() { 109 double distance = Math.sqrt((point1.getX()-point2.getX())*(point1.getX()-point2.getX())+(point1.getY()-point2.getY())*(point1.getY()-point2.getY())); 110 return distance; 111 } 112 113 public void display() { 114 String str = String.format("%.2f", this.getDistance()); 115 System.out.println("The line's color is:"+color); 116 System.out.println("The line's begin point's Coordinate is:"); 117 point1.display(); 118 System.out.println("The line's end point's Coordinate is:"); 119 point2.display(); 120 System.out.println("The line's length is:"+str); 121 122 } 123 }
分析:就是根据类图的要求进行代码的实现
心得:更好的使用类
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();类结构如下图所示。

1 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 Scanner input = new Scanner(System.in); 8 double x1 = input.nextDouble(); 9 double y1 = input.nextDouble(); 10 double x2 = input.nextDouble(); 11 double y2 = input.nextDouble(); 12 input.nextLine(); 13 String s = input.nextLine(); 14 Point p1 = new Point(); 15 Point p2 = new Point(); 16 Line line = new Line(); 17 Plane plane = new Plane(); 18 if(x1>0&&x1<=200&&x2>0&&x2<=200&&y1>0&&y1<=200&&y2>0&&y2<=200) { 19 p1.setX(x1); 20 p1.setY(y1); 21 p2.setX(x2); 22 p2.setY(y2); 23 line.setPoint1(p1); 24 line.setPoint2(p2); 25 line.setColor(s); 26 plane.setColor(s); 27 Element element ; 28 element = p1;//起点Point 29 element.display(); 30 31 element = p2;//终点Point 32 element.display(); 33 34 element = line;//线段 35 element.display(); 36 37 element = plane;//面 38 element.display(); 39 } 40 else { 41 System.out.println("Wrong Format"); 42 } 43 44 } 45 46 } 47 abstract class Element{ 48 public abstract void display(); 49 } 50 51 class Line extends Element{ 52 private Point point1; 53 private Point point2; 54 private String color; 55 56 public Line() { 57 } 58 59 public Line(Point point1,Point point2,String color) { 60 this.point1 = point1; 61 this.point2 = point2; 62 char []arr = color.toCharArray(); 63 this.color = new String(arr); 64 } 65 66 public Point getPoint1() { 67 return point1; 68 } 69 70 public void setPoint1(Point point1) { 71 this.point1 = point1; 72 } 73 74 public Point getPoint2() { 75 return point2; 76 } 77 78 public void setPoint2(Point point2) { 79 this.point2 = point2; 80 } 81 82 public String getColor() { 83 return color; 84 } 85 86 public void setColor(String color) { 87 char []arr = color.toCharArray(); 88 this.color = new String(arr); 89 } 90 91 public double getDistance() { 92 float x =((float)point1.getX()-(float)point2.getX()); 93 float y =((float)point1.getY()-(float)point2.getY()); 94 double distance = Math.sqrt(x*x+y*y); 95 return distance; 96 } 97 98 public void display() { 99 String str = String.format("%.2f", this.getDistance()); 100 System.out.println("The line's color is:"+color); 101 System.out.println("The line's begin point's Coordinate is:"); 102 point1.display(); 103 System.out.println("The line's end point's Coordinate is:"); 104 point2.display(); 105 System.out.println("The line's length is:"+str); 106 107 } 108 } 109 110 class Plane extends Element{ 111 private String color; 112 113 public Plane() { 114 115 } 116 117 public Plane(String color) { 118 char []arr = color.toCharArray(); 119 this.color = new String(arr); 120 } 121 122 public String getColor() { 123 return color; 124 } 125 126 public void setColor(String color) { 127 char []arr = color.toCharArray(); 128 this.color = new String(arr); 129 } 130 131 public void display() { 132 System.out.println("The Plane's color is:"+color); 133 } 134 135 } 136 137 class Point extends Element{ 138 private double x; 139 private double y; 140 141 public Point() { 142 143 } 144 145 public Point(double x,double y) { 146 this.x = x; 147 this.y = y; 148 } 149 150 public double getX() { 151 return x; 152 } 153 154 public void setX(double x) { 155 this.x = x; 156 } 157 158 public double getY() { 159 return y; 160 } 161 162 public void setY(double y) { 163 this.y = y; 164 } 165 166 public void display() { 167 String str1 = String.format("%.2f", x); 168 String str2 = String.format("%.2f", y); 169 System.out.println("("+str1+","+str2+")"); 170 } 171 } 172 173 174 175 176 177 178 179
分析:在第一题的基础上,进行继承和多态,考验的是继承的一个使用,再根据类图进行一个修改,并不算难
心得:更清楚的认识到类,继承,父类,子类等的一个使用
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()方法进行输出。
类图如下所示:

1 import java.util.ArrayList; 2 import java.util.Scanner; 3 4 import homework.E; 5 import homework.LList; 6 7 public class Main { 8 9 public static void main(String[] args) { 10 // TODO Auto-generated method stub 11 Scanner input = new Scanner(System.in); 12 GeometryObject geometryObject = new GeometryObject(); 13 int choice = input.nextInt(); 14 while(choice !=0) { 15 switch(choice) { 16 case 1://insert Point object into list 17 double x1 = input.nextDouble(); 18 double y1 = input.nextDouble(); 19 Point point1 = new Point (x1,y1); 20 geometryObject.add(point1); 21 break; 22 case 2://insert Line object into list 23 x1 = input.nextDouble(); 24 y1 = input.nextDouble(); 25 double x2 = input.nextDouble(); 26 double y2 = input.nextDouble(); 27 input.nextLine(); 28 String s = input.nextLine(); 29 Line line = new Line(); 30 point1 = new Point (x1,y1); 31 Point point2 = new Point (x2,y2); 32 line.setPoint1(point1); 33 line.setPoint2(point2); 34 line.setColor(s); 35 geometryObject.add(line); 36 break; 37 case 3://insert Plane object into list 38 Plane plane = new Plane(); 39 input.nextLine(); 40 s = input.nextLine(); 41 plane.setColor(s); 42 geometryObject.add(plane); 43 break; 44 case 4://delete index - 1 object from list 45 int index = input.nextInt(); 46 geometryObject.remove(index); 47 break; 48 } 49 choice = input.nextInt(); 50 } 51 ArrayList<Element> list = geometryObject.getList(); 52 for(Element element:list){ 53 element.display(); 54 } 55 } 56 } 57 58 59 60 61 public abstract class Element{ 62 public abstract void display(); 63 } 64 import java.util.ArrayList; 65 66 class GeometryObject { 67 ArrayList<Element> list = new ArrayList<>(); 68 69 public GeometryObject() { 70 71 } 72 73 public void add(Element element) { 74 list.add(element); 75 } 76 77 public void remove(int index){ 78 for(int i=0;i<list.size();i++){ 79 if(i==index-1){ 80 list.remove(i); 81 break; 82 } 83 } 84 } 85 86 87 public ArrayList<Element> getList(){ 88 return list; 89 } 90 } 91 92 class Line extends Element{ 93 private Point point1; 94 private Point point2; 95 private String color; 96 97 public Line() { 98 } 99 100 public Line(Point point1,Point point2,String color) { 101 this.point1 = point1; 102 this.point2 = point2; 103 char []arr = color.toCharArray(); 104 this.color = new String(arr); 105 } 106 107 public Point getPoint1() { 108 return point1; 109 } 110 111 public void setPoint1(Point point1) { 112 this.point1 = point1; 113 } 114 115 public Point getPoint2() { 116 return point2; 117 } 118 119 public void setPoint2(Point point2) { 120 this.point2 = point2; 121 } 122 123 public String getColor() { 124 return color; 125 } 126 127 public void setColor(String color) { 128 char []arr = color.toCharArray(); 129 this.color = new String(arr); 130 } 131 132 public double getDistance() { 133 float x =((float)point1.getX()-(float)point2.getX()); 134 float y =((float)point1.getY()-(float)point2.getY()); 135 double distance = Math.sqrt(x*x+y*y); 136 return distance; 137 } 138 139 public void display() { 140 String str = String.format("%.2f", this.getDistance()); 141 System.out.println("The line's color is:"+color); 142 System.out.println("The line's begin point's Coordinate is:"); 143 point1.display(); 144 System.out.println("The line's end point's Coordinate is:"); 145 point2.display(); 146 System.out.println("The line's length is:"+str); 147 148 } 149 } 150 151 class Plane extends Element{ 152 private String color; 153 154 public Plane() { 155 156 } 157 158 public Plane(String color) { 159 char []arr = color.toCharArray(); 160 this.color = new String(arr); 161 } 162 163 public String getColor() { 164 return color; 165 } 166 167 public void setColor(String color) { 168 char []arr = color.toCharArray(); 169 this.color = new String(arr); 170 } 171 172 public void display() { 173 System.out.println("The Plane's color is:"+color); 174 } 175 176 } 177 class Point extends Element{ 178 private double x; 179 private double y; 180 181 public Point() { 182 183 } 184 185 public Point(double x,double y) { 186 this.x = x; 187 this.y = y; 188 } 189 190 public double getX() { 191 return x; 192 } 193 194 public void setX(double x) { 195 this.x = x; 196 } 197 198 public double getY() { 199 return y; 200 } 201 202 public void setY(double y) { 203 this.y = y; 204 } 205 206 public void display() { 207 String str1 = String.format("%.2f", x); 208 String str2 = String.format("%.2f", y); 209 System.out.println("("+str1+","+str2+")"); 210 } 211 } 212 213
分析:在第二题的基础上,运用了容器类,并且增加了泛型
心得:学会了怎么运用动态数组,要注意容器尺寸的大小进行删除
三.踩坑心得
- 超星中链表类练习题
在每个模块种无论删除还是增加都应该对pre指针进行处理和判断,在之前有一个函数忘记对pre进行处理,导致逆输出时,原本该删除的数据却没有被删除,对代码进行查看发现pre并没有处理
- PTA题目集04 7-2 点线形系列2-线的计算
一开始并没有在纸上做好类图,直接就上手实现代码,最后提交代码发现有很多的bug,很多格式错误并没有检查出来,并且算斜率的时候,并没有考虑斜率是否存在的情况,很多代码都很复杂,并没有做到简洁明了,自己也得看很久,类名也应该用易懂的英文名,不可以用拼音。
- 期中7-3 点线面问题再重构(容器类)
写好每个类后,在Main函数中直接使用数组,忘记使用容器类,导致增删数据失败,并不是使用自己写的方法进行增删,而是使用数组自带的函数add和remove,所以耗费了很多时间在增删上找错误,最后发现自己没有用GeometryObject,修改后将GeometryObject 中的Element元素传给数组进行输出,中间还是有很多盲区。
四.改进建议
期中考试7-3 点线面问题再重构(容器类)
可以在每次输入一组数据后,判断这组输入的数据是否合理,不合理时提示,请重新输入,而不是在全部数据输入完后才输出相应的结果和不合理的提示。
五.总结
- 通过本阶段7-10周的学习自己的收获
熟练了基本的数据类型,for,if,等基础知识
了解到正则表达式,很方便,会继续加深了解和使用
可以基础的使用对象和类、类的继承、多态、异常处理、抽象类以及接口这些知识
- 通过本阶段7-10周的学习发现自己在知识掌握方面存在的不足
ArrayList的基本知识并没有很好的掌握,后续需要对这方面加强
要学会自己根据题目的需求先做好类图,再进行代码的实现
- 通过本阶段7-10周的学习对教师、课程、作业、实验、课上及课下组织方式等方面提出的改进建议及意见
希望老师上课前可以提前说一下讲课的内容,我们可以提前预习一下,有的时候是真的听不懂
实验方面希望老师可以有反馈,每次都不知道自己做的对不对

浙公网安备 33010602011771号