Blog2

1.前言:

(1)题量,难度

    这几次作业题量比较适中,不会过多,但是也不会很少,能起到让我们练习的效果。

    pta上布置了一次大作业,一共三道题。第一题第三题难度不大,但是第二题是点线形系列的题,也是我之前得分不高的pta作业系列的,这道题占了70分,而我只拿到了39分。

    期中考虽然有三道题,但是难度不算太大,每一道题都是在前一道题的基础上稍加修改即可。就是由于自己写题目的时候没有好好把握时间,慢慢悠悠导致第三题没有写完考试就结束了。

    实验一的话是只有两道题,一道是自己写程序,一道是修改已给出的代码。个人感觉第二题比第一道题要稍微难一点,因为要先理解已给出的代码还要在此基础上修改,对于刚学不久java的我来说,给别人代码改错属实是有点难改了。但是总体题目难度不会像作业一样大,都能完整地做完题目,给的时间也充裕。

    实验二也只有两道题,第二道题是在第一题的基础上修改的,虽然看上去很复杂,但是实际上题目已经给出了整体框架,只需要自己写实际功能代码进去即可。

(2)知识点

    pta作业涉及到正则表达式的运用,类的设计及其方法的调用。

    期中考涉及到继承与多态和容器类的问题,关于点线的简单处理。

    实验一涉及到类的构造方法、方法的调用、参数传递、对象的构造与使用,循环结构、控制结构。

    实验二涉及到方法重载,private、protected、public等关键的使用场合与使用方法、方法调用时引用类型参数的传递过程。

2.设计与分析

(1)大作业四

7-2 点线形系列4-凸四边形的计算

核心代码如下:

  1 class Functions{
  2 
  3     public int Quad(Point[] points) {
  4         if(!pointSame(points)) {
  5             return 1;
  6         }
  7         else {
  8             Line[] lines = new Line[4];
  9             for(int i = 0;i<3;i++) {
 10                 lines[i] = new Line(points[i],points[i+1]);
 11             }
 12             lines[3] = new Line(points[3],points[0]);
 13 
 14             if(lines[0].parallel(lines[1]) || lines[0].parallel(lines[3]) || lines[2].parallel(lines[1]) || lines[2].parallel(lines[3])) {
 15                 return 2;
 16             }
 17             else if(lines[0].intersection(lines[1]).equals(points[1]) && lines[1].intersection(lines[2]).equals(points[2]) && lines[2].intersection(lines[3]).equals(points[3]) && lines[3].intersection(lines[0]).equals(points[0])) {
 18                 if(lines[0].parallel(lines[2]) && lines[1].parallel(lines[3])) {
 19                     return 3;
 20                 }
 21                 else {
 22                     return 4;
 23                 }
 24             }
 25             else {
 26                 return 5;
 27             }    
 28         }
 29     }
 30 
 31     public void Quad(int a) {
 32         switch(a) {
 33         case 1:System.out.println("points coincide");break;
 34         case 2:System.out.println("false false");break;
 35         case 3:System.out.println("true true");break;
 36         case 4:System.out.println("true false");break;
 37         case 5:System.out.println("false,false");break;
 38         }
 39     }
 40 
 41     public void Speuad(Point[] points) {
 42         if(!pointSame(points)) {
 43             System.out.println("points coincide");
 44         }
 45         else {
 46             if(Quad(points) == 3 || Quad(points) == 4) {
 47                 Line[] lines = new Line[4];
 48                 for(int i = 0;i<3;i++) {
 49                     lines[i] = new Line(points[i],points[i+1]);
 50                 }
 51                 lines[3] = new Line(points[3],points[0]);
 52 
 53                 if(lines[0].parallel(lines[2]) && lines[1].parallel(lines[3])) {
 54                     if(lines[0].distance() == lines[1].distance()) {
 55                         if(lines[0].vertical(lines[1])) {
 56                             System.out.println("true true true");
 57                         }
 58                         else {
 59                             System.out.println("true false false");
 60                         }
 61                     }
 62                     else if(lines[0].vertical(lines[1])) {
 63                         System.out.println("false true false");
 64                     }
 65                 }
 66                 else {
 67                     System.out.println("false false false");
 68                 }
 69             }
 70             else {
 71                 System.out.println("not a quadrilateral");
 72             }
 73         }
 74     }
 75 
 76     public void aotu(Point[] points) {
 77         if(!pointSame(points)) {
 78             System.out.println("points coincide");
 79         }
 80 
 81         else {
 82             if(Quad(points) == 3 || Quad(points) == 4) {
 83                 Line[] lines = new Line[4];
 84                 for(int i = 0;i<3;i++) {
 85                     lines[i] = new Line(points[i],points[i+1]);
 86                 }
 87                 lines[3] = new Line(points[3],points[0]);
 88 
 89                 Line line1 = new Line(points[0],points[2]);
 90                 Line line2 = new Line(points[1],points[3]);
 91                 double s1 = area(lines[0],lines[1],line1);
 92                 double s2 = area(lines[2],lines[3],line1);
 93                 double s3 = area(lines[0],lines[3],line2);
 94                 double s4 = area(lines[2],lines[1],line2);
 95                 double c = lines[0].distance()+lines[1].distance()+lines[2].distance()+lines[3].distance();
 96                 float s = 0;
 97                 if(s1 + s2 == s3 + s4) {
 98                     System.out.println("true " + judgmentString(c) + " " + (s1+s2));
 99                 }
100                 else {
101                     if(s1 + s2 < s3 + s4){
102                         s =(float) (s1 + s2);
103                     }
104                     else {
105                         s = (float)(s3 + s4);
106                     }
107 
108                     System.out.println("false " + judgmentString(c) + " " + s);
109                 }
110             }
111             else {
112                 System.out.println("not a quadrilateral");
113             }
114 
115 
116 
117         }
118     }
View Code

 

   SourceMonitor报表:

类图:

    该题格式较为复杂,不同功能输入点的数量不同,故写了多条正则表达式以判断数据输入格式的正确与否。如果格式不正确,则输出"Wrong Format",如果格式正确,但点的数量不对,则输出"wrong number of points",如果都正确则将选项及点的数据通过split方法存入字符数组,再转换为int或double型,然后进入switch语句调用Functions类方法以实现功能。

    本题我设置了四个类,Line、Point类用来定义平面上的点和线,Functions类构造了多个个方法来实现题目五个功能(虽然只实现了三个功能,最后两个方法也没写完),Match类用来匹配输入格式是否合法。

第一个功能:首先调用pointSame方法判断四个点是否有重合点,有则输出"points coincide",否则将四个点转换成四根线,通过parallel方法去判断相邻两条直线是否平行,平行则输出"false false",否则通过再intersection方法计算相邻两条直线交点是否为线段上点。否则输出"false,false",是则再判断不相邻两边是否平行,是则输出"true true",否则输出"true false"。

第二个功能:首先判断点重合,然后判断是否构成四边形,否则输出"not a quadrilateral",然后判断对边是否平行,否则输出"false false false",是则判断任意一组相邻线段长度是否相等,否则输出"false true false",是则通过vertical方法判断任意一组相邻线段互相垂直,是则输出"true true true",否则输出"true false false"。

第三个功能:首先判断点重合,然后判断是否构成四边形,否则输出"not a quadrilateral"。将四个点两两相连构成边的线段数组,再将构成对角线,每条对角线将四边形分为两个三角形,分别计算两条对角线构成的四个三角形的面积,如果第一条对角线构成的三角形面积之和等于第二条对角线构成的三角形面积之和,则是凸四边形,如果第一条对角线构成的三角形面积之和不等于第二条对角线构成的三角形面积之和,则是凹四边形,且四边形面积为两个三角形面积之和小的那一个。

第四个功能和第五个功能未能完整实现。

    Function类中还设计了许多方法,代码如下:

  1 boolean pointSame(Point[] points) {   //判断点重合
  2         for(int i = 0;i<4;i++) {
  3             for(int j = i+1;j<4;j++) {
  4                 if(points[i].x == points[j].x && points[i].y == points[j].y) {
  5                     return false;
  6                 }
  7             }
  8         }
  9         return true;
 10     }
 11 
 12     float area(Line a,Line b,Line c) {   //计算三角形面积
 13         double p = (a.distance() + b.distance() + c.distance())/2;
 14         float s = (float)(Math.sqrt(p*(p-a.distance())*(p-b.distance())*(p-c.distance())));
 15         return s;
 16     }
 17 
 18     public String judgmentString(double c) {   //输出三位小数
 19         String tokenString = String.valueOf(c);
 20         tokenString = tokenString.substring(tokenString.indexOf(".") + 1);
 21         int len = tokenString.length();
 22         len = Math.min(len, 3);
 23         return String.format("%." + len + "f", c);
 24     }
 25 
 26     public boolean Tria(Point[] points) {   //判断三角形
 27         int a = 0,sum = 0;
 28         for(int i = 2;i<6;i++) {
 29             for(int j = i+1;j<6;j++) {
 30                 if(points[i].x == points[j].x && points[i].y == points[j].y) {
 31                     a = i;
 32                     sum++;
 33                 }
 34             }
 35         }
 36         if(sum == 1) {
 37             if(a == 2) {
 38                 if(Tria(points[3],points[4],points[5])) {
 39                     return true;
 40                 }
 41             }
 42             else if(a == 3) {
 43                 if(Tria(points[2],points[4],points[5])) {
 44                     return true;
 45                 }
 46             }
 47             else if(a == 4) {
 48                 if(Tria(points[2],points[3],points[5])) {
 49                     return true;
 50                 }
 51             }
 52             return false;
 53 
 54         }
 55         else if(sum>1) {
 56             return false;
 57         }
 58         else {
 59             Point[] point = new Point[4];
 60             for(int i = 0,j = 2;i<4;i++) {
 61                 point[i] = points[j];
 62                 j++;
 63             }
 64             if(!(Quad(point) == 3 || Quad(point) == 4)) {
 65                 
 66                 if(Tria(points[2],points[4],points[5])) {
 67                     if((points[3].x-points[2].x)*(points[4].x-points[3].x) >=0 && (points[3].y-points[2].y)*(points[4].y-points[3].y) >=0) {
 68                         return true;
 69                     }
 70                 }
 71                 else if(Tria(points[2],points[3],points[5])) {
 72                     if((points[3].x-points[4].x)*(points[4].x-points[5].x) >=0 && (points[3].y-points[4].y)*(points[4].y-points[5].y) >=0) {
 73                         return true;
 74                     }
 75                 }
 76                 else if(Tria(points[4],points[3],points[5])) {
 77                     if((points[3].x-points[2].x)*(points[2].x-points[5].x) >=0 && (points[3].y-points[2].y)*(points[2].y-points[5].y) >=0) {
 78                         return true;
 79                     }
 80                 }
 81                 else if(Tria(points[4],points[3],points[2])) {
 82                     if((points[2].x-points[5].x)*(points[5].x-points[4].x) >=0 && (points[2].y-points[5].y)*(points[5].y-points[4].y) >=0) {
 83                         return true;
 84                     }
 85                 }
 86             }
 87             return false;
 88 
 89         }    
 90     }
 91 
 92     boolean Tria(Point m,Point n,Point k) {
 93         Line a = new Line(m,n);
 94         Line b = new Line(n,k);
 95         Line c = new Line(k,m);
 96         if(a.parallel(c) || a.parallel(b) || b.parallel(c))
 97             return false;
 98         else if(a.intersection(b).equals(n) && b.intersection(c).equals(k) && c.intersection(a).equals(m)) {
 99             return true;
100         }
101         return false;
102     }
View Code

 

    做这个作业一开始写的时候是没考虑每个功能之间是有一定相关性的,比如第一个功能判断是否是平行四边形这个后面的功能基本都需要判断,一开始写的时候就是一个void方法,但是后开那些第二个功能的时候发现可以直接用第一问的方法,就又该第一个功能代码改为带返回值类型的,以方便后面使用。还有其它一些方法,在第一次实现的时候只考虑了本功能的传参,但是没想到后面方法调用的时候可能传参的数据就会有些问题,要导致修正代码或者重载方法。

 

(2)期中考

7-1 点与线(类设计)

核心代码如下:

  1 import java.util.Scanner;
  2 
  3 public class test {
  4 
  5     public static void main(String[] args) {
  6         Scanner sc = new Scanner(System.in);
  7         double x1 = Double.parseDouble(sc.next());
  8         double y1 = Double.parseDouble(sc.next());
  9         double x2 = Double.parseDouble(sc.next());
 10         double y2 = Double.parseDouble(sc.next());
 11         String color = sc.next();
 12         Point p1=new Point(x1,y1);    
 13         Point p2=new Point(x2,y2);
 14         Line line = new Line(p1,p2,color);
 15         line.display();
 16         
 17         sc.close();
 18     }
 19 
 20 }
 21 
 22 class Point{
 23     private double x,y;
 24     
 25     Point() {
 26         
 27     }
 28     Point(double x,double y){
 29         if(x>0 && x<=200 && y>0 && y<=200)
 30         {
 31             this.x = x;
 32             this.y = y;
 33         }
 34         else {
 35             System.out.println("Wrong Format");
 36             System.exit(0);
 37         }
 38     }
 39 
 40     public double getX() {
 41         return x;
 42     }
 43 
 44     public void setX(double x) {
 45         this.x = x;
 46     }
 47 
 48     public double getY() {
 49         return y;
 50     }
 51 
 52     public void setY(double y) {
 53         this.y = y;
 54     }
 55 
 56 
 57     public void display() {
 58         String x1 = String.format("%.2f", x);
 59         String y1 = String.format("%.2f", y);
 60         System.out.println("("+x1+","+y1+")");
 61         
 62     }
 63 
 64 }
 65 
 66 
 67 class Line{
 68     private Point point1,point2;
 69     private String color;
 70 
 71     Line(Point point1,Point point2,String color){
 72         this.point1 = point1;
 73         this.point2 = point2;
 74         this.color = color;
 75     }
 76     Line(){
 77         
 78     }
 79     
 80     public Point getPoint1() {
 81         return point1;
 82     }
 83 
 84 
 85     public void setPoint1(Point point1) {
 86         this.point1 = point1;
 87     }
 88 
 89 
 90     public Point getPoint2() {
 91         return point2;
 92     }
 93 
 94 
 95     public void setPoint2(Point point2) {
 96         this.point2 = point2;
 97     }
 98 
 99     public String getColor() {
100         return color;
101     }
102 
103 
104     public void setColor(String color) {
105         this.color = color;
106     }
107 
108     public double getDistance() {
109         double dis = Math.sqrt(Math.pow((point1.getX() - point2.getX()),2) + Math.pow((point1.getY() - point2.getY()),2));
110         return dis;
111         
112     }
113     
114     public void display() {
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         String dis = String.format("%.2f", getDistance());
121         
122         System.out.println("The line's length is:"+dis);
123     }
124     
125     
126 }
View Code

 

SourceMonitor报表:

     本题一共设计了两个类。一个是Point类,一个是Line类。

    Point类用来定义一个点,构造一个有参构造器,里面设置判断条件,如果成立则构造点,如果不成立则输出"Wrong Format"并结束进程。该类中还构造了一个display方法,方便后续输出点坐标。Line类用来定义一条线,构造有参构造器来接收Point类和String类数据,构造getDistance方法来计算线段长度,构造display方法输出结果。

 

7-2 点线面问题重构(继承与多态)

 

核心代码如下:

  1 class GeometryObject{
  2     private ArrayList<Element> list = new ArrayList<>();
  3     
  4     public void add(Element element) {
  5         list.add(element);
  6     }
  7     public void remove(int index) {
  8         if(index < list.size()) {
  9             list.remove(index);
 10         }
 11     }
 12     
 13     public ArrayList<Element> getList(){
 14         return list;
 15     }
 16     
 17 }
 18 
 19 abstract class Element{
 20     
 21     public abstract void display();
 22     
 23 }
 24 
 25 class Point extends Element{
 26     private double x,y;
 27     
 28     Point() {
 29         
 30     }
 31     Point(double x,double y){
 32         if(x>0 && x<=200 && y>0 && y<=200)
 33         {
 34             this.x = x;
 35             this.y = y;
 36         }
 37         else {
 38             System.out.println("Wrong Format");
 39             System.exit(0);
 40         }
 41     }
 42 
 43     public double getX() {
 44         return x;
 45     }
 46 
 47     public void setX(double x) {
 48         this.x = x;
 49     }
 50 
 51     public double getY() {
 52         return y;
 53     }
 54 
 55     public void setY(double y) {
 56         this.y = y;
 57     }
 58 
 59     @Override
 60     public void display() {
 61         String x1 = String.format("%.2f", x);
 62         String y1 = String.format("%.2f", y);
 63         System.out.println("("+x1+","+y1+")");
 64         
 65     }
 66 
 67 }
 68 
 69 class Line extends Element{
 70     
 71     private Point point1,point2;
 72     private String color;
 73 
 74     Line(Point point1,Point point2,String color){
 75         this.point1 = point1;
 76         this.point2 = point2;
 77         this.color = color;
 78     }
 79     Line(){
 80         
 81     }
 82     
 83     public Point getPoint1() {
 84         return point1;
 85     }
 86 
 87 
 88     public void setPoint1(Point point1) {
 89         this.point1 = point1;
 90     }
 91 
 92 
 93     public Point getPoint2() {
 94         return point2;
 95     }
 96 
 97 
 98     public void setPoint2(Point point2) {
 99         this.point2 = point2;
100     }
101 
102     public String getColor() {
103         return color;
104     }
105 
106 
107     public void setColor(String color) {
108         this.color = color;
109     }
110 
111     public double getDistance() {
112         double dis = Math.sqrt(Math.pow((point1.getX() - point2.getX()),2) + Math.pow((point1.getY() - point2.getY()),2));
113         return dis;
114         
115     }
116     
117     @Override
118     public void display() {
119         System.out.println("The line's color is:"+color);
120         System.out.println("The line's begin point's Coordinate is:");
121         point1.display();
122         System.out.println("The line's end point's Coordinate is:");
123         point2.display();
124         String dis = String.format("%.2f", getDistance());
125         
126         System.out.println("The line's length is:"+dis);
127     }
128 }
129 
130 class Plane extends Element{
131     private String color;
132     Plane(){
133         
134     }
135     Plane(String color){
136         this.color = color;
137     }
138     
139     public String getColor() {
140         return color;
141     }
142     public void setColor(String color) {
143         this.color = color;
144     }
145     
146     @Override
147     public void display() {
148         System.out.println("The Plane's color is:"+color);
149     }
150 }
View Code

   SourceMonitor报表:

  

     本题一共设计了四个类。在第一题的基础上,增加一个抽象类Element作为Point、Line、Plane的父类,其中只定义一个抽象方法display。在每个子类中重写display方法以实现不同的输出内容。

    一开始写的时候没看到题干中给出的实现多态特性的那段代码,虽然不影响最后结果的输出。 

 

7-3 点线面问题再重构(容器类)

 

核心代码如下:

  1 abstract class Element{
  2     
  3     public abstract void display();
  4     
  5 }
  6 
  7 class Point extends Element{
  8     private double x,y;
  9     
 10     Point() {
 11         
 12     }
 13     Point(double x,double y){
 14         if(x>0 && x<=200 && y>0 && y<=200)
 15         {
 16             this.x = x;
 17             this.y = y;
 18         }
 19         else {
 20             System.out.println("Wrong Format");
 21             System.exit(0);
 22         }
 23     }
 24 
 25     public double getX() {
 26         return x;
 27     }
 28 
 29     public void setX(double x) {
 30         this.x = x;
 31     }
 32 
 33     public double getY() {
 34         return y;
 35     }
 36 
 37     public void setY(double y) {
 38         this.y = y;
 39     }
 40 
 41     @Override
 42     public void display() {
 43         String x1 = String.format("%.2f", x);
 44         String y1 = String.format("%.2f", y);
 45         System.out.println("("+x1+","+y1+")");
 46         
 47     }
 48 
 49 }
 50 
 51 class Line extends Element{
 52     
 53     private Point point1,point2;
 54     private String color;
 55 
 56     Line(Point point1,Point point2,String color){
 57         this.point1 = point1;
 58         this.point2 = point2;
 59         this.color = color;
 60     }
 61     Line(){
 62         
 63     }
 64     
 65     public Point getPoint1() {
 66         return point1;
 67     }
 68 
 69 
 70     public void setPoint1(Point point1) {
 71         this.point1 = point1;
 72     }
 73 
 74 
 75     public Point getPoint2() {
 76         return point2;
 77     }
 78 
 79 
 80     public void setPoint2(Point point2) {
 81         this.point2 = point2;
 82     }
 83 
 84     public String getColor() {
 85         return color;
 86     }
 87 
 88 
 89     public void setColor(String color) {
 90         this.color = color;
 91     }
 92 
 93     public double getDistance() {
 94         double dis = Math.sqrt(Math.pow((point1.getX() - point2.getX()),2) + Math.pow((point1.getY() - point2.getY()),2));
 95         return dis;
 96         
 97     }
 98     
 99     @Override
100     public void display() {
101         System.out.println("The line's color is:"+color);
102         System.out.println("The line's begin point's Coordinate is:");
103         point1.display();
104         System.out.println("The line's end point's Coordinate is:");
105         point2.display();
106         String dis = String.format("%.2f", getDistance());
107         
108         System.out.println("The line's length is:"+dis);
109     }
110 }
111 
112 class Plane extends Element{
113     private String color;
114     Plane(){
115         
116     }
117     Plane(String color){
118         this.color = color;
119     }
120     
121     public String getColor() {
122         return color;
123     }
124     public void setColor(String color) {
125         this.color = color;
126     }
127     
128     @Override
129     public void display() {
130         System.out.println("The Plane's color is:"+color);
131     }
132 }
View Code

   SourceMonitor报表:

     本题一共设计了五个类。在第二题的基础上,新增加一个容器类GeometryObject,创建一个泛型链表以储存element类型数据。类中创建add方法来添加成员,remove方法来删除指定位置的成员。

    在一开始没意识到容器类这个东西,导致虽然写了这个类,却没有用这个类去声明对象然后调用方法,导致和题目给出的类图不一样,然后快写完的时候才发现错误,所以又花了一些时间去重新写。

 

(3)实验二

农夫过河1

         一个农夫带着一匹狼、一只羊、一颗白菜要过河,河上只有一条船能够渡河,而且农夫每次最多只能带一个动物或物品过河。当农夫不在的时候狼会吃羊,羊会吃白菜。

核心代码如下:

 1 public class Sheep {
 2     
 3     public boolean crossRiver = false;
 4     public boolean isAlive = true;
 5     public boolean hasCross = false;
 6     
 7     public void eatCabbage(Farmer farmer,Cabbage cabbage) {            //吃菜
 8         if(farmer.hasCross != hasCross) {    //判断农夫与自己在不在同一侧
 9             if(this.hasCross == cabbage.hasCross) {     //判断菜与自己在不在同一侧
10                 cabbage.isAlive = false;
11             }
12         }
13     }
14     
15     void letCross(Farmer farmer) {         //过河
16         if(farmer.hasCross == hasCross) {           //判断满不满足过河条件
17             this.hasCross = !(this.hasCross);
18             farmer.letCross();
19         }
20         
21     }
22     
23     
24     public void showStatus() {
25         System.out.println("Sheep is alive  :" + isAlive + "  Sheep has Cross  :" + hasCross);
26     }
27     
28 }
29 
30 //这里只贴了Sheep类的,其他类的也都差不多
View Code

 SourceMonitor报表:

类图:

    该程序首先要判断农夫与所带物品在同一侧,不然无法进行过河操作。然后还要判断那个当狼和羊在同一侧,羊和白菜在同一侧时,农夫是否在,要是农夫不在的话,有一个被吃则游戏结束。由于狼不会被吃,所以狼的存活状态一直都是true。

    当时写的时候没考虑过农夫与所带物品位置还需要判断,所以hasCross这个成员一直没用上。

 

农夫过河2

(1)      对farmer,wolf,sheep,cabbage进行数据封装的改进,将属性隐藏。

(2)      增加Boat(船)类,设计船类的相应方法,并修改main方法中相关的代码,改进农夫带东西过河的功能,解决之前代码中存在的农夫带东西过河的判断问题。

(3)      为wolf,sheep 两个类添加带Stirng name参数的构造函数,当创建wolf,sheep对象时,假设输入了name参数,程序分别输出:

                              啊呜~~~我 *name* 狼又回来了

                              咩咩,我是可爱的小羊*name*

                              当wolf,sheep对象使用showStatus()方法输出状态时,输出内容要带上name的信息,如:

                              Sheep *name* is alive :true   Sheep *name* has Cross : false

核心代码如下:

 1 public class Game {
 2     Wolf wolf;
 3     Sheep sheep;
 4     Cabbage cabbage;
 5     Farmer farmer;
 6     GameGui gui;
 7     Boat boat;
 8     
 9     Game(String wolfName,String sheepName) {
10         wolf = new Wolf(wolfName);
11         sheep = new Sheep(sheepName);
12         cabbage = new Cabbage();
13         farmer = new Farmer();
14         gui = new GameGui();
15         boat = new Boat();
16     }
17     
18     
19     protected void play() {
20         Scanner input = new Scanner(System.in);
21         int choice = 0;            //用户输入选择
22         boolean gameOver=false,//游戏结束标志,默认为false,代表游戏进行中,未结束
23                 win=false;     //游戏输赢标志,默认为false,代表未赢得游戏。
24         while(!gameOver)
25         {
26             gui.menu();
27             choice = input.nextInt();
28             switch(choice)
29             {
30                 case 0: gameOver=true;
31                     break;
32                 case 1:/* 农夫独自过河的处理 */
33                     boat.crossRiver(farmer);
34                     break;
35                 case 2:/* 农夫带狼的处理 */
36                     boat.crossRiver(farmer,wolf);
37                     break;
38                 case 3:/* 农夫带羊的处理 */
39                     boat.crossRiver(farmer,sheep);
40                     break;
41                 case 4:/* 农夫带白菜的处理 */
42                     boat.crossRiver(farmer,cabbage);
43                     break;
44             }
45             wolf.eatSheep(farmer,sheep);//狼吃羊,如果羊不在同一边,则吃不到,如果在同一边,羊被吃
46             sheep.eatCabbage(farmer,cabbage);//同上
47             gui.showStatus(farmer,wolf,sheep,cabbage);
48             if(choice != 0) {
49                 gameOver = isGameOver();
50             }
51         }        
52         win=this.hasWin();
53         if(win) {
54             System.out.println("game over: you win !");
55         }else {
56             System.out.println("game over: you lose !");
57         }
58         input.close();
59 
60     }
61     /*
62      * 判断游戏是否结束
63      * 输入:无
64      * 运算:羊、白菜任一实体被吃,游戏结束,或者狼、羊、白菜均未被吃且全部渡过河,游戏结束
65      * 输出:游戏结束--返回true ,未结束--返回false
66      */
67     public boolean isGameOver() {
68         if(sheep.getisAlive()==false||cabbage.getisAlive()==false) {
69             return true;
70         }            
71         if(wolf.gethasCross()&&sheep.gethasCross()&&cabbage.gethasCross()) {
72             return true;
73         }
74         return false;
75     }
76     /*
77      * 判断游戏是否胜利
78      * 前置条件:游戏结束
79      * 输入:无
80      * 运算:狼、羊、白菜均未被吃且全部渡过河,游戏胜利,否则失败
81      * 输出:游戏胜利--返回true ,失败--返回false
82      */
83     public boolean hasWin() {
84         if(sheep.getisAlive()==false||cabbage.getisAlive()==false) {
85             return false;
86         }            
87         if(wolf.gethasCross()&&sheep.gethasCross()&&cabbage.gethasCross()) {
88             return true;
89         }else {
90             return false;
91         }        
92     }
93     
94 }
View Code

 SourceMonitor报表:

类图:

 

    这个实验主要就是考方法的重载,在船类中crossRiver方法根据每次所传入的参数不同,被调用不同的方法。如何再对其他类稍微修改一下。

3.踩坑心得

    其实这次作业没有什么特别的问题,大部分无非也就是那几样,看题不仔细或者在题目有强制格式要求时,做题的时候写着写着就会按照自己的思维想法写,导致写出来的东西虽然能过测试点,但是和题目格式不符导致返工重新修改,浪费了很多时间。就像这次期中考试第二题有个多态的体现,那个点可能是我没看到,也可能是我看完之后没过脑子,写的时候就忘了,然后还是直接用了Point,Line类声明对象。第三题也是绕了些弯导致返工,最后也没提交上第三题的代码。

4.改进建议

    对于实验二中的一些含有多个相同的成员和方法可以构造一个父类来简化代码。比如:

    在写大作业四时,判断点在多边形内外部时,我是将点的坐标拿去比对,导致结果不对,但是其实换个思路,可以先判断面积,如果点与每条边构成的三角形面积之和等于这个多边形的面积,则在内部或线上,若是不等于则在外部,然后在通过坐标判断是否在线上就可以了。

5.总结

    在七到十周的学习过程中,通过这些实验考试作业进一步巩固了我在课堂上学习到的知识,比如类的声明、创建与使用方法、类的构造方法的定义与使用方法、引用变量与对象实例之间的关系与区别、方法重载的实现方式等等等等,也通过这些补上了我上课就没听太懂以及不太理解的东西,比如期中考试第三题涉及到的容器类。但是对那个构造一个对象数组的构造上还是有点迷糊,还有抽象父类中方法是否需要用抽象方法或者空方法体还没去细致了解这方面的差别,之后也会再去花时间了解了解。

posted @ 2022-05-09 21:59  Ash_vin  阅读(128)  评论(0)    收藏  举报