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个数据。具体实现还是比较简单的,相较于几次大作业,其中考试题目属于简单题了,可能是因为有时间限制,写起来有些慌乱,成绩不是很理想。上面删除函数已重新修改。

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

原文地址:http://www.cnblogs.com/pillowtalk/p/16838498.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性