首页 > 其他分享 >2022-下-面向对象-期中考试

2022-下-面向对象-期中考试

时间:2022-10-29 12:33:13浏览次数:47  
标签:&& String Point 期中考试 面向对象 2022 line public color

7-1 点与线(类设计) 分数 20  作者 段喜龙  单位 南昌航空大学

  • 设计一个类表示平面直角坐标系上的点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:

    在这里给出一组输入。例如:

    5
    9.4
    12.3
    84
    Red
       

    输出样例1:

    在这里给出相应的输出。例如:

    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
       

    输入样例2:

    在这里给出一组输入。例如:

    80.2356
    352.12
    24.5
    100
    Black
       

    输出样例2:

    在这里给出相应的输出。例如:

    Wrong Format

    这题依照类图实现相应的点类和线类即可,属于简单题。唯一需要注意的是,input.nextdouble()函数和input.nextint()函数后如果去接收字符,需要使用一个input.nextLine()去把前面两个函数输入结束的回车接收掉,如果不接收该回车,则后面所接收的字符就会是一个回车,就无法接收到正确的字符。相关代码如下:
  • Main类
    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();
            if(x1>0&&x1<=200&&y1>0&&y1<=200&&x2>0&&x2<=200&&y2>0&&y2<=200){
                input.nextLine();
                String color=input.nextLine();
                Point p1=new Point(x1,y1);
                Point p2=new Point(x2,y2);
                line l=new line(p1,p2,color);
                l.display();
            }
            else{
                System.out.println("Wrong Format");
            }
            
        }
    }
  • Point类
    class Point{
        private double x;
        private double y;
        public Point(){};
        public Point(double x,double y){
            if(x>0&&x<=200&&y>0&&y<=200){
                this.x=x;
                this.y=y;
            }
            else{
                System.out.println("Wrong Format");
            }
        }
    
        public double getX() {
            return x;
        }
    
        public void setX(double x) {
            if(x>0&&x<=200)
                this.x=x;
            else
                System.out.println("Wrong Format");
        }
    
        public double getY() {
            return y;
        }
    
        public void setY(double y) {
            if(y>0&&y<=200)
                this.y=y;
            else
                System.out.println("Wrong Format");
            
        }
        public void display(){
            System.out.println("("+String.format("%.2f", getX())+","+String.format("%.2f", getY())+")");
        }
    }
class line{
    private Point point1;
    private Point point2;
    private String color;
    public line(){};
    public 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 double getDistance(){
        return Double.parseDouble(String.format("%.2f", Math.sqrt(Math.pow(getPoint1().getX()-getPoint2().getX(),2)+Math.pow(getPoint1().getY()-getPoint2().getY(),2))));
    }
    public void display(){
        System.out.println("The line's color is:"+getColor());
        System.out.println("The line's begin point's Coordinate is:");
        getPoint1().display();
        System.out.println("The line's end point's Coordinate is:");
        getPoint2().display();
        System.out.println("The line's length is:"+Double.parseDouble(String.format("%.2f", getDistance())));
    }
}

主要考察封装思想,实现该代码比较简单。

7-2 点线面问题重构(继承与多态) 分数 40  作者 段喜龙  单位 南昌航空大学

在“点与线(类设计)”题目基础上,对题目的类设计进行重构,以实现继承与多态的技术性需求。

  • 对题目中的点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();
   

类结构如下图所示。

其中,所有数值均保留两位小数,建议可用String.format("%.2f", data)方法。

  • 以下情况为无效作业
    • 无法运行
    • 设计不符合所给类图要求
    • 未通过任何测试点测试
    • 判定为抄袭

输入格式:

分别输入线段的起点横坐标、纵坐标、终点的横坐标、纵坐标以及颜色,中间可用一个或多个空格、tab或者回车分隔。

输出格式:

(x1,y1)
(x2,y2)
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:长度值
The Plane's color is:颜色值
   

输入样例1:

在这里给出一组输入。例如:

5
9.4
12.3
84
Red
   

输出样例1:

在这里给出相应的输出。例如:

(5.00,9.40)
(12.30,84.00)
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
The Plane's color is:Red
   

输入样例2:

在这里给出一组输入。例如:

5
9.4
12.3
845
Black
   

输出样例2:

在这里给出相应的输出。例如:

Wrong Format

该题考查Java中的继承和多态,使用Element类作为Point和Line和Plane的父类,通过在主函数中分别使用Element的父类new一个不同类型的子类,再调用其display(),展示不同子类的各项属性,但是不知道为什么无法通过非法输入的样例,判断其非法输入应该只需要判断两个点的x、y均在(0,200]区间内应该都是合法。相关代码如下:

Main类

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();
        if(x1>0&&x1<=200&&y1>0&&y1<=200&&x2>0&&x2<=200&&y2>0&&y2<=200){
            input.nextLine();
            String color=input.nextLine();
            Point p1=new Point(x1,y1);
            Point p2=new Point(x2,y2);
            line line=new line(p1,p2,color);
            Plane p=new Plane(color);
            Element element=p1;
            element.display();
            element=p2;
            element.display();
            element=line;
            element.display();
            element=p;
            element.display();
        }
        else{
            System.out.println("Wrong Format");
        }

    }
}

Point类

class Point extends Element{
    private double x;
    private double y;
    public Point(){};
    public Point(double x,double y){
        if(x>0&&x<=200&&y>0&&y<=200){
            this.x=x;
            this.y=y;
        }
        else{
            System.out.println("Wrong Format");
        }
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        if(x>0&&x<=200)
            this.x=x;
        else
            System.out.println("Wrong Format");
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        if(y>0&&y<=200)
            this.y=y;
        else
            System.out.println("Wrong Format");

    }
    public void display(){
        System.out.println("("+String.format("%.2f", getX())+","+String.format("%.2f", getY())+")");
    }
}

Line类

class line extends Element{
    private Point point1;
    private Point point2;
    private String color;
    public line(){};
    public 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 double getDistance(){
        return Double.parseDouble(String.format("%.2f", Math.sqrt(Math.pow(getPoint1().getX()-getPoint2().getX(),2)+Math.pow(getPoint1().getY()-getPoint2().getY(),2))));
    }
    public void display(){
        System.out.println("The line's color is:"+getColor());
        System.out.println("The line's begin point's Coordinate is:");
        getPoint1().display();
        System.out.println("The line's end point's Coordinate is:");
        getPoint2().display();
        System.out.println("The line's length is:"+Double.parseDouble(String.format("%.2f", getDistance())));
    }
}

Element类

abstract class Element{
    abstract public void display();
}

Plane类

class Plane extends Element{
    private String color;
    Plane(){};
    Plane(String color){
        this.color=color;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
    @Override
    public void display(){
        System.out.println("The Plane's color is:"+getColor());
    }
}

该题思路不难,只需要注意一些细节即可。

7-3 点线面问题再重构(容器类) 分数 40  作者 段喜龙  单位 南昌航空大学

在“点与线(继承与多态)”题目基础上,对题目的类设计进行重构,增加容器类保存点、线、面对象,并对该容器进行相应增、删、遍历操作。

  • 在原有类设计的基础上,增加一个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()方法进行输出。
    类图如下所示:

  • 以下情况为无效作业
    • 无法运行
    • 设计不符合所给类图要求
    • 未通过任何测试点测试
    • 判定为抄袭

输入格式:

switch(choice) {
            case 1://insert Point object into list 
              输入“点”对象的x,y值
                break;
            case 2://insert Line object into list
                输入“线”对象两个端点的x,y值
                break;
            case 3://insert Plane object into list
                输入“面”对象的颜色值
                break;
            case 4://delete index - 1 object from list
                输入要删除的对象位置(从1开始)
                ...
            }
   

输出格式:

  • Point、Line、Plane的输出参考题目2
  • 删除对象时,若输入的index超出合法范围,程序自动忽略该操作

输入样例:

在这里给出一组输入。例如:

1
3.4
5.6
2
4.4
8.0
0.98
23.888
Red
3
Black
1
9.8
7.5
3
Green
4
3
0
   

输出样例:

在这里给出相应的输出。例如:

(3.40,5.60)
The line's color is:Red
The line's begin point's Coordinate is:
(4.40,8.00)
The line's end point's Coordinate is:
(0.98,23.89)
The line's length is:16.25
(9.80,7.50)
The Plane's color is:Green

该题增加了一个动态数组存储各对象,在Main函数中循环接收用户要执行的操作,增加和删除对象。实现起来相对简单。代码如下:

Main类

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int choice = input.nextInt();
        GeometryObject geo = new GeometryObject();
        while (choice != 0) {
            switch (choice) {
                case 1://insert Point object into list
                {
                    double x = input.nextDouble();
                    double y = input.nextDouble();
                    if (x > 0 && x <= 200 && y > 0 && y <= 200) {
                        Point p = new Point(x, y);
                        geo.add(p);
                    } else
                        System.out.println("Wrong Format");
                    break;
                }
                case 2://insert Line object into list
                {
                    double x1 = input.nextDouble();
                    double y1 = input.nextDouble();
                    double x2 = input.nextDouble();
                    double y2 = input.nextDouble();
                    if (x1 > 0 && x1 <= 200 && y1 > 0 && y1 <= 200 && x2 > 0 && x2 <= 200 && y2 > 0 && y2 <= 200) {
                        input.nextLine();
                        String color = input.nextLine();
                        Point p1 = new Point(x1, y1);
                        Point p2 = new Point(x2, y2);
                        line line = new line(p1, p2, color);
                        geo.add(line);
                    }
                    break;
                }
                case 3://insert Plane object into list
                {
                    input.nextLine();
                    String color = input.nextLine();
                    Plane p = new Plane(color);
                    geo.add(p);
                    break;
                }
                case 4://delete index - 1 object from list
                {
                    int index = input.nextInt();
                    if (index < geo.getList().size()) {
                        geo.remove(index);

                    }
                }
            }
            choice= input.nextInt();
        }
        for (int i = 0; i < geo.getList().size(); i++) {
            geo.getList().get(i).display();
        }
    }
}

Point类

class Point extends Element {
    private double x;
    private double y;

    public Point() {
    }

    ;

    public Point(double x, double y) {
        if (x > 0 && x <= 200 && y > 0 && y <= 200) {
            this.x = x;
            this.y = y;
        } else {
            System.out.println("Wrong Format");
        }
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        if (x > 0 && x <= 200)
            this.x = x;
        else
            System.out.println("Wrong Format");
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        if (y > 0 && y <= 200)
            this.y = y;
        else
            System.out.println("Wrong Format");

    }

    public void display() {
        System.out.println("(" + String.format("%.2f", getX()) + "," + String.format("%.2f", getY()) + ")");
    }
}

line类

class line extends Element {
    private Point point1;
    private Point point2;
    private String color;

    public line() {
    }

    ;

    public 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 double getDistance() {
        return Double.parseDouble(String.format("%.2f", Math.sqrt(Math.pow(getPoint1().getX() - getPoint2().getX(), 2) + Math.pow(getPoint1().getY() - getPoint2().getY(), 2))));
    }

    public void display() {
        System.out.println("The line's color is:" + getColor());
        System.out.println("The line's begin point's Coordinate is:");
        getPoint1().display();
        System.out.println("The line's end point's Coordinate is:");
        getPoint2().display();
        System.out.println("The line's length is:" + Double.parseDouble(String.format("%.2f", getDistance())));
    }
}

Plane类

class Plane extends Element {
    private String color;

    Plane() {
    }

    ;

    Plane(String color) {
        this.color = color;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public void display() {
        System.out.println("The Plane's color is:" + getColor());
    }
}

Element类

abstract class Element {
    abstract public void display();
}

GeometryObject类

class GeometryObject {
    private ArrayList<Element> list = new ArrayList<>();

    GeometryObject() {
    }

    ;

    public void add(Element element) {
        this.list.add(element);
    }

    public void remove(int index) {
        //for(int i=index;i<list.size();i++)
            //this.list.remove(i);
        if(index>=0&&index<list.size())
            this.list.remove(index-1);
    }

    public ArrayList<Element> getList() {
        return list;
    }
}

这题写blog时才发现问题,为啥删除容器数据的点全部报错。考试时看错了,以为是删除index后所有元素,写blog才发现原来是删除第index-1个数据。具体实现还是比较简单的,相较于几次大作业,其中考试题目属于简单题了,可能是因为有时间限制,写起来有些慌乱,成绩不是很理想。上面删除函数已重新修改。

代码复杂度还是较高,写的好烂。

标签:&&,String,Point,期中考试,面向对象,2022,line,public,color
From: https://www.cnblogs.com/pillowtalk/p/16838498.html

相关文章

  • 面向对象程序设计期中心得
    题目集7-17-1点与线(类设计)分数20作者 段喜龙单位 南昌航空大学设计一个类表示平面直角坐标系上的点Point,私有属性分别为横坐标x与纵坐标y,数据......
  • 2022.10.29-2021级王建民JAVA期中测试
     校园社团活动管理系统(20分)1、项目需求:校园社团作为高校课外活动的重要组成部分,发展十分迅速,也受到越来越多学生的欢迎,社团规模、数量等都在日益增长,社团活动也更为多......
  • 【JS】面向对象-继承-私有和受保护的属性和方法
    在面向对象的编程中,属性和方法分为两组:内部接口——可以通过该类的其他方法访问,但不能从外部访问的方法和属性。外部接口——也可以从类的外部访问的方法和......
  • 2022年java期中测试
     校园社团活动管理系统(20分)1、项目需求:校园社团作为高校课外活动的重要组成部分,发展十分迅速,也受到越来越多学生的欢迎,社团规模、数量等都在日益增长,社团活动也更为多......
  • 有趣的注释-2022-10-29
    /**         _ooOoo_ *         o8888888o *         88"."88 *         (|......
  • IDEA 编写的第一个程序-2022-10-29
    用IDEA编写第一个程序1、打开IDEA2、新建文件hello,选择java,选择安装JAVA版本号3、在生成的右边列表src右键newclass4、输入PSVM5、输入SOUT6、输入“Hellonew......
  • 2022.44 AI在视觉领域进展
    最近几年,AI在视觉领域的发展可谓是“神速”。去年1月,致力于“用通用人工智能造福全人类”的OpenAI公司基于GPT-3模型发布了划时代的DALL·E,实现了从文本生成图像。今年4......
  • 周六900C++班级2022-10-29 广搜
    7588:农夫抓牛农夫知道一头牛的位置,想要抓住它。农夫和牛都位于数轴上,农夫起始位于点N(0<=N<=100000),牛位于点K(0<=K<=100000)。农夫有两种移动方式:1、从X移动到X-1或X+......
  • Blog2:nchu-software-oop-2022-4+5+期中
    Blog2:nchu-software-oop-2022-4+5+期中一、前言两次大作业是关于四边形和五边形的相关操作,类似于之前的三角形,但由于图形边数的变化,难度大大增加。对数学知识的运用考察......
  • PTA-四边形、五边形、期中考试
    目录一、前言1.难度分析2.题量分析3.知识点4-7-1sdut-String-2识蛟龙号载人深潜,立科技报国志(II)(正则表达式)4-7-2点线形系列4-凸四边形的计算5-7-1点线形系列5-凸五边形......