Blog2

(1)前言:总结前面几次实验·难度,题量

第一次实验:通过设计了几个类分别对不同的对象经行相对应的行为和属性,一开始我还没有想到使用不同类,导致一开始根本无从下手,然后通过设计愈多类后,每一个对象应该做什么都很清楚

第二次实验:通过在原来基础上增加父类,一开始我以为很难以至于我一直拖了好几天,可是到后来发现使用了方法重写和方法重载后事情就变的恒简单了,难度相较于之前来说就比较符合现在我们的学习状态

期中考试:期中考试来说的话由该高兴的地方也有难受的地方,首先开心的事我通过自己把题目卓出来了,但是的话我的效率太低了,室友都写了一大半而我却还在写第一题,这一刻我才清楚我们之间距离有多大,以至于后面我都没有住够时间去完成后面题目

(2)设计分析

期中考试(1)

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        double x1 = in.nextDouble();
        double y1 = in.nextDouble();
        double x2 = in.nextDouble();
        double y2 = in.nextDouble();
        String color = in.next();
        if (x1 > 0 && x1 <= 200 && y1 > 0 && y1 <= 200&&x2 > 0 && x2<= 200 && y2 > 0 && y2 <= 200) {
            
                Point point1 = new Point(x1, y1);
                Point point2 = new Point(x2, y2);
                Line line = new Line(point1, point2, color);

                line.display();
            
        } else {
            System.out.println("Wrong Format");
        }
        

    }

}

class Line {
    private Point point1;
    private Point point2;
    private String color;

    public Line() {
        point1 = new Point();
        point2 = new Point();
    }

    Line(Point p1, Point p2, String color) {
        this.point1 = p1;
        this.point2 = p2;
        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;
    }

    double getDistance() {
        return Math.sqrt(Math.pow(point1.getX() - point2.getX(), 2) + Math.pow(point1.getY() - point2.getY(), 2));

    }

    public void display() {
        System.out.println("The line's color is:" + this.color);
        System.out.println("The line's begin point's Coordinate is:");
        System.out.format("(%.2f,%.2f)\n", point1.getX(), point1.getY());

        System.out.println("The line's end point's Coordinate is:");
        System.out.printf("(%.2f,%.2f)\n", point2.getX(), point2.getY());

        System.out.printf("The line's length is:%.2f", getDistance());

    }

}

class Point {
    private double x;
    private double y;

    Point() {

    }

    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;
    }

    void display() {
            System.out.printf("(%.2f,%.2f)\n", this.x, this.y);

    }

}

在这里通过封装将每个类里面的private变量隐藏在类方法里,只有调用其方法时才能使用

实验1

import java.util.Scanner;

public class Game {
    Wolf wolf;
    Sheep sheep;
    Cabbage cabbage;
    Farmer farmer;
    // GameGui gui;

    Game() {
        wolf = new Wolf();
        sheep = new Sheep();
        cabbage = new Cabbage();
        farmer = new Farmer();
    }

    protected void play() {
        Scanner input = new Scanner(System.in);
        int choice = 0; // 用户输入选择
        boolean gameOver = false, // 游戏结束标志,默认为false,代表游戏进行中,未结束
                win = false; // 游戏输赢标志,默认为false,代表未赢得游戏。
        while (!gameOver) {
            GameGui.menu();
            choice = input.nextInt();
            switch (choice) {
            case 0:
                gameOver = true;
                break;
            case 1:/* 农夫独自过河的处理 */
                farmer.crossRiver = !(farmer.crossRiver);
                break;
            case 2:/* 农夫带狼的处理 */
                farmer.crossRiver = !(farmer.crossRiver);
                wolf.crossRiver = !(wolf.crossRiver);
                break;
            case 3:/* 农夫带羊的处理 */
                farmer.crossRiver = !(farmer.crossRiver);
                sheep.crossRiver = !(sheep.crossRiver);
                break;
            case 4:/* 农夫带白菜的处理 */
                farmer.crossRiver = !(farmer.crossRiver);
                cabbage.crossRiver = !(cabbage.crossRiver);
                break;
            }
            wolf.eatSheep(sheep);// 狼吃羊,如果羊不在同一边,则吃不到,如果在同一边,羊被吃
            sheep.eatCabbage(cabbage);// 同上
            GameGui.showStatus(farmer, wolf, sheep, cabbage);
            gameOver = isGameOver();
        }
        win = this.hasWin();
        if (win) {
            System.out.println("game over: you win !");
        } else {
            System.out.println("game over: you lose !");
        }
        input.close();

    }

    public boolean isGameOver() {
        if (sheep.isAlive == false || cabbage.isAlive == false) {
            return true;
        }
        if (wolf.hasCross && sheep.hasCross && cabbage.hasCross) {
            return true;
        }
        return false;
    }

    /*
     * 判断游戏是否胜利 前置条件:游戏结束 输入:无 运算:狼、羊、白菜均未被吃且全部渡过河,游戏胜利,否则失败 输出:游戏胜利--返回true
     * ,失败--返回false
     */
    public boolean hasWin() {
        if (sheep.isAlive == false || cabbage.isAlive == false) {
            return false;
        }
        if (wolf.hasCross && sheep.hasCross && cabbage.hasCross) {
            return true;
        } else {
            return false;
        }
    }

}

这个就是我在实验1里面主要的代码,里面调用了其他类里面的变量,也不算太难的代码

期中考试(2)

public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        double x1 = in.nextDouble();
        double y1 = in.nextDouble();
        double x2 = in.nextDouble();
        double y2 = in.nextDouble();
        String color = in.next();
        if (x1 > 0 && x1 <= 200 && y1 > 0 && y1 <= 200) {
            if (x2 > 0 && x2 <= 200 && y2 > 0 && y2 <= 200) {
                Point point1 = new Point(x1, y1);
                Point point2 = new Point(x2, y2);
                Line line = new Line(point1, point2, color);

                line.display();
            }
        } else {
            System.out.println("Wrong Format");
        }

    }

}

class Line {
    private Point point1;
    private Point point2;
    private String color;

    public Line() {
        point1 = new Point();
        point2 = new Point();
    }

    Line(Point p1, Point p2, String color) {
        this.point1 = p1;
        this.point2 = p2;
        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;
    }

    double getDistance() {
        return Math.sqrt(Math.pow(point1.getX() - point2.getX(), 2) + Math.pow(point1.getY() - point2.getY(), 2));

    }

    public void display() {
        System.out.println("The line's color is:" + this.color);
        System.out.println("The line's begin point's Coordinate is:");
        System.out.format("(%.2f,%.2f)\n", point1.getX(), point1.getY());

        System.out.println("The line's end point's Coordinate is:");
        System.out.printf("(%.2f,%.2f)\n", point2.getX(), point2.getY());

        System.out.printf("The line's length is:%.2f", getDistance());

    }

}

class Point {
    private double x;
    private double y;

    Point() {

    }

    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;
    }

    void display() {
        if (x > 0 && x <= 200 && y > 0 && y <= 200) {
            System.out.printf("(%.2f,%.2f)", this.x, this.y);
        } else {
            System.out.println("Wrong Format");
        }

    }

}

(3)改进建议:

希望多布置一些和我们学习的内容有关的作业,不用过多布置考数学作业

(4)总结:

首先在这里我使用了新的知识java,同时也学到了如何使用字符串中的方法,同时还学到了创建一个新的类,对我在这几道题的帮助特别大,同时我还需要继续学习父类和子类之间如何使用的,同时老师在对我们学习这方面帮助很大,作业和实验每次也都刚刚好,希望老师在课上时能够多多与示例相结合来讲解书本内容。

 

posted @ 2022-05-09 21:10  祝文宇  阅读(51)  评论(0)    收藏  举报