OO题目集1~3作业总结

OO题目集1~3作业总结

1.前言

针对于PTA上面的三次题目集的作业,有如下分析:

第一次作业知识点、题量、难度:

①计算两个数的和:使用Java的Scanner进行简单的输入输出,了解int的输入输出以及其定义;

②电话键盘字母数字转换:使用Java的if-else选择结构将字母转换为数字并输出;

③成绩分级管理:将学生分数使用选择结构转换为相应的等级并输出;

④计算税率:使用选择结构计算纳税金额;

⑤计算钱币:对数据进行相除计算,多次对纸币进行判断;

⑥使用一维数组求平均值:使用for循环,将数组里面的整数进行叠加之后求出平均值并输出;

⑦对多个整数进行排序:对输入的整数进行排序并输出,考察对排序方法的使用;

⑧判断三角形类型:对输入的三条边数据使用选择结构进行判断,输出三角形相应的类型。

第一次作业的难度,相对于我个人而言,我觉得比较简单,因为并不是第一次学习编程语言,以前学习过基础的C语言,所以这第一次作业的难易程度对我而言,比较简单,使用以前学过的一些编程语言的知识也能解决问题,之前听一个老师说过,编程语言大部分都是一通百通,真正把一门语言学精了,之后学其他的语言也能很快的上手,这应该也是为什么我会觉得Java第一次作业比较简单吧。

 

第二次作业知识点、题量、难度:

①IP地址转换:分割字符串进行转换,考察了对String类型数据的操作;

②合并两个有序数组为新的有序数组:合并数组并升序输出;

③判断闰年及星期几:对输入数据进行判断,调用方法实现程序的相应功能;

④求下一天:输入日期求取下一天,调用方法实现相应功能;⑤求前N天:对输入数据进行处理,输出前N天。

第二次作业的难度,相对于我个人而言,我觉得难度一般,我感觉主要是为了考察学生的逻辑思维能力以及对题目的的思考,需要对程序的一个完整性有一个很好的了解,需要考虑到输出的多种情况,以及对于数据的边界也有了一个严格的的要求,不能越过数据定义的边界,否则程序无法输出下一天或者前N天等信息。

 

第三次作业知识点、题量、难度:

①创建账户类Account:给出Account类得相关属性以及方法,设计Account,主要为了考察类与对象、封装性的知识点;

②定义日期类:给出了Date类的结构图,通过结构图对题目程序进行编写,主要考查类的封装性;

③一元多项式求导:编写程序,对简单多项式的导函数求解,主要考查了正则表达式、数组的应用、去除字符串的空格。

第三次作业的难度,对于我个人而言,是这前三次作业里面最难的,对于设计Account类以及设计Date类的题目难度感觉还可以,但是感觉写一元多项式求导这题很难,不知道如何下手,对于正则表达式的理解不够透彻,不能熟练的使用正则表达式。

2.设计与分析

由于篇幅限制,因此使用比较有代表性的题目代码进行分析。

第一次作业:

7-8判断三角形类型代码如下:

 1 import java.util.Scanner;
 2 public class Main {
 3 
 4     public static void main(String[] args) {
 5         // TODO Auto-generated method stub
 6         Scanner input = new Scanner(System.in);
 7         double a = input.nextDouble();
 8         double b = input.nextDouble();
 9         double c = input.nextDouble();
10         if(a >= 1 && a <= 200 && b >= 1 && b <= 200 && c >= 1 && c <= 200) {
11             if(a + b > c && a + c > b && b + c > a) {
12                 if(a == b && b==c) {
13                     System.out.println("Equilateral triangle");
14                 }
15                 else if((a == b && a != c) || (a == c && a != b) || (b == c && b != a)) {
16                     if((a * a + b * b - c * c <= 1e-6) || (a * a + c * c - b * b <= 1e-6) || (c * c + b * b - a * a <= 1e-6)){
17                         System.out.println("Isosceles right-angled triangle");
18                     }
19                     else {
20                         System.out.println("Isosceles triangle");
21                     }
22                 }
23                 else if((a * a + b * b - c * c <= 1e-6) || (a * a + c * c - b * b <= 1e-6) || (c * c + b * b - a * a <= 1e-6)) {
24                     System.out.println("Right-angled triangle");
25                 }
26                 else {
27                     System.out.println("General triangle");
28                 }
29             }
30             else {
31                 System.out.println("Not a triangle");
32             }
33         }
34         else {
35             System.out.println("Wrong Format");
36         }
37     }
38 
39 }

 判断三角形类型,1.判断输入的数据是否符合题目定义的边界;2.判断是否为等边三角形;3.判断是否为等腰三角形;4.判断是否为直角三角形。

通过相应的判断,输出相应的结果。判断三角形这题,需要了解浮点型的数据如何判断相等,否则对于直角三角形的判断无法进行,写这道题目,需要了解浮点型数据的一些知识,写起来需要逻辑清晰,判定条件不能出错,判定条件之间如何进行嵌套才能更好的解决此题。

 

第二次作业:

7-4求下一天代码如下:

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static void main(String args[]) {
 5         Scanner input = new Scanner(System.in);
 6         int year = input.nextInt();
 7         int month = input.nextInt();
 8         int day = input.nextInt();
 9         if(checkInputValidity(year,month,day) == false) {
10             System.out.println("Wrong Format");
11         }else {
12             System.out.print("Next date is:");
13             nextDate(year,month,day);
14         }
15     }
16     
17     public static boolean checkInputValidity(int year,int month,int day) {
18         // TODO Auto-generated method stub
19         boolean flag = true;
20         if(year < 1820||year > 2020||month < 1||month > 12 ||day < 1) {
21             flag = false;
22         }else if(month == 4 || month == 6 || month == 9 || month == 11) {
23             if(day > 30) {
24                 flag = false;
25             }
26         }else if(month == 1||month == 3||month == 5||month == 7||month == 8||month == 10||month ==12) {
27             if(day > 31) {
28                 flag = false;
29             }
30         }else {
31             if(isLeapYear(year) == true) {
32                 if(day > 29) {
33                     flag = false;
34                 }
35             }else {
36                 if(day > 28) {
37                     flag = false;
38                 }
39             }
40         }
41         return flag;
42     }
43     
44     public static boolean isLeapYear(int year) {
45         // TODO Auto-generated method stub
46         if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
47             return true;
48         }else {
49             return false;
50         }
51     }
52     
53     public static void nextDate(int year,int month,int day) {
54         // TODO Auto-generated method stub
55         if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10) {
56             if(day == 31) {
57                 System.out.println(year + "-" + (month + 1) + "-1");
58             }else {
59                 System.out.println(year + "-" + month + "-" + (day + 1));
60             }
61         }else if(month == 4 || month == 6 || month == 9 || month == 11) {
62             if(day == 30) {
63                 System.out.println(year + "-" + (month + 1) + "-1");
64             }else {
65                 System.out.println(year + "-" + month + "-" + (day + 1));
66             }
67         }else if(month == 12){
68             if(day == 31) {
69                 System.out.println((year + 1) + "-" + (month - 11) + "-1");
70             }else {
71                 System.out.println(year + "-" + month + "-" + (day + 1));
72             }
73 
74         }else {
75             if(isLeapYear(year) == true) {
76                 if(day == 29) {
77                     System.out.println(year + "-" + (month + 1) + "-1");    
78                 }else {
79                     System.out.println(year + "-" + month + "-" + (day + 1));
80                 }
81             }else {
82                 if(day == 28) {
83                     System.out.println(year + "-" + (month + 1) + "-1");    
84                 }else {
85                     System.out.println(year + "-" + month + "-" + (day + 1));
86                 }
87             }
88         }
89     }
90 }

Main类的几个方法如下:

main:主方法

isLeapYear:判断year是否为闰年,返回boolean类型

checkInputValidity:判断输入日期是否合法,返回布尔值

nextDate:求输入日期的下一天

本题通过调用各种方法,传参来进行判断日期是否合法、是否为闰年,以及输出日期的下一天,对于这道题,需要了解方法的调用以及传参,我在写这道题的时候,对于日期是否合法设定了一个标志位flag,通过返回flag的值判断是否合法;这道题需要对闰年有一个了解:普通闰年:公历年份是4的倍数的,且不是100的倍数,为普通闰年;世纪闰年:公历年份是整百数的,必须是400的倍数才是世纪闰年。编写这道题时,checkInputValidity方法需要注意输入数据的边界;在nextDate方法中时,需要注意大小月、2月以及12月的情况。

 

7-5求前N天代码如下:

  1 import java.util.Scanner;
  2 
  3 public class Main {
  4     public static void main(String args[]) {
  5         Scanner input = new Scanner(System.in);
  6         int year = input.nextInt();
  7         int month = input.nextInt();
  8         int day = input.nextInt();
  9         int n = input.nextInt();
 10         if(checkInputValidity(year,month,day,n) == false) {
 11             System.out.println("Wrong Format");
 12         }else {
 13             System.out.print(n + " days ago is:");
 14             nextDate(year,month,day,n);
 15         }
 16     }
 17     
 18     public static boolean checkInputValidity(int year,int month,int day,int n) {
 19         // TODO Auto-generated method stub
 20         boolean flag = true;
 21         if(n < -10||n > 10) {
 22             flag = false;
 23         }
 24         if(year < 1820||year > 2020||month < 1||month > 12 ||day < 1) {
 25             flag = false;
 26         }else if(month == 4 || month == 6 || month == 9 || month == 11) {
 27             if(day > 30) {
 28                 flag = false;
 29             }
 30         }else if(month == 1||month == 3||month == 5||month == 7||month == 8||month == 10||month ==12) {
 31             if(day > 31) {
 32                 flag = false;
 33             }
 34         }else {
 35             if(isLeapYear(year) == true) {
 36                 if(day > 29) {
 37                     flag = false;
 38                 }
 39             }else {
 40                 if(day > 28) {
 41                     flag = false;
 42                 }
 43             }
 44         }
 45         return flag;
 46     }
 47     
 48     public static boolean isLeapYear(int year) {
 49         // TODO Auto-generated method stub
 50         if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
 51             return true;
 52         }else {
 53             return false;
 54         }
 55     }
 56     
 57     public static void nextDate(int year,int month,int day,int n) {
 58         // TODO Auto-generated method stub
 59         int sum = day - n;
 60         if(month == 5 || month == 7 || month == 10) {
 61             if(sum > 31) {
 62                 System.out.println(year + "-" + (month + 1) + "-" + (sum - 31));
 63             }else if(sum > 0) {
 64                 System.out.println(year + "-" + month + "-" + sum);
 65             }else {
 66                 System.out.println(year + "-" + (month - 1) + "-" + (30 + sum));
 67             }
 68         }else if(month == 4 || month == 6 || month == 9 || month == 11) {
 69             if(sum > 30) {
 70                 System.out.println(year + "-" + (month + 1) + "-" + (sum - 30));
 71             }else if(sum > 0) {
 72                 System.out.println(year + "-" + month + "-" + sum);
 73             }else {
 74                 System.out.println(year + "-" + (month - 1) + "-" + (31 + sum));
 75             }
 76         }else if(month == 12){
 77             if(sum > 31) {
 78                 System.out.println((year + 1) + "-" + (month - 11) + "-" + (sum - 31));
 79             }else if(sum > 0) {
 80                 System.out.println(year + "-" + month + "-" + sum);
 81             }else {
 82                 System.out.println(year + "-" + (month - 1) + "-" + (30 + sum));
 83             }
 84         }else if(month == 1){
 85             if(sum > 31) {
 86                 System.out.println((year + 1) + "-" + (month + 1) + "-" + (sum - 31));
 87             }else if(sum > 0) {
 88                 System.out.println(year + "-" + month + "-" + sum);
 89             }else {
 90                 System.out.println((year - 1) + "-" + (month + 11) + "-" + (31 + sum));
 91             }
 92         }else if(month == 2) {
 93             if(isLeapYear(year) == true) {
 94                 if(sum > 29) {
 95                     System.out.println(year + "-" + (month + 1) + "-" + (sum - 29));
 96                 }else if(sum > 0) {
 97                     System.out.println(year + "-" + month + "-" + sum);
 98                 }else {
 99                     System.out.println(year + "-" + (month - 1) + "-" + (31 + sum));
100                 }
101             }else {
102                 if(sum > 28) {
103                     System.out.println(year + "-" + (month + 1) + "-" + (sum - 28));
104                 }else if(sum > 0) {
105                     System.out.println(year + "-" + month + "-" + sum);
106                 }else {
107                     System.out.println(year + "-" + (month - 1) + "-" + (31 + sum));
108                 }
109             }
110         }else if(month == 3) {
111             if(isLeapYear(year) == true) {
112                 if(sum > 31) {
113                     System.out.println(year + "-" + (month + 1) + "-" + (sum - 31));
114                 }else if(sum > 0) {
115                     System.out.println(year + "-" + month + "-" + sum);
116                 }else {
117                     System.out.println(year + "-" + (month - 1) + "-" + (29 + sum));
118                 }
119             }else {
120                 if(sum > 31) {
121                     System.out.println(year + "-" + (month + 1) + "-" + (sum - 31));
122                 }else if(sum > 0) {
123                     System.out.println(year + "-" + month + "-" + sum);
124                 }else {
125                     System.out.println(year + "-" + (month - 1) + "-" + (28 + sum));
126                 }
127             }
128         }else {
129             if(sum > 31) {
130                 System.out.println(year + "-" + (month + 1) + "-" + (sum - 31));
131             }else if(sum > 0) {
132                 System.out.println(year + "-" + month + "-" + sum);
133             }else {
134                 System.out.println(year + "-" + (month - 1) + "-" + (31 + sum));
135             }
136         }
137     }
138 }

本题与7-4其实相差不是很多,不过一个是求下一天,一个是求前N天,代码大同小异,都是通过调用方法,传参进行判断。但是本题求的是前N天,所以需要对N进行一个思考,当N为正值时,求前N天;当其为负值时,求后-N天。同时需要对N的值进行判断,N是否符合题目要求的取值范围、前N天是否横跨月份、是否横跨年份等。

 

 

 第三次作业:

7-2定义日期类代码如下:

  1 import java.util.Scanner;
  2 
  3 public class Main {
  4     public static void main(String args[]) {
  5         Date date = new Date();
  6         Scanner input = new Scanner(System.in);
  7         int year = input.nextInt();
  8         int month = input.nextInt();
  9         int day = input.nextInt();
 10         date.setYear(year);
 11         date.setMonth(month);
 12         date.setDay(day);
 13         if(date.checkInputValidity() == false) {
 14             System.out.println("Date Format is Wrong");
 15         }else {
 16             System.out.print("Next day is:");
 17             date.getNextDate();
 18         }
 19     }
 20 }
 21 class Date {
 22     private int year;
 23     private int month;
 24     private int day;
 25     public Date() {
 26         
 27     }
 28     public Date(int year,int month,int day) {
 29         this.year = year;
 30         this.month = month;
 31         this.day = day;
 32     }
 33     public int getYear() {
 34         return year;
 35     }
 36     public void setYear(int year) {
 37         this.year = year;
 38     }
 39     public int getMonth() {
 40         return month;
 41     }
 42     public void setMonth(int month) {
 43         this.month = month;
 44     }
 45     public int getDay() {
 46         return day;
 47     }
 48     public void setDay(int day) {
 49         this.day = day;
 50     }
 51     public boolean isLeapYear(int year) {
 52         if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
 53             return true;
 54         }else {
 55             return false;
 56         }
 57     }
 58     public boolean checkInputValidity() {
 59         boolean flag = true;
 60         if(getYear() < 1900||getYear() > 2000||getMonth() < 1||getMonth() > 12 ||getDay() < 1) {
 61             flag = false;
 62         }else if(getMonth() == 1||getMonth() == 3||getMonth() == 5||getMonth() == 7||getMonth() == 8||getMonth() == 10||getMonth() ==12) {
 63             if(getDay() > 31) {
 64                 flag = false;
 65             }
 66         }else if(getMonth() == 4 || getMonth() == 6 || getMonth() == 9 || getMonth() == 11) {
 67             if(getDay() > 30) {
 68                 flag = false;
 69             }
 70         }else {
 71             if(isLeapYear(getYear()) == true) {
 72                 if(getDay() > 29) {
 73                     flag = false;
 74                 }
 75             }else {
 76                 if(getDay() > 28) {
 77                     flag = false;
 78                 }
 79             }
 80         }
 81         return flag;
 82     }
 83     public void getNextDate() {
 84         if(getMonth() == 1 || getMonth() == 3 || getMonth() == 5 || getMonth() == 7 || getMonth() == 8 || getMonth() == 10) {
 85             if(getDay() == 31) {
 86                 System.out.println(getYear() + "-" + (getMonth() + 1) + "-1");
 87             }else {
 88                 System.out.println(getYear() + "-" + getMonth() + "-" + (getDay() + 1));
 89             }
 90         }else if(getMonth() == 4 || getMonth() == 6 || getMonth() == 9 || getMonth() == 11) {
 91             if(getDay() == 30) {
 92                 System.out.println(getYear() + "-" + (getMonth() + 1) + "-1");
 93             }else {
 94                 System.out.println(getYear() + "-" + getMonth() + "-" + (getDay() + 1));
 95             }
 96         }else if(getMonth() == 12){
 97             if(getDay() == 31) {
 98                 System.out.println((getYear() + 1) + "-" + (getMonth() - 11) + "-1");
 99             }else {
100                 System.out.println(getYear() + "-" + getMonth() + "-" + (getDay() + 1));
101             }
102 
103         }else {
104             if(isLeapYear(getYear()) == true) {
105                 if(getDay() == 29) {
106                     System.out.println(getYear() + "-" + (getMonth() + 1) + "-1");    
107                 }else {
108                     System.out.println(getYear() + "-" + getMonth() + "-" + (getDay() + 1));
109                 }
110             }else {
111                 if(getDay() == 28) {
112                     System.out.println(getYear() + "-" + (getMonth() + 1) + "-1");    
113                 }else {
114                     System.out.println(getYear() + "-" + getMonth() + "-" + (getDay() + 1));
115                 }
116             }
117         }
118     }
119     
120 }

 

 

 本题主要是考察调用类,以及考察类的封装性。刚开始写这道题是我没用封装,因为还不了解封装是什么,后来通过网上查阅看到了一个博主对封装的介绍:封装就是把普通的对象进行封装,对象的属性设为私有的,对外提供get和set方法,其他类只能通过get和set对对象属性值进行操作。通过博主的介绍,我对Date类使用了封装,将year、month、day三个属性进行了封装,Main类通过调用Date类的get和set方法来对对象属性进行操作。

 

7-3一元多项式求导(类设计)代码如下:

  1 import java.util.Scanner;
  2 import java.math.BigInteger;
  3 import java.util.LinkedList;
  4 
  5 public class Main {
  6     public static void main(String[] args) {
  7         Scanner input = new Scanner(System.in);
  8         String m = "";
  9         String n = "";
 10         m = input.nextLine().replace(" ", "");
 11         for(int i = 0;i < m.length();i++) {
 12             if(m.charAt(0) == '+'||m.charAt(0) == '0') {
 13                 System.out.println("Wrong Format");
 14                 return;
 15             }if(m.charAt(i) == '0') {
 16                     System.out.println("Wrong Format");
 17                     return;
 18             }
 19         }
 20         Polynomial polynomial = new Polynomial(m);
 21         if(!m.equals("")) {
 22             if(polynomial.isPolynomial()) {
 23                 n = m.replaceAll(" ","");
 24                 if(n.matches("^((([+-]?)([0-9]|([1-9][0-9]*))))+$")) {
 25                     System.out.println("0");
 26                 }else {
 27                     n = n.replace("^+","^");
 28                     n = n.replace("-","+-");
 29                     n = n.replace("^+-","^-");
 30                     Expression expression = new Expression(polynomial.getPolynomial(n));
 31                     System.out.println(expression.toString());
 32                 }
 33             }
 34         }
 35     }
 36 }
 37 class Term{
 38     Term(){
 39     }
 40     public Term getDerivative() {
 41         Term term = null;
 42         return term;
 43     }
 44 }
 45 class Expression{
 46     private LinkedList<Term> list=new LinkedList<>();
 47     Expression(){
 48     }
 49     public Expression(LinkedList<Term> list) {
 50         this.list = list;
 51     }
 52     public LinkedList<Term> getList() {
 53         return list;
 54     }
 55     public String toString() {
 56         String m = "";
 57         String n = "";
 58         for(int i = 0;i < list.size();i++) {
 59             m = m + list.get(i);
 60         }
 61         if(m.charAt(0) == '+') {
 62             for(int j = 1;j < m.length();j++) {
 63                 n = n + m.charAt(j);
 64             }
 65             return n;
 66         }else {
 67             return m;
 68         }
 69     }
 70 }
 71 class PowerFunctionTerm extends Term{
 72     BigInteger coeff;
 73     BigInteger index;
 74     PowerFunctionTerm(){
 75     }
 76     public PowerFunctionTerm(BigInteger coeff,BigInteger index) {
 77         this.coeff = coeff;
 78         this.index = index;
 79     }
 80     public BigInteger getCoeff() {
 81         return coeff;
 82     }
 83     public BigInteger getIndex() {
 84         return index;
 85     }
 86     public PowerFunctionTerm getDerivative() {
 87          BigInteger m,n;
 88          BigInteger x = new BigInteger("1");
 89          m = coeff.multiply(index);
 90          n = index.subtract(x);
 91          PowerFunctionTerm powerFunctionTerm = new PowerFunctionTerm(m,n);
 92          return powerFunctionTerm;
 93     }
 94     public String toString() {
 95         BigInteger m = new BigInteger("1");
 96         BigInteger n = new BigInteger("0");
 97         String str = "";
 98         if(getDerivative().getCoeff().compareTo(n) == 1) {
 99             if(getDerivative().getIndex().compareTo(n) == 0) {
100                 str = "+" + getDerivative().getCoeff();
101             }else if(getDerivative().getIndex().compareTo(m)==0) {
102                     str = "+" + getDerivative().getCoeff() + "*x";
103             }else {
104                 str = "+" + getDerivative().getCoeff() + "*x^" + getDerivative().getIndex();
105             }
106         }else {
107             if(getDerivative().getIndex().compareTo(n) == 0) {
108                 str = getDerivative().getCoeff() + str;
109             }else if(getDerivative().getIndex().compareTo(m) == 0) {
110                     str = getDerivative().getCoeff() + "*x";
111             }else {
112                 str = getDerivative().getCoeff() + "*x^" + getDerivative().getIndex();
113             }
114         }
115         return str;
116     }
117 }
118 class ConstantTerm extends Term{
119     BigInteger value;
120     ConstantTerm(){
121     }
122     public ConstantTerm(BigInteger value){
123         this.value = value;
124     }
125     public void setValue(BigInteger value) {
126         this.value = value;
127     }
128     public ConstantTerm getDerivative() {
129         ConstantTerm constantTerm=new ConstantTerm();
130         BigInteger x= new BigInteger("0");
131         constantTerm.setValue(x);
132         return constantTerm;
133     }
134     public String toString() {
135         return "";
136     }
137 }
138 class Polynomial{
139     String item;
140     Polynomial(){
141     }
142     public Polynomial(String item){
143         this.item = item;
144     }
145     public boolean isInteger(String str) {
146         if(str.matches("^(([+-]?)([0-9]|([1-9][0-9]*)))")) {
147             return true;
148         }else {
149             return false;
150         }
151     }
152     public LinkedList<Term> getPolynomial(String str){
153         LinkedList<Term> list = new LinkedList<Term>();
154         if(str.charAt(0) == '+') {
155             String strin = "";
156             for(int j = 1;j < str.length();j++) {
157                 strin = strin+str.charAt(j);
158             }
159             String[] ptr = strin.split("\\+");
160             for(int i = 0;i < ptr.length;i++) {
161                 if(isInteger(ptr[i])) {
162                     BigInteger big = new BigInteger(ptr[i],10);
163                     list.add(new ConstantTerm(big));
164                 }else {
165                     list.add(new PowerFunctionTerm(getCoeff(ptr[i]),getIndex(ptr[i])));
166                 }
167             }
168         }
169         return list;
170     }
171     public BigInteger getCoeff(String str) {
172         if(str.charAt(0) == 'x') {
173             BigInteger big = new BigInteger("1");
174             return big;
175         }else {
176             String x = "";
177             for(int i = 0;i < str.length();i++) {
178                 if(str.charAt(i) != '*'&&str.charAt(i) != 'x') {
179                     x = x + str.charAt(i);
180                 }else {
181                     break;
182                 }
183             }
184             BigInteger big1 = new BigInteger(x,10);
185             return big1;
186         }
187     }
188     public BigInteger getIndex(String  str) {
189         int i;
190         for(i = 0;i < str.length();i++) {
191             if( str.charAt(i) == 'x')
192                 break;
193         }
194         if(i == str.length()-1) {
195             BigInteger big = new BigInteger("1");
196             return big;
197         }else {
198             String x = "";
199             for(i = i + 2;i < str.length();i++) {
200                 x = x + str.charAt(i);
201             }
202             BigInteger big1 = new BigInteger(x,10);
203             return big1;
204         }
205     }
206     
207     public boolean isPolynomial() {
208         if(item.replace(" ", "").matches(polynomial())) {
209             return true;
210         }else {
211             return false;
212         }
213     }
214     public String polynomial() {
215         String str = "";
216         str = "^([+-]?([0-9]+)([\\*]?)x*(\\^[+-]?[0-9]+)?)+";
217         return str;
218     }
219 }

本题代码出现了很多错误(代码仅供参考),对于正则表达式、数组的掌握还不够,也不够了解继承。思路不清晰,不知道如何进行解题。

3.踩坑心得

源码提交过程中的问题:

1.对PTA的提交规则不了解,刚开始提交作业将package也提交了,导致程序编译错误;

2.对PTA的提交规则不了解,刚开始提交作业的时候没有把类名改为Main,导致程序编译错误;

3.多个类提交时,没有删除其他类的public,导致程序格式错误;

4.代码在编译器上运行无误,提交时答案错误,需要善于利用PTA的自定义测试;

5.一元多项式求导其中一个测试点能过,其他的的测试点过不去。

总结:由于这是完成作业后的总结blog,所以只有部分截图,望谅解。

前N天测试结果如下:

 

 定义日期类测试如下:

 

 一元多项式求导测试如下:

 

4.改进建议

对于题目而言感觉没什么问题,但是定义日期类我感觉有点不懂,为什么要使用mon_maxnum数组,因为年份分为闰年和平年,使用数组需要时刻对数组中2月份的值进行更改,感觉可以不使用数组,而是直接进行输出。

5.总结

本阶段三次题目集,让我对于Java编程有了一个简单的了解,能够自主编写简单的Java程序;对于类与对象也有了一个深刻的了解,懂得了如何调用类,调用不同类的方法;本阶段也让我对于类的封装性有了一个清晰的认识,将类中的属性设为私有,通过get和set对对象的属性值进行操作。在进行一元多项式求导的程序编写时,学会了如何分割字符串,通过spilt方法来对字符串进行分割,分割后存入字符数组。并且学会了replace方法,使用replace方法除去字符串中的空格。但是这阶段的学习,我对于正则表达式还是不够了解,对于ArrayList数组并不了解,不能熟练使用,知识点过于薄弱,之后的需要好好学习正则表达式和ArrayList数组。关于课程方面的建议,我觉得这种上课方式挺适合我的,例题解析进行知识点讲解,生动形象。这一小阶段的Java已经结束了,但是我许要学的还有很多,加油!

posted @ 2021-04-03 17:56  Imauselessp!  阅读(93)  评论(0)    收藏  举报