(1)前言

第四次PTA作业:

第四次PTA作业有三道题,两道简单,一道难度较高。一个纯粹的正则表达式的考察。一个时对我们类的设计的考察,一个则是综合的考察

第五次PTA作业:

第五次PTA作业就只有一道题,也即时上次作业的延续。

期中PTA作业:

期中三道题分别考察类的设计,封装和继承,容器类。

(2)设计与分析

第四次PTA作业:

1,3道题目是比较简单的,解决起来难度不大。下面是源码
第一题:


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        boolean flag1 = true;
        int i = 0;
        int total = 0,temp;
        String s =in.nextLine();
        if(s.equals("end"))
            flag1 = false;
        while(flag1){
                String[] s1 = s.split("\\D");
                for(i = 0;i < s1.length;i++) {
                    if(!s1[i].equals("")) {
                    temp = Integer.parseInt(s1[i]);
                    total += temp;
                    }
                }
                System.out.println(total);
                total = 0;
                 s =in.nextLine();
                if(s.equals("end"))
                    flag1 = false;
        }
    }

}
第三题:


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        BankBusiness account = new BankBusiness();
        account.welcome();
        account.setaccount(in.next(), in.next());
        account.deposit(in.next(), in.nextDouble());
        account.withdraw(in.next(), in.nextDouble());
        account.withdraw(in.next(), in.nextDouble());
        account.withdraw(in.next(), in.nextDouble());
        account.welcomeNext();
    }

}
class BankBusiness{
    public static String bankName = "中国银行";
    private String name;
    private double balance;
    private String password;
    void welcome() {
        System.out.println(bankName  +  "欢迎您的到来!");
    }
    void welcomeNext() {
        System.out.println("请收好您的证件和物品,欢迎您下次光临!");
    }
    public  void setaccount(String a ,String b) {
            this.name = a;
            this.password = b;
            this.balance = 0;
    }
    public void deposit(String c, double d) {
        if(this.password .equals(c)) {
            this.balance += d;
            System.out.println("您的余额有"+balance +"元。");
        }
        else {
            System.out.println("您的密码错误!");
        }
    }
    public void withdraw(String c, double d) {
        if(this.password .equals(c)) {
            if(this.balance < d) {
                System.out.println("您的余额不足!");
            }
            else {
                this.balance -= d;
                System.out.println("请取走钞票,您的余额还有" + balance +"元。");
            }
        }
        else {
            System.out.println("您的密码错误!");
        }
    }
}
第二题的难度较高,解决起来比较复杂,如果类的设计不好的话,很容易会造成后面的选项越写越困难,甚至把自己也给绕进去,所以类的设计尽量做到单一职责。而且方法也要尽可能的可以多次调用,如果一个方法只能调用一次,只是徒有其表。

 

 


第五次PTA作业:

在上一次的基础上变成了五边形的计算。下面是五边形部分的源码:




class Pentagon{
    Line l = new Line();
    
    boolean is_pen(Point p)
    {
        
        if((l.is_xj(p.x1, p.y1, p.x2, p.y2, p.x3, p.y3, p.x4, p.y4) == false)&&(l.is_xj(p.x1, p.y1, p.x2, p.y2, p.x4, p.y4, p.x5, p.y5) == false)&&(l.is_xj(p.x2, p.y2, p.x3, p.y3, p.x4, p.y4, p.x5, p.y5) == false)&&(l.is_xj(p.x2, p.y2, p.x3, p.y3, p.x5, p.y5, p.x1, p.y1) == false)&&(l.is_xj(p.x3, p.y3, p.x4, p.y4, p.x5, p.y5, p.x1, p.y1) == false))
            return true;
        else
            return false;
    }
    boolean is_pen1(Point p)
    {
        
        if((l.is_xj(p.x2, p.y2, p.x3, p.y3, p.x4, p.y4, p.x5, p.y5) == false)&&(l.is_xj(p.x2, p.y2, p.x3, p.y3, p.x5, p.y5, p.x6, p.y6) == false)&&(l.is_xj(p.x3, p.y3, p.x4, p.y4, p.x5, p.y5, p.x6, p.y6) == false)&&(l.is_xj(p.x3, p.y3, p.x4, p.y4, p.x6, p.y6, p.x2, p.y2) == false)&&(l.is_xj(p.x4, p.y4, p.x5, p.y5, p.x6, p.y6, p.x2, p.y2) == false))
            return true;
        else
            return false;
    }
    
    boolean AT_pen(Point p)
    {
        double t1,t2,t3,t4,t5;
        t1=(p.x1-p.x3)*(p.y2-p.y3)-(p.x2-p.x3)*(p.y1-p.y3);
        t2=(p.x2-p.x4)*(p.y3-p.y4)-(p.x3-p.x4)*(p.y2-p.y4);
        t3=(p.x3-p.x5)*(p.y4-p.y5)-(p.x4-p.x5)*(p.y3-p.y5);
        t4=(p.x4-p.x1)*(p.y5-p.y1)-(p.x5-p.x1)*(p.y4-p.y1);
        t5=(p.x5-p.x2)*(p.y1-p.y2)-(p.x1-p.x2)*(p.y5-p.y2);
        if (t1*t2*t3*t4*t5 < 0.0000000001)
            return false;
        else
            return true;
    }
    public double P_area(Point p)
    {
        double t1,t2,t3,t4,t5;
        t1=(p.x1-p.x3)*(p.y2-p.y3)-(p.x2-p.x3)*(p.y1-p.y3);
        t2=(p.x2-p.x4)*(p.y3-p.y4)-(p.x3-p.x4)*(p.y2-p.y4);
        t3=(p.x3-p.x5)*(p.y4-p.y5)-(p.x4-p.x5)*(p.y3-p.y5);
        t4=(p.x4-p.x1)*(p.y5-p.y1)-(p.x5-p.x1)*(p.y4-p.y1);
        t5=(p.x5-p.x2)*(p.y1-p.y2)-(p.x1-p.x2)*(p.y5-p.y2);
        double s = 0;
        s = (t1+t2+t3+t4+t5);
        s = s/8*3;
        s = Math.abs(s);
        return s;
    }
    public double P_c(Point p)
    {
        double c1 = Math.sqrt((Math.pow(p.x1-p.x2, 2)+Math.pow(p.y1-p.y2, 2)));
        double c2 = Math.sqrt((Math.pow(p.x2-p.x3, 2)+Math.pow(p.y2-p.y3, 2)));
        double c3 = Math.sqrt((Math.pow(p.x1-p.x5, 2)+Math.pow(p.y1-p.y5, 2)));
        double c4 = Math.sqrt((Math.pow(p.x3-p.x4, 2)+Math.pow(p.y3-p.y4, 2)));
        double c5 = Math.sqrt((Math.pow(p.x4-p.x5, 2)+Math.pow(p.y4-p.y5, 2)));
        return c1+c2+c3+c4+c5;
    }
}

我也是只实现了一部分的功能,因为初期的类的设计过于粗糙,后面完全把自己绕进去了,所以我后面把自己写的垃圾给删掉了。

 

 


期中作业:

期中的题目也是比较简单的。考察的知识点也是基础的。
下面是源码:


import java.util.Scanner;
import java.text.DecimalFormat;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        Point p1 = new Point(in.nextDouble(),in.nextDouble());
        Point p2 = new Point(in.nextDouble(),in.nextDouble());
        Line l = new Line(p1,p2,in.next());
        Plane p = new Plane(l.color);
        if((p1.getX() > 200 || p1.getX() <= 0) || (p1.getY() > 200 || p1.getY() <= 0) || (p2.getX() > 200 || p2.getX() <= 0) || (p2.getY() > 200 || p2.getY() <= 0))
            System.out.println("Wrong Format");
        else {
          Element e1 = p1;//起点Point
          e1.display();
          
          Element e2 = p2;//终点Point
          e2.display();
          
          Element e3 = l;//线段
          e3.display();
          
          Element e4 = p;//面
          e4.display();
        }

    }

}
class Element{
    
    public void display() {
        // TODO Auto-generated method stub
        
    }

    

    
    


    
}

class Point extends Element{
    DecimalFormat df = new DecimalFormat("0.00");
      double x;
      double y;
    
    public Point(double x, double y) {
        super();
        this.x = x;
        this.y = y;
    }
    public double getX() {
        return x;
    }
    public void setX(double x) {
        this.x = x;
    }
    public double getY() {
        return y;
    }
    public void setY(double y) {
        this.y = y;
    }
    @Override
    public void display(){
        System.out.println("("+ df.format(x) + "," + df.format(y) +")");
    }
    
}

class Line extends Element{
    DecimalFormat df = new DecimalFormat("0.00");
     Point Point1;
     Point Point2;
    String color;
    public Point getPoint1() {
        return Point1;
    }
    public void setPoint1(Point point1) {
        Point1 = point1;
    }
    public Point getPoint2() {
        return Point2;
    }
    public void setPoint2(Point point2) {
        Point2 = point2;
    }
    public Line(Point point1, Point point2, String color) {
        super();
        Point1 = point1;
        Point2 = point2;
        this.color = color;
    }
    double getDistance(Point p1,Point p2) {
         return Math.sqrt(Math.pow(p1.getX()-p2.getX(), 2)+Math.pow(p1.getY()-p2.getY(), 2));
    }
    @Override
    public void display() {
        if((Point1.getX() > 200 || Point1.getX() <= 0) || (Point1.getY() > 200 || Point1.getY() <= 0) || (Point2.getX() > 200 || Point2.getX() <= 0) || (Point2.getY() > 200 || Point2.getY() <= 0))
            System.out.println("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:" + df.format(getDistance(Point2, Point1)));
        }
    }
}
class Plane extends Element{
     String color;

    public String getColor() {
        return color;
    }

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

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

我的第三题没时间写了,主要是我在第二题时,把display和dispaly拼错了耗费了一个小时的时间在这个上面。

(3)踩坑心得

类的设计一定要足够的完善,基本的需求一定要完成,不要面对过程编程,这样这个学期的课就白上了。
字母一定要拼对,错了的话真的很难发现,一定要小心在小心。

(4)改进建议

类的设计要更加的完善,先画好类图,每个部分要实现的功能,再去写,而不是想道哪就写到那,这样虽然可能会很快解决一些基础的问题,但是当这些问题稍微深入一点,解决起来就会非常的困难。
基础知识要稳固,写题要细心,做事不要急躁。

(5)总结

对类的设计还是不够到位,像现在这样的连续性的作业到后面全是推翻重来,折磨自己,而且最重要的时完全没有办法解决问题。
写代码时一定要细心,不要急躁。
简单的题目也要去做到面向对象程序设计,而不是面向过程程序设计。
posted on 2022-05-15 22:39  青我刚门会上影  阅读(39)  评论(0)    收藏  举报