经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
OO第一次博客作业
来源:cnblogs  作者:羁绊少年  时间:2021/4/6 10:20:32  对本文有异议

目录

1. 前言

2.代码分析

3.三次作业遇到的bug及采坑心得

4.改进建议

5.总结

一、前言

这学期开始接触java,面向对象以前也只是在c++学过一点,所以一开始对于写这次blug有点手足无措,不过还好学会了SourceMonitor软件的使用,对于我分析代码提供了很大的帮助。

下面是我对三次题目集的大体分析:

题目集1

题量:8道

知识点:java的简单运用:顺序结构、选择结构、循环结构,数组的建立等

难度:只要有别的语言基础如c、c++等,难度偏易

题目集2

题量:5道

知识点:字符串的运用,java中方法的引用

难度:一般

题目集3

题量:3道

知识点:对象和类,java中类的封装性,正则表达式的运用

难度:前两题难度一般,最后一题较难

二、代码分析

由于学校要求,本次代码分析就分析题目集中的重点题目即题目集1的7-8,题目集2的7-4、7-5以及题目集3的7-2、7-3。

题目集1

7-8

 该题代码如下:

  1. import java.util.*;
  2. import java.lang.*;
  3. public class Main{
  4. public static void main(String[] args){
  5. Scanner in = new Scanner(System.in);
  6. double [] arr = new double[3];
  7. for(int i=0;i<arr.length;i++){
  8. arr[i]=in.nextDouble();
  9. }
  10. Arrays.sort(arr,0,3);
  11. if(arr[0]<1||arr[0]>200||arr[1]<1||arr[1]>200||arr[2]<1||arr[2]>200)
  12. {
  13. System.out.println("Wrong Format");
  14. }
  15. else
  16. {
  17. if((arr[0]+arr[1])>arr[2])
  18. {
  19. if(Math.abs(arr[2]*arr[2]-arr[0]*arr[0]-arr[1]*arr[1])<0.1)
  20. {
  21. if((arr[0]==arr[1]&&arr[0]!=arr[2])||(arr[0]==arr[2]&&arr[1]!=arr[2])||(arr[1]==arr[2]&&arr[0]!=arr[2]))
  22. {
  23. System.out.println("Isosceles right-angled triangle");
  24. }
  25. else
  26. {
  27. System.out.println("Right-angled triangle");
  28. }
  29. }
  30. else if(arr[0]==arr[1]&&arr[1]==arr[2]&&arr[0]==arr[2])
  31. {
  32. System.out.println("Equilateral triangle");
  33. }
  34. else if((arr[0]==arr[1]&&arr[0]!=arr[2])||(arr[0]==arr[2]&&arr[0]!=arr[1])||(arr[1]==arr[2]&&arr[0]!=arr[2]))
  35. {
  36. System.out.println("Isosceles triangle");
  37. }
  38. else
  39. {
  40. System.out.println("General triangle");
  41. }
  42. }
  43. else
  44. {
  45. System.out.println("Not a triangle");
  46. }
  47. }
  48. }
  49. }

度量:

 

分析:这第一次作业比较简单,只要用if条件句找到就输出就基本可以拿到分了,但经过分析发现块的深度有点高,下次一定得改正。

题目集2

这次的作业因为要调用类中的方法所以整体难度比第一次作业要高,与c中函数的调用很像。

7-4

该题代码如下:

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

度量:

 

分析:这道题因为方法设置的有点多,所以导致方法平均语句有点高甚至超过了题目设置的代码长度限制,后来改了下代码风格就没问题了。

7-5

该题代码如下:

  1. import java.util.Scanner;
  2. public class Main {
  3. public static boolean isLeapYear(int year) {
  4. if(year%4==0&&year%100!=0)
  5. return true;
  6. else if(year%400==0&&year%100==0)
  7. return true;
  8. else
  9. return false;
  10. }
  11. public static boolean checkInputValidity(int year,int month,int day,int n) {
  12. if(year>=1820&&year<=2020&&month<=12&&month>=1&&day>=1&&day<=31&&n<=10&&n>=-10)
  13. {
  14. if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
  15. {
  16. if(day<=31&&day>=1)
  17. {
  18. return true;
  19. }
  20. else
  21. {
  22. return false;
  23. }
  24. }
  25. else if(month==4||month==6||month==9||month==11)
  26. {
  27. if(day<=30&&day>=1)
  28. {
  29. return true;
  30. }
  31. else
  32. {
  33. return false;
  34. }
  35. }
  36. else if(month==2)
  37. {
  38. if(isLeapYear(year))
  39. {
  40. if(day<=29&&day>1)
  41. {
  42. return true;
  43. }
  44. else
  45. {
  46. return false;
  47. }
  48. }
  49. else
  50. {
  51. if(day<=28&&day>=1)
  52. {
  53. return true;
  54. }
  55. else
  56. {
  57. return false;
  58. }
  59. }
  60. }
  61. }
  62. return false;
  63. }
  64. public static void lastDate(int year,int month,int day,int n) {
  65. if(n>0)
  66. {
  67. if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
  68. {
  69. if(day-n>=1)
  70. {
  71. day=day-n;
  72. System.out.println(n+" days ago is:"+year+"-"+month+"-"+day);
  73. }
  74. else
  75. {
  76. if(month==3)
  77. {
  78. if(isLeapYear(year))
  79. {
  80. month--;
  81. day=29-Math.abs(day-n);
  82. System.out.println(n+" days ago is:"+year+"-"+month+"-"+day);
  83. }
  84. else
  85. {
  86. month--;
  87. day=28-Math.abs(day-n);
  88. System.out.println(n+" days ago is:"+year+"-"+month+"-"+day);
  89. }
  90. }
  91. else if(month==1)
  92. {
  93. year--;
  94. month=12;
  95. day=31-Math.abs(day-n);
  96. System.out.println(n+" days ago is:"+year+"-"+month+"-"+day);
  97. }
  98. else if(month==8)
  99. {
  100. month--;
  101. day=31-Math.abs(day-n);
  102. System.out.println(n+" days ago is:"+year+"-"+month+"-"+day);
  103. }
  104. else
  105. {
  106. month--;
  107. day=30-Math.abs(day-n);
  108. System.out.println(n+" days ago is:"+year+"-"+month+"-"+day);
  109. }
  110. }
  111. }
  112. else if(month==4||month==6||month==9||month==11||month==2)
  113. {
  114. if(day-n>=1)
  115. {
  116. day=day-n;
  117. System.out.println(n+" days ago is:"+year+"-"+month+"-"+day);
  118. }
  119. else
  120. {
  121. month--;
  122. day=31-Math.abs(day-n);
  123. System.out.println(n+" days ago is:"+year+"-"+month+"-"+day);
  124. }
  125. }
  126. }
  127. else if(n<0)
  128. {
  129. if(month==1||month==3||month==5||month==7||month==8||month==10)
  130. {
  131. if(day-n<=31)
  132. {
  133. day=day-n;
  134. System.out.println(n+" days ago is:"+year+"-"+month+"-"+day);
  135. }
  136. else
  137. {
  138. month++;
  139. day=day-n-31;
  140. System.out.println(n+" days ago is:"+year+"-"+month+"-"+day);
  141. }
  142. }
  143. else if(month==12)
  144. {
  145. if(day-n<=31)
  146. {
  147. day=day-n;
  148. System.out.println(n+" days ago is:"+year+"-"+month+"-"+day);
  149. }
  150. else
  151. {
  152. year++;
  153. month=1;
  154. day=day-n-31;
  155. System.out.println(n+" days ago is:"+year+"-"+month+"-"+day);
  156. }
  157. }
  158. else if(month==2)
  159. {
  160. if(isLeapYear(year))
  161. {
  162. if(day-n<=29)
  163. {
  164. day=day-n;
  165. System.out.println(n+" days ago is:"+year+"-"+month+"-"+day);
  166. }
  167. else
  168. {
  169. day=day-n-29;
  170. month++;
  171. System.out.println(n+" days ago is:"+year+"-"+month+"-"+day);
  172. }
  173. }
  174. else
  175. {
  176. if(day-n<=28)
  177. {
  178. day=day-n;
  179. System.out.println(n+" days ago is:"+year+"-"+month+"-"+day);
  180. }
  181. else
  182. {
  183. day=day-n-28;
  184. month++;
  185. System.out.println(n+" days ago is:"+year+"-"+month+"-"+day);
  186. }
  187. }
  188. }
  189. else if(month==4||month==6||month==9||month==11)
  190. {
  191. if(day-n<=30)
  192. {
  193. day=day-n;
  194. System.out.println(n+" days ago is:"+year+"-"+month+"-"+day);
  195. }
  196. else
  197. {
  198. month++;
  199. day=day-n-30;
  200. System.out.println(n+" days ago is:"+year+"-"+month+"-"+day);
  201. }
  202. }
  203. }
  204. else if(n==0)
  205. {
  206. System.out.println(n+" days ago is:"+year+"-"+month+"-"+day);
  207. }
  208. }
  209. public static void main(String[] args) {
  210. Scanner in = new Scanner(System.in);
  211. int year =in.nextInt();
  212. int month=in.nextInt();
  213. int day=in.nextInt();
  214. int n=in.nextInt();
  215. if(checkInputValidity(year,month,day,n))
  216. {
  217. lastDate(year,month,day,n);
  218. }
  219. else
  220. {
  221. System.out.println("Wrong Format");
  222. }
  223. }
  224. }

度量:

 

 分析:这道题有部分方法与7-4题中方法一样,但lastDate方法因为判断情况过多所以较长,导致代码块平均深度较大。

题目集3

第三次作业前两道题比第两次作业中的题多了对于数据域封装性的运用和对与外部类的调用还有对于封装的理解,并没有别的对于我来说而第三次作业的最后一题不仅要运用类的封装性更要会使用正则表达式,正确写出可以符合条件的字符串。

7-2

该题代码如下:

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

度量:

 

分析: 这道题外部类中的方法与第二次作业中的方法很接近,但由于数据域的封装性使得类中块的深度特别高因为要调用私有属性的数据,但总的来说没有太大的难度。

7-3

这道题我一直没有完全过,只能达到部分正确,因为对于用正则表达式表示求导过程一直不知道应该怎么处理。

该题代码如下:

  1. import java.util.*;
  2. class regex{
  3. private String str;
  4. regex(String str) {
  5. this.str=str;
  6. }
  7. boolean panduan(String str) {
  8. if(str.matches("^(([-+]([1-9][0-9]*)(\\\\*x(\\\\^[+-]?([1-9][0-9]*))?))|(([1-9][0-9]*)\\\\*(x(\\\\^[+-]?([1-9][0-9]*))?))|([-+](x(\\\\^[+-]?([1-9][0-9]*))?))|([-+]([1-9][0-9]*))|(([1-9][0-9]*))|((x(\\\\^[+-]?([1-9][0-9]*))?)))+$"))
  9. {
  10. return true;
  11. }
  12. else
  13. {
  14. return false;
  15. }
  16. }
  17. void derivation(String str) {
  18. for(int i=0;i<str.length();i++) {
  19. if(str.matches("^([-+]?([1-9][0-9]*))+$"))
  20. {
  21. System.out.println("0");
  22. }
  23. }
  24. }
  25. }
  26. public class Main {
  27. public static void main(String[] args) {
  28. Scanner sc =new Scanner(System.in);
  29. String str=sc.nextLine();
  30. regex p =new regex(str);
  31. if(p.panduan(str))
  32. {
  33. p.derivation(str);
  34. }
  35. else
  36. {
  37. System.out.println("Wrong Format");
  38. }
  39. }
  40. }

度量:

 

分析: 这道题运用正则表达式来判断输入的数据是否正确是一个符合条件的一元多项式。

而正则表达式我是这样运用的:

  • 匹配首项系数为1、指数可能存在;x(\\^[+-]?([1-9][0-9]*))?)
  • 匹配首项带正系数且不为1、指数可能存在;([1-9][0-9]*)(\\*x(\\^[+-]?([1-9][0-9]*))?) 
  • 匹配首项或非首项、系数不为1、指数可能存在;[-+]([1-9][0-9]*)(\\*x(\\^[+-]?([1-9][0-9]*))?)
  • 匹配首项或非首项、系数为1、指数可能存在;[-+](x(\\^[+-]?([1-9][0-9]*))?)
  • 匹配首项或非首项常数;[-+]([1-9][0-9]*)
  • 匹配首项或非首项常数;[-+]([1-9][0-9]*)
  • 在表达式开始加^且末尾加$表示多次重复出现

将一元多项式分为了6种情况,这样就可以判断其输入多项式是否符合条件了。

但多项式求导只知道若输入常量可变为0,其余的就不知如何运用正则表达式去写。

三、三次作业遇到的bug及采坑心得

第一次作业

自己写的时候并没有遇到什么bug,写7-7题时一开始用的是冒泡排序给数组里的数排序后来发现直接用sort函数更加省时省力,而在帮别人看过代码发现有的同学在7-8题中的判断直角三角形中遇到bug,代码并没有正确判断出这个三角形是直角三角形,因为数据输入的都是实数型,程序在运行时会出现小数点后面多个数无法正确判断,这时可以运用数据类型的强制转换变成整数型或者将数据相减小于1e-6就可以正确判断出直角三角形。

第二次作业

7-1题写时不知道如何将二进制数转换成十进制数,后来用了Interage类可以将其直接转换,比很多用数学方法转换省事了许多,再用sunstring函数可以直接截取字符串中一段然后直接将其转换。

7-2题和7-3题写时自己没有遇到什么bug,都是直接就过了。

7-4题刚写完提交时是部分错误,因为闰年最后一天这一点没有完全过,后来发现是因为平年有365天而闰年有366天,这会导致从闰年变成平年时会有代码变化所以后来加上了这一情况,代码就过了。

7-5题跟前两题中的方法有相同的地方,但在判断是前几天时要将n分情况进行判断。

7-3题、7-4题、7-5题中闰年的判断方法:

  1. public static boolean isLeapYear(int year) {
  2. if(year%4==0&&year%100!=0)
  3. return true;
  4. else if(year%400==0&&year%100==0)
  5. return true;
  6. else
  7. return false;
  8. }

第三次作业

7-1题自己写时没有遇到bug,但有同学问我localDate是什么类型数据,我输出日期用的是localDate.of函数,这样输出日期直接可以过。

7-2题自己写时没有遇到bug,整体思路跟第二次作业的后面几题很像。

7-3题自己遇到了很多问题,但现在有些问题都没有解决,刚开始写这题时还去自学了一下正则表达式,知道了如何用正则表达式表示一元多项式才开始写代码,但当时不知道如何用正则表达式写出一元多显示的求导过程,现在有些了解就是用正则表达式写出一元多项式求导后的式子并进行匹配,输出即可。

四、改进建议

题目集1

7-8题代码可以将if条件句用缩进变短,并将三角形类型可以整合在一起。

题目集2

7-4题和7-5题都有代码长度过长这一毛病,事实上可以将代码风格改变一下,并将类中的方法进行一些优化,可以适当减小其代码的时间复杂度。

题目集3

7-2题可以将外部类调用到主类中变为内部类,这样可以直接使用,而并不需要再声明类了。

7-3题正则表达式还可以进行替换,可以匹配到符合正则表达式的输入数据时直接将其替换成输出所要的已经求过导的一元多项式;输入一元多项式的数据存储可以改为用数组数列即ArraysList,更加方便数据的录用以及调用。

五、总结

这三次作业我收获颇多,学到了java语法的基本运用,以及加深了对于许多函数,包的理解比如Interage类,sort类,Random类等等,还有java中类的封装性,数据域的封装性,最重要的是学会了如何使用正则表达式,这实在不失为一个检查字符串格式的一把利器,同时在对于OO的学习中也改变了我学代码不喜欢换行加空格的风格。对于日后的学习我会继续加深对于面向对象这一概念更深的理解,还有java中的继承和多态以及对于正则表达式更多的运用使其真正的成为自己的“武器”。最后也希望在学习OO的过程中能在课堂上学习到更多书上本没有的但对我编代码十分有用的知识,写到更加具有代表性的题型,扩展自己的视野。

原文链接:http://www.cnblogs.com/djr-blog/p/14616966.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号