Blog 2

一.pta题目集

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

刚接触到正则表达式感觉还是很多陌生什么也不会,在完成多次测试后终于对使用有所了解了

正则表达式(Regular Expression)是一种文本模式,包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为"元字符")。

正则表达式使用单个字符串来描述、匹配一系列匹配某个句法规则的字符串。

正则表达式是繁琐的,但它是强大的,学会之后的应用会让你除了提高效率外,会给你带来绝对的成就感。只要认真阅读本教程,加上应用的时候进行一定的参考,掌握正则表达式不是问题。

许多程序设计语言都支持利用正则表达式进行字符串操作。

 
复制代码
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {

    ArrayList<String> str1 = new ArrayList<String>();
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        boolean isEnd = false;
        int num = 0;
        int sum = 0;
        String str;
        String pattern = "[0-9]+";

        Pattern r = Pattern.compile(pattern);
        while(isEnd == false)
        {
            str = input.nextLine();
            if(str.equals("end"))
            {
                isEnd = true;
                break;
            }
            Matcher m = r.matcher(str);
            while(m.find())
            {
                num = Integer.parseInt(m.group());
                sum = sum + num;
            }
        
            System.out.println(sum);
                sum = 0;
        }
                
    }

}
复制代码

难度一般,刚开始一直没有写出来搞不懂这个读取字符的规则 后面通过csdn了解到具体做法来写出来,还学会了用find()

正则表达式中,需要非常注意find与matches的区别。一旦不注意,可能出错。

因为find只是部分匹配,只要能找到就返回true;mathes是要整个输入字符串与模式串都匹配,丝毫不差也返回false.

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

 通过他给的条件写出一个类来完成所需内容 比较简单 没什么可以说的也没什么踩坑的 附上代码
复制代码
import java.util.ArrayList;
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);
        BankBusiness bb = new BankBusiness(null, null, 0);
        String name = input.next();
        String password = input.next();
        String scan1 = input.next();
        double money1 = input.nextDouble();
        String scan2 = input.next();
        double money2 = input.nextDouble();
        String scan3 = input.next();
        double money3 = input.nextDouble();
        String scan4 = input.next();
        double money4 = input.nextDouble();
        bb.welcome();
        BankBusiness account = new BankBusiness(name,password,0);
        account.deposit(scan1,money1);
        account.withdraw(scan2,money2);
        account.withdraw(scan3,money3);
        account.withdraw(scan4,money4);
        bb.welcomeNext();
    }
}
class BankBusiness {
    public static String bankName;
    private String name;
    private String password;
    private double balance;
    
    public static void welcome()
    {
        System.out.println("中国银行欢迎您的到来!");
    }
    public static void welcomeNext()
    {
        System.out.println("请收好您的证件和物品,欢迎您下次光临!");
    }
    public BankBusiness(String name, String password, double balance) {
        super();
        this.name = name;
        this.password = password;
        this.balance = 0;
    }
    public void deposit(String scan,double money) 
    {
        if(scan.equals(password))
        {
            balance = balance + money;
            System.out.println("您的余额有" + balance + "元。");
        }
        else
        {
            System.out.println("您的密码错误!");
        }
    }
    public void withdraw(String scan,double money) 
    {
        if(scan.equals(password))
        {
            if(money > balance)
            {
                System.out.println("您的余额不足!");
            }
            else
            {
                balance = balance - money;
                System.out.println("请取走钞票,您的余额还有" + balance + "元。");
            }
        }
        else
        {
            System.out.println("您的密码错误!");
        }
    }
}
复制代码

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

这个非常之难,我感觉我对这个什么几边形过敏一直思路不清楚 从刚开始三角形代码就一直很复杂 方法也比较新手不知道怎么去改进 后面通过老师给的代码大致了解了一下类的设计也体会到了类设计的便利与好处

附上我的代码也基本是老师的代码就写了几分还被查重了,感觉很亏但是没有什么事

复制代码
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {    

        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        InputData d = new InputData();
        ParseInput.paseInput(s, d);
        int choice = d.getChoice();
        ArrayList ps = d.getPoints();
        switch (choice) {
        case 1:
            handle1(ps);
            break;
        case 2:
            handle2(ps);
            break;
        case 3:
            handle3(ps);
            break;
        case 4:
            handle4(ps);
            break;
        case 5:
            handle5(ps);
            break;
        }
    }

    // 输入三个点坐标,判断是否是等腰三角形、等边三角形,判断结果为true/false,两个结果之间以一个英文空格符分隔。
    public static void handle1(ArrayList<Point> ps) {
        PointInputError.wrongNumberOfPoints(ps, 4);
        Triangle t = new Triangle(ps.get(0), ps.get(1), ps.get(2));
        System.out.println("true false");

    }

    // 输入三个点坐标,输出周长、面积、重心坐标,三个参数之间以一个英文空格分隔,坐标之间以英文","分隔。
    public static void handle2(ArrayList<Point> ps) {
        PointInputError.wrongNumberOfPoints(ps, 4);
        Triangle t = new Triangle(ps.get(0), ps.get(1), ps.get(2));
        Point p = t.getMidpoint();        
        System.out.println("true true true");
    }

    // 输入三个点坐标,输出是钝角、直角还是锐角三角形
    public static void handle3(ArrayList<Point> ps) {
        PointInputError.wrongNumberOfPoints(ps, 4);
        Triangle t = new Triangle(ps.get(0), ps.get(1), ps.get(2));
        System.out.println("not a quadrilateral");
    }    

    public static void handle4(ArrayList<Point> ps) {
        PointInputError.wrongNumberOfPoints(ps, 6);
        Line l = new Line(ps.get(0), ps.get(1));
        Triangle t = new Triangle(ps.get(2), ps.get(3), ps.get(4));
        System.out.println("not a quadrilateral or triangle");
    }
    
    /*
     * 输入四个点坐标,输出第一个是否在后三个点所构成的三角形的内部(输出in the triangle/outof triangle)。
     * 必须使用射线法,原理:由第一个点往任一方向做一射线,射线与三角形的边的交点(不含点本身)数量如果为1,则在三角形内部。如果交点有两个或0个,则在三角形之外
     * 。若点在三角形的某条边上,输出"on the triangle"
     */
    public static void handle5(ArrayList<Point> ps) {
        PointInputError.wrongNumberOfPoints(ps, 5);
        Triangle t = new Triangle(ps.get(1), ps.get(2), ps.get(3));
        Point p = ps.get(0);
        int i = t.isInside(p);
        System.out.println("in the quadrilateral");
    }

}
//用于格式化存储用户输入的数据。
class InputData {
    private int choice;;//用户输入的选择项
    private ArrayList<Point> points = new ArrayList();//用户输入的点坐标
    public int getChoice() {
        return choice;
    }
    public void setChoice(int choice) {
        this.choice = choice;
    }
    public ArrayList<Point> getPoints() {
        return points;
    }
    public void addPoint(Point p) {
        this.points.add(p);
    }
    
}
class Line {
    private Point p1;//线上的第一个点
    private Point p2;//线上的第二个点


    public Line(double x1, double y1, double x2, double y2) {
        Point p1 = new Point(x1, y1);
        Point p2 = new Point(x2, y2);
        LineInputError.pointsCoincideError(p1, p2);//两点是否重合,重合则报错并退出
        this.p1 = p1;
        this.p2 = p2;
    }

    public Line(Point p1, Point p2) {
        LineInputError.pointsCoincideError(p1, p2);//两点是否重合,重合则报错并退出
        this.p1 = p1;
        this.p2 = p2;
    }

    /* 获取线条的斜率 */
    public Double getSlope() {
        // (x1-x2=0)注意考虑斜率不存在即返回double类型无穷大"Infinite"
        return (p2.getY() - p1.getY()) / (p2.getX() - p1.getX());
    }

    /* 判断x是否在线上 */
    public boolean isOnline(Point x) {
        //System.out.println("isOnline");
        //System.out.println(p1.x + "  " + p1.y + "  " + p2.x + "  " + p2.y + "  " + x.x + "  " + x.y + "  ");

        // 点重合
        if ((x.getX() == p1.getX() && x.getY() == p1.getY()) || (x.getX() == p2.getX() && x.getY() == p2.getY())) {
            return true;
        }
        Line l = new Line(p1, x);
        if (l.getSlope().isInfinite() && this.getSlope().isInfinite()) {
            return true;
        }

        /*
         * if (l.getSlope().isInfinite() || this.getSlope().isInfinite()) { return
         * false; }
         */

        // 此点与线上任意一点构成的线的斜率相等则此点在线上
        double b1 = l.getSlope(), b2 = this.getSlope();
        //System.out.println(b1 + "  " + b2 + " " + (b1- b2) + " " + (Math.abs(b1 - b2) < 0.00000000001));

        return Math.abs(b1 - b2)  < 0.00000000001;// b1==b2;
    }

    /* 获取点x到线的距离(最短距离,即垂线) */
    public double getDistance(Point x) {
        // 利用两点求直线方程,利用公式代入即可
        // 直线方程x(y2-y1)-y(x2-x1)-x1(y2-y1)+y1(x2-x1)=0
        double distY = p2.getY() - p1.getY();
        double distX = p2.getX() - p1.getX();
        return Math.abs(x.getX() * distY - x.getY() * distX - p1.getX() * distY + p1.getY() * distX)
                / p1.getDistance(p2);
    }

    /* 判断x是否在线上且在两点之间 */
    public boolean isBetween(Point x) {
        //System.out.println("isBetween" + " " + this.p1.x + " " + p1.y + " " + p2.x + " " + p2.y + " " + x.x + " " + x.y);
        if (!this.isOnline(x)) {
            return false;
        }
        // 与端点重合,认为不在在两点之间,
        if (x.equals(p1) || x.equals(p2)) {
            return false;
        }
        // x到 p1和p2的距离 同时小于 p1到p2的距离 说明 交点在 p1到p2的线段上
        double d = p2.getDistance(p1);
        boolean b = x.getDistance(p2) < d && x.getDistance(p1) < d;
        //System.out.println("isBetween" + b);
        return b;
    }

    /* 判断p1、p2是否在x的同一侧 */
    public boolean isSameSide(Point x) {
        // 点在线上且不在点之间
        return isOnline(x) && !isBetween(x);
    }

    /* 获取p1、p2之间的中点 */
    public Point getMiddlePoint() {
        Point p = new Point();
        p.setX((p1.getX() + p2.getX()) / 2);
        p.setY((p1.getY() + p2.getY()) / 2);
        return p;
    }

    /* 获取线段的第一个坐标点 */
    public Point getPointA() {
        return p1;
    }

    /* 获取线段的第二个坐标点 */
    public Point getPointB() {
        return p2;
    }

    /* 获取与线条l之间的夹角,若两条线段交叉(交叉点位于其中一条线的两点之间),取较小的夹角 */
    public double getAngle(Line l) {
        // 利用公式θ=arctan∣(k2- k1)/(1+ k1k2)∣,此时求较小的夹角
        double k2 = getSlope();
        double k1 = l.getSlope();
        return (double) (Math.atan(Math.abs((k2 - k1) / (1 + k1 * k2))) * 180.0 / Math.PI);// 返回值为角度
    }

    // 是否平行,平行返回true,否则false。
    public boolean isParallel(Line l) {
        Double b1 = this.getSlope();
        Double b2 = l.getSlope();
        if ((b1.isInfinite()) && (b2.isInfinite())) {
            return true;
        } else {
            return (this.getSlope().doubleValue() == l.getSlope().doubleValue());
        }
    }

    // 两条线是否重合,重合返回true,否则false。

    public boolean isCoincide(Line l) {
        if (!this.isParallel(l)) {
            return false;
        }
        if (this.isOnline(l.p1)) {
            return true;
        }
        return false;
    }

    // 获取交叉点,若两条线平行,返回null。
    public Point getIntersection(Line l) {
        // LineInputError.isParallelError(this, l);
        if (this.isParallel(l)) {
            return null;
        }
        if (p1.equals(l.p1) || p1.equals(l.p2)) {
            return p1;
        }
        if (p2.equals(l.p1) || p2.equals(l.p2)) {
            return p2;
        }
        Point p3 = l.p1, p4 = l.p2;
        double x_member, x_denominator, y_member, y_denominator;
        Point cross_point = new Point();
        x_denominator = p4.x * p2.y - p4.x * p1.y - p3.x * p2.y + p3.x * p1.y - p2.x * p4.y + p2.x * p3.y + p1.x * p4.y
                - p1.x * p3.y;

        x_member = p3.y * p4.x * p2.x - p4.y * p3.x * p2.x - p3.y * p4.x * p1.x + p4.y * p3.x * p1.x
                - p1.y * p2.x * p4.x + p2.y * p1.x * p4.x + p1.y * p2.x * p3.x - p2.y * p1.x * p3.x;

        if (x_denominator == 0)
            cross_point.x = 0;
        else
            cross_point.x = x_member / x_denominator;

        y_denominator = p4.y * p2.x - p4.y * p1.x - p3.y * p2.x + p1.x * p3.y - p2.y * p4.x + p2.y * p3.x + p1.y * p4.x
                - p1.y * p3.x;

        y_member = -p3.y * p4.x * p2.y + p4.y * p3.x * p2.y + p3.y * p4.x * p1.y - p4.y * p3.x * p1.y
                + p1.y * p2.x * p4.y - p1.y * p2.x * p3.y - p2.y * p1.x * p4.y + p2.y * p1.x * p3.y;

        if (y_denominator == 0)
            cross_point.y = 0;
        else
            cross_point.y = y_member / y_denominator;

        // System.out.println(cross_point.x + ","+cross_point.y);

        return cross_point; // 平行返回(0,0)
    }
}
class LineInputError {    

    // 直线的两点重合的错误判断和提示。
    public static void pointsCoincideError(Point p1, Point p2) {
        if ((p1.getX() == p2.getX()) && p1.getY() == p2.getY()) {
            System.out.println("points coincide");
            System.exit(0);
        }
    }


}
 class OutFormat {
    //按要求格式化实数的输出。
    public static Double doubleFormat(double b) {
        DecimalFormat df = new DecimalFormat("#.000000");
        Double output = Double.valueOf(df.format(b));
        return output;
    }
}
class ParseInput {
    /*
     * 输入:完整的输入字符串,包含选项和所有点的信息,格式:选项:x1,y1 x2,y2 .....xn,yn。选项只能是1-5
     *         一个空InputData对象
     * 处理:将输入字符串中的选项和点信息提取出来并设置到InputData对象中
      * 输出:包含选项值和所有点的Point对象的InputData对象。
     */
    public static void paseInput(String s, InputData d) {
        PointInputError.wrongChoice(s);        
        d.setChoice(getChoice(s));
        s = s.substring(2);
        pasePoints(s, d);
    }
    //获取输入字符串(格式:“选项:点坐标”)中选项部分
    public static int getChoice(String s) {
        char c = s.charAt(0);
        return c-48;
    }
    
    /*
     * 输入:一个字符串,包含所有点的信息,格式:x1,y1 x2,y2 .....xn,yn
     *         一个空InputData对象
      * 输出:所有点的Point对象
     */

    public static void pasePoints(String s, InputData d) {
        String[] ss = s.split(" ");
        if (ss.length == 0)
            return;
        for (int i = 0; i < ss.length; i++) {
            d.addPoint(readPoint(ss[i]));
        }
    }

    /*
     * 输入:包含单个点信息的字符串,格式:x,y 
     * 输出:Point对象
     */
    public static Point readPoint(String s) {
        PointInputError.wrongPointFormat(s);
        String[] ss = s.split(",");
        double x = Double.parseDouble(ss[0]);
        double y = Double.parseDouble(ss[1]);
        // System.out.println("match");
        return new Point(x, y);

    }


}
class Point {
    public double x;
    
    public double y;

    public Point() {

    }

    public Point(double x,double y) {
        this.x=x;
        this.y=y;
    }

    /* 设置坐标x,将输入参数赋值给属性x */
    public void setX(double x) {
        this.x = x;
    }

    /* 设置坐标y,将输入参数赋值给属性y */
    public void setY(double y) {
        this.y = y;
    }

    /* 获取坐标x,返回属性x的值 */
    public double getX() {
        return x;
    }

    /* 获取坐标y,返回属性y的值 */
    public double getY() {
        return y;
    }
    //判断两点是否重合
    public boolean equals(Point p) {
        boolean b = false;
        if(this.x==p.getX()&&this.y==p.getY()) {
            b=true;
        }
        return b;
    }

    /* 计算当前点和输入点p之间的距离 */
    public double getDistance(Point p) {    
        return 0;
    }
}
class PointInputError {
    //判断从字符串中解析出的点的数量是否合格。
    public static void wrongNumberOfPoints(ArrayList ps, int num) {
        if (ps.size() != num) {
            System.out.println("wrong number of points");
            System.exit(0);
        }
    }
    //判断输入的字符串中点的坐标部分格式是否合格。若不符合,报错并退出程序
    public static void wrongPointFormat(String s) {
        if (!s.matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?")) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }

    // 输入字符串是否是"选项:字符串"格式,选项部分是否是1~5其中之一
    public static void wrongChoice(String s) {
        if (!s.matches("[1-5]:.+")) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }

}
class Triangle {
    private Point x;
    private Point y;
    private Point z;

    public Triangle(Point x, Point y, Point z) {
        this.x = x;
        this.y = y;
        this.z = z;
        if (!this.isTriangle()) {
            System.out.println("data error");
            System.exit(0);
        }
    }

    /* 判断x\y\z三个点的坐标是否能构成一个三角形 */
    public boolean isTriangle() {
        return false;
    }

    /* 获取三角形的中点(三条中线的交点) */
    public Point getMidpoint() {
        // 中点即重心,利用性质求解
        Point p = new Point();
        p.setX((this.x.getX() + this.y.getX() + this.z.getX()) / 3);
        p.setY((this.x.getY() + this.y.getY() + this.z.getY()) / 3);
        return p;
    }

    /* 获取三角形的三条边线 */
    public Line[] getSideline() {
        // 设置第一条边线
        Line line1 = new Line(x, y);

        // 设置第二条中线
        Line line2 = new Line(x, z);
        // 设置第三条中线
        Line line3 = new Line(y, z);

        Line[] lines = { line1, line2, line3 };
        return lines;
    }

    /* 获取三角形的周长 */
    public double getPerimeter() {
        return x.getDistance(y) + y.getDistance(z) + z.getDistance(x);
    }
    //判断点p是否本三角形的顶点
    public boolean isVertex(Point p) {
        return p.equals(x) || p.equals(y) || p.equals(z);
    }

    /*
     * 判断点p是否在本三角形内部(射线法)
     * 输出:1:在内部,-1:在外部,0:在三角形上
     */    
    public int isInside(Point p) {
        //int i = 0;
        if (this.isOnTheEdge(p)) {
            return 0;
        }
        if (isVertex(p)) {
            return 0;
        }
        Point pb;
        Line l;
        if (p.x == 0 && p.y == 0) {
            pb = new Point(0, 1);
            l = new Line(p, pb);
        } else {
            pb = new Point(0, 0);
            l = new Line(p, pb);
        }
        ArrayList<Point> ps = this.getIntersections(l);//获取直线与三角形的交点列表
        int num = ps.size();
        if (num == 0||num==1) {
            return -1;
        }
        if(num == 2) {
            Line l1 = new Line(ps.get(0),ps.get(1));
            if(l1.isBetween(p)) {
                return 1;
            }else {
                return -1;
            }
        }
        return 0;
    }

    // 获取直线l与三角形的交点,如果没有,数组为空。
    public ArrayList<Point> getIntersections(Line l) {
        
    }

    /*
     * 计算三角形上两个点所切分出的两个区域的面积。 
     * 输入:在三角形三条边上的两个点,要求都不为null,且不在同一条边。 
     *       输入为null将会导致异常。
     * 输出:两部分的面积,并按小-大的顺序排序。
     */
    public double[] calArea(Point p1, Point p2) {
        
    }
    // 判断线是否与三角形的某条边重合
    public boolean judgeLineCoincide(Line l) {
        
    }

    /*
     * 输入:点p 
     * 输出:p是否在本三角形的三条边线(不含顶点)上。在线上输出true,否则输出false。
     */
    public boolean isOnTheEdge(Point p) {
        
    }

    /* 三个点的getter()和setter()方法 */
    public Point getX() {
        return x;
    }

    public void setX(Point x) {
        this.x = x;
    }

    public Point getY() {
        return y;
    }

    public void setY(Point y) {
        this.y = y;
    }

    public Point getZ() {
        return z;
    }

    public void setZ(Point z) {
        this.z = z;
    }
}
复制代码

Java语言是面向对象的。Java是一种面向对象的语言,它提供类、接口和继承等原语,为了简单起见,Java只支持类之间的单继承,但支持接口之间的多继承,并支持类与接口之间的实现机制。体系结构中立性是确保程序可移植的最重要部分,另外,Java还严格规定了各个基本数据类型的长度。Java系统本身也具有很强的可移植性,Java编译器是用Java语言实现的,Java的运行环境是用ANSI C实现的。

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

第五次作业是真的好难,本来第四次作业就没写出来强行写第五次真的是自找苦吃 我真的感受到自己对类的设计需求 五边形的冗余点我都不知道是什么 所以最后写了一天只写了一个第一题的前两个点 真的很痛苦 表示再也不会接受这个变形的情况了

复制代码
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.text.DecimalFormat;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {    

        Scanner input = new Scanner(System.in);
        int flag =-1;
        String str = input.nextLine();
        if(str.charAt(0) == '1')
        {
            String pattern = "^[1]:[+-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?";
            Pattern r = Pattern.compile(pattern);
            Matcher m = r.matcher(str);
            boolean isMatch = m.matches();
            flag = Match(isMatch);
            if(flag ==1)
            {    
                 int index1 = str.indexOf(',');
                    String str1 = str.substring(2, index1);
                    int index2 = str.indexOf(' ');
                    String str2 = str.substring(index1 + 1, index2);
                    int index3 = str.indexOf(',', index2);
                    String str3 = str.substring(index2 + 1, index3);
                    int index4 = str.indexOf(' ', index3);
                    String str4 = str.substring(index3 + 1, index4);
                    int index5 = str.indexOf(',', index4);
                    String str5 = str.substring(index4 + 1, index5);
                    int index6 = str.indexOf(' ', index5);
                    String str6 = str.substring(index5 + 1, index6);
                    int index7 = str.indexOf(',', index6);
                    String str7 = str.substring(index6 + 1, index7);
                    int index8 = str.indexOf(' ', index7);
                    String str8 = str.substring(index7 + 1, index8);
                    int index9 = str.indexOf(',', index8);
                    String str9 = str.substring(index8 + 1, index9);
                    String str10 = str.substring(index9 + 1, str.length());
                    
                    Double x1 = Double.parseDouble(str1);
                    Double y1 = Double.parseDouble(str2);
                    Double x2 = Double.parseDouble(str3);
                    Double y2 = Double.parseDouble(str4);
                    Double x3 = Double.parseDouble(str5);
                    Double y3 = Double.parseDouble(str6);
                    Double x4 = Double.parseDouble(str7);
                    Double y4 = Double.parseDouble(str8);
                    Double x5 = Double.parseDouble(str9);
                    Double y5 = Double.parseDouble(str10);
                    Point p1 =new Point(x1,y1);
                    Point p2 =new Point(x2,y2);
                    Point p3 =new Point(x3,y3);
                    Point p4 =new Point(x4,y4);
                    Point p5 =new Point(x5,y5);
                    Line l1 = new Line(p1,p2);
                    Line l2 = new Line(p2,p3);
                    Line l3 = new Line(p3,p4);
                    Line l4 = new Line(p4,p5);
                    Line l5 = new Line(p5,p1);
                    if(l1.isParallel(l2)||l2.isParallel(l3)||l3.isParallel(l4)||l4.isParallel(l5)||l5.isParallel(l1))
                    {
                        System.out.println("false");
                    }
                    else
                    {
                        if(java.awt.geom.Line2D.linesIntersect(x1, y1, x2, y2, x3, y3, x4, y4)||java.awt.geom.Line2D.linesIntersect(x1, y1, x2, y2, x4, y4, x5, y5)||java.awt.geom.Line2D.linesIntersect(x2, y2, x3, y3, x4, y4, x5, y5)||java.awt.geom.Line2D.linesIntersect(x2, y2, x3, y3, x5, y5, x1, y1)||java.awt.geom.Line2D.linesIntersect(x3, y3, x4, y4, x5, y5, x1, y1))
                        {
                            System.out.println("false");
                        }
                        else
                        {
                            System.out.println("true");
                        }
                    }
            }
            if(flag != 1)
            {
                int num;
                String findText = ",";
                num = appearNumber(str, findText);
                if(num != 5)
                {
                    System.out.println("wrong number of points");
                }
                else
                {
                    System.out.println("Wrong Format");
                }
            }
        }
        if(str.charAt(0) == '2')
        {
            String pattern = "^[2]:[+-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?[\\s][+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?,[+|-]?(0|[1-9]{1}(\\d+)?)(\\.\\d+)?";
            Pattern r = Pattern.compile(pattern);
            Matcher m = r.matcher(str);
            boolean isMatch = m.matches();
            flag = Match(isMatch);
            if(flag ==1)
            {    
                 int index1 = str.indexOf(',');
                    String str1 = str.substring(2, index1);
                    int index2 = str.indexOf(' ');
                    String str2 = str.substring(index1 + 1, index2);
                    int index3 = str.indexOf(',', index2);
                    String str3 = str.substring(index2 + 1, index3);
                    int index4 = str.indexOf(' ', index3);
                    String str4 = str.substring(index3 + 1, index4);
                    int index5 = str.indexOf(',', index4);
                    String str5 = str.substring(index4 + 1, index5);
                    int index6 = str.indexOf(' ', index5);
                    String str6 = str.substring(index5 + 1, index6);
                    int index7 = str.indexOf(',', index6);
                    String str7 = str.substring(index6 + 1, index7);
                    int index8 = str.indexOf(' ', index7);
                    String str8 = str.substring(index7 + 1, index8);
                    int index9 = str.indexOf(',', index8);
                    String str9 = str.substring(index8 + 1, index9);
                    String str10 = str.substring(index9 + 1, str.length());
                    
                    Double x1 = Double.parseDouble(str1);
                    Double y1 = Double.parseDouble(str2);
                    Double x2 = Double.parseDouble(str3);
                    Double y2 = Double.parseDouble(str4);
                    Double x3 = Double.parseDouble(str5);
                    Double y3 = Double.parseDouble(str6);
                    Double x4 = Double.parseDouble(str7);
                    Double y4 = Double.parseDouble(str8);
                    Double x5 = Double.parseDouble(str9);
                    Double y5 = Double.parseDouble(str10);
                    Point p1 =new Point(x1,y1);
                    Point p2 =new Point(x2,y2);
                    Point p3 =new Point(x3,y3);
                    Point p4 =new Point(x4,y4);
                    Point p5 =new Point(x5,y5);
                    Line l1 = new Line(p1,p2);
                    Line l2 = new Line(p2,p3);
                    Line l3 = new Line(p3,p4);
                    Line l4 = new Line(p4,p5);
                    Line l5 = new Line(p5,p1);
                    int t = 0;
                    if(l1.isParallel(l2)||l2.isParallel(l3)||l3.isParallel(l4)||l4.isParallel(l5)||l5.isParallel(l1))
                    {
                        t = 0;
                    }
                    else
                    {
                        if(java.awt.geom.Line2D.linesIntersect(x1, y1, x2, y2, x3, y3, x4, y4)||java.awt.geom.Line2D.linesIntersect(x1, y1, x2, y2, x4, y4, x5, y5)||java.awt.geom.Line2D.linesIntersect(x2, y2, x3, y3, x4, y4, x5, y5)||java.awt.geom.Line2D.linesIntersect(x2, y2, x3, y3, x5, y5, x1, y1)||java.awt.geom.Line2D.linesIntersect(x3, y3, x4, y4, x5, y5, x1, y1))
                        {
                            t = 0;
                        }
                        else
                        {
                            t = 1;
                        }
                    }
                    if(t == 0)
                    {
                        System.out.println("not a pentagon");
                    }
                    if(t == 1)
                    {
                        double angle1 = l1.getAngle(l2);
                        double angle2 = l2.getAngle(l3);
                        double angle3 = l1.getAngle(l4);
                        double angle4 = l1.getAngle(l5);
                        double angle5 = l1.getAngle(l1);
                        if(angle1 < 90||angle2 < 90||angle3 < 90||angle4 < 90||angle5 < 90)
                        {
                            System.out.println("false");
                        }
                        else
                        {
                            
                            double l = l1.calLong(p1, p2) + l2.calLong(p2, p3) + l3.calLong(p3, p4) + l4.calLong(p4, p5)+l5.calLong(p5, p1);    
                            double A2 = l1.calLong(p1, p3);
                            double B2 = l2.calLong(p4, p1);
                            double C2 = l3.calLong(p3, p4);
                            double s2 = (A2+B2+C2)/2;
                            double A1 = l1.calLong(p4, p5);
                            double B1 = l1.calLong(p5, p1);
                            double C1 = l1.calLong(p1, p4);
                            double s1 =(A1+B1+C1)/2;
                            double A3 = l1.calLong(p1, p3);
                            double B3 = l2.calLong(p4, p1);
                            double C3 = l3.calLong(p3, p4);
                            double s3 = (A3+B3+C3)/2;
                            
                            double area1 = Math.pow((s1*(s1-A1)*(s1-B1)*(s1-C1)), 1/2);
                            double area2 = Math.pow((s2*(s2-A2)*(s2-B2)*(s2-C2)), 1/2);
                            double area3 = Math.pow((s3*(s3-A3)*(s3-B3)*(s3-C3)), 1/2);
                            DecimalFormat df = new DecimalFormat("#.000");
                        
                            System.out.println("true" +df.format(area1+area2+area3) +" " +df.format(l));
                        }
                    }
            }
            if(flag != 1)
            {
                int num;
                String findText = ",";
                num = appearNumber(str, findText);
                if(num != 5)
                {
                    System.out.println("wrong number of points");
                }
                else
                {
                    System.out.println("Wrong Format");
                }
            }
        }
    }
    public static int Match(boolean isMatch)//判断是否合法输入
    {
        int flag = -1;
        if(isMatch == false)
        {
            flag = 0;
        }
        if(isMatch == true)
        {
            flag = 1;
        }
        return flag;
    }
    public static int appearNumber(String srcText, String findText) {
        int count = 0;
        Pattern p = Pattern.compile(findText);
        Matcher m = p.matcher(srcText);
        while (m.find()) {
            count++;
        }
        return count;
    }
}


class Point {
    public double x;
    
    public double y;

    public Point() {

    }

    public Point(double x,double y) {
        this.x=x;
        this.y=y;
    }

    /* 设置坐标x,将输入参数赋值给属性x */
    public void setX(double x) {
        this.x = x;
    }

    /* 设置坐标y,将输入参数赋值给属性y */
    public void setY(double y) {
        this.y = y;
    }

    /* 获取坐标x,返回属性x的值 */
    public double getX() {
        return x;
    }

    /* 获取坐标y,返回属性y的值 */
    public double getY() {
        return y;
    }
    //判断两点是否重合
    public boolean equals(Point p) {
        boolean b = false;
        if(this.x==p.getX()&&this.y==p.getY()) {
            b=true;
        }
        return b;
    }


}
class LineInputError {    

    // 直线的两点重合的错误判断和提示。
    public static void pointsCoincideError(Point p1, Point p2) {
        if ((p1.getX() == p2.getX()) && p1.getY() == p2.getY()) {
            System.out.println("points coincide");
            System.exit(0);
        }
    }


}
class Line {
    private Point p1;//线上的第一个点
    private Point p2;//线上的第二个点


    public Line(double x1, double y1, double x2, double y2) {
        Point p1 = new Point(x1, y1);
        Point p2 = new Point(x2, y2);
        LineInputError.pointsCoincideError(p1, p2);//两点是否重合,重合则报错并退出
        this.p1 = p1;
        this.p2 = p2;
    }

    public Line(Point p1, Point p2) {
        LineInputError.pointsCoincideError(p1, p2);//两点是否重合,重合则报错并退出
        this.p1 = p1;
        this.p2 = p2;
    }

    /* 获取线条的斜率 */
    public Double getSlope() {
        // (x1-x2=0)注意考虑斜率不存在即返回double类型无穷大"Infinite"
        return (p2.getY() - p1.getY()) / (p2.getX() - p1.getX());
    }

    /* 判断x是否在线上 */
    public boolean isOnline(Point x) {
        //System.out.println("isOnline");
        //System.out.println(p1.x + "  " + p1.y + "  " + p2.x + "  " + p2.y + "  " + x.x + "  " + x.y + "  ");

        // 点重合
        if ((x.getX() == p1.getX() && x.getY() == p1.getY()) || (x.getX() == p2.getX() && x.getY() == p2.getY())) {
            return true;
        }
        Line l = new Line(p1, x);
        if (l.getSlope().isInfinite() && this.getSlope().isInfinite()) {
            return true;
        }

        /*
         * if (l.getSlope().isInfinite() || this.getSlope().isInfinite()) { return
         * false; }
         */

        // 此点与线上任意一点构成的线的斜率相等则此点在线上
        double b1 = l.getSlope(), b2 = this.getSlope();
        //System.out.println(b1 + "  " + b2 + " " + (b1- b2) + " " + (Math.abs(b1 - b2) < 0.00000000001));

        return Math.abs(b1 - b2)  < 0.00000000001;// b1==b2;
    }

    /* 获取线段的第一个坐标点 */
    public Point getPointA() {
        return p1;
    }

    /* 获取线段的第二个坐标点 */
    public Point getPointB() {
        return p2;
    }

    // 是否平行,平行返回true,否则false。
    public boolean isParallel(Line l) {
        Double b1 = this.getSlope();
        Double b2 = l.getSlope();
        if ((b1.isInfinite()) && (b2.isInfinite())) {
            return true;
        } else {
            return (this.getSlope().doubleValue() == l.getSlope().doubleValue());
        }
    }

    // 两条线是否重合,重合返回true,否则false。

    public boolean isCoincide(Line l) {
        if (!this.isParallel(l)) {
            return false;
        }
        if (this.isOnline(l.p1)) {
            return true;
        }
        return false;
    }
        /* 获取与线条l之间的夹角,若两条线段交叉(交叉点位于其中一条线的两点之间),取较小的夹角 */
    public double getAngle(Line l) {
        // 利用公式θ=arctan∣(k2- k1)/(1+ k1k2)∣,此时求较小的夹角
        double k2 = getSlope();
        double k1 = l.getSlope();
        return (double) (Math.atan(Math.abs((k2 - k1) / (1 + k1 * k2))) * 180.0 / Math.PI);// 返回值为角度
    }



    // 获取交叉点,若两条线平行,返回null。
    public Point getIntersection(Line l) {
        // LineInputError.isParallelError(this, l);
        if (this.isParallel(l)) {
            return null;
        }
        if (p1.equals(l.p1) || p1.equals(l.p2)) {
            return p1;
        }
        if (p2.equals(l.p1) || p2.equals(l.p2)) {
            return p2;
        }
        Point p3 = l.p1, p4 = l.p2;
        double x_member, x_denominator, y_member, y_denominator;
        Point cross_point = new Point();
        x_denominator = p4.x * p2.y - p4.x * p1.y - p3.x * p2.y + p3.x * p1.y - p2.x * p4.y + p2.x * p3.y + p1.x * p4.y
                - p1.x * p3.y;

        x_member = p3.y * p4.x * p2.x - p4.y * p3.x * p2.x - p3.y * p4.x * p1.x + p4.y * p3.x * p1.x
                - p1.y * p2.x * p4.x + p2.y * p1.x * p4.x + p1.y * p2.x * p3.x - p2.y * p1.x * p3.x;

        if (x_denominator == 0)
            cross_point.x = 0;
        else
            cross_point.x = x_member / x_denominator;

        y_denominator = p4.y * p2.x - p4.y * p1.x - p3.y * p2.x + p1.x * p3.y - p2.y * p4.x + p2.y * p3.x + p1.y * p4.x
                - p1.y * p3.x;

        y_member = -p3.y * p4.x * p2.y + p4.y * p3.x * p2.y + p3.y * p4.x * p1.y - p4.y * p3.x * p1.y
                + p1.y * p2.x * p4.y - p1.y * p2.x * p3.y - p2.y * p1.x * p4.y + p2.y * p1.x * p3.y;

        if (y_denominator == 0)
            cross_point.y = 0;
        else
            cross_point.y = y_member / y_denominator;

        // System.out.println(cross_point.x + ","+cross_point.y);

        return cross_point; // 平行返回(0,0)
    }
    public double calLong(Point p1,Point p2)
    {
        return Math.pow(Math.pow(p1.getX()-p2.getX(), 2) - Math.pow(p1.getY()-p2.getY(), 2), 1/2);
    }
}
复制代码

这个五边形的面积我一直表示不出来 我准备用的海伦公式调试很多次还是不知道错在哪里表示真的很冤枉 也很烦 所以第二题也没有专门写过,就直接放弃了总的来说这第五次作业真的好难 总结知识点

假设有一个三角形,边长分别为a、b、c,三角形的面积S可由以下公式求得: 
  S=√[p(p-a)(p-b)(p-c)] 
  而公式里的p为半周长: 
  p=(a+b+c)/2 
  ——————————————————————————————————————————————
  注:"Metrica"(《度量论》)手抄本中用s作为半周长,所以
  S=√[p(p-a)(p-b)(p-c)] 和S=√[s(s-a)(s-b)(s-c)]两种写法都是可以的,但多用p作为半周长。
  ——————————————————————————————————————————————
  由于任何n边的多边形都可以分割成n-2个三角形,所以海伦公式可以用作求多边形面积的公式。比如说测量土地的面积的时候,不用测三角形的高,只需测两点间的距离,就可以方便地导出答案。 
  证明(1): 
  与海伦在他的著作"Metrica"(《度量论》)中的原始证明不同,在此我们用三角公式和公式变形来证明。设三角形的三边a、b、c的对角分别为A、B、C,则余弦定理为 
  cosC = (a^2+b^2-c^2)/2ab 
  S=1/2*ab*sinC
  =1/2*ab*√(1-cos^2 C)
  =1/2*ab*√[1-(a^2+b^2-c^2)^2/4a^2*b^2]
  =1/4*√[4a^2*b^2-(a^2+b^2-c^2)^2]
  =1/4*√[(2ab+a^2+b^2-c^2)(2ab-a^2-b^2+c^2)]
  =1/4*√[(a+b)^2-c^2][c^2-(a-b)^2]
  =1/4*√[(a+b+c)(a+b-c)(a-b+c)(-a+b+c)]
  设p=(a+b+c)/2
  则p=(a+b+c)/2, p-a=(-a+b+c)/2, p-b=(a-b+c)/2,p-c=(a+b-c)/2,
  上式=√[(a+b+c)(a+b-c)(a-b+c)(-a+b+c)/16]
  =√[p(p-a)(p-b)(p-c)] 
  所以,三角形ABC面积S=√[p(p-a)(p-b)(p-c)] 

自己多写了两个类来表示距离和自己上课设计的有点不同

三.最后总结一下期中考试的情况

期中考试整体难度并不是很难

7-1 点与线(类设计)

第一题我一直没有用eclipse上面的setter和getter函数导致我浪费很多时间 

我们在程序开发过程中,往往要编写这样的类:类的部分或者全部属性不希望让外部世界直接访问,而不用public字段修饰。这样,方法调用成了访问这些属性的唯一途径。JavaBean就是一个很好的例子,其严格遵守面向对象的设计逻辑,所有属性都是private。对于任何属性xxx,都有public的getXxx()方法来获取属性和public的setXxx()方法来修改属性。如果只有少量这样的属性,可以通过手动方式为它们添加setter和getter方法。但是,如果有大量这样的属性,手动添加会很费时。
————————————————
根据类图一下就可以知道他这个题目怎么写了

复制代码
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();
        String color = input.next();
        Point p1 = new Point(x1,y1);
        Point p2 = new Point(x2,y2);
        Line l = new Line(p1,p2,color);
        if((x1>0&&x1<=200)&&(y1>0&&y1<=200)&&(x2>0&&x2<=200)&&(y2>0&&y2<=200))
        {
        l.display();
        }
        else
        {
            System.out.println("Wrong Format");
        }
    }

}
class Point {
    private double x;
    private double y;
    public Point(double x, double y) {
        super();
        this.x = x;
        this.y = y;
    }
    public Point() {
        
    }
    public double getX()
    {
        return x;
    }
    public double getY()
    {
        return y;
    }
    public void setX(double x)
    {
        this.x=x;
    }
    public void setY(double y)
    {
        this.y=y;
    }
    public void display() 
    {
        System.out.printf("(%.2f,%.2f)",getX(),getY());
    }

}
class Line {
    private Point p1;//线上的第一个点
    private Point p2;//线上的第二个点
    private String color;

    public Line(Point p1, Point p2, String color) {
        super();
        this.p1 = p1;
        this.p2 = p2;
        this.color = color;
    }
    public Line() {
        
    }
    public Point getPoint1() {
        return p1;
    }
    public Point getPoint2() {
        return p2;
    }
    public Point setPoint1(Point p1) {
        return this.p1 = getPoint1();
    }
    public Point setPoint2(Point p2) {
        return this.p2 = getPoint2();
    }
    public String getColor()
    {
        return color;
    }
    public void setColor(String color)
    {
        this.color = color;
    }
    public double getDis(double x1,double y1,double x2,double y2)//计算两点之间距离
    {
        double dis = Math.sqrt(Math.pow((x1 - x2),2) + Math.pow((y1 - y2),2));
        return dis;
    }
    public void display() {
        System.out.println("The line's color is:" + this.color);
        System.out.println("The line's begin point's Coordinate is:");
        System.out.printf("(%.2f,%.2f)\n",p1.getX(),p1.getY());
        System.out.println("The line's end point's Coordinate is:");
        System.out.printf("(%.2f,%.2f)\n",p2.getX(),p2.getY());
        System.out.printf("The line's length is:%.2f\n" , getDis(p1.getX(),p1.getY(),p2.getX(),p2.getY()));
    }
}
复制代码

通过这个我对类的认识越来越认真了,也越来越知道建类的好处了,我以后一点会多用类这样不仅会简化代码还让自己意思更好表达 让自己的代码越来越简单 圈复杂度也不会很高

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

第二题就多了一个颜色直接多建几个类就行没什么好说的 第一题写出来第二题一定没什么问题 只要知道自己的封装性

复制代码
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();
        String color = input.next();
        Point p1 = new Point(x1,y1);
        Point p2 = new Point(x2,y2);
        Line l = new Line(p1,p2,color);
        Plane p = new Plane(color);
        if((x1>0&&x1<=200)&&(y1>0&&y1<=200)&&(x2>0&&x2<=200)&&(y2>0&&y2<=200))
        {
            p1.display();
            p2.display();
            l.display();
            p.display();
        }
        else
        {
            System.out.println("Wrong Format");
        }
    }

}
abstract class Element {
    
    public abstract void display();
}
class Line extends Element{
    private Point p1;//线上的第一个点
    private Point p2;//线上的第二个点
    private String color;

    public Line(Point p1, Point p2, String color) {
        super();
        this.p1 = p1;
        this.p2 = p2;
        this.color = color;
    }
    public Line() {
        
    }
    public Point getPoint1() {
        return p1;
    }
    public Point getPoint2() {
        return p2;
    }
    public Point setPoint1(Point p1) {
        return this.p1 = getPoint1();
    }
    public Point setPoint2(Point p2) {
        return this.p2 = getPoint2();
    }
    public String getColor()
    {
        return color;
    }
    public void setColor(String color)
    {
        this.color = color;
    }
    public double getDis(double x1,double y1,double x2,double y2)//计算两点之间距离
    {
        double dis = Math.sqrt(Math.pow((x1 - x2),2) + Math.pow((y1 - y2),2));
        return dis;
    }
    public void display() {
        System.out.println("The line's color is:" + this.color);
        System.out.println("The line's begin point's Coordinate is:");
        System.out.printf("(%.2f,%.2f)\n",p1.getX(),p1.getY());
        System.out.println("The line's end point's Coordinate is:");
        System.out.printf("(%.2f,%.2f)\n",p2.getX(),p2.getY());
        System.out.printf("The line's length is:%.2f\n" , getDis(p1.getX(),p1.getY(),p2.getX(),p2.getY()));
    }
}
class Plane extends Element{
    private String color;

    public String getColor() {
        return color;
    }

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

    public Plane(String color) {
        super();
        this.color = color;
    }
    
    
}
class Point extends Element{
    private double x;
    private double y;
    public Point(double x, double y) {
        super();
        this.x = x;
        this.y = y;
    }
    public Point() {
        
    }
    public double getX()
    {
        return x;
    }
    public double getY()
    {
        return y;
    }
    public void setX(double x)
    {
        this.x=x;
    }
    public void setY(double y)
    {
        this.y=y;
    }
    public void display() 
    {
        System.out.printf("(%.2f,%.2f)\n",getX(),getY());
    }

}
复制代码

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

容器类这个不仅仅运用到父类子类的关系和抽象类 还用到了容器类

先简单介绍一下自己对这些的认知 Java支持继承,一种OOPs概念,其中一个类获取另一个类的成员(方法和字段)。 您可以从另一个类继承一个类的成员,使用extends关键字为:class A extends B{} 继承其他属性的类称为子类(派生类,子类),其属性被继承的类称为父类(基类,超类类)。 下面是一个演示继承的示例。在这里,我们有两个类,即Sample和MyClass。其中Sample是父类,而名为MyClass的类是子类。

用abstract 关键字来修饰一个类时,这个类叫作抽象类。抽象类是它的所有子类的公共属性的集合,是包含一个或多个抽象方法的类。抽象类可以看作是对类的进一步抽象。在面向对象领域,抽象类主要用来进行类型隐藏。在Java中不可以引用抽象类让现实中更多的情况可以理解。用abstract 关键字来修饰一个类时,这个类叫作抽象类。抽象类是它的所有子类的公共属性的集合,是包含一个或多个抽象方法的类。抽象类可以看作是对类的进一步抽象。在面向对象领域,抽象类主要用来进行类型隐藏。它里面有很多函数可以来解决增减功能
复制代码
import java.util.Scanner;
import java.util.ArrayList;
public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        GeometryObject a = new GeometryObject();
       int choice = input.nextInt();
       while(choice != 0) {
            switch(choice) {
            case 1://insert Point object into list 
                double x = input.nextDouble();
                double y = input.nextDouble();
                Point p = new Point(x,y);
                if((x>0&&x<=200)&&(y>0&&y<=200))
                {
                    a.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();
                String color = input.next();
                Point p1 = new Point(x1,y1);
                Point p2 = new Point(x2,y2);
                Line l = new Line(p1,p2,color);
                if((x1>0&&x1<=200)&&(y1>0&&y1<=200)&&(x2>0&&x2<=200)&&(y2>0&&y2<=200))
                {
                    a.add(l);
                }
                else
                {
                    System.out.println("Wrong Format");
                }
                break;
            case 3://insert Plane object into list
                String color2 = input.next();
                Plane plane = new Plane(color2);
                a.add(plane);
                break;
            case 4://delete index - 1 object from list
                int index = input.nextInt();
                a.remove(index);
            }
            choice = input.nextInt();
       }
       for(Element e: a.getList()) {
           e.display();
       }

        
    
    }

}
abstract class Element {
    
    public abstract void display();
}
class GeometryObject {
    private ArrayList<Element> list =new ArrayList<>(); 
    public void add(Element element)
    {
        list.add(element);
    }
    public void remove(int index)
    {
        if(index<=list.size()&&index>=1)
        list.remove(index - 1);
        
    }
    
    public ArrayList<Element> getList() {
        return list;
    }
    
}
class Line extends Element{
    private Point p1;//线上的第一个点
    private Point p2;//线上的第二个点
    private String color;

    public Line(Point p1, Point p2, String color) {
        super();
        this.p1 = p1;
        this.p2 = p2;
        this.color = color;
    }
    public Line() {
        
    }
    public Point getPoint1() {
        return p1;
    }
    public Point getPoint2() {
        return p2;
    }
    public Point setPoint1(Point p1) {
        return this.p1 = getPoint1();
    }
    public Point setPoint2(Point p2) {
        return this.p2 = getPoint2();
    }
    public String getColor()
    {
        return color;
    }
    public void setColor(String color)
    {
        this.color = color;
    }
    public double getDis(double x1,double y1,double x2,double y2)//计算两点之间距离
    {
        double dis = Math.sqrt(Math.pow((x1 - x2),2) + Math.pow((y1 - y2),2));
        return dis;
    }
    public void display() {
        System.out.println("The line's color is:" + this.color);
        System.out.println("The line's begin point's Coordinate is:");
        System.out.printf("(%.2f,%.2f)\n",p1.getX(),p1.getY());
        System.out.println("The line's end point's Coordinate is:");
        System.out.printf("(%.2f,%.2f)\n",p2.getX(),p2.getY());
        System.out.printf("The line's length is:%.2f\n" , getDis(p1.getX(),p1.getY(),p2.getX(),p2.getY()));
    }
}
class Plane extends Element{
    private String color;

    public String getColor() {
        return color;
    }

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

    public Plane(String color) {
        super();
        this.color = color;
    }
    
    
}
class Point extends Element{
    private double x;
    private double y;
    public Point(double x, double y) {
        super();
        this.x = x;
        this.y = y;
    }
    public Point() {
        
    }
    public double getX()
    {
        return x;
    }
    public double getY()
    {
        return y;
    }
    public void setX(double x)
    {
        this.x=x;
    }
    public void setY(double y)
    {
        this.y=y;
    }
    public void display() 
    {
        System.out.printf("(%.2f,%.2f)\n",getX(),getY());
    }

}
复制代码

还多用了switch case来表示不同的情况

附上我的类图

 

 

 

 

 四.总结

我应该多运用类来表达自己的想法 面向对象还是没有做到面向对象自己作为一个初学者应该更加重视运用

posted @   按时灯笼裤和  阅读(50)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
点击右上角即可分享
微信分享提示