首页 > 其他分享 >第二次Blog总结

第二次Blog总结

时间:2022-10-29 22:58:21浏览次数:34  
标签:总结 getX double Blog getY && new 第二次 Line

  • 前言

本次Blog是对6-9周所做的PTA以及期中考试进行多方面总结,四边形、五边形以及期中考试。对知识点,题量,难度等方面进行分析。

首先是第四次大作业,考察了正则表达式、四边形以及银行业务类。因为从三角形开始用类进行编写,难度在第三次的基础上也增加了不少,题量虽然不大,但是在此次作业也花了很多时间,对多边形的类和方法也有了更好的掌握。

第五次作业难度也是层层递进,方法与类也是越变越多,考虑的情况也更加复杂,时间花的更多但完成度也是越来越低,自身能力还得提升。

最后就是期中考试,对类的设计,继承与多态,以及容器类进行一次较为简单的题目测试。总共三道题目,难度不是很大但也因为一些细节问题没拿到满分。


  •  第四次PTA
1.  7-2 点线形系列4-凸四边形的计算 分数 70 作者 蔡轲 单位 南昌航空大学 用户输入一组选项和数据,进行与四边形有关的计算。
以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
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"。   设计与分析:           在三角形基础上设计一个四边形的类,包含判断四边形的方法,判断凸凹的方法,计算面积周长的方法,以及判断是否是菱形、矩形、正方形的方法,交点个数方法在线类中实现。   部分重要代码与类图:
class Quadrilateral {

    Point x = new Point();
    Point y = new Point();
    Point z = new Point();
    Point a = new Point();
    Line xy = new Line();
    Line yz = new Line();
    Line za = new Line();
    Line ax = new Line();
    Line ya = new Line();
    Line xz = new Line();
    Triangle tr = new Triangle();

//    public Quadrilateral() {
//
//    }

    void Quadrilateral_set(Point x, Point y, Point z, Point a) {
        this.x = x;
        this.y = y;
        this.z = z;
        this.a = a;
        xy = new Line(x, y);
        yz = new Line(y, z);
        za = new Line(z, a);

        ax = new Line(a, x);
        ya = new Line(y, a);
        xz = new Line(x, z);

    }

    /* 判断x\y\z\a四个点的坐标是否能构成一个四边形 */
    public boolean ifQuadrilateral() {

        if (xy.parallel(yz) == -1 && yz.parallel(za) == -1 && za.parallel(ax) == -1 && ax.parallel(xy) == -1) {
            if (!(xy.if_in_line(xy.jiaodian(za)) && za.if_in_line(xy.jiaodian(za))) && !(yz.if_in_line(yz.jiaodian(ax)) && ax.if_in_line(yz.jiaodian(ax))))
                return true;
        }
        return false;
    }

    //判断四个点是否组成三角形
    public boolean sidianif_triangle() {
        if (x.checkpoint(y) && !x.checkpoint(a) && !x.checkpoint(z) && !a.checkpoint(z)) {
            tr.sjxbian(x, z, a);
            return true;
        }
        if (x.checkpoint(a) && !x.checkpoint(y) && !x.checkpoint(z) && z.checkpoint(y)) {
            tr.sjxbian(x, y, z);
            return true;
        }
        if (x.checkpoint(z) && !x.checkpoint(y) && !x.checkpoint(a) && !y.checkpoint(a)) {
            tr.sjxbian(x, y, a);
            return true;
        }
        if (x.checkpoint(y) && !y.checkpoint(z) && !y.checkpoint(a) && !z.checkpoint(a)) {
            tr.sjxbian(y, z, a);
            return true;
        }

        if (ya.if_in_line(x) || ya.if_in_line(z) || xz.if_in_line(y) || xz.if_in_line(a))
            return true;
        return false;
    }

    /* 判断是否平行四边形 */
    public boolean ifParallelogram() {
        double k1 = (this.x.getY() - this.y.getY()) * (this.x.getY() - this.y.getY()) + (this.x.getX() - this.y.getX()) * (this.x.getX() - this.y.getX());
        double k2 = (this.y.getY() - this.z.getY()) * (this.y.getY() - this.z.getY()) + (this.y.getX() - this.z.getX()) * (this.y.getX() - this.z.getX());
        double k3 = (this.z.getY() - this.a.getY()) * (this.z.getY() - this.a.getY()) + (this.z.getX() - this.a.getX()) * (this.z.getX() - this.a.getX());
        double k4 = (this.a.getY() - this.x.getY()) * (this.a.getY() - this.x.getY()) + (this.a.getX() - this.x.getX()) * (this.a.getX() - this.x.getX());
        if (k1 == k3 && k2 == k4) {
            return true;
        } else {
            return false;
        }
    }

    /* 获取四边形的面积,此处采用海伦公式
    public double getArea() {

    }
    */

    /* 获取四边形的周长 */
    public double getPerimeter() {
        double k1 = (this.x.getY() - this.y.getY()) * (this.x.getY() - this.y.getY()) + (this.x.getX() - this.y.getX()) * (this.x.getX() - this.y.getX());
        double k2 = (this.y.getY() - this.z.getY()) * (this.y.getY() - this.z.getY()) + (this.y.getX() - this.z.getX()) * (this.y.getX() - this.z.getX());
        double k3 = (this.z.getY() - this.a.getY()) * (this.z.getY() - this.a.getY()) + (this.z.getX() - this.a.getX()) * (this.z.getX() - this.a.getX());
        double k4 = (this.a.getY() - this.x.getY()) * (this.a.getY() - this.x.getY()) + (this.a.getX() - this.x.getX()) * (this.a.getX() - this.x.getX());
        return Math.sqrt(k1) + Math.sqrt(k2) + Math.sqrt(k3) + Math.sqrt(k4);
    }

    /* 判断是否菱形 */
    public boolean ifLozenge() {
        double k1 = (this.x.getY() - this.y.getY()) * (this.x.getY() - this.y.getY()) + (this.x.getX() - this.y.getX()) * (this.x.getX() - this.y.getX());
        double k2 = (this.y.getY() - this.z.getY()) * (this.y.getY() - this.z.getY()) + (this.y.getX() - this.z.getX()) * (this.y.getX() - this.z.getX());
        double k3 = (this.z.getY() - this.a.getY()) * (this.z.getY() - this.a.getY()) + (this.z.getX() - this.a.getX()) * (this.z.getX() - this.a.getX());
        double k4 = (this.a.getY() - this.x.getY()) * (this.a.getY() - this.x.getY()) + (this.a.getX() - this.x.getX()) * (this.a.getX() - this.x.getX());
        if (k1 == k2 && k2 == k3 && k3 == k4) {
            return true;
        } else {
            return false;
        }
    }

    /* 判断是否矩形 */
    public boolean ifEquilateralQuadrilateral() {
        double k1 = (this.x.getY() - this.y.getY()) * (this.x.getY() - this.y.getY()) + (this.x.getX() - this.y.getX()) * (this.x.getX() - this.y.getX());
        double k2 = (this.y.getY() - this.z.getY()) * (this.y.getY() - this.z.getY()) + (this.y.getX() - this.z.getX()) * (this.y.getX() - this.z.getX());
        double k3 = (this.z.getY() - this.a.getY()) * (this.z.getY() - this.a.getY()) + (this.z.getX() - this.a.getX()) * (this.z.getX() - this.a.getX());
        double k4 = (this.a.getY() - this.x.getY()) * (this.a.getY() - this.x.getY()) + (this.a.getX() - this.x.getX()) * (this.a.getX() - this.x.getX());
        double k5 = (this.x.getX() - this.z.getX()) * (this.x.getX() - this.z.getX()) + (this.x.getY() - this.z.getY()) * (this.x.getY() - this.z.getY());
        double k6 = (this.y.getX() - this.a.getX()) * (this.y.getX() - this.a.getX()) + (this.y.getY() - this.a.getY()) * (this.y.getY() - this.a.getY());
        if (k1 == k3 && k2 == k4 && k5 == k6) {
            return true;
        } else if ((xy.xielv() * yz.xielv() == -1 && xy.check_slope() && yz.check_slope()) || (!xy.check_slope() && yz.check_slope()) || (xy.check_slope() && !yz.check_slope())) {
            return true;
        } else {
            return false;
        }
    }

    /* 判断是否正方形 */
    public boolean ifRightQuadrilateral() {
        double k1 = (this.x.getY() - this.y.getY()) * (this.x.getY() - this.y.getY()) + (this.x.getX() - this.y.getX()) * (this.x.getX() - this.y.getX());
        double k2 = (this.y.getY() - this.z.getY()) * (this.y.getY() - this.z.getY()) + (this.y.getX() - this.z.getX()) * (this.y.getX() - this.z.getX());
        double k3 = (this.z.getY() - this.a.getY()) * (this.z.getY() - this.a.getY()) + (this.z.getX() - this.a.getX()) * (this.z.getX() - this.a.getX());
        double k4 = (this.a.getY() - this.x.getY()) * (this.a.getY() - this.x.getY()) + (this.a.getX() - this.x.getX()) * (this.a.getX() - this.x.getX());
        double k5 = (this.x.getX() - this.z.getX()) * (this.x.getX() - this.z.getX()) + (this.x.getY() - this.z.getY()) * (this.x.getY() - this.z.getY());
        double k6 = (this.y.getX() - this.a.getX()) * (this.y.getX() - this.a.getX()) + (this.y.getY() - this.a.getY()) * (this.y.getY() - this.a.getY());
        if (k1 == k2 && k2 == k3 && k3 == k4 && k5 == k6 && ifEquilateralQuadrilateral()) {
            return true;
        } else {
            return false;
        }
    }

    public static Double doubleFormat(double b) {
        DecimalFormat df = new DecimalFormat("#.000");
        Double output = Double.valueOf(df.format(b));
        return output;
    }

    /* 判断是否凹四边形 还是凸四边形*/
    public void ifTuAo() {
        double s1, c1;
        double k1 = Math.sqrt(Math.pow(this.y.getX() - this.x.getX(), 2) + Math.pow(this.y.getY() - this.x.getY(), 2));
        double k2 = Math.sqrt(Math.pow(this.z.getX() - this.a.getX(), 2) + Math.pow(this.z.getY() - this.a.getY(), 2));
        double k3 = Math.sqrt(Math.pow(this.x.getX() - this.a.getX(), 2) + Math.pow(this.x.getY() - this.a.getY(), 2));
        double k4 = Math.sqrt(Math.pow(this.y.getX() - this.z.getX(), 2) + Math.pow(this.y.getY() - this.z.getY(), 2));

        double c = k1 + k2 + k3 + k4;
        double s = 0.5 * Math.abs(x.x * y.y + y.x * z.y + z.x * a.y + a.x * x.y - y.x * x.y - z.x * y.y - a.x * z.y - x.x * a.y);

        double t1 = (a.x - x.x) * (y.y - x.y) - (a.y - x.y) * (y.x - x.x);
        double t2 = (x.x - y.x) * (z.y - y.y) - (x.y - y.y) * (z.x - y.x);
        double t3 = (y.x - z.x) * (a.y - z.y) - (y.y - z.y) * (a.x - z.x);
        double t4 = (z.x - a.x) * (x.y - a.y) - (z.y - a.y) * (x.x - a.x);
        s1 = doubleFormat(s);
        c1 = doubleFormat(c);
        if (t1 * t2 * t3 * t4 > 0) {
            System.out.print("true " + c1 + " " + s1);
            return;
        } else {
            System.out.print("false " + c1 + " " + s1);
            return;
        }
    }

    public boolean ifinQuadrilateral(Point p) {
        double S;//四边形面积

        double s = this.x.calculate_distance(this.y);
        double s1 = this.x.calculate_distance(this.a);
        double s2 = this.y.calculate_distance(this.z);
        double s3 = this.z.calculate_distance(this.a);
        Point d = new Point();
        d.x = (this.x.getX() + this.y.getX()) / 2;
        d.y = (this.x.getY() + this.y.getY()) / 2;
        double l1 = yz.linedistance(d);
        double l2 = ax.linedistance(d);
        double l3 = za.linedistance(d);
        S = (s3 * l3) / 2 + (s2 * l1) / 2 + (s1 * l2) / 2;

        double m1 = (xy.linedistance(p) * s)/2;
        double m2 = (yz.linedistance(p) * s2)/2;
        double m3 = (za.linedistance(p) * s3)/2;
        double m4 = (ax.linedistance(p) * s1)/2;
        return Math.abs(S - m1 -m2 -m3 -m4) < 0.0000001;


    }
}
View Code

  踩坑心得:       因为前一次作业用了斜率方法判断三角形,但是也会有斜率不存在的情况导致判断测试点过不了,为了以后判断多边形所以改用了判断多边形方法,邻边不平行,对边交点不在线段上。一开始正则表达式判断放错了位置,导致会产生输入点个数的低级错误,还有精度问题,一开始设为1e-16导致精度太小,找了很久才发现这个问题,因为自身能力问题所以选项四没有完成编写,只有大概思路。   改进建议:       通过上面图片可以看出代码复杂度较高,对方法应该放在哪个类里还需更加明确,写好注释,在调用的时候才不会浪费多余时间,思路需要更加严谨,判断方法应该考虑更多特殊情况。 2.7-3 设计一个银行业务类 分数 20 作者 吴光生 单位 新余学院

编写一个银行业务类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) {
        BankBusiness.welcome();
        Scanner input = new Scanner(System.in);
        String name = input.next();
        String password = input.next();
        BankBusiness yonhu = new BankBusiness(name, password);
        yonhu.deposit(input.next(), input.nextDouble());
        yonhu.withdraw(input.next(), input.nextDouble());
        yonhu.withdraw(input.next(), input.nextDouble());
        yonhu.withdraw(input.next(), input.nextDouble());
        BankBusiness.welcomeNext();

    }
}
    class BankBusiness{
        public static String bankName = "中国银行";
        private String name,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 password,double cunk){
            if(!password.equals(this.password)){
                System.out.println("您的密码错误!");
                return;
            }
            this.balance+=cunk;
            System.out.println("您的余额有"+this.balance+"元。");
        }
        public void withdraw(String password,double qukuan){
            if(!password.equals(this.password)){
                System.out.println("您的密码错误!");
                return;
            }if(this.balance<qukuan){
                System.out.println("您的余额不足!");
                return;
            }this.balance-=qukuan;
            System.out.println("请取走钞票,您的余额还有"+this.balance+"元。");
        }

    }
View Code 踩坑心得:         第三题还是较为简单,根据题目题干编写类与方法还是比较容易完成。
  • 第五次PTA
1.7-1 点线形系列5-凸五边形的计算-1 分数 50 作者 蔡轲 单位 南昌航空大学

用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
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

设计与分析:

     设计一个五边行的类,判断是否为五边形,判断凹凸性,考虑五边形五个点的构造,五点构成四边形或者三角形。做选项3需要考虑线与五边形交点的情况,一个交点还是两个交点,也有可能与边重合,因为只考虑凸五边形,然后用计算四边形或者三角形的面积方法进行面积输出。

部分重要代码以及类图:

class Pentagon {

    Point a = new Point();
    Point b = new Point();
    Point c = new Point();
    Point d = new Point();
    Point e = new Point();
    Line ab = new Line();
    Line bc = new Line();
    Line cd = new Line();
    Line de = new Line();
    Line ea = new Line();
    Line ac = new Line();
    Line be = new Line();
    Line ad = new Line();
    Line bd = new Line();
    Line ec = new Line();

    Quadrilateral q = new Quadrilateral();
    public ArrayList<Point> points(){
        ArrayList<Point> points = new ArrayList<>();
        points.add(a);
        points.add(b);
        points.add(c);
        points.add(d);
        points.add(e);
        return points;
    }
    //构造五边形
    public void set_Pentagon(Point a, Point b, Point c, Point d, Point e) {
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
        this.e = e;
        ab = new Line(a, b);
        bc = new Line(b, c);
        cd = new Line(c, d);
        de = new Line(d, e);
        ea = new Line(e, a);
        ac = new Line(a, c);
        be = new Line(b, e);
        ad = new Line(a, d);
        bd = new Line(b, d);
        ec = new Line(e, c);

    }
    //去掉三点共线的中点
public void qudian(ArrayList<Point> points){
    Line line = new Line();
    for (int i = 0; i < points.size(); i++) {
        int num1 = i + 1;
        int num2 = i + 2;
        if (num1 >= points.size()) {
            num1 = num1 - points.size();
        }
        if (num2 >= points.size()) {
            num2 = num2 - points.size();
        }
        if (line.sdgx(points.get(i), points.get((i + 1) % points.size()), points.get((i + 2) % points.size()))) {
            points.remove(points.get((i + 2) % points.size()));
        }
    }

}

    public boolean ifPentagon() {
        Line line = new Line();
        ArrayList<Point> points = new ArrayList<>();
        points.add(a);
        points.add(b);
        points.add(c);
        points.add(d);
        points.add(e);
        for (int i = 0; i < points.size(); i++) {
            int num1 = i + 1;
            int num2 = i + 2;
            if (num1 >= points.size()) {
                num1 = num1 - points.size();
            }
            if (num2 >= points.size()) {
                num2 = num2 - points.size();
            }
            if (line.sdgx(points.get(i), points.get((i + 1) % 5), points.get((i + 2) % 5))) {
                return false;
            }
        }
        if (ab.parallel(bc) == -1 && bc.parallel(cd) == -1 && cd.parallel(de) == -1 && de.parallel(ea) == -1 && ea.parallel(ab) == -1) {
            if (!(ab.if_in_line(ab.jiaodian(cd)) && cd.if_in_line(ab.jiaodian(cd))) && !(ab.if_in_line(ab.jiaodian(de)) && de.if_in_line(ab.jiaodian(de)))
                    && !(bc.if_in_line(ea.jiaodian(bc)) && ea.if_in_line(ea.jiaodian(bc))) && !(de.if_in_line(bc.jiaodian(de)) && bc.if_in_line(bc.jiaodian(de)))
                    && !(cd.if_in_line(cd.jiaodian(ea)) && ea.if_in_line(cd.jiaodian(ea))))
                return true;
        }
        return false;
    }


    //凸为true 凹为false
    public boolean ifTuAo() {
        if ((a.chachen(b, c) && b.chachen(c, d) && c.chachen(d, e) && d.chachen(e, a) && e.chachen(a, b)) || (!a.chachen(b, c) && !b.chachen(c, d) && !c.chachen(d, e) && !d.chachen(e, a) && !e.chachen(a, b)))
            return true;
        else return false;


    }

    //  public boolean Wudian_Pentagon() {
    //}
    //计算五边形周长
    public double calculate_C() {
        double c1, c2, c3, c4, c5;
        c1 = a.calculate_distance(b);
        c2 = b.calculate_distance(c);
        c3 = c.calculate_distance(d);
        c4 = d.calculate_distance(e);
        c5 = e.calculate_distance(a);
        double C = c1 + c2 + c3 + c4 + c5;
        return C;

    }

    public double calculate_S() {
        double s1, s2, s3;
        double l1, l2, l3;//三角形底边
        double g1, g2, g3;//三角形三条高
        l1 = b.calculate_distance(c);
        l2 = c.calculate_distance(d);
        l3 = d.calculate_distance(e);
        g1 = bc.linedistance(a);
        g2 = cd.linedistance(a);
        g3 = de.linedistance(a);
        s1 = l1 * g1 * 0.5;
        s2 = l2 * g2 * 0.5;
        s3 = l3 * g3 * 0.5;
        double S = s1 + s2 + s3;
        return S;

    }

}
View Code

 

 

踩坑心得:

       这次作业也没有踩很多坑,因为每次作业大同小异,更多是难度上的增加,情况更加多种多样,但明显一次比一次更加熟练。

 改进建议:

       选项3分割面积依旧没有拿到满分,情况多种多样,还是超出了能力范围,可以看到这次代码质量并不好,深度还是复杂度都超过了正常范围,需要减少代码中的冗余变量,以及简化if else的书写,可以多用switch。

2.

7-2 点线形系列5-凸五边形的计算-2 分数 50 作者 蔡轲 单位 南昌航空大学

用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
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

设计与分析:         第二题是第一题的延伸,其实就是一道题目,考虑多边形的六种位置关系,只是初步了解了一下题目,因为时间问题也没有深入思考编写,导致分数并不多。


  • 期中考试PTA
1. 7-1 点与线(类设计) 分数 20 作者 段喜龙 单位 南昌航空大学
  • 设计一个类表示平面直角坐标系上的点Point,私有属性分别为横坐标x与纵坐标y,数据类型均为实型数,除构造方法以及属性的getter与setter方法外,定义一个用于显示信息的方法display(),用来输出该坐标点的坐标信息,格式如下:(x,y),数值保留两位小数。为简化题目,其中,坐标点的取值范围设定为(0,200]。若输入有误,系统则直接输出Wrong Format

  • 设计一个类表示平面直角坐标系上的线Line,私有属性除了标识线段两端的点point1、point2外,还有一个字符串类型的color,用于表示该线段的颜色,同样,除构造方法以及属性的getter与setter方法外,定义一个用于计算该线段长度的方法getDistance(),还有一个用于显示信息的方法display(),用来输出线段的相关信息,输出格式如下:

      ```
          The line's color is:颜色值
          The line's begin point's Coordinate is:
          (x1,y1)
          The line's end point's Coordinate is:
          (x2,y2)
          The line's length is:长度值
      ```
    
     

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

      设计类图如下图所示。
    
     

1641304523(1).jpg

** 题目要求:在主方法中定义一条线段对象,从键盘输入该线段的起点坐标与终点坐标以及颜色,然后调用该线段的display()方法进行输出。**

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

设计与分析:

       根据类图以及继承关系,分辨抽象类以及继承类,补充好主类,难度并不大。

踩坑心得:

       掌握好了继承与抽象知识点,本题并不难,很顺利完成。

 

2.

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

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

  • 对题目中的点Point类和线Line类进行进一步抽象,定义一个两个类的共同父类Element(抽象类),将display()方法在该方法中进行声明(抽象方法),将Point类和Line类作为该类的子类。
  • 再定义一个Element类的子类面Plane,该类只有一个私有属性颜色color,除了构造方法和属性的getter、setter方法外,display()方法用于输出面的颜色,输出格式如下:The Plane's color is:颜色
  • 在主方法内,定义两个Point(线段的起点和终点)对象、一个Line对象和一个Plane对象,依次从键盘输入两个Point对象的起点、终点坐标和颜色值(Line对象和Plane对象颜色相同),然后定义一个Element类的引用,分别使用该引用调用以上四个对象的display()方法,从而实现多态特性。示例代码如下:
          element = p1;//起点Point
          element.display();
          
          element = p2;//终点Point
          element.display();
          
          element = line;//线段
          element.display();
          
          element = plane;//面
          element.display();
    
      类结构如下图所示。

1641340607(1).jpg

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

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

设计与分析:

     本次新增一个类,element类作为父类,用子类分别构造对象,之后定义一个element引用,分别调用每个子类以及父类的dispel()。

import java.util.*;

public class Main{
    public static void main(String[] args) {
        //Point p1=new Point();
        //Point p2=new Point();
        Line line = new Line();
        
        Scanner input = new Scanner(System.in);
        double x1, y1, x2, y2;
        x1 = input.nextDouble();
        y1 = input.nextDouble();
        x2 = input.nextDouble();
        y2 = input.nextDouble();
        String str = input.next();
        if (x1 > 0 && x1 <= 200 && y1 > 0 && y1 <= 200 && x2 > 0 && x2 <= 200 && y2 > 0 && y2 <= 200)  //控制输入坐标的范围
        {
            Point p1 = new Point();
            Point p2 = new Point();
            p1.setX(x1);
            p1.setY(y1);
            p2.setX(x2);
            p2.setY(y2);
            line.setPoint1(p1);
            line.setPoint2(p2);
            line.setColor(str);
            Plane plane = new Plane();;

            plane.setColor(str);
            Element element;
            element = p1;//起点Point
            element.display();

            element = p2;//终点Point
            element.display();

            element = line;//线段
            element.display();

            element = plane;//面
            element.display();
        } else {
            System.out.println("Wrong Format");
        }


    }
}

abstract class Element {
    abstract void display();
}

class Point extends Element {

    private double x;
    private double y;

    Point() {
    }

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

    double getX() {
        return x;
    }

    void setX(double x) {
        this.x = x;
    }

    double getY() {
        return y;
    }

    void setY(double y) {
        this.y = y;
    }

    public void display() {
        System.out.printf("(%.2f,%.2f)\n", x, y);
    }
}

class Line extends Element {
    private static Point point1;
    private static Point point2;
    static double l;
    static String color;

    Line() {
    }

    Line(Point p1, Point p2, String color) {
        this.setPoint1(p1);
        this.setPoint2(p2);
        this.color = color;
    }

    public Point getPoint1() {
        return point1;
    }

    public void setPoint1(Point point1) {
        this.point1 = point1;
    }

    public Point getPoint2() {
        return point2;
    }

    public void setPoint2(Point point2) {
        this.point2 = point2;
    }

    public String getColor() {
        return color;
    }

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

    public static double getDistance() {
        double x1, x2, y1, y2;
        x1 = point1.getX();
        y1 = point1.getY();
        x2 = point2.getX();
        y2 = point2.getY();
        l = Math.sqrt((y1 - y2) * (y1 - y2) + (x1 - x2) * (x1 - x2));
        return l;
    }

    public void display() {
        if (point1.getX() == point2.getX() && point1.getY() == point2.getY())
            System.out.print("Wrong Format");
        else {
            System.out.println("The line's color is:" + 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 {
    private String color;

    public Plane() {
        super();
    }

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

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

    public String getColor() {
        return color;
    }

    public void display() {
        System.out.print("The " + "Plane's color is:" +this.color);
    }
}
View Code

 

踩坑心得:

     第二次很疑惑,所有测试点过了但是没有满分,直到结束也没有发现错误,回到寝室发现一个可能的原因可能是输出格式,两位小数的原因,没有按照题目给的方式去编写。

改进建议:

     采用String.format("%.2f", data)这个保留两位小数方法。

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

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

  • 在原有类设计的基础上,增加一个GeometryObject容器类,其属性为ArrayList<Element>类型的对象(若不了解泛型,可以不使用<Element>
  • 增加该类的add()方法及remove(int index)方法,其功能分别为向容器中增加对象及删除第index - 1(ArrayList中index>=0)个对象
  • 在主方法中,用户循环输入要进行的操作(choice∈[0,4]),其含义如下:
    • 1:向容器中增加Point对象
    • 2:向容器中增加Line对象
    • 3:向容器中增加Plane对象
    • 4:删除容器中第index - 1个数据,若index数据非法,则无视此操作
    • 0:输入结束
    示例代码如下:
       choice = input.nextInt();
        while(choice != 0) {
            switch(choice) {
            case 1://insert Point object into list 
              ...
                break;
            case 2://insert Line object into list
                ...
                break;
            case 3://insert Plane object into list
                ...
                break;
            case 4://delete index - 1 object from list
                int index = input.nextInt();
                ...
            }
            choice = input.nextInt();
        }
    
      输入结束后,按容器中的对象顺序分别调用每个对象的display()方法进行输出。
    类图如下所示:

classdiagram.jpg

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

设计与分析:

增加一个GeometryObject容器类,其属性为ArrayList<Element>类型的对象,

向容器中增加Point对象

向容器中增加Line对象

向容器中增加Plane对象

删除容器中第index - 1个数据,若index数据非法,则无视此操作。

重要代码如下:

    public static void main(String[] args) {
        double x1, y1, x2, y2;
        String color;
        Scanner input = new Scanner(System.in);
        int choice = 0, index = 0;

        GeometryObject container = new GeometryObject();
        choice = input.nextInt();
        while (choice != 0) {
            switch (choice) {
                case 1:
                    x1 = input.nextDouble();
                    y1 = input.nextDouble();
                    container.add(new Point(x1, y1));
                    break;
                case 2:
                    x1 = input.nextDouble();
                    y1 = input.nextDouble();
                    x2 = input.nextDouble();
                    y2 = input.nextDouble();
                    color = input.next();
                    container.add(new Line(new Point(x1, y1), new Point(x2, y2), color));
                    break;
                case 3:
                    color = input.next();
                    container.add(new Plane(color));
                    break;
                case 4:
                    index = input.nextInt();
                    container.remove(index);
                    break;
            }
            choice = input.nextInt();
        }

        for (Element element : container.getList()) {
            element.display();
        }
    }

}
View Code

踩坑心得:

     这题难度也不大,很顺利完成。


 

 

(6-9周)总结

          这几周java的编程能力得到了很大提升,对类的多态,封装,继承,接口,抽象都有了初步的掌握。类方法的调用,用子类还是父类对象去调用都有了很好的区分,老师布置的作业实验也是难度不一,做了pta的作业之后再做实验更加得心应手,老师在课堂也举出了较多的例子解析java这些主要知识点,归根结底还是得靠自己多去敲,多钻研,多思考才是对代码能力提升的最好方式!

 

 


 

标签:总结,getX,double,Blog,getY,&&,new,第二次,Line
From: https://www.cnblogs.com/dtyyyy-1016/p/16840101.html

相关文章

  • BLOG-2
    1.前言第四次作业:第一题:考察正则表达式。第三题:考察java构造方法的创建和使用。 四边形,五边形需要复杂的算法和细致的思路,考虑到每个可能的情况,计算面积比较困难。期......
  • 学期2022-2023-1 学号20221417 《计算机基础与程序设计》第九周学习总结
    这个作业属于哪个课程<班级的链接>(如2022-2023-1-计算机基础与程序设计)这个作业要求在哪里<作业要求的链接>(如2022-2023-1计算机基础与程序设计第九周作业这......
  • JAVA的PTA题目集4、5和期中考试的总结
    一、前言:(1)题量,难度     1、题目集4(四边形):一共有三道题,第一题和第三题较为简单,第二题关于凸四边形的计算难度加大。 2、题目集5(五边形):一共有两道题,都是......
  • 20201317 LYX 第九周学习总结
    信号和信号处理知识点总结介绍信号和中断的统一处理,有助于从正确的角度看待信号;将信号视为进程中断,将进程从正常执行转移到信号处理;解释信号的来源;解释Unix/Linux......
  • 题目集4~6的总结
    前言:前两次大作业题目主要是对java基本语法的考察,例如:基础类型的使用以及String类方法的使用。第三次大作业则开始进入到面向对象程序设计的范畴,考察了类的设计。总体......
  • 第九周学习总结(2022-2023-1 计算机基础与程序设计)姚博茗
    学期(2022-2023-1)学号(20221407)计算机基础与程序设计第九周学习总结作业信息这个作业属于哪个课程2022-2023-1-计算机基础与程序设计这个作业要求在哪里2022-......
  • 题目集4、5以及期中考试的总结性Blog
    前言:这两次的pta题目也是多边形系列的,题目要求复杂,测试点多,难度高,虽然题目数量少了,但所需时间更多。知识点涉及的其实主要还是类的运用,只是为实现功能所需的各种算法难度较......
  • PTA题目集阶段总结2及期中考试
    PTA题目集阶段总结2及期中考试 前沿概要 经过前一次的点线系列三角形熏陶后,第四次和第五次大作业就进入了四边形和五边形的相关计算。总体来说代码难度提升,期中考试的......
  • 常用网站源代码小功能总结
    一、跑马灯文字或图<marqueescrollamount="8">跑马灯文字或图片</marquee>注:scrollamount="8",数字8为文字移动的速度二、从某表格上调用P1_6记录<%=(webol.Fields.Item......
  • 【分布式技术专题】「架构实践于案例分析」总结和盘点目前常用分布式技术特别及问题分
    分布式架构互联⽹架构演进单体应⽤架构定义⼀个归档包(例如war格式)包含所有功能的应⽤程序,我们通常称为单体应⽤。⽽架构单体应⽤的⽅法论,就是单体应⽤架构。架构示意图优缺......