ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

OO课程第二阶段(实验和期中试题)总结Blog2

2022-05-09 23:03:37  阅读:159  来源: 互联网

标签:OO Point double Blog2 System 期中 println public out


                                                                                           OO课程第二阶段(实验和期中试题)总结Blog2

前言:学习OOP课程的第二阶段已经结束了,在此进行对于知识点,题量,难度的个人看法。

学习OOP课程的第二阶段已经结束了,较第一次阶段学习难度加大,学习的内容更多,但收获也更多。第二阶段通过三次题目集落下帷幕,第一次阶段总结的最高潮是类设计,而本次的几次题目集基本所有的题目都以类设计为基础,完成题目的构建。在类设计的基础上,通过对继承,多态,接口等多方面学习,认识到类设计不是单一的去设计一个方法完成功能。面向对象编程的思想,也在本次学习真正拉开了序幕。面向对象编程不同于面向过程编程,在创立一个对象后便有明确的功能性,通过继承,多态,接口可以实现不同对象实现不同的功能。其中在类设计上,通过学习类属性的访问权限,初步了解了四种访问权限:public,private,protected,default;以及不同类的类型:实体类,业务类,接口类;对于类设计上,学习的多,代表思路与编程思想变得更为开阔且严谨,开始真正考虑到如何将一个题目设计到恰如其分,不同的设计思路带来的不同效果。

各题目:设计与分析、采坑心得、改进建议 。

期中试题(1)

设计与分析

 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();
        Point point1 = new Point(x1,y1);
        Point point2 = new Point(x2,y2);

        Line line = new Line();
        line.setColor(input.nextLine());
        line.display();
    }

}

class Point{
    private double x = 0;
    private double y = 0;

    Point(){} //无参构造
    Point(double a,double b){
        this.x = a;
        this.y = b;
    }

    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;
    }
}
class Line{
    private Point point1;
    private Point point2;
    private String color ;

    Line(){}

    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;
    }
    public void display(){
        double result = Math.sqrt(this.point2.getX()*this.point2.getX()-this.point1.getX()*this.point1.getX())+Math.sqrt(this.point2.getY()*this.point2.getY()-this.point1.getY()*this.point1.getY());
        if(this.getColor() != null ){
            System.out.println("Wrong Format");
        }else {
            System.out.println("("+this.getPoint1()+")");
            System.out.println("("+this.getPoint2()+")");
            System.out.println("The line's color is:"+this.getColor());

 

 

 该题较简单,不需要构建多个类去完成,仅仅是通过简单的主函数便可以完成全部的功能,虽然形式简单,但是还是出现了对bug的不断调试。以下阐述我的采坑心得:

1)定义两个Point对象p1,p2;

2)写出有参构造方法,传递两个对象值给p1,p2

3)为p1,p2写出setters,和getters方法

4)为Line写出一个getLength方法求直线中两点的长度

5) 为LIne写一个ToString方法,方法如下所示:

public String toString() { return "Line [p1=" + p1 + ", p2=" + p2 + "]"; }

在Main类的main方法中,定义一个Line数组,数组长度通过键盘给出,然后通过键盘为每线段的两个Point对象的坐标赋值,并生成相应Line对象放入数组中,循环数组,输出每条直线的信息,以及两个点之间的距离。

期中考试(2)

设计与分析

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();
        Point point1 = new Point(x1,y1);
        Point point2 = new Point(x2,y2);

        Line line = new Line();
        line.setColor(input.nextLine());
        line.display();
    }

}

class Point{
    private double x = 0;
    private double y = 0;

    Point(){} //无参构造
    Point(double a,double b){
        this.x = a;
        this.y = b;
    }

    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;
    }
}
class Line{
    private Point point1;
    private Point point2;
    private String color ;

    Line(){}

    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;
    }
    public void display(){
        double result = Math.sqrt(this.point2.getX()*this.point2.getX()-this.point1.getX()*this.point1.getX())+Math.sqrt(this.point2.getY()*this.point2.getY()-this.point1.getY()*this.point1.getY());
        if(this.getColor() != null ){
            System.out.println("Wrong Format");
        }else {
            System.out.println("("+this.getPoint1()+")");
            System.out.println("("+this.getPoint2()+")");
            System.out.println("The line's color is:"+this.getColor());

 

 以下是我的踩坑心得:

继承的作用:

(1)使用继承可以有效实现代码复用,避免重复代码的出现。

(2)继承通过增强一致性来减少模块间的接口和界面,大大增加了程序的易维护性。

(3)继承可以更方便程序的维护和更新,像老师所说的那样,我们分成多个模块要是需要更新只需要更新对应模块的内容。

(4)继承是在一些比较一般的类的基础上构造、建立和扩充新类的最有效的手段。

 

多态的作用:

(1)可以增强程序的可扩展性及可维护性,使代码更加简洁。

(2)不但能减少编码的工作量,也能大大提高程序的可维护性及可扩展性。

(3)应用程序不必为每一个派生类编写功能调用,只需要对抽象基类进行处理即可。大大提高程序的可复用性。
期中考试(3)

分析和设计

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();
        Point point1 = new Point(x1,y1);
        Point point2 = new Point(x2,y2);

        Line line = new Line();
        line.setColor(input.nextLine());
        line.display();
    }

}

class Point{
    private double x = 0;
    private double y = 0;

    Point(){} //无参构造
    Point(double a,double b){
        this.x = a;
        this.y = b;
    }

    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;
    }
}
class Line{
    private Point point1;
    private Point point2;
    private String color ;

    Line(){}

    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;
    }
    public void display(){
        double result = Math.sqrt(this.point2.getX()*this.point2.getX()-this.point1.getX()*this.point1.getX())+Math.sqrt(this.point2.getY()*this.point2.getY()-this.point1.getY()*this.point1.getY());
        if(this.getColor() != null ){
            System.out.println("Wrong Format");
        }else {
            System.out.println("("+this.getPoint1()+")");
            System.out.println("("+this.getPoint2()+")");
            System.out.println("The line's color is:"+this.getColor());

 

 以下是我的踩坑心得:

 ArrayList是采用数组实现的列表,因此它支持随机访问,不适合频繁删除和插入操作。对于需要经常进行查询的数据建议采用此结构。但是我对容器的使用不够熟练,经常使用越界,在下一阶段的学习中会更加注意这方面的学习。

实验

设计与分析

import java.util.ArrayList;
import java.util.List;

public class CrossRiver {
        List<String> listThis = new ArrayList<String>();
        List<String> listThat = new ArrayList<String>();
        public  CrossRiver() {
            listThis.add("狼");
            listThis.add("草");
            listThis.add("羊");
        }
        public boolean isSafe(List< String > list){
            if(list.contains("狼")&&list.contains("羊")||list.contains("羊")&&list.contains("草")){
                return false;
            }else{
                return true;
            }
        }
        public void thisTothat(){
            String str = listThis.get(0);
            listThis.remove(str);
            if(this.isSafe(listThis)){
                System.out.println("农夫带着 " + str + " 从此岸到彼岸");
                System.out.println("此岸" + listThis + "," + "彼岸" + listThat);
                System.out.println();
                listThat.add(str);
                thatToThis();
            }else{
                listThis.add(str);
                thisTothat();
            }
        }
        public void thatToThis(){
            if(listThis.isEmpty()){
                System.out.println("此岸" + listThis + "," + "彼岸" + listThat);
                return;
            }
            if(isSafe(listThat)){
                System.out.println("农夫从彼岸到此岸");
                System.out.println("此岸" + listThis + "," + "彼岸" + listThat);
                System.out.println();
                thisTothat();
            }else{
                String str = listThat.get(0);
                listThat.remove(0);
                if(isSafe(listThat)){
                    System.out.println("农夫带着 " + str + " 从彼岸到此岸");
                    System.out.println("此岸" + listThis + "," + "彼岸" + listThat);
                    System.out.println();
                    listThis.add(str);
                    thisTothat();
                }else{
                    listThat.add(str);
                    thatToThis();
                }
            }
        }
        public static void main(String[] args){
            System.out.println("测试结果为:");
            System.out.println();
            new CrossRiver().thisTothat();
        }
}

 

 我的踩坑心得如下:

1、针对实现整个过程需要多步,不同步骤中各个事物所处位置不同的情况,可定义一个结构体来实现对四个对象狼、羊、白菜和农夫的表示。对于起始岸和目的岸,可以用0或者1来表示,以实现在程序设计中的简便性。

2、题目要求给出四种事物的过河步骤,没有对先后顺序进行约束,这就需要给各个事物依次进行编号,然后依次试探,若试探成功,进行下一步试探。这就需要使用循环或者递归算法,避免随机盲目运算且保证每种情况均试探到。

3、题目要求求出农夫带一只羊,一条狼和一颗白菜过河的办法,所以依次成功返回运算结果后,需要继续运算,直至求出结果,即给出农夫的过河方案。

4、输出界面要求具有每一步中农夫所带对象及每步之后各岸的物体,需要定义不同的数组来分别存储上述内容,并使界面所示方案清晰简洁。

mport java.unil.Scanner;
public class Crossriver{
    public static void main(String[] args){
        Game game = new Game();
        game.play();
        }
    }
class GameGui{
    public static void menu(){
        System.out.println("please choose operation");
        System.out.println("1.cross the river alone");
        System.out.println("2.cross the river with wolf");
        System.out.println("3.cross the river with sheep");
        System.out.println("4.cross the river with cabbage");
        System.out.println("0.quit");
        }
        public static void showStatus(Farmer farmer,Wolf wolf,Sheep sheep,Cabbage cabbage){
            farmer.showStatus();
            wolf.showStatus();
            sheep.showStatus();
            cabbage.showStatus();
            }
    }
    class Game{
        Wolf wolf;
        Sheep sheep;
        Cabbage cabbage;
        Farmer farmer;
        Boat boat;
        Game(){
            Scanner put = new Sanner(System.in);
            System.out.println("输入羊的名字");
            String sheepname = put.nextLine();
            System.out.println("输入狼的名字");
            String wolfname = put.nextLine();
            wolf = new Wolf(wolfname);
            sheep = new Sheep(sheepname);
            cabbage = new Cabbage();
            farmer = new Farmer();
            boat = new Boat();
            }
            protected void play(){
                Scanner input = new Sanner(System.in);
                int choice = 0;
                boolean gameOver = flase;
                win = false;
                while(!gameOver){
                    GameGui.menu();
                    choice = input.nextlnt();
                    switch(choice){
                        case 0:gameOver = true;
                        break;
                        case 1:if(boat.getcrossriver()==false){
                            farmer.setcrossriver(true);
                            boat.setcrossriver(true);
                            }
                            else{
                            farmer.setcrossriver(true);
                            boat.setcrossriver(false);
                            }
                            break;
                        case 2:if(boat.getcrossriver()==flase){
                            farmer.setcrossriver(true);
                            wolf.setcrossriver(true);
                            boat.setcrossriver(true);
                            }
                            else{
                            farmer.setcrossriver(true);
                            wolf.setcrossriver(false);
                            boat.setcrossriver(false);
                            }
                            break;
                        case 3:if(boat.crossriver()==false){
                            famer.setcrossriver(true);
                            sheep.setcrossriver(true);
                            boat.setcrossriver(true);
                            }
                            else{
                            farmer.setcrossriver(true);
                            sheep.setcrossriver(false);
                            boat.setcrossriver(false);
                            }
                            break;
                        case 4:if(boat.getcrossriver==false){
                            farmer.setcrossriver(true);
                            cabbage.setcrossriver(true);
                            boat.setcrossriver(true);
                            }
                            else{
                            famer.setcrossriver(true);
                            cabbage.setcrossriver(false);
                            boat.setcrossriver(false);
                            }
                            break;
                        default:fameOver = true;
                                    break;
                        
                        }
                        wolf.eatSheep(sheep,boat);
                        sheep.eatCabbage(cabbage,boat);
                        GameGui.showStatus(farmer,wolf,sheep,cabbage);
                        gameOver = isGameOver(wolf,sheep,cabbage);
                    }
                    win = this.hasWin(wolf,sheep,cabbage);
                    if(win){
                        System.out.println("game over:you win !");
                    }
                    else{
                        System.out.println("game over:you lose !");
                    }
                    input.close();
                }
                public boolean isGameOver(Wolf wolf,Sheep sheep,Cabbage cabbage){
                    if(sheep.getIsAlive==false||cabbage.getlsAlive()==false){
                        return true;
                        }
                    if(wolf.getcrossriver()==true&&sheep.getcrosseriver()==true&&cabbage.getcrossriver()==true){
                        return true;
                        }
                    else{
                        return false;
                        }
                    }
        }
class Total{
    protected boolean iscrossriver = false;
    protected boolean isAlive = true;
    protected void setcrossrive(boolean iscrossriver){
        this.iscrossriver = iscrossriver;
        }
    protected boolean getcrossriver(){
        return iscrossriver;
        }
    protected void setlsAlive(boolean isAlive){
        this.isAlive=isAlive;
        }
    protected boolearn getIsAlive(){
        return isAlive;
        }
    }
class Sheep extends Total{
    String name;
    Sheep(String name){
        this.name = name;
        System.out.println("咩咩,我是可爱的小羊"+name);
        }
        public void eatCabbage(Cabbage cabbage,Boat boat){
            if(isAlive==true&&iscrossriver==cabbage.getcrossriver()&&iscrossriver!=boat.getcrossriver()){
                cabbage.seltsAlive(false);
                }
            }
            public void showStatus(){
                System.out.println("Sheep"+" "+"name"+"is alive "+"isAlive"+" "+"Wolf"+" "+"name"+" "+"has Cross");
                }
    }
class Cabbage extends Total{
    public void showStatus(){
        System.out.println("Cabbage is alive"+isAlive+" "+"Cabbage has Cross"+iscrossriver);
        }
    }
class Farmer extends Total{
    public void showStatus(){
        System.out.println("Farmer has Cross"+iscrossriver);
        }
    }
class Wolf extends Total{
    String name;
    Wolf(String name){
        this.name = name;
        System.out.println("我"+name+"狼回来了");
        }
        public void eatSheep(Sheep sheep,Boat boat){
            if(iscrossriver==sheep.getcrossriver()&&iscrossriver!=boat.getcrossriver()){
                sheep.setlsAlilve(false);
                }
            }
            public void showStatus(){
                System.out.println("Wolf"+" "+name+"is alive"+isAlive+" "+"Wolf"+name+"has cross");
                }
    }
class Boat{
    protected bolean iscrossriver = false;
    protected void setcrossriver(boolean iscrossriver){
        this.iscrossriver = iscrossriver;
        }
    protected bloolean getcrossriver(){
        return iscrossriver;
        }
    }

 

 以下是我的踩坑心得:

创建一个Farmer类,其中包括cross_cabbage ()方法,cross_sheep()方法,cross_wolf()方法,这3个方法分别用来表示农夫和白菜,羊,狼过河的情况,而且农夫必须和物体在一边,否则会输出提示表示物体与农夫的位置不一样无法进行过河。

改进建议 :

对于技能上的学习,在对字符串的校验处理上,通过对正则表达式的深入学习,掌握对复杂数据的校验,截取,替换等,作为一项工具使用,在程序复杂度上大大降低。以及对于list的学习,可以对多对象处理上更为便捷可观,多对象处理上更为有效。还有对于hashmap的学习,对于其中遍历的运用进行了学习,对于多字符校验上有了更广阔的思路。不仅于此,题目集中许多边边角角运用的技能,也是会通过学习得以利用。

总结:

对于课程的学习建议,个人认为目前的pta便是不错的,有难有易,好上手,若想拿满分则必须要去自我学习。自我学习上,课程自带的SPOC课程则是选择之一,我们仍可自己去选择去论坛学习或者其他网课上学习。在实验上,目前进行的农夫过河的迭代性编程,面对同一个问题随着技术加深,我们需要运用更为上层的知识解决同一个问题,这是有趣也充满挑战的。线下课程中,希望老师不仅可以有对知识的讲解,与编程思想的演练,更可以对pta或者实验的难题加以点评与讲解,让作业不会得过且过,而是让我们更为深刻的了解一道题目应该如何去设计。作为学生自身,已经了解到这是一门需要自我探究,自我学习的课程。时间,精力,投入,缺一不可,不去假大空,而是去真正的做。

  第二阶段于此就告一段落了,首先感谢OOP课程的老师与助教的付出,再而感谢还在学习的自己。希望有更好的心态迎接第三阶段!

 

标签:OO,Point,double,Blog2,System,期中,println,public,out
来源: https://www.cnblogs.com/wusuHYB/p/16251431.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有