ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

第二次BLOG总结

2022-05-15 20:33:10  阅读:131  来源: 互联网

标签:总结 p2 p1 return Point double BLOG 第二次 public


前言

1.前几次作业作业量相对来说比较大,难度也比较高,需要综合运用许多学过的和没学过的知识,没学过的需要自己找资料

2.期中考试、第4.5次大作业主要运用的知识有

(1)类的设计

(2)继承与多态

(3)动态数组

(4)正则表达式

3.主要是点线型的题目很难,需要设计很多个类

设计与分析

期中考试第一题

  • 设计一个类表示平面直角坐标系上的点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:长度值
      ```
  1 package 期中考试;
  2 import java.util.Scanner;
  3 public class 第一题 {
  4 
  5     public static void main(String[] args) {
  6         // TODO Auto-generated method stub
  7         Scanner in = new Scanner(System.in);
  8         double x1 = in.nextDouble();
  9         double y1 = in.nextDouble();
 10         double x2 = in.nextDouble();
 11         double y2 = in.nextDouble();
 12         String color = in.next();
 13         Point p1 = new Point(x1, y1);
 14         Point p2 = new Point(x2, y2);
 15         
 16         Line line = new Line(p1, p2, color);
 17         line.display();
 18         
 19         
 20         in.close();
 21     }
 22 }
 23 
 24 class Point {
 25     private double x;
 26     private double y;
 27     
 28     public  Point() {
 29         
 30     }
 31     
 32     public Point(double x, double y) {
 33         this.x = x;
 34         this.y = y;
 35     }
 36     
 37     public void setX(double x) {
 38         this.x = x;
 39     }
 40     
 41     public double getX() {
 42         return x;
 43     }
 44     
 45     public void setY(double y) {
 46         this.y = y;
 47     }
 48     
 49     public double getY() {
 50         return y;
 51     }
 52     
 53     public void display() {
 54         System.out.println("("+String.format("%.2f",getX() )+","+String.format("%.2f",getY() )+")");
 55     }
 56 }
 57 class Line {
 58     private Point p1;
 59     private Point p2;
 60     private String color;
 61     
 62     public Line() {
 63         
 64     }
 65     
 66     public Line(Point p1, Point p2, String color) {
 67         this.p1 = p1;
 68         this.p2 = p2;
 69         this.color = color;
 70     }
 71     
 72     public void setPoint1(Point p1) {
 73         this.p1 = p1;
 74     }
 75     
 76     public Point getPoint1() {
 77         return p1;
 78     }
 79     
 80     public void setPoint2(Point p2) {
 81         this.p2 = p2;
 82     }
 83     
 84     public Point getPoint2() {
 85         return p2;
 86     }
 87     
 88     
 89     public double getDistance() {
 90         double distance;
 91         distance = Math.sqrt(Math.pow(p1.getX() - p2.getX(), 2) + Math.pow(p1.getY() - p2.getY(),2 ));
 92         return distance;
 93     }
 94     
 95     public void display() {
 96         if(p1.getX() > 0 && p1.getX() <=200 && p1.getY() > 0 && p1.getY() <= 200 && p2.getX() > 0 && p2.getX() <= 200 && p2.getY() > 0 && p2.getY() <=200) {
 97                 System.out.println("The line's color is:"+color);
 98                 System.out.println("The line's begin point's Coordinate is:");
 99                 p1.display();
100                 System.out.println("The line's end point's Coordinate is:");
101                 p2.display();
102                 System.out.println("The line's length is:"+String.format("%.2f", getDistance()));
103         }
104         else {
105             System.out.println("Wrong Format");
106         }
107     }
108     
109 }

 

 这道题并不难,主要是类的设计,加深了我对类的理解,类与int、double、String这种的相似。

在第四题的第一题中需要用到动态数组加上正则表达式,把每一行的数字全部提取出来

7-1

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner in = new Scanner(System.in);
        String num = "";
        int sum = 0;
        ArrayList<String> strList = new ArrayList<String>();          //动态数组
        while(!num.equals("end")) {          //结束字符end
            
            num = in.nextLine();
            strList.add(num);                //添加至动态数组
                
        }
        String reg = "[0-9]+";
        for(int i = 0; i < strList.size() - 1; i++) {
            
            Pattern pattern = Pattern.compile(reg);                   //编译正则表达式
            Matcher matcher = pattern.matcher(strList.get(i));        //指定要匹配的字符串
            
            while(matcher.find()) {
                
                sum = sum + Integer.parseInt(matcher.group());     //将匹配的字符转换为整数并且累加
                
                
            }
            
            
            System.out.println(sum);
            sum = 0;
        }
        
    }

}

定义一个动态数组:ArrayList<类> 名称 = new ArrayList<类>();

使用add.()添加get.()提取,用使用Integer.parseInt()强制转换把字符串转换为整数

 

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()方法。

输入格式:

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

输出格式:

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

 1 import java.util.Scanner;
 2 public class Main {
 3 
 4     public static void main(String[] args) {
 5         // TODO Auto-generated method stub
 6 
 7         BankBusiness account = new BankBusiness();
 8         account.weclome();
 9         account.Open();
10         account.deposit();
11         account.withdraw();
12         account.withdraw();
13         account.withdraw();
14         account.welcomeNext();
15     }
16 
17 }
18 
19 class BankBusiness{
20     
21     Scanner in = new Scanner(System.in);
22     String input = new String();
23     public String bankname = "中国银行";
24     private String name;         
25     private String password;         //密码
26     private int balance;          //余额
27     
28     public void weclome() {
29         System.out.println(bankname+"欢迎您的到来!");
30     }
31     
32     public void welcomeNext() {
33         System.out.println("请收好您的证件和物品,欢迎您下次光临!");
34     }
35     
36     void Open() {
37         name = in.next();
38         password = in.next();
39         balance = 0;
40     }
41     
42     public void deposit() {
43         String password;
44         password = in.next();
45         
46         if(password.equals(this.password)) {
47             this.balance = in.nextInt();
48             System.out.println("您的余额有"+balance+".0元。");
49         }
50         else {
51             
52             System.out.println("您的密码错误!");
53             
54         }
55         
56     }
57     
58     public void withdraw() {
59         int balance;
60         String password;
61         password = in.next();
62         balance = in.nextInt();            //取款
63         if(password.equals(this.password)) {
64             if(balance > this.balance) {
65                 System.out.println("您的余额不足!");
66             }
67             else {
68                 this.balance = this.balance - balance;
69                 System.out.println("请取走钞票,您的余额还有"+this.balance+".0元。");
70             }
71         }
72         else {
73             System.out.println("您的密码错误!");
74         }
75         
76     }
77     
78 }

这道题只需在类里面设计一些功能即可

 最难的是点线型的题目

点类的设计是最基本的

点类:

class point {
    private double x;
    private double y;
    
    public point() {
        
    }
    
    public point(double x, double y) {
        this.x = x;
        this.y = y;
    }
    
    public void setX (double x) {
        this.x = x;
    }
    
    public double getX () {
        return x;
    }
    
    public void setY (double y) {
        this.y = y;
    }
    
    public double getY () {
        return y;
    }
}

对x,y进行封装

 

线类:

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

 

用于判断线段是否相交,是否平行

 

再添加一些其他类来完善点线型题目的功能

一个判断直线是否相加我认为特别重要:java.awt.geom.Line2D.linesIntersect(x1,y1,x2,y2,x3,y3,x4,y4);

总结

总的来说这次的题集让我对1.动态数组2.继承3.正则表达式都有着理解的加深

 

标签:总结,p2,p1,return,Point,double,BLOG,第二次,public
来源: https://www.cnblogs.com/XuXu-/p/16271038.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有