一、前言

第四次作业难度还算可以,第一题和第三题比较简单。第一题主要是考察正则表达式,第三题主要考察方法的构造。第二题比较难,承接上次作业的三角形,开始了四边形的进程。
第五次作业难度较难,第一题还算稍微简单点,接着四边形直接写,第二题比较难主要是因为我代码的重构有问题。没有使用多态、继承等。
期中考试难度对于作业来说是很简单了,主要是考方法构造、类的继承,多态的使用,以及泛型难度层层递增。只要读清题目所给条件做起来难度不大。

二、程序设计分析

第四次作业

7-1 sdut-String-2 识蛟龙号载人深潜,立科技报国志(II)(正则表达式)

背景简介:

“蛟龙号”载人深潜器是我国首台自主设计、自主集成研制的作业型深海载人潜水器,设计最大下潜深度为7000米级,也是目前世界上下潜能力最强的作业型载人潜水器。“蛟龙号”可在占世界海洋面积99.8%的广阔海域中使用,对于我国开发利用深海的资源有着重要的意义。

中国是继美、法、俄、日之后世界上第五个掌握大深度载人深潜技术的国家。在全球载人潜水器中,“蛟龙号”属于第一梯队。目前全世界投入使用的各类载人潜水器约90艘,其中下潜深度超过1000米的仅有12艘,更深的潜水器数量更少,目前拥有6000米以上深度载人潜水器的国家包括中国、美国、日本、法国和俄罗斯。除中国外,其他4国的作业型载人潜水器最大工作深度为日本深潜器的6527米,因此“蛟龙号”载人潜水器在西太平洋的马里亚纳海沟海试成功到达7020米海底,创造了作业类载人潜水器新的世界纪录。

从2009年至2012年,蛟龙号接连取得1000米级、3000米级、5000米级和7000米级海试成功。下潜至7000米,说明蛟龙号载人潜水器集成技术的成熟,标志着我国深海潜水器成为海洋科学考察的前沿与制高点之一。

2012年6月27日11时47分,中国“蛟龙”再次刷新“中国深度”——下潜7062米。6月3日,“蛟龙”出征以来,已经连续书写了5个“中国深度”新纪录:6月15日,6671米;6月19日,6965米;6月22日,6963米;6月24日,7020米;6月27日,7062米。下潜至7000米,标志着我国具备了载人到达全球99%以上海洋深处进行作业的能力,标志着“蛟龙”载人潜水器集成技术的成熟,标志着我国深海潜水器成为海洋科学考察的前沿与制高点之一,标志着中国海底载人科学研究和资源勘探能力达到国际领先水平。

‘蛟龙’号是我国载人深潜发展历程中的一个重要里程碑。它不只是一个深海装备,更代表了一种精神,一种不畏艰险、赶超世界的精神,它是中华民族进军深海的号角。

了解蛟龙号”载人深潜器“的骄人业绩,为我国海底载人科学研究和资源勘探能力达到国际领先水平而自豪,小伙伴们与祖国同呼吸、共命运,一定要学好科学文化知识、提高个人能力,增强创新意识,做事精益求精,立科技报国之志!

请编写程序,实现如下功能:读入关于蛟龙号载人潜水器探测数据的多行字符串,从给定的信息找出数字字符,输出每行的数字之和。

提示 若输入为“2012年2月”,则该行的输出为:2014。若干个连续的数字字符作为一个整体,以十进制形式相加。

输入格式:
读入关于蛟龙号载人潜水器探测数据的多行字符串,每行字符不超过80个字符。

以”end”结束。

输出格式:
与输入行相对应的各个整数之和。

代码展示

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String regex = "[0-9]+";//要提取的内容格式
        double sum;
        Pattern p = Pattern.compile(regex);
        String s;
        s = input.nextLine();
        while(!s.equals("end")) {
            sum = 0;
            Matcher m = p.matcher(s);
            while(m.find()) {
                sum += (Double.parseDouble(m.group()));
            }
            System.out.println((int)sum);
            s = input.nextLine();
        }

    }
}

解释与心得
本题稍微比较简单,大家注意把握好题意,写正则表达式的时候认真一点基本没什么问题。

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

用户输入一组选项和数据,进行与四边形有关的计算。
以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入四个点坐标,判断是否是四边形、平行四边形,判断结果输出true/false,结果之间以一个英文空格符分隔。
2:输入四个点坐标,判断是否是菱形、矩形、正方形,判断结果输出true/false,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出”not a quadrilateral”
3:输入四个点坐标,判断是凹四边形(false)还是凸四边形(true),输出四边形周长、面积,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出”not a quadrilateral”
4:输入六个点坐标,前两个点构成一条直线,后四个点构成一个四边形或三角形,输出直线与四边形(也可能是三角形)相交的交点数量。如果交点有两个,再按面积从小到大输出四边形(或三角形)被直线分割成两部分的面积(不换行)。若直线与四边形或三角形的一条边线重合,输出”The line is coincide with one of the lines”。若后四个点不符合四边形或三角形的输入,输出”not a quadrilateral or triangle”。
后四个点构成三角形的情况:假设三角形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z 不与xy都相邻,如z x y s、x z s y、x s z y
5:输入五个点坐标,输出第一个是否在后四个点所构成的四边形(限定为凸四边形,不考虑凹四边形)或三角形(判定方法见选项4)的内部(若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。如果点在多边形的某条边上,输出”on the triangle或者on the quadrilateral”。若后四个点不符合四边形或三角形,输出”not a quadrilateral or triangle”。

输入格式:
基本格式:选项+”:”+坐标x+”,”+坐标y+” “+坐标x+”,”+坐标y。点的x、y坐标之间以英文”,”分隔,点与点之间以一个英文空格分隔。

输出格式:
基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出”Wrong Format”。
如果符合基本格式,但输入点的数量不符合要求,输出”wrong number of points”。
注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0

选项1、2、3中,若四边形四个点中有重合点,输出”points coincide”。
选项4中,若前两个输入线的点重合,输出”points coincide”。

代码展示


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import static java.lang.Double.POSITIVE_INFINITY;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String s = input.nextLine();
        Judge jd = new Judge();
        jd.judge(s);
        if(!jd.number()){
            System.out.println("wrong number of points");
            System.exit(0);
        }
        if(jd.getIndex() == 1) {
            if(!jd.coincidence(jd.pt)) {
                System.exit(0);
            }
            quadrilateral fl = new quadrilateral(jd.pt.get(0), jd.pt.get(1), jd.pt.get(2), jd.pt.get(3));
            if(fl.isquadrilateral()) {
                System.out.print("true ");
            }
            else {
                System.out.print("false ");
            }
            if(fl.ispingxing()) {
                System.out.print("true");
            }
            else {
                System.out.print("false");
            }
        }
        else if(jd.getIndex() == 2) {
            if(!jd.coincidence(jd.pt)) {
                System.exit(0);
            }
            quadrilateral fl = new quadrilateral(jd.pt.get(0), jd.pt.get(1), jd.pt.get(2), jd.pt.get(3));
            if(!fl.isquadrilateral()) {
                System.out.println("not a quadrilateral");
                System.exit(0);
            }
            if(fl.islingxing()) {
                System.out.print("true ");
            }
            else
                System.out.print("false ");
            if(fl.isjuxing())
                System.out.print("true ");
            else
                System.out.print("false ");
            if(fl.iszhengfangxing())
                System.out.println("true");
            else
                System.out.println("false");
        }
        else if(jd.getIndex() == 3) {
            if(!jd.coincidence(jd.pt)) {
                System.exit(0);
            }
            quadrilateral fl = new quadrilateral(jd.pt.get(0), jd.pt.get(1), jd.pt.get(2), jd.pt.get(3));
            if(!fl.isquadrilateral()) {
                System.out.println("not a quadrilateral");
                System.exit(0);
            }
            if(fl.istusibainxing()) {
                System.out.print("true ");
            }
            else
                System.out.print("false ");
            System.out.println(Judge.sswr(fl.perimeter()) + " " + Judge.sswr(fl.area()));

        }
        else if(jd.getIndex() == 4) {
            if(jd.pt.get(0) == jd.pt.get(1)) {
                System.out.println("points coincide");
                System.exit(0);
            }
            Line line = new Line(jd.pt.get(0),jd.pt.get(1));
            jd.pt.remove(0);jd.pt.remove(0);
            quadrilateral fl = new quadrilateral(jd.pt.get(0), jd.pt.get(1), jd.pt.get(2), jd.pt.get(3));

            if(fl.isquadrilateral()) {
                if(fl.chonghe(line)){
                    System.out.println("The line is coincide with one of the lines");
                    System.exit(0);
                }
                fl.Onequarilateral(line);
            }
            else {
                jd.coincidence2();
                if(jd.pt.size() == 4) {
                    System.out.println("not a quadrilateral or triangle");
                    System.exit(0);
                }
                if(jd.pt.size() == 3) {
                    triangle te = new triangle(jd.pt.get(0),jd.pt.get(1),jd.pt.get(2));
                    if(!te.istriangle()) {
                        System.out.println("not a quadrilateral or triangle");
                        System.exit(0);
                    }
                    if(te.chonghe(line)){
                        System.out.println("The line is coincide with one of the lines");
                        System.exit(0);
                    }
                    te.OneTriangle(line);
                }
            }
        }
        else if(jd.getIndex() == 5) {
            Point point = new Point();
            point  = jd.pt.get(0);
            jd.pt.remove(0);
            quadrilateral fl = new quadrilateral(jd.pt.get(0), jd.pt.get(1), jd.pt.get(2), jd.pt.get(3));
            if(fl.isquadrilateral()) {
                if(fl.pupline(point)) {
                    System.out.println("on the quadrilateral");
                }
                else if(fl.pupin(point)) {
                    System.out.println("in the quadrilateral");
                }
                else
                    System.out.println("outof the quadrilateral");
            }
            else {
                jd.coincidence2();
                if(jd.pt.size() == 4) {
                    System.out.println("not a quadrilateral or triangle");
                    System.exit(0);
                }
                if(jd.pt.size() == 3) {
                    triangle te = new triangle(jd.pt.get(0),jd.pt.get(1),jd.pt.get(2));
                    if(!te.istriangle()) {
                        System.out.println("not a quadrilateral or triangle");
                        System.exit(0);
                    }
                    if(te.pupline(point)) {
                        System.out.println("on the triangle");
                    }
                    else if(te.pupin(point)) {
                        System.out.println("in the triangle");
                    }
                    else
                        System.out.println("outof the triangle");
                }
            }
        }

    }
    //处理数据double


}
class Judge {
    ArrayList<Point> pt = new ArrayList<>();
    private int index;

    public int getIndex() {
        return index;
    }

    public void judge(String s) {
        int flag =0;
        String regex = "[1-5]:([+-]?(0|(0\\.[0-9]+)|([1-9][0-9]*(\\.[0-9]+)?)),[+-]?(0|(0\\.[0-9]+)|([1-9][0-9]*(\\.[0-9]+)?))\\s?)+$";
        if(s.matches(regex)) {
            String[] string01 = s.split(":");
            index = Integer.parseInt(string01[0]);
            String[] strings = string01[1].split(" ");
            for (int i = 0; i < strings.length; i++) {
                Point pot = new Point();
                String[] s1 = strings[i].split(",");
                pot.setX(Double.parseDouble(s1[0]));
                pot.setY(Double.parseDouble(s1[1]));
                pt.add(pot);
            }
        }
        else {
            System.out.println("Wrong Format");
            System.exit(0);
        }

    }
    public boolean number(){
        if((pt.size() == 4 && (this.index >= 1 && this.index < 4 )) || (pt.size() == 6 && this.index == 4 ) || (pt.size() == 5 && this.index == 5)) {
            return true;
        }
        else
            return false;
    }
    //判断点是否重合
     public boolean coincidence(ArrayList<Point> a) {
        if(a.size() % 2 == 0) {
            for (int i = 0; i < a.size(); i+=1) {
                for (int j = i+1; j < a.size(); j++) {
                    //System.out.println(a.get(i).getX() + "," + a.get(j).getX() + "," + a.get(i).getY() + "," + a.get(j).getY());
                    if(a.get(i).getX() == a.get(j).getX() && a.get(i).getY() == a.get(j).getY()) {
                        System.out.println("not a quadrilateral");
                        return false;
                    }
                }
            }
        }
        else {
            for (int i = 0; i < a.size(); i++) {
                for (int j = i+1; j < a.size(); j++) {
                    //System.out.println(a.get(i).getX() + "," + a.get(j).getX() + "," + a.get(i).getY() + "," + a.get(j).getY());
                    if(a.get(i).getX() == a.get(j).getX() && a.get(i).getY() == a.get(j).getY()) {
                        System.out.println("not a quadrilateral");
                        return false;
                    }
                }
            }
        }

        return true;

    }
    //去冗余
    public void coincidence2() {
        if(pt.get(0).chonghe(pt.get(2)))
            pt.remove(0);
        else if(pt.get(1).chonghe( pt.get(3)))
            pt.remove(1);
        //判断重复的点
        for (int i = 0; i < pt.size() - 1; i++) {
            if(pt.get(i).chonghe(pt.get(i + 1))) {
                pt.remove(i);
            }
        }
        if(pt.get(0).chonghe(pt.get(pt.size()-1))) {
            pt.remove(0);
        }

        //判断点在两点之间
        for (int i = 0; i < pt.size(); i++) {
            if(Line.threeline(pt.get(i%pt.size()),pt.get((i+1)%pt.size()),pt.get((i+2)%pt.size()))) {
                pt.remove((i+1)%pt.size());
            }
        }
    }
    public static double sswr(double s) {
        String str=String.valueOf(s);
        String substring = str.substring(str.indexOf(".") + 1);
        int str_len;
        if(substring.length()>3)
        {
            str_len=3;
        }else {
            str_len=substring.length();
        }
        String formats="%."+str_len+"f";
        String out=String.format(formats,s);
        double res=Double.parseDouble(out);
        return res;
    }
}
class Point {
    private double x;
    private double 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;
    }

    /*传入一个点
        计算两点间的距离
        返回两点间距离
        */
    public double distance(Point a) {
        double dis;
        dis = Math.sqrt(Math.pow((a.x - this.x),2) + Math.pow((a.y - this.y),2));
        return dis;
    }
    public boolean CheckPoint(Point p){
        if( Math.abs(this.getX() - p.getX() )< 0.000001  && Math.abs(this.getY() - p.getY() )< 0.000001){
            return true;
        }
        else
            return false;
    }
    public boolean chonghe(Point a){
        if(this.getX() == a.getX() && this.getY() == a.getY())
            return true;
        return false;
    }

}

class Line {
    private Point m,n;
    private double b;//y=kx+b
    private double k;
    private double A;
    private double B;
    private double C;
    public Point getM() {
        return m;
    }
    public Line() {

    }
    public Line(Point m,Point n) {
        this.m = m;
        this.n = n;
        this.slope();
        this.equation();//求出直线方程
    }
    public void setMN(Point m,Point n) {
        this.m = m;
        this.n = n;
        this.slope();
        this.equation();//求出直线方程
    }
    public Point getN() {
        return n;
    }
    public double getA() {
        return A;
    }
    public double getC() {
        return C;
    }
    public double getK() {
        return k;
    }
    public double getB() {
        return B;
    }

    //直线的斜率
    public void slope() {
        double se;
        if(m.getX() == n.getX())
            k = POSITIVE_INFINITY;
        else if(m.getY() == n.getY())
            k = 0;
        else {
            se = (m.getY()-n.getY())/(m.getX()-n.getX());
            k = se;
        }

    }
    public void equation() {
        double b;
        b = m.getY() - this.k * m.getX();
        A = n.getY() - m.getY();
        B = m.getX() - n.getX();
        C = n.getX() * m.getY() - m.getX() * n.getY();
    }
    //点到直线的距离
    double p_to_l(Point a,Line l) {
        //double dis = Math.abs(l.getA()*a.getX() + l.getB()*a.getY() + l.getC())/Math.sqrt(l.A * l.A + l.B* l.B);
        //return dis;
        if(l.k == 0) {
            return Math.abs(l.getM().getX()-a.getX());
        }
        else if(l.k == 1) {
            return Math.abs(l.getM().getY()-a.getY());
        }
        else {
            return Math.abs(l.getK()*a.getX()-a.getY()+ this.b)/(Math.sqrt(1+l.getK()*l.getK()));
        }
    }

    //判断三点一线,用于判断选项四的三角形情况
    public static boolean threeline(Point a, Point b, Point c) {
        Line line1 = new Line();
        line1.setMN(a,c);
        return line1.isallline(b) && line1.pointupline(b);
        /*Line line2 = new Line();
         line2.setMN(b,c);
        if(line2.k - line1.k < 0.000001)
            return true;
        else
            return false;*/
    }
    public boolean fourpoint(Line a) {
        if(Math.abs(a.k - this.k) < 0.000001)
            return true;
        else
            return false;
    }
    //求两直线交点坐标
    public Point intersection(Line a) {

        Point point = new Point();
        //if(a.k == this.k)
        //    return null;
        double det = this.getA() * a.getB() - a.getA() * this.getB();
        //if(det == 0)
        //    return null;
        point.setX(( this.getB() * a.getC() - a.getB() * this.getC() )/det);
        point.setY(( a.getA() * this.getC() - this.getA() * a.getC() )/det);
        return point;
    }
    //点在线端内,点到线上两端点的距离==两端点距离
    public boolean pointupline(Point a) {
        if(Math.abs(a.distance(this.getM()) + a.distance(this.getN()) - this.distance()) < 0.000001)
            return true;
        else
            return false;
    }
    //点在线上不包含端点
    public boolean pointupline2(Point a) {
        if(this.pointupline(a) && !this.getM().chonghe(a) && !this.getN().chonghe(a))
            return true;
        return false;
    }
    //点是否在直线上
    public boolean isallline(Point a) {
        if(Math.abs( getA() * a.getX() + getB() * a.getY() + getC() ) < 0.000001)
            return true;
        return false;
    }
    //线段距离
    public double distance() {
        double dis;
        dis = Math.sqrt(Math.pow((m.getX() - n.getX()),2) + Math.pow((m.getY() - n.getY()),2));
        return dis;
    }
    //判断两点是否在直线一侧
    public boolean istongce(Point a,Point b) {
        double x1;
        double x2;
        x1 = this.getA() * a.getX() + this.getB() * a.getY() + this.getC();
        x2 = this.getA() * b.getX() + this.getB() * b.getY() + this.getC();
        if(x1 * x2 > 0)
            return true;
        else
            return false;
    }
    //判断两条线重合
    public boolean chonghe(Line l) {
        if(this.isallline(l.getM()) && this.isallline(l.getN())) {
            return true;
        }
        return false;
    }
}
class triangle {
    private Line l1 = new Line();
    private Line l2 = new Line();
    private Line l3 = new Line();

    public triangle(Line l1, Line l2, Line l3) {
        this.l1 = l1;
        this.l2 = l2;
        this.l3 = l3;
    }

    public triangle(Point a, Point b, Point c) {
        this.l1.setMN(a, b);
        this.l2.setMN(b, c);
        this.l3.setMN(c, a);
    }

    public Line getL1() {
        return l1;
    }

    public void setL1(Line l1) {
        this.l1 = l1;
    }

    public Line getL2() {
        return l2;
    }

    public void setL2(Line l2) {
        this.l2 = l2;
    }

    public Line getL3() {
        return l3;
    }

    public void setL3(Line l3) {
        this.l3 = l3;
    }

    //是否能构成三角形
    public boolean istriangle() {

        if (l1.isallline(l2.getN()) || l2.isallline(l3.getN()) || l3.isallline(l1.getN()))
            return false;
        return true;
    }

    //等腰三角形
    public boolean istwoline() {
        if (Math.abs(l1.distance() - l2.distance()) < 0.000001 || Math.abs(l2.distance() - l3.distance()) < 0.000001 || Math.abs(l2.distance() - l3.distance()) < 0.000001) {
            return true;
        } else
            return false;
    }

    //等边三角形
    public boolean threeline() {
        if (Math.abs(l1.distance() - l2.distance()) < 0.000001 && Math.abs(l1.distance() - l3.distance()) < 0.000001 && Math.abs(l2.distance() - l3.distance()) < 0.000001) {
            return true;
        } else
            return false;
    }

    //三角形周长
    public double Perimeter() {

        return l1.distance() + l2.distance() + l3.distance();
    }

    //三角形面积
    public double area() {
        double s = this.Perimeter() * 0.5;
        return Math.sqrt(s * (s - l1.distance()) * (s - l2.distance()) * (s - l3.distance()));

    }
    public double area3(){
        return l1.p_to_l(l1.getM(),l1) * l1.distance() *0.5;
    }

    public double area2(Point a, Point b, Point c) {
        double d1, d2, d3;
        d1 = a.distance(b);
        d2 = b.distance(c);
        d3 = a.distance(c);
        double s = 0.5 * (d1 + d2 + d3);
        return Math.sqrt(s * (s - d1) * (s - d2) * (s - d3));
    }

    //重心
    public Point focus() {
        Point weight = new Point();
        weight.setX((l1.getM().getX() + l1.getN().getX() + l2.getN().getX()) * 1.0 / 3);
        weight.setY((l1.getM().getY() + l1.getN().getY() + l2.getN().getY()) * 1.0 / 3);
        return weight;
    }

    //求余弦
    public double[] getcos() {
        double[] cos = new double[3];
        cos[0] = (l1.distance() * l1.distance() + l2.distance() * l2.distance() - l3.distance() * l3.distance()) / (2 * l1.distance() * l2.distance());
        cos[1] = (l1.distance() * l1.distance() + l3.distance() * l3.distance() - l2.distance() * l2.distance()) / (2 * l1.distance() * l3.distance());
        cos[2] = (l3.distance() * l3.distance() + l2.distance() * l2.distance() - l1.distance() * l1.distance()) / (2 * l3.distance() * l2.distance());
        return cos;
    }

    public double getcos(Point a, Point b, Point c) {
        double l1 = a.distance(b);
        double l2 = a.distance(c);
        double l3 = b.distance(c);
        return (l1 * l1 + l2 * l2 - l3 * l3) / (2 * l1 * l2);

    }
    // 点在三角形的边上
    public boolean pupline(Point a) {
        if(l1.pointupline(a) && l1.isallline(a)){
            return true;
        }
        else if(l2.isallline(a) && l2.pointupline(a)) {
            return true;
        }
        else if(l3.pointupline(a) && l3.isallline(a)) {
            return true;
        }
        else
            return false;
    }
    //点在三角形内
    public boolean pupin(Point a) {
        triangle tl1 = new triangle(l1.getM(), l1.getN(), a);
        triangle tl2 = new triangle(l2.getM(), l2.getN(), a);
        triangle tl3 = new triangle(l3.getM(), l3.getN(), a);
        if(!this.pupline(a) && Math.abs(tl1.area() + tl2.area() + tl3.area() - this.area()) < 0.0001)
            return true;
        return false;
    }
    public boolean chonghe(Line l) {
        if(l1.chonghe(l))
            return true;
        if(l2.chonghe(l))
            return true;
        if(l3.chonghe(l))
            return true;
        return false;
    }
    public void OneTriangle(Line line){
        List<Line> lineList = new ArrayList<>();
        lineList.add(l1);lineList.add(l2);lineList.add(l3);
        List<Point> list = new ArrayList<>();
        int cnt = 0;
        Point u;
        for(int i=0;i<lineList.size();i++){
            u = lineList.get(i).intersection(line);

            boolean flag = false;
            if( u != null){
                if(lineList.get(i).pointupline(u)){
                    flag = true;
                }

                for (Point p : list){
                    if( p.CheckPoint(u) ){
                        flag = false;
                        break;
                    }
                }

                if(flag){
                    list.add(u);
                    //System.out.println(u.getX() + " " + u.getY() );
                    cnt++;
                }
            }
        }
        if(cnt < 2){
            System.out.println(cnt);
        }
        else {
            double s1,s2,s;
            System.out.print("2 ");
            Line lline = new Line(list.get(0), list.get(1));
            Point p1 = list.get(0),p2 = list.get(1);
            Point m;
            if(l1.isallline(p1) && l2.isallline(p2) || l2.isallline(p2) && l1.isallline(p1))
                m = l1.getN();
            else if( l1.isallline(p1) && l3.isallline(p2) || l3.isallline(  p2) && l1.isallline(p1) )
                m = l1.getM();
            else
                m = l2.getN();
            triangle triangle1 = new triangle(m,list.get(0),list.get(1));

            s1 = triangle1.area();
            s1 = Judge.sswr(s1);
            s = this.area();
            s = Judge.sswr(s);
            s2 = s - s1;
            s2 = Judge.sswr(s2);

            System.out.println(Math.min(s1,s2) + " " + Math.max(s1,s2));
        }
    }
}
class quadrilateral {
    private Line line01;
    private Line line02;
    private Line line03;
    private Line line04;
    Point[] point = new Point[4];
    public quadrilateral(Point a,Point b,Point c,Point d) {
        point[0] = a;point[1] = b;point[2] = c; point[3] = d;
        Line line01 = new Line(a,b);this.line01 = line01;
        Line line02 = new Line(b,c);this.line02 = line02;
        Line line03 = new Line(c,d);this.line03 = line03;
        Line line04 = new Line(d,a);this.line04 = line04;
    }
    public boolean isquadrilateral() {
        if((line01.getK() == line02.getK()  || line02.getK() == line03.getK() || line03.getK() == line04.getK() || line04.getK() == line01.getK())
                || (line01.pointupline(line01.intersection(line03)) && line03.pointupline(line01.intersection(line03)))
                || (line02.pointupline(line02.intersection(line04)) && line04.pointupline(line02.intersection(line04)))) {
            return false;
        }
        else {
            return true;
        }
    }
    public boolean ispingxing() {
        if(line01.getK() == line03.getK() && line02.getK() == line04.getK()){
            return true;
        }
        else
            return false;
    }
    public boolean islingxing() {
        if(this.ispingxing() && this.line01.distance() == this.line02.distance() && this.line01.distance() == this.line03.distance()
                && this.line01.distance() == this.line04.distance()) {
            return true;
        }
        else
            return false;
    }
    public boolean isjuxing() {
        if(this.ispingxing() && ( (this.line01.getK() == POSITIVE_INFINITY && this.line02.getK() == 0) || (this.line01.getK() == 0 && this.line02.getK() == POSITIVE_INFINITY) || this.line01.getK() * this.line02.getK() == -1)) {
            return true;
        }
        else
            return false;
    }
    public boolean iszhengfangxing() {
        if(this.isjuxing() && this.islingxing()) {
            return true;
        }
        else
            return false;
    }
    //判断凸四边形
    public boolean istusibainxing() {
        if(line01.istongce(line03.getM(),line03.getN()) && line02.istongce(line04.getM(),line04.getN())
                && line03.istongce(line01.getM(),line01.getN()) && line04.istongce(line02.getM(),line02.getN())) {
            return true;
        }
        else
            return false;
    }
    //四边形周长
    public double perimeter() {
        return line01.distance() + line02.distance() + line03.distance() + line04.distance();
    }
    //四边形面积
    public double area() {
        triangle triangle01 = new triangle(line01.getM(), line01.getN(), line02.getN());
        triangle triangle02 = new triangle(line03.getM(), line03.getN(), line04.getN());
        triangle triangle03 = new triangle(line02.getM(), line02.getN(), line03.getN());
        triangle triangle04 = new triangle(line04.getM(), line04.getN(), line01.getN());
        double qarea01,qarea02;
        qarea01 = triangle01.area() + triangle02.area();
        qarea02 = triangle03.area() + triangle04.area();
        if(qarea02 < qarea01)//凹四边形面积比较特殊,要找凹点,这里偷懒直接判段小的输出
            return qarea02;
        else
            return qarea01;
    }
    //点在四边形边上
    public boolean pupline(Point a) {
        if(line01.pointupline(a) && line01.isallline(a))
            return true;
        else if(line02.pointupline(a) && line02.isallline(a))
            return true;
        else if(line03.pointupline(a) && line03.isallline(a))
            return true;
        else if(line04.pointupline(a) && line04.isallline(a))
            return true;
        else
            return false;
    }
    //点在四边形内部
    public boolean pupin(Point a) {
        triangle tl01 = new triangle(line01.getM(), line01.getN(), a);
        triangle tl02 = new triangle(line02.getM(), line02.getN(), a);
        triangle tl03 = new triangle(line03.getM(), line03.getN(), a);
        triangle tl04 = new triangle(line04.getM(), line04.getN(), a);
        if(!this.pupline(a) && Math.abs(tl01.area() + tl02.area() + tl03.area() + tl04.area() - this.area()) < 0.0001)
            return true;
        else
            return false;
    }
    public boolean chonghe(Line l) {
        if(line01.chonghe(l))
            return true;
        if(line02.chonghe(l))
            return true;
        if(line03.chonghe(l))
            return true;
        if(line04.chonghe(l))
            return true;
        return false;
    }
    //四边形分割成两个求面积,有两个交点
    public void fengeArea(Point a,Point b,Point c) {
        triangle tr1 = new triangle(a,b,c);
        double area1 = tr1.area();
        double area = this.area();
        double area2 = area - area1;
        area1 = Judge.sswr(area1);
        area2 = Judge.sswr(area2);
        System.out.println("2 " + Math.min(area1,area2) + " " + Math.max(area1,area2));
    }
    public void Onequarilateral(Line l) {
        Point[] points = new Point[4];
        ArrayList<Line> lines = new ArrayList<>();
        lines.add(line01);lines.add(line02);lines.add(line03);lines.add(line04);
        int[] p_in = new int[4];

        //判断交点在线上还是端点
        for (int i = 0; i < 4; i++) {
            p_in[i] = 0;
            //求直线与四边形的交点
            points[i] = lines.get(i).intersection(l);
            if(points[i]!=null) {
                //判断交点是否在边上,不包括端点
                if(lines.get(i).pointupline2(points[i]))
                    p_in[i] = 1;
            }
        }
        //有顶角在线上
        for (int i = 0; i < 4; i++) {
            if(l.isallline(point[i])) {
                if(l.isallline((point[(i+2)%4]))) {
                    fengeArea(point[i], point[(i + 2) % 4], point[(i + 1) % 4]);
                    return;
                }
                else if(p_in[(i+1)%4] == 1) {
                    fengeArea(point[i], point[(i + 1) % 4], points[(i + 1) % 4]);
                    return;
                }
                else if(p_in[(i+2)%4] == 1) {
                    fengeArea(point[i], point[(i + 3) % 4], points[(i + 2) % 4]);
                    return;
                }
                else {
                    System.out.println("1");
                    return;
                }
            }
        }
        //两个临边
        for (int i = 0; i < 4; i++) {
            if(p_in[i] == 1 && p_in[(i+1)%4] == 1) {
                fengeArea(point[(i + 1) % 4], points[i], points[(i + 1) % 4]);
                return;
            }
        }
        //两个对边
        for (int i = 0; i < 2; i++) {
            if(p_in[i] == 1 && p_in[i+2] == 1) {
                triangle tr1 = new triangle(point[i], points[i], points[(i + 2) % 4]);
                triangle tr2 = new triangle(point[(i+3)%4], point[i], points[(i + 2) % 4]);
                double area1 = tr1.area() + tr2.area();
                double area = this.area();
                double area2 = area - area1;
                area1 = Judge.sswr(area1);
                area2 = Judge.sswr(area2);
                System.out.println("2 " + Math.min(area1,area2) + " " + Math.max(area1,area2));
                return;
            }
        }
        System.out.println("0");
        return;
    }
}

解释与心得
本题可以沿用上次三角形的代码另外重新加一个四边形类还是可以容易过的。但是一定要注意写完之后要进行重构,提取出一些类的父类,否则下一次作业会很难完成。

7-3 设计一个银行业务类

编写一个银行业务类BankBusiness,具有以下属性和方法:
(1)公有、静态的属性:银行名称bankName,初始值为“中国银行”。
(2)私有属性:账户名name、密码password、账户余额balance。
(3)银行对用户到来的欢迎(welcome)动作(静态、公有方法),显示“中国银行欢迎您的到来!”,其中“中国银行”自动使用bankName的值。
(4)银行对用户离开的提醒(welcomeNext)动作(静态、公有方法),显示“请收好您的证件和物品,欢迎您下次光临!”
(5)带参数的构造方法,完成开户操作。需要账户名name、密码password信息,同时让账户余额为0。
(6)用户的存款(deposit)操作(公有方法,需要密码和交易额信息),密码不对时无法存款且提示“您的密码错误!”;密码正确、完成用户存款操作后,要提示用户的账户余额,例如“您的余额有1000.0元。”。
(7)用户的取款(withdraw)操作(公有方法,需要密码和交易额信息)。密码不对时无法取款且提示“您的密码错误!”;密码正确但余额不足时提示“您的余额不足!”;密码正确且余额充足时扣除交易额并提示用户的账户余额,例如“请取走钞票,您的余额还有500.0元。”。

编写一个测试类Main,在main方法中,先后执行以下操作:
(1)调用BankBusiness类的welcome()方法。
(2)接收键盘输入的用户名、密码信息作为参数,调用BankBusiness类带参数的构造方法,从而创建一个BankBusiness类的对象account。
(3)调用account的存款方法,输入正确的密码,存入若干元。密码及存款金额从键盘输入。
(4)调用account的取款方法,输入错误的密码,试图取款若干元。密码及取款金额从键盘输入。
(5)调用account的取款方法,输入正确的密码,试图取款若干元(取款金额大于余额)。密码及取款金额从键盘输入。
(6)调用account的取款方法,输入正确的密码,试图取款若干元(取款金额小于余额)。密码及取款金额从键盘输入。
(7)调用BankBusiness类的welcomeNext()方法。

输入格式:
输入开户需要的姓名、密码
输入正确密码、存款金额
输入错误密码、取款金额
输入正确密码、大于余额的取款金额
输入正确密码、小于余额的取款金额

输出格式:
中国银行(银行名称)欢迎您的到来!
您的余额有多少元。
您的密码错误!
您的余额不足!
请取走钞票,您的余额还有多少元。
请收好您的证件和物品,欢迎您下次光临!

代码展示


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        BankBusiness.welcome();
        String s = input.nextLine();
        String[] info = s.split(" ");
        BankBusiness bankBusiness = new BankBusiness(info[0], info[1]);
        s = input.nextLine();
        info = s.split(" ");
        bankBusiness.deposit(info[0], Double.parseDouble(info[1]));
        int i = 0;
        while (i < 3) {
            s = input.nextLine();
            info = s.split(" ");
            bankBusiness.withdraw(info[0], Double.parseDouble(info[1]));
            i++;
        }
        BankBusiness.welcomeNext();
    }
}

class BankBusiness {
    public static String bankName = "中国银行";
    private String name;
    private String password;
    private double balance;


    public static void welcome() {
        System.out.println(bankName + "欢迎您的到来!");
    }

    public static void welcomeNext() {
        System.out.println("请收好您的证件和物品,欢迎您下次光临!");
    }

    public BankBusiness(String name, String password) {
        this.name = name;
        this.password = password;
        this.balance = 0;
    }

    public void deposit(String pw, double money) {
        if (!pw.equals(this.password)) {
            System.out.println("您的密码错误!");
        } else {
            this.balance += money;
            System.out.println("您的余额有" + this.balance + "元。");
        }
    }

    public void withdraw(String pw, double money) {
        if (!pw.equals(this.password)) {
            System.out.println("您的密码错误!");
        } else if (this.balance < money) {
            System.out.println("您的余额不足!");
        } else {
            this.balance -= money;
            System.out.println("请取走钞票,您的余额还有" + this.balance + "元。");
        }
    }
}

解释与心得
本题考察的主要是java属性,方法的构造,对类进行封装。难度较小,主要是让大家掌握类的封装。

第五次作业

7-1 点线形系列5-凸五边形的计算-1

用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入五个点坐标,判断是否是五边形,判断结果输出true/false。
2:输入五个点坐标,判断是凹五边形(false)还是凸五边形(true),如果是凸五边形,则再输出五边形周长、面积,结果之间以一个英文空格符分隔。 若五个点坐标无法构成五边形,输出”not a pentagon”
3:输入七个点坐标,前两个点构成一条直线,后五个点构成一个凸五边形、凸四边形或凸三角形,输出直线与五边形、四边形或三角形相交的交点数量。如果交点有两个,再按面积从小到大输出被直线分割成两部分的面积(不换行)。若直线与多边形形的一条边线重合,输出”The line is coincide with one of the lines”。若后五个点不符合五边形输入,若前两点重合,输出”points coincide”。

以上3选项中,若输入的点无法构成多边形,则输出”not a polygon”。输入的五个点坐标可能存在冗余,假设多边形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如:x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z不与xy都相邻,如:z x y s、x z s y、x s z y

输入格式:
基本格式:选项+”:”+坐标x+”,”+坐标y+” “+坐标x+”,”+坐标y。点的x、y坐标之间以英文”,”分隔,点与点之间以一个英文空格分隔。

输出格式:
基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出”Wrong Format”。
如果符合基本格式,但输入点的数量不符合要求,输出”wrong number of points”。
注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0

代码展示


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import static java.lang.Double.POSITIVE_INFINITY;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String s = input.nextLine();
        Judge jd = new Judge();
        jd.judge(s);
        if(!jd.number()){
            System.out.println("wrong number of points");
            System.exit(0);
        }
        if(jd.getIndex() == 1) {
            Point[] point = new Point[5];
            for (int i = 0; i < 5; i++) {
                point[i]=jd.pt.get(i);
            }
            pentagon pg = new pentagon(point);
            if(pg.ispentagon()&&jd.coincidence(jd.pt))
                System.out.println("true");
            else
                System.out.println("false");
        }
        else if(jd.getIndex() == 2) {
            Point[] point = new Point[5];
            for (int i = 0; i < 5; i++) {
                point[i]=jd.pt.get(i);
            }
            pentagon pg = new pentagon(point);
            if(pg.ispentagon()&&jd.coincidence(jd.pt)) {
                if(pg.istupentagon())
                    System.out.println("true " + pg.perimeter() + " " + pg.area());
                else
                    System.out.println("false");
            }
            else
                System.out.println("not a pentagon");
        }
        else if(jd.getIndex() == 3) {
            Line line = new Line(jd.pt.get(0), jd.pt.get(1));
            if(jd.pt.get(0).chonghe(jd.pt.get(1))) {
                System.out.println("points coincide");
                System.exit(0);
            }
            jd.pt.remove(0);jd.pt.remove(0);
            jd.coincidence2();
            if(jd.pt.size() == 5){
                Point[] point = new Point[5];
                for (int i = 0; i < 5; i++) {
                    point[i]=jd.pt.get(i);
                }
                pentagon pg = new pentagon(point);
                if(pg.chonghe(line))
                    System.out.println("The line is coincide with one of the lines");
                else
                    pg.Onepentagon(line);
            }
            else if(jd.pt.size() == 4){
                quadrilateral ql = new quadrilateral(jd.pt.get(0), jd.pt.get(1), jd.pt.get(2), jd.pt.get(3));
                if(ql.chonghe(line))
                    System.out.println("The line is coincide with one of the lines");
                else {
                    ql.Onequarilateral(line);
                }
            }
            else if(jd.pt.size() == 3) {
                triangle tl = new triangle(jd.pt.get(0), jd.pt.get(1), jd.pt.get(2));
                if(tl.chonghe(line))
                    System.out.println("The line is coincide with one of the lines");
                else {
                    tl.OneTriangle(line);
                }
            }
            else {
                System.out.println("not a polygon");
            }
        }
        else if(jd.getIndex() == 4) {

        }
        else if(jd.getIndex() == 5) {

        }
        else if(jd.getIndex() == 6) {
            Point p = new Point();
            p = jd.pt.get(0);jd.pt.remove(0);
            jd.coincidence2();
            if(jd.pt.size() == 5){
                Point[] point = new Point[5];
                for (int i = 0; i < 5; i++) {
                    point[i]=jd.pt.get(i);
                }
                pentagon pg = new pentagon(point);
               if(pg.pinpentagon(p) == 0)
                   System.out.println("outof the pentagon");
               else if(pg.pinpentagon(p) == 1)
                   System.out.println("on the pentagon");
               else if(pg.pinpentagon(p) == 2)
                   System.out.println("in the pentagon");

            }
            else if(jd.pt.size() == 4){
                quadrilateral ql = new quadrilateral(jd.pt.get(0), jd.pt.get(1), jd.pt.get(2), jd.pt.get(3));
                if(ql.pinquadrilateral(p) == 0)
                    System.out.println("outof the quadrilateral");
                else if(ql.pinquadrilateral(p) == 1)
                    System.out.println("on the quadrilateral");
                else if(ql.pinquadrilateral(p) == 2)
                    System.out.println("in the quadrilateral");

            }
            else if(jd.pt.size() == 3) {
                triangle tl = new triangle(jd.pt.get(0), jd.pt.get(1), jd.pt.get(2));
                if(tl.pintriangle(p) == 0)
                    System.out.println("outof the triangle");
                else if(tl.pintriangle(p)== 1)
                    System.out.println("on the triangle");
                else if(tl.pintriangle(p) == 2)
                    System.out.println("in the triangle");
            }
            else {
                System.out.println("not a polygon");
            }
        }
    }


}
class Judge {
    ArrayList<Point> pt = new ArrayList<>();
    private int index;

    public int getIndex() {
        return index;
    }

    public void judge(String s) {
        int flag =0;
        String regex = "[1-6]:([+-]?(0|(0\\.[0-9]+)|([1-9][0-9]*(\\.[0-9]+)?)),[+-]?(0|(0\\.[0-9]+)|([1-9][0-9]*(\\.[0-9]+)?))\\s?)+$";
        if(s.matches(regex)) {
            String[] string01 = s.split(":");
            index = Integer.parseInt(string01[0]);
            String[] strings = string01[1].split(" ");
            for (int i = 0; i < strings.length; i++) {
                Point pot = new Point();
                String[] s1 = strings[i].split(",");
                pot.setX(Double.parseDouble(s1[0]));
                pot.setY(Double.parseDouble(s1[1]));
                pt.add(pot);
            }
        }
        else {
            System.out.println("Wrong Format");
            System.exit(0);
        }

    }
    public boolean number(){
        if((pt.size() == 5 && (this.index == 1 || this.index == 2 )) || pt.size() == 7 && this.index == 3 || (pt.size() == 10 && (this.index == 4 || this.index == 5 )) || pt.size() == 6 && this.index == 6) {
            return true;
        }
        else
            return false;
    }
    //判断点是否重合
    public boolean coincidence(ArrayList<Point> a) {
        for (int i = 0; i < a.size(); i+=1) {
            for (int j = i+1; j < a.size(); j++) {
                //System.out.println(a.get(i).getX() + "," + a.get(j).getX() + "," + a.get(i).getY() + "," + a.get(j).getY());
                if(a.get(i).getX() == a.get(j).getX() && a.get(i).getY() == a.get(j).getY()) {
                    System.out.println("points coincide");
                    return false;
                }
            }
        }
        return true;
    }
    //去冗余
    public void coincidence2() {
        if(pt.get(0).chonghe(pt.get(2)))
            pt.remove(0);
        else if(pt.get(1).chonghe( pt.get(3)))
            pt.remove(1);
        for (int i = 0; i < pt.size()-2; i++) {
            if(pt.get(i).chonghe(pt.get(i+2)))
                pt.remove(i);
        }
        //判断重复的点
        for (int i = 0; i < pt.size() - 1; i++) {
            if(pt.get(i).chonghe(pt.get(i + 1))) {
                pt.remove(i);
            }
        }
        if(pt.get(0).chonghe(pt.get(pt.size()-1))) {
            pt.remove(0);
        }

        //判断点在两点之间
        for (int i = 0; i < pt.size(); i++) {
            if(Line.threeline(pt.get(i%pt.size()),pt.get((i+1)%pt.size()),pt.get((i+2)%pt.size()))) {
                pt.remove((i+1)%pt.size());i--;
            }
        }
    }
    public static double sswr(double s) {
        String str=String.valueOf(s);
        String substring = str.substring(str.indexOf(".") + 1);
        int str_len;
        if(substring.length()>3)
        {
            str_len=3;
        }else {
            str_len=substring.length();
        }
        String formats="%."+str_len+"f";
        String out=String.format(formats,s);
        double res=Double.parseDouble(out);
        return res;
    }
}
class Point {
    private double x;
    private double 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;
    }

    /*传入一个点
        计算两点间的距离
        返回两点间距离
        */
    public double distance(Point a) {
        double dis;
        dis = Math.sqrt(Math.pow((a.x - this.x),2) + Math.pow((a.y - this.y),2));
        return dis;
    }
    public boolean CheckPoint(Point p){
        if( Math.abs(this.getX() - p.getX() )< 0.000001  && Math.abs(this.getY() - p.getY() )< 0.000001){
            return true;
        }
        else
            return false;
    }
    public boolean chonghe(Point a){
        if(this.getX() == a.getX() && this.getY() == a.getY())
            return true;
        return false;
    }

}

class Line {
    private Point m,n;
    private double b;//y=kx+b
    private double k;
    private double A;
    private double B;
    private double C;
    public Point getM() {
        return m;
    }
    public Line() {

    }
    public Line(Point m,Point n) {
        this.m = m;
        this.n = n;
        this.slope();
        this.equation();//求出直线方程
    }
    public void setMN(Point m,Point n) {
        this.m = m;
        this.n = n;
        this.slope();
        this.equation();//求出直线方程
    }
    public Point getN() {
        return n;
    }
    public double getA() {
        return A;
    }
    public double getC() {
        return C;
    }
    public double getK() {
        return k;
    }
    public double getB() {
        return B;
    }

    //直线的斜率
    public void slope() {
        double se;
        if(m.getX() == n.getX())
            k = POSITIVE_INFINITY;
        else if(m.getY() == n.getY())
            k = 0;
        else {
            se = (m.getY()-n.getY())/(m.getX()-n.getX());
            k = se;
        }

    }
    public void equation() {
        double b;
        b = m.getY() - this.k * m.getX();
        A = n.getY() - m.getY();
        B = m.getX() - n.getX();
        C = n.getX() * m.getY() - m.getX() * n.getY();
    }
    //点到直线的距离
    double p_to_l(Point a,Line l) {
        //double dis = Math.abs(l.getA()*a.getX() + l.getB()*a.getY() + l.getC())/Math.sqrt(l.A * l.A + l.B* l.B);
        //return dis;
        if(l.k == 0) {
            return Math.abs(l.getM().getX()-a.getX());
        }
        else if(l.k == 1) {
            return Math.abs(l.getM().getY()-a.getY());
        }
        else {
            return Math.abs(l.getK()*a.getX()-a.getY()+ this.b)/(Math.sqrt(1+l.getK()*l.getK()));
        }
    }

    //判断三点一线,用于判断选项四的三角形情况
    public static boolean threeline(Point a, Point b, Point c) {
        Line line1 = new Line();
        line1.setMN(a,c);
        return line1.isallline(b) && line1.pointupline(b);
        /*Line line2 = new Line();
         line2.setMN(b,c);
        if(line2.k - line1.k < 0.000001)
            return true;
        else
            return false;*/
    }
    public boolean fourpoint(Line a) {
        if(Math.abs(a.k - this.k) < 0.000001)
            return true;
        else
            return false;
    }
    //求两直线交点坐标
    public Point intersection(Line a) {

        Point point = new Point();
        //if(a.k == this.k)
        //    return null;
        double det = this.getA() * a.getB() - a.getA() * this.getB();
        //if(det == 0)
        //    return null;
        point.setX(( this.getB() * a.getC() - a.getB() * this.getC() )/det);
        point.setY(( a.getA() * this.getC() - this.getA() * a.getC() )/det);
        return point;
    }
    //点在线端内,点到线上两端点的距离==两端点距离
    public boolean pointupline(Point a) {
        if(Math.abs(a.distance(this.getM()) + a.distance(this.getN()) - this.distance()) < 0.000001)
            return true;
        else
            return false;
    }
    //点在线上不包含端点
    public boolean pointupline2(Point a) {
        if(this.pointupline(a) && !this.getM().chonghe(a) && !this.getN().chonghe(a))
            return true;
        return false;
    }
    //点是否在直线上
    public boolean isallline(Point a) {
        if(Math.abs( getA() * a.getX() + getB() * a.getY() + getC() ) < 0.000001)
            return true;
        return false;
    }
    //线段距离
    public double distance() {
        double dis;
        dis = Math.sqrt(Math.pow((m.getX() - n.getX()),2) + Math.pow((m.getY() - n.getY()),2));
        return dis;
    }
    //判断点是否在直线一侧
    public boolean istongce(Point a,Point b) {
        double x1;
        double x2;
        x1 = this.getA() * a.getX() + this.getB() * a.getY() + this.getC();
        x2 = this.getA() * b.getX() + this.getB() * b.getY() + this.getC();
        if(x1 * x2 > 0)
            return true;
        else
            return false;
    }
    public boolean istongce(Point[] points) {
        double x1,x2;
        for (int i = 0; i < points.length-1; i++) {
            x1 = this.getA() * points[i].getX() + this.getB() * points[i].getY() + this.getC();
            x2 = this.getA() * points[i+1].getX() + this.getB() * points[i+1].getY() + this.getC();
            if(x1 * x2 < 0)
                return false;
        }
        return true;

    }
    //判断两条线重合
    public boolean chonghe(Line l) {
        if(this.isallline(l.getM()) && this.isallline(l.getN())) {
            return true;
        }
        return false;
    }
}
class triangle {
    private Line l1 = new Line();
    private Line l2 = new Line();
    private Line l3 = new Line();

    public triangle(Line l1, Line l2, Line l3) {
        this.l1 = l1;
        this.l2 = l2;
        this.l3 = l3;
    }

    public triangle(Point a, Point b, Point c) {
        this.l1.setMN(a, b);
        this.l2.setMN(b, c);
        this.l3.setMN(c, a);
    }

    public Line getL1() {
        return l1;
    }

    public void setL1(Line l1) {
        this.l1 = l1;
    }

    public Line getL2() {
        return l2;
    }

    public void setL2(Line l2) {
        this.l2 = l2;
    }

    public Line getL3() {
        return l3;
    }

    public void setL3(Line l3) {
        this.l3 = l3;
    }

    //是否能构成三角形
    public boolean istriangle() {

        if (l1.isallline(l2.getN()) || l2.isallline(l3.getN()) || l3.isallline(l1.getN()))
            return false;
        return true;
    }

    //等腰三角形
    public boolean istwoline() {
        if (Math.abs(l1.distance() - l2.distance()) < 0.000001 || Math.abs(l2.distance() - l3.distance()) < 0.000001 || Math.abs(l2.distance() - l3.distance()) < 0.000001) {
            return true;
        } else
            return false;
    }

    //等边三角形
    public boolean threeline() {
        if (Math.abs(l1.distance() - l2.distance()) < 0.000001 && Math.abs(l1.distance() - l3.distance()) < 0.000001 && Math.abs(l2.distance() - l3.distance()) < 0.000001) {
            return true;
        } else
            return false;
    }

    //三角形周长
    public double Perimeter() {

        return l1.distance() + l2.distance() + l3.distance();
    }

    //三角形面积
    public double area() {
        double s = this.Perimeter() * 0.5;
        return Math.sqrt(s * (s - l1.distance()) * (s - l2.distance()) * (s - l3.distance()));

    }
    public double area3(){
        return l1.p_to_l(l1.getM(),l1) * l1.distance() *0.5;
    }

    public double area2(Point a, Point b, Point c) {
        double d1, d2, d3;
        d1 = a.distance(b);
        d2 = b.distance(c);
        d3 = a.distance(c);
        double s = 0.5 * (d1 + d2 + d3);
        return Math.sqrt(s * (s - d1) * (s - d2) * (s - d3));
    }

    //重心
    public Point focus() {
        Point weight = new Point();
        weight.setX((l1.getM().getX() + l1.getN().getX() + l2.getN().getX()) * 1.0 / 3);
        weight.setY((l1.getM().getY() + l1.getN().getY() + l2.getN().getY()) * 1.0 / 3);
        return weight;
    }

    //求余弦
    public double[] getcos() {
        double[] cos = new double[3];
        cos[0] = (l1.distance() * l1.distance() + l2.distance() * l2.distance() - l3.distance() * l3.distance()) / (2 * l1.distance() * l2.distance());
        cos[1] = (l1.distance() * l1.distance() + l3.distance() * l3.distance() - l2.distance() * l2.distance()) / (2 * l1.distance() * l3.distance());
        cos[2] = (l3.distance() * l3.distance() + l2.distance() * l2.distance() - l1.distance() * l1.distance()) / (2 * l3.distance() * l2.distance());
        return cos;
    }

    public double getcos(Point a, Point b, Point c) {
        double l1 = a.distance(b);
        double l2 = a.distance(c);
        double l3 = b.distance(c);
        return (l1 * l1 + l2 * l2 - l3 * l3) / (2 * l1 * l2);

    }
    // 点在三角形的边上
    public boolean pupline(Point a) {
        if(l1.pointupline(a) && l1.isallline(a)){
            return true;
        }
        else if(l2.isallline(a) && l2.pointupline(a)) {
            return true;
        }
        else if(l3.pointupline(a) && l3.isallline(a)) {
            return true;
        }
        else
            return false;
    }
    //点与三角行关系
    public int pintriangle(Point a) {
        triangle tl1 = new triangle(l1.getM(), l1.getN(), a);
        triangle tl2 = new triangle(l2.getM(), l2.getN(), a);
        triangle tl3 = new triangle(l3.getM(), l3.getN(), a);
        if(this.pupline(a))
            return 1;
        else if(!this.pupline(a) && Math.abs(tl1.area() + tl2.area() + tl3.area() - this.area()) < 0.0001)
            return 2;
        else
            return 0;
    }
    public boolean chonghe(Line l) {
        if(l1.chonghe(l))
            return true;
        if(l2.chonghe(l))
            return true;
        if(l3.chonghe(l))
            return true;
        return false;
    }
    public void OneTriangle(Line line){
        List<Line> lineList = new ArrayList<>();
        lineList.add(l1);lineList.add(l2);lineList.add(l3);
        List<Point> list = new ArrayList<>();
        int cnt = 0;
        Point u;
        for(int i=0;i<lineList.size();i++){
            u = lineList.get(i).intersection(line);

            boolean flag = false;
            if( u != null){
                if(lineList.get(i).pointupline(u)){
                    flag = true;
                }

                for (Point p : list){
                    if( p.CheckPoint(u) ){
                        flag = false;
                        break;
                    }
                }

                if(flag){
                    list.add(u);
                    //System.out.println(u.getX() + " " + u.getY() );
                    cnt++;
                }
            }
        }
        if(cnt < 2){
            System.out.println(cnt);
        }
        else {
            double s1,s2,s;
            System.out.print("2 ");
            Line lline = new Line(list.get(0), list.get(1));
            Point p1 = list.get(0),p2 = list.get(1);
            Point m;
            if(l1.isallline(p1) && l2.isallline(p2) || l2.isallline(p2) && l1.isallline(p1))
                m = l1.getN();
            else if( l1.isallline(p1) && l3.isallline(p2) || l3.isallline(  p2) && l1.isallline(p1) )
                m = l1.getM();
            else
                m = l2.getN();
            triangle triangle1 = new triangle(m,list.get(0),list.get(1));

            s1 = triangle1.area();
            s1 = Judge.sswr(s1);
            s = this.area();
            s = Judge.sswr(s);
            s2 = s - s1;
            s2 = Judge.sswr(s2);

            System.out.println(Math.min(s1,s2) + " " + Math.max(s1,s2));
        }
    }
}
class quadrilateral {
   Line[] lines = new Line[4];
    Point[] points = new Point[4];
    public quadrilateral(Point a,Point b,Point c,Point d) {
        points[0] = a;points[1] = b;points[2] = c; points[3] = d;
        for (int i = 0; i < 4; i++) {
            Line line = new Line(points[i], points[(i+1)%4]);
            lines[i] = line;
        }
    }
    public boolean isquadrilateral() {
        for (int i = 0; i < 4; i++) {
            if(lines[i%4].getK() == lines[(i+1)%4].getK()){
                return false;
            }
            if(lines[i].pointupline(lines[i%4].intersection(lines[(i+2)%4])) && lines[(i+2)%4].pointupline(lines[i%4].intersection(lines[(i+2)%4]))){
                return false;
            }
        }
        return true;
    }
    public boolean ispingxing() {
        if(lines[0].getK() == lines[2].getK() && lines[1].getK() == lines[3].getK()){
            return true;
        }
        else
            return false;
    }
    public boolean islingxing() {
        if(!this.ispingxing())
            return false;
        for (int i = 0; i < lines.length-1; i++) {
            if(lines[i].distance() != lines[i + 1].distance())
                return false;
        }
            return true;
    }
    //判断凸四边形
    public boolean istusibainxing() {
        for (int i = 0; i < lines.length; i++) {
            for (int j = 1; j <= points.length-3 ; j++) {
                if(!lines[i].istongce(points[(i+j+1)%points.length],points[(i+j+2)%points.length]))
                    return false;
            }
        }
        return true;
    }
    //四边形周长
    public double perimeter() {
        double p=0;
        for (int i = 0; i < lines.length; i++) {
            p+=lines[i].distance();
        }
        return Judge.sswr(p);
    }
    //四边形面积
    public double area() {
        double a=0;
        triangle triangle01 = new triangle(lines[0].getM(), lines[0].getN(), lines[1].getN());
        triangle triangle02 = new triangle(lines[2].getM(), lines[2].getN(), lines[3].getN());
        triangle triangle03 = new triangle(lines[1].getM(), lines[1].getN(), lines[2].getN());
        triangle triangle04 = new triangle(lines[3].getM(), lines[3].getN(), lines[0].getN());
        double qarea01,qarea02;
        qarea01 = triangle01.area() + triangle02.area();
        qarea02 = triangle03.area() + triangle04.area();
        if(qarea02 < qarea01)//凹四边形面积比较特殊,要找凹点,这里偷懒直接判段小的输出
            return qarea02;
        else
            return qarea01;
    }
    //点在四边形内部
    public int pinquadrilateral(Point a) {
        double area1 = 0;
        for (int i = 0; i < lines.length; i++) {
            if(lines[i].pointupline(a))
                return 1;
            triangle t = new triangle(a, lines[i].getM(), lines[i].getN());
            area1 += t.area();
        }
        if(Math.abs(area1-this.area()) < 0.01)
            return 2;
        else
            return 0;
    }
    public boolean chonghe(Line line) {
        for (int i = 0; i < lines.length; i++) {
            if(lines[i].chonghe(line))
                return true;
        }
        return false;
    }
    //四边形分割成两个求面积,有两个交点
    public void fengeArea(Point a,Point b,Point c) {
        triangle tr1 = new triangle(a,b,c);
        double area1 = tr1.area();
        double area = this.area();
        double area2 = area - area1;
        area1 = Judge.sswr(area1);
        area2 = Judge.sswr(area2);
        System.out.println("2 " + Math.min(area1,area2) + " " + Math.max(area1,area2));
    }
    public void Onequarilateral(Line l) {
        Point[] point = new Point[4];
        ArrayList<Line> liness = new ArrayList<>();
        liness.add(lines[0]);liness.add(lines[1]);liness.add(lines[2]);liness.add(lines[3]);
        int[] p_in = new int[4];

        //判断交点在线上还是端点
        for (int i = 0; i < 4; i++) {
            p_in[i] = 0;
            //求直线与四边形的交点
            point[i] = liness.get(i).intersection(l);
            if(point[i]!=null) {
                //判断交点是否在边上,不包括端点
                if(liness.get(i).pointupline2(point[i]))
                    p_in[i] = 1;
            }
        }
        //有顶角在线上
        for (int i = 0; i < 4; i++) {
            if(l.isallline(points[i])) {
                if(l.isallline((points[(i+2)%4]))) {
                    fengeArea(points[i], points[(i + 2) % 4], points[(i + 1) % 4]);
                    return;
                }
                else if(p_in[(i+1)%4] == 1) {
                    fengeArea(points[i], points[(i + 1) % 4], point[(i + 1) % 4]);
                    return;
                }
                else if(p_in[(i+2)%4] == 1) {
                    fengeArea(points[i], points[(i + 3) % 4], point[(i + 2) % 4]);
                    return;
                }
                else {
                    System.out.println("1");
                    return;
                }
            }
        }
        //两个临边
        for (int i = 0; i < 4; i++) {
            if(p_in[i] == 1 && p_in[(i+1)%4] == 1) {
                fengeArea(points[(i + 1) % 4], point[i], point[(i + 1) % 4]);
                return;
            }
        }
        //两个对边
        for (int i = 0; i < 2; i++) {
            if(p_in[i] == 1 && p_in[i+2] == 1) {
                triangle tr1 = new triangle(points[i], point[i], point[(i + 2) % 4]);
                triangle tr2 = new triangle(points[(i+3)%4], points[i], point[(i + 2) % 4]);
                double area1 = tr1.area() + tr2.area();
                double area = this.area();
                double area2 = area - area1;
                area1 = Judge.sswr(area1);
                area2 = Judge.sswr(area2);
                System.out.println("2 " + Math.min(area1,area2) + " " + Math.max(area1,area2));
                return;
            }
        }
        System.out.println("0");
        return;
    }
}
class pentagon{
    public Line[] lines = new Line[5];
    Point[] points = new Point[5];

    public pentagon(Line[] lines) {
        this.lines = lines;
        for (int i = 0; i < 5; i++) {
            points[(i+1)%5]=lines[i].getN();
        }
    }
    public pentagon(Point[] points){
        this.points=points;
        for (int i = 0; i < 5; i++) {
            Line line = new Line(points[i % 5], points[(i + 1) % 5]);
            this.lines[i]=line;
        }
    }

    public boolean ispentagon(){
        for (int i = 0; i < 5; i++) {
            if(lines[i%5].getK() == lines[(i+1)%5].getK()){
                return false;
            }
            if(lines[i].pointupline(lines[i%5].intersection(lines[(i+2)%5])) && lines[(i+2)%5].pointupline(lines[i%5].intersection(lines[(i+2)%5]))){
                return false;
            }
        }
        return true;
    }
    public boolean istupentagon(){
        for (int i = 0; i < lines.length; i++) {
            for (int j = 1; j <= points.length-3 ; j++) {
                if(!lines[i].istongce(points[(i+j+1)%points.length],points[(i+j+2)%points.length]))
                    return false;
            }
        }
        return true;
    }
    public double perimeter(){
        double p=0;
        for (int i = 0; i < lines.length; i++) {
            p+=lines[i].distance();
        }
        return Judge.sswr(p);
    }
    public double area(){
        double a=0;
        for (int i = 1; i < points.length-1; i++) {
            triangle t = new triangle(points[0], points[i], points[i + 1]);
            a+=t.area();
        }
        return Judge.sswr(a);
    }
    public void fengeArea(Point a,Point b,Point c) {
        triangle tr1 = new triangle(a,b,c);
        double area1 = tr1.area();
        double area = this.area();
        double area2 = area - area1;
        area1 = Judge.sswr(area1);
        area2 = Judge.sswr(area2);
        System.out.println("2 " + Math.min(area1,area2) + " " + Math.max(area1,area2));
    }
    public boolean chonghe(Line line) {
        for (int i = 0; i < lines.length; i++) {
            if(lines[i].chonghe(line))
                return true;
        }
        return false;
    }
    public void fengeArea(Point a,Point b,Point c,Point d) {
        quadrilateral ql1 = new quadrilateral(a, b, c, d);
        double area1 = ql1.area();
        double area = this.area();
        double area2 = area - area1;
        area1 = Judge.sswr(area1);
        area2 = Judge.sswr(area2);
        System.out.println("2 " + Math.min(area1,area2) + " " + Math.max(area1,area2));
    }
    public void Onepentagon(Line line) {
        Point[] pn = new Point[5];
        int[] p_in = new int[5];
        for (int i = 0; i < lines.length; i++) {
            p_in[i] = 0;//初始没有交点
            pn[i] = lines[i].intersection(line);//求边于线是否有交点
            if(pn[i] != null) {
                //判断交点是否在边上,不包括端点
                if(lines[i].pointupline2(pn[i]))
                    p_in[i] = 1;
            }
        }
        //有顶角在线上
        for (int i = 0; i < points.length; i++) {
            if(line.isallline(points[i])) {
                for (int j = 0; j < points.length-3; j++) {//直线通过对角切割
                    if(line.isallline(points[(i+j+2)%points.length])) {
                        if(j==0) {
                            fengeArea(points[i],points[(i+j+2)%points.length],points[(i+j+2-1)%points.length]);
                            return;
                        }
                        else {
                            fengeArea(points[i],points[(i+j+2)%points.length],points[(i+j+2+1)%points.length]);
                            return;
                        }
                    }
                }
                for (int j = 0; j < lines.length-2; j++) {
                    if(p_in[(i+j+1)%lines.length] == 1) {
                        if(i+j+1>1 && i+j+1<points.length-2){
                            fengeArea(points[i],points[(i+j+1-1)%points.length],points[(i+j+1)%points.length],pn[(i+j+1)%lines.length]);
                            return;
                        }
                        else{
                            if(j==0){
                                fengeArea(points[i],points[(i+j+1)%points.length],pn[(i+j+1)%lines.length]);
                                return;
                            }
                            else if(i+j+1==lines.length-2) {
                                fengeArea(points[i],points[((i+j+1)+1)%points.length],pn[(i+j+1)%lines.length]);
                                return;
                            }

                        }
                    }
                }
                    System.out.println("1");
                    return;
                }
            }

        //两个临边
        for (int i = 0; i < points.length; i++) {
            if(p_in[i] == 1 && p_in[(i+1)%lines.length] == 1) {
                fengeArea(points[(i + 1) % points.length], pn[i], pn[(i + 1) % points.length]);
                return;
            }
        }
    }
    public int pinpentagon(Point a) {
        double area1 = 0;
        for (int i = 0; i < lines.length; i++) {
            if(lines[i].pointupline(a))
                return 1;
            triangle t = new triangle(a, lines[i].getM(), lines[i].getN());
            area1 += t.area();
        }
        if(Math.abs(area1-this.area()) < 0.01)
            return 2;
        else
            return 0;
    }
}

解释与心得
对于本题我觉得难度还能接受,直接在原四边形代码基础上直接添加一个五边形类完成其相关操作,稍微花点时间完全可以写出来。

7-2点线形系列5-凸五边形的计算-2

用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
4:输入十个点坐标,前、后五个点分别构成一个凸多边形(三角形、四边形、五边形),判断它们两个之间是否存在包含关系(一个多边形有一条或多条边与另一个多边形重合,其他部分都包含在另一个多边形内部,也算包含)。
两者存在六种关系:1、分离(完全无重合点) 2、连接(只有一个点或一条边重合) 3、完全重合 4、被包含(前一个多边形在后一个多边形的内部)5、交错 6、包含(后一个多边形在前一个多边形的内部)。
各种关系的输出格式如下:
1、no overlapping area between the previous triangle/quadrilateral/ pentagon and the following triangle/quadrilateral/ pentagon
2、the previous triangle/quadrilateral/ pentagon is connected to the following triangle/quadrilateral/ pentagon
3、the previous triangle/quadrilateral/ pentagon coincides with the following triangle/quadrilateral/ pentagon
4、the previous triangle/quadrilateral/ pentagon is inside the following triangle/quadrilateral/ pentagon
5、the previous triangle/quadrilateral/ pentagon is interlaced with the following triangle/quadrilateral/ pentagon
6、the previous triangle/quadrilateral/ pentagon contains the following triangle/quadrilateral/ pentagon

5:输入十个点坐标,前、后五个点分别构成一个凸多边形(三角形、四边形、五边形),输出两个多边形公共区域的面积。注:只考虑每个多边形被另一个多边形分割成最多两个部分的情况,不考虑一个多边形将另一个分割成超过两个区域的情况。
6:输入六个点坐标,输出第一个是否在后五个点所构成的多边形(限定为凸多边形,不考虑凹多边形),的内部(若是五边形输出in the pentagon/outof the pentagon,若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。输入入错存在冗余点要排除,冗余点的判定方法见选项5。如果点在多边形的某条边上,输出”on the triangle/on the quadrilateral/on the pentagon”。
以上4、5、6选项输入的五个点坐标可能存在冗余,假设多边形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如:x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z不与xy都相邻,如:z x y s、x z s y、x s z y

输入格式:
基本格式:选项+”:”+坐标x+”,”+坐标y+” “+坐标x+”,”+坐标y。点的x、y坐标之间以英文”,”分隔,点与点之间以一个英文空格分隔。

输出格式:
输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0

代码展示
本题我没有做出来,代码就不展示了

解释与心得
本题一定要对之前的代码进行重构,用好继承这个知识点,要不然题目真的很难做。

期中考试

7-1 点与线(类设计)

设计一个类表示平面直角坐标系上的点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)方法。
类图如下

代码展示


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Point[] points = new Point[2];
        for (int i = 0; i < 2; i++) {
            double x = input.nextDouble();
            double y = input.nextDouble();
            if(x<0 || x> 200 || y<0 || y>200)
            {
                System.out.println("Wrong Format");
                System.exit(0);
            }
            Point point = new Point(x, y);
            points[i]=point;
        }
        Line line = new Line(points[0],points[1],input.next());
        line.display();
    }
}
class Point {
    double x,y;
    public Point(){

    }
    public 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;
    }
    public void display(){
        System.out.println("("+ String.format("%.2f",x) + "," + String.format("%.2f",y) + ")");
    }
}
class Line{
    Point point1 = new Point();
    Point point2 = new Point();
    String color;

    public Line() {
    }

    public Line(Point point1, Point point2, String color) {
        this.point1 = point1;
        this.point2 = point2;
        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  Math.sqrt(Math.pow((point1.getX() - point2.getX()),2) + Math.pow((point1.getY() - point2.getY()),2));
    }
    /*  ```
      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:长度值
  ```
*/
    public void display() {
        System.out.println("The line's color is:" + this.color);
        System.out.println("The line's begin point's Coordinate is:");
        point1.display();
        System.out.println("The line's end point's Coordinate is:");
        point2.display();
        System.out.println("The line's length is:" + String.format("%.2f",this.getDistance()));
    }
}

解释与心得
题目不难,但是一定要把题目所有的要求输入输出写进去,要不然会不断报错影响心态。

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

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

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

类结构如下图所示。

代码展示


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Point[] points = new Point[2];
        for (int i = 0; i < 2; i++) {
            double x = input.nextDouble();
            double y = input.nextDouble();
            if(x<0 || x> 200 || y<0 || y>200)
            {
                System.out.println("Wrong Format");
                System.exit(0);
            }
            Point point = new Point(x, y);
            points[i]=point;
        }
        Line line = new Line(points[0],points[1],input.next());
        Plane plane = new Plane(line.getColor());
        Element element;
        element = points[0];
        element.display();
        element = points[1];
        element.display();
        element = line;
        element.display();
        element = plane;
        element.display();
    }
}
abstract class Element {
    public void display() {}
}
class Point extends Element{
    double x,y;
    public Point(){

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

    @Override
    public void display() {
        System.out.println("("+ String.format("%.2f",x) + "," + String.format("%.2f",y) + ")");
    }
}
class Line extends Element{
    Point point1 = new Point();
    Point point2 = new Point();
    String color;

    public Line() {
    }

    public Line(Point point1, Point point2, String color) {
        this.point1 = point1;
        this.point2 = point2;
        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  Math.sqrt(Math.pow((point1.getX() - point2.getX()),2) + Math.pow((point1.getY() - point2.getY()),2));
    }
    /*  ```
      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:长度值
  ```
*/

    @Override
    public void display() {
        System.out.println("The line's color is:" + this.color);
        System.out.println("The line's begin point's Coordinate is:");
        point1.display();
        System.out.println("The line's end point's Coordinate is:");
        point2.display();
        System.out.println("The line's length is:" + String.format("%.2f",this.getDistance()));
    }
}
class Plane extends Element {
    String color;

    public Plane() {
    }

    public 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:" + this.color);
    }
}

解释与心得
本题也不难,主要考察类的继承和多态,主要是考察这些知识点基本的掌握情况。我在这道题时在多态的地方卡了下,主要是知识点熟练度还是欠缺了点。

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

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

在原有类设计的基础上,增加一个GeometryObject容器类,其属性为ArrayList
类型的对象(若不了解泛型,可以不使用



增加该类的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()方法进行输出。

类图如下所示:




代码展示

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        GeometryObject c = new GeometryObject();
        int choice = input.nextInt();
        while(choice != 0) {
            switch(choice) {
                case 1:double x = input.nextDouble();
                    double y = input.nextDouble();
                    if(x<0 || x> 200 || y<0 || y>200)
                    {
                        System.out.println("Wrong Format");
                        System.exit(0);
                    }
                    Point point = new Point(x, y);
                    c.add(point);
                    break;
                case 2:Point[] points = new Point[2];
                    for (int i = 0; i < 2; i++) {
                        double x1 = input.nextDouble();
                        double y1 = input.nextDouble();
                        if(x1<0 || x1> 200 || y1<0 || y1>200)
                        {
                            System.out.println("Wrong Format");
                            System.exit(0);
                        }
                        Point point1 = new Point(x1, y1);
                        points[i]=point1;
                    }
                    Line line = new Line(points[0],points[1],input.next());
                    c.add(line);
                    break;
                case 3:String color;
                    Plane plane = new Plane(input.next());
                    c.add(plane);
                    break;
                case 4:
                    int index = input.nextInt();
                    c.remove(index);
            }
            choice = input.nextInt();
        }
        for (Element i:c.getList()) {
            i.display();
        }
    }
}
class GeometryObject{
    private ArrayList<Element> list = new ArrayList<>();

    public GeometryObject() {

    }

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

    public void remove(int index) {
        if(index < 1 || index > list.size()) {
            return;
        }

        list.remove(index - 1);
    }

    public ArrayList<Element> getList(){
        return this.list;
    }
}
abstract class Element {
    public void display() {}
}
class Point extends Element{
    double x,y;
    public Point(){

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

    @Override
    public void display() {
        System.out.println("("+ String.format("%.2f",x) + "," + String.format("%.2f",y) + ")");
    }
}
class Line extends Element{
    Point point1 = new Point();
    Point point2 = new Point();
    String color;

    public Line() {
    }

    public Line(Point point1, Point point2, String color) {
        this.point1 = point1;
        this.point2 = point2;
        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  Math.sqrt(Math.pow((point1.getX() - point2.getX()),2) + Math.pow((point1.getY() - point2.getY()),2));
    }
    /*  ```
      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:长度值
  ```
*/

    @Override
    public void display() {
        System.out.println("The line's color is:" + this.color);
        System.out.println("The line's begin point's Coordinate is:");
        point1.display();
        System.out.println("The line's end point's Coordinate is:");
        point2.display();
        System.out.println("The line's length is:" + String.format("%.2f",this.getDistance()));
    }
}
class Plane extends Element {
    String color;

    public Plane() {
    }

    public 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:" + this.color);
    }
}

解释与心得
本题相对上题增加了一些功能,将代码完善使代码实用性更强。更加面向对象。主要考察对象的容器以及其遍历,需要新建一个GeometryObject容器类,其包含的属性为ArrayList
,之后完成类图要求,增加ArrayList的增加元素方法和减少元素方法以及获取List表的方法,同时修改main类,将构造改成与题目提示相同,

三、踩坑心得

1.对于一些题目好多隐含输出格式他没有明说一定要自己去悟,输出格式这有一点坑。
2.对于代码在写的时候要注意代码的复用性,要逐渐面向对象,如果面向过程的写题目很难做出来,而且代码也很容易超过内存限制。

四、代码改进建议

我觉得我的代码,还有很大的问题,首先命名问题,命名太乱就算是我重新看自己代码没有注释也很难一下子知道自己的一些方法是什么功能。其次,代码没有使用继承使得代码复用性比较差,使用继承的话可以提高代码复用性也可以使代码的长度减少。最后自己在主方法里的内容有点繁琐,应该使用多态去进行简化。

五、作业总结

对于多态还有抽象类这块知识点掌握还有一些问题,后续一定几时去把这块知识点补上。
对于题目一定要先进行设计,再去进行代码的编写,这样有助于提高自己代码的结构清晰度、使代码复用性更强,

原文地址:http://www.cnblogs.com/1127sj/p/16838443.html

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