Blog作业02
目录
前言
设计与分析
踩坑心得
改进建议
总结
前言
这三次作业的题目数量虽然增多,但是在题量加大的同时,这三次作业集的难度也相应的下去了,难度降低的同时也保证了作业集题目的质量。这三次的作业的知识面比前两次覆盖的更加广泛。
第四次作业集的第一题应用了正则表达式,第二题就考察了类和构造方法的使用,第三题则考查了继承,父类子类的知识。
第五次作业集的前三个题目,都是对字符串相关知识的考查,第四题则可以使用接口,也可以用字符串的知识解决,最后一题则复习了基础和类还有构造方法。
第六次作业的前四个题目,都是考查正则表达式,,前四个题目较为简单,但是后面两个题目就比较难一些,第五题的图形继承是及考察了父类子类,也考察了使用的规则。第六题则是对新学知识点的考查,就是插口的知识点。
题目集4
题量:3道
难度:较难
7-2:参考题目7-2的要求,设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1900,2050] ,month∈[1,12] ,day∈[1,31] , 设计类图如下:
应用程序共测试三个功能:
- 求下n天
- 求前n天
- 求两个日期相差的天数
7-3:
编写程序,实现图形类的继承,并定义相应类对象并进行测试。
- 类Shape,无属性,有一个返回0.0的求图形面积的公有方法
public double getArea();//求图形面积 - 类Circle,继承自Shape,有一个私有实型的属性radius(半径),重写父类继承来的求面积方法,求圆的面积
- 类Rectangle,继承自Shape,有两个私有实型属性width和length,重写父类继承来的求面积方法,求矩形的面积
- 类Ball,继承自Circle,其属性从父类继承,重写父类求面积方法,求球表面积,此外,定义一求球体积的方法
public double getVolume();//求球体积 - 类Box,继承自Rectangle,除从父类继承的属性外,再定义一个属性height,重写父类继承来的求面积方法,求立方体表面积,此外,定义一求立方体体积的方法
public double getVolume();//求立方体体积 - 注意:
- 每个类均有构造方法,且构造方法内必须输出如下内容:
Constructing 类名 - 每个类属性均为私有,且必须有getter和setter方法(可用Eclipse自动生成)
- 输出的数值均保留两位小数
题目集5
题量:5道
难度:适中
7-4:编写程序统计一个输入的Java源码中关键字(区分大小写)出现的次数。说明如下:
- Java中共有53个关键字(自行百度)
- 从键盘输入一段源码,统计这段源码中出现的关键字的数量
- 注释中出现的关键字不用统计
- 字符串中出现的关键字不用统计
- 统计出的关键字及数量按照关键字升序进行排序输出
- 未输入源码则认为输入非法
7-5:参考题目7-2的要求,设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1900,2050] ,month∈[1,12] ,day∈[1,31] , 设计类图如下:
应用程序共测试三个功能:
- 求下n天
- 求前n天
- 求两个日期相差的天数
题目集6
题量:6道
难度:适中
7-5:
- 如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边关系),则输出
Wrong Format。 - 如果输入合法,则正常输出,输出内容如下(输出格式见输入输出示例):
- 各个图形的面积;
- 所有图形的面积总和;
- 排序后的各个图形面积;
- 再次所有图形的面积总和。
7-6:
- GetArea为一个接口,无属性,只有一个GetArea(求面积)的抽象方法;
- Circle及Rectangle分别为圆类及矩形类,分别实现GetArea接口
- 要求:在Main类的主方法中分别定义一个圆类对象及矩形类对象(其属性值由键盘输入),使用接口的引用分别调用圆类对象及矩形类对象的求面积的方法,直接输出两个图形的面积值。(要求只保留两位小数)
设计与分析




import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int year = 0; int month = 0; int day = 0; int choice = input.nextInt(); if (choice == 1) { // test getNextNDays method int m = 0; year = Integer.parseInt(input.next()); month = Integer.parseInt(input.next()); day = Integer.parseInt(input.next()); DateUtil date = new DateUtil(year, month, day); if (!date.checkInputValidity()) { System.out.println("Wrong Format"); System.exit(0); } m = input.nextInt(); if (m < 0) { System.out.println("Wrong Format"); System.exit(0); } System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:"); System.out.println(date.getNextNDays(m).showDate()); } else if (choice == 2) { // test getPreviousNDays method int n = 0; year = Integer.parseInt(input.next()); month = Integer.parseInt(input.next()); day = Integer.parseInt(input.next()); DateUtil date = new DateUtil(year, month, day); if (!date.checkInputValidity()) { System.out.println("Wrong Format"); System.exit(0); } n = input.nextInt(); if (n < 0) { System.out.println("Wrong Format"); System.exit(0); } System.out.print( date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:"); System.out.println(date.getPreviousNDays(n).showDate()); } else if (choice == 3) { //test getDaysofDates method year = Integer.parseInt(input.next()); month = Integer.parseInt(input.next()); day = Integer.parseInt(input.next()); int anotherYear = Integer.parseInt(input.next()); int anotherMonth = Integer.parseInt(input.next()); int anotherDay = Integer.parseInt(input.next()); DateUtil fromDate = new DateUtil(year, month, day); DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay); if (fromDate.checkInputValidity() && toDate.checkInputValidity()) { System.out.println("The days between " + fromDate.showDate() + " and " + toDate.showDate() + " are:" + fromDate.getDaysofDates(toDate)); } else { System.out.println("Wrong Format"); System.exit(0); } } else{ System.out.println("Wrong Format"); System.exit(0); } } } class DateUtil { private int year; private int month; private int day; public DateUtil(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } public DateUtil(){} public void setYear(int year) { this.year = year; } public void setMonth(int month) { this.month = month; } public void setDay(int day) { this.day = day; } public int getYear() { return year; } public int getMonth() { return month; } public int getDay() { return day; } private final int[] DAY_OF_MONTH = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; private int getDayOfMonth(int year, int month) { int days = DAY_OF_MONTH[month - 1]; if (month == 2 && isLeapYear(year)) { days = 29; } return days; } public boolean checkInputValidity() { if (year < 1820 || year > 2020) return false; if (month < 1 || month > 12) return false; // int _day = this.getDayOfMonth(year, month); // return day <= _day; return day >= 1 && day <= 31; } public boolean isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; } public DateUtil getNextNDays(int n) { int year = this.year; int month = this.month; int day = this.day; for (int i = 0; i < n; i++) { day++; if (day > getDayOfMonth(year, month)) { day = 1; month++; if (month > 12) { month = 1; year++; } } } return new DateUtil(year, month, day); } public DateUtil getPreviousNDays(int n)//取得year-month-day的前n天日期 { int year = this.year; int month = this.month; int day = this.day; for (int i = 0; i < n; i++) { day--; while (day < 1) { month--; if (month < 1) { month = 12; year--; } day += getDayOfMonth(year, month); } } return new DateUtil(year, month, day); } public boolean compareDates(DateUtil date) { if (this.year > date.year) return true; if (this.year == date.year) { if (this.month > date.month) return true; if (this.month == date.month) { if (this.day >= date.day) return true; } } return false; } public boolean equalTwoDates(DateUtil date) { if (date != null) { if (year == date.year && month == date.month && day == date.day) { return true; } } return false; } private static final int[] mon = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; public int getDaysofDates(DateUtil date) { DateUtil dateUtil1 = this; // 小 DateUtil dateUtil2 = date; // 大 if (this.compareDates(date)) { dateUtil1 = date; dateUtil2 = this; } int days; int leapYearNum = 0; for (int i = dateUtil1.getYear(); i < dateUtil2.getYear(); i++) { if (isLeapYear(i)) { leapYearNum++; } } days = 365 * (dateUtil2.getYear() - dateUtil1.getYear()) + leapYearNum; int d1 = mon[dateUtil1.getMonth() - 1] + dateUtil1.getDay() + (dateUtil1.getMonth() > 2 && isLeapYear(dateUtil1.getYear()) ? 1 : 0); int d2 = mon[dateUtil2.getMonth() - 1] + dateUtil2.getDay() + (dateUtil2.getMonth() > 2 && isLeapYear(dateUtil2.getYear()) ? 1 : 0); return days - d1 + d2; } public String showDate() { return year + "-" + month + "-" + day; } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int x = in.nextInt(); switch(x) { case 1: double radius = in.nextDouble(); if (radius <= 0){ System.out.println("Wrong Format"); System.exit(0); } Circle c = new Circle(); c.setRadius(radius); System.out.printf("Circle's area:%.2f", c.getArea()); break; case 2: double width= in.nextDouble(); double length= in.nextDouble(); if (width <= 0||length<=0){ System.out.println("Wrong Format"); System.exit(0); } Rectangle rectangle = new Rectangle(); rectangle.setwidth(width); rectangle.setlength(length); System.out.printf("Rectangle's area:%.2f", rectangle.getArea()); break; case 3 :{ double radius1 = in.nextDouble(); if (radius1 <= 0){ System.out.println("Wrong Format"); System.exit(0); } Ball ball = new Ball(); ball.setRadius(radius1); if (radius1 <= 0){ System.out.println("Wrong Format"); System.exit(0); } System.out.printf("Ball's surface area:%.2f\n", ball.getArea()); System.out.printf("Ball's volume:%.2f\n", ball.getVolume()); break; } case 4 :{ double length1 = in.nextDouble(); double width1 = in.nextDouble(); double height = in.nextDouble(); if (length1 <= 0 || width1 <= 0 || height <= 0){ System.out.println("Wrong Format"); System.exit(0); } Box box = new Box(); box.setlength(length1); box.setwidth(width1); box.setHeight(height); System.out.printf("Box's surface area:%.2f\n", box.getArea()); System.out.printf("Box's volume:%.2f\n", box.getVolume()); break; } default :{ System.out.println("Wrong Format"); System.exit(0); } } } } class Shape{ public double getArea(){ return 0.0; } public Shape(){ System.out.println("Constructing Shape"); } } class Circle extends Shape{ private double radius; public Circle(){ System.out.println("Constructing Circle"); } public double getRadius(){ return radius; } public void setRadius(double radius){ this.radius = radius; } public double getArea(){ return radius*radius* Math.PI; } } class Rectangle extends Shape{ private double width; private double length; public Rectangle(){ System.out.println("Constructing Rectangle"); } public double getwidth(){ return width; } public void setwidth(double width){ this.width = width; } public double getlength(){ return length; } public void setlength(double length){ this.length=length; } public double getArea(){ return width*length; } } class Ball extends Circle{ public Ball(){ System.out.println("Constructing Ball"); } public double getArea(){ return 4 * super.getRadius() * super.getRadius() * Math.PI; } public double getVolume(){ return 4.0 / 3.0 * Math.PI * super.getRadius()* super.getRadius()* super.getRadius(); } } class Box extends Rectangle{ private double height; public Box(){ System.out.println("Constructing Box"); } public double getHeight(){ return height; } public void setHeight(double height){ this.height = height; } public double getArea(){ return super.getArea()*2 + height * super.getwidth()*2 + height * super.getlength() * 2; } public double getVolume(){ return height * super.getArea(); } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a = in.nextInt(); int b = in.nextInt(); int x = in.nextInt(); if (a <= 0||b<=0||x<=0){ System.out.println("Wrong Format"); System.exit(0); } int i; double[] radius = new double[a]; double[] sr = new double[a+b+x]; for(i=0;i<a;i++) { radius[i] = in.nextDouble(); if (radius[i] <= 0){ System.out.println("Wrong Format"); System.exit(0); } } for(i=0;i<a;i++) { Circle ci = new Circle(); ci.setRadius(radius[i]); sr[i]= ci.getArea(); } double width[]= new double[b]; double length[]= new double[b]; for(i=0;i<b;i++) { width[i] = in.nextDouble(); length[i] = in.nextDouble(); if (width[i] <= 0||length[i]<=0){ System.out.println("Wrong Format"); System.exit(0); } } for(i=0;i<b;i++) { Rectangle rectanglei = new Rectangle(); rectanglei.setwidth(width[i]); rectanglei.setlength(length[i]); sr[i+a]=rectanglei.getArea(); } double s1[]= new double[x]; double s2[]= new double[x]; double s3[]= new double[x]; double st[]= new double[x]; for(i=0;i<x;i++) { s1[i] = in.nextDouble(); s2[i] = in.nextDouble(); s3[i] = in.nextDouble(); if (s1[i]<= 0 || s2[i] <= 0 || s3[i] <= 0){ System.out.println("Wrong Format"); System.exit(0); if (s1[i]+s2[i]<s3[i]||s1[i]+s3[i]<s2[i]||s3[i]+s2[i]<s1[i]){ System.out.println("Wrong Format"); System.exit(0);} } } for(i=0;i<x;i++) { Triangle trianglei = new Triangle(); trianglei.setS1(s1[i]); trianglei.setS2(s2[i]); trianglei.setS3(s3[i]); sr[i+a+b]=trianglei.getArea(); } System.out.println("Original area:"); for(i=0;i<x+a+b;i++) { System.out.printf("%.2f",sr[i]); System.out.printf(" "); } System.out.println(" "); System.out.printf("Sum of area:"); double sum=0; for(i=0;i<x+a+b;i++) { sum+=sr[i]; } System.out.printf("%.2f",sum); System.out.printf("\n"); int j; for( i=0;i<sr.length-1;i++) { for( j=0;j<sr.length-1-i;j++) { if(sr[j]>sr[j+1]) { double temp=sr[j]; sr[j]=sr[j+1]; sr[j+1]=temp; } } } System.out.println("Sorted area:"); for(i=0;i< sr.length;i++) { System.out.printf("%.2f",sr[i]); System.out.printf(" "); } System.out.println(" "); System.out.printf("Sum of area:"); System.out.printf("%.2f",sum); } } class Shape{ public double getArea(){ return 0.0; } } class Circle extends Shape{ private double radius; Circle(){ } public double getRadius(){ return radius; } public void setRadius(double radius){ this.radius = radius; } public double getArea(){ return radius*radius* Math.PI; } } class Rectangle extends Shape{ private double width; private double length; public double getwidth(){ return width; } public void setwidth(double width){ this.width = width; } public double getlength(){ return length; } public void setlength(double length){ this.length=length; } public double getArea(){ return width*length; } } class Triangle extends Shape{ private double s1; private double s2; private double s3; public double getS1(){ return s1; } public void setS1(double s1){ this.s1 = s1; } public double getS2(){ return s2; } public void setS2(double s2){ this.s2 = s2; } public double getS3(){ return s3; } public void setS3(double s3){ this.s3 = s3; } public double getArea(){ double p=(s1+s2+s3)/2; return Math.sqrt(p*(p-s1)*(p-s2)*(p-s3)); } }
踩坑心得
作业集4
7-2:最开始没有看准题目,就直接嘎嘎乱写一通,把类图里面的东西全都创建了一遍,最后发现好多东西并没有用到,就全都删了,真的浪费了很多时间。
然后这个悲惨的故事告诉我们,看到题别冲动,要冷静思考后再下手。以免浪费时间,白白耗费自己的精力。
7-3:这个是我做的第一个继承的题目,其实上课讲了好多那些函数还有用法,我在上课时就只是听听,并没有把他们记下来,后面只能通过上网自己查资料来写来看,好在最后写对了。
作业集5
7-4:这个首先是想用字符串和array list的方法去写,但是最后输出的顺序不是按照题目要求的那顺序输出的,最后还是同学提供了一个那个关键字的模板,因为那个模板是从小到大排序的,
所以这个输出就没有问题。
作业集6
7-5:这个在我感觉应该是最好的一个对类的考查的题目,不仅考查了一点点的继承,还有很多小的知识点,这个题我花的时间不少,最后一想,应该创建一个对象数组,这样会更简单
改进建议
pta的作业的每一题希望可以多弄些样例,就是输出的例子,可以让做题者更好的理解题的意思,能更快的,更精准的做题这三次作业的知识跨度比较大,如果能在题目上说明本题所用的知识点会更好
另外可以支持多种方法解决问题,不单单局限于一种方法
我自己的改进方法如下:
对齐下括号.
良好的编码格式和命名。
从语法和算法上精简代码,永远永远不要把同一个变量用于多个不同的目的。
使用自描述的变量名和方法名
方法名应该小写字母开头,其后用字母大写的单词连接(veryLongVariableName)
类名应该都使用首字母大写的单词连接而成常量名应该全部大写,
用下划线连接左大括号应该跟 if 语句在同一行
分清楚类名和构造方法名,不会混
总结
第4单元作业花费时间:花费时间较长
第5单元作业花费时间:花费时间适中
最后6部分作业:花费时间适中。
通过这三次作业,我还是收获了很多知识的。
第4次作业:每个题目的点都不一样,第一题还是正则表达式,第二题是类和构造方法,第三题是继承。
第5次作业:学会了字符串String的使用方法和基本操作
第6次作业:学习了解了接口,和深度的继承知识,对象数组,类的创建和声明。
每次作业花费的时间较长,需要查阅大量的资料才能解决问题,作业的成功率不高,得到的分数不高。解决方法:遇到不懂的知识点上网查阅资料,请教他人,多看书记住一些基本的知识点。
作为Java的思维来说,
当你拿到一个问题时,你分析这个问题不再是第一步先做什么,
第二步再做什么,这是面向过程的思维,你应该分析这个问题里面有哪些类和对象,这是第一点,然后再分析这些类和对象应该具有哪些属性和方法。这是第二点。最后分析类和类之间具体有什么关系,
这是第三点。这些都是面向过程思想所不具备的,在解决一些问题的时候,运用面向对象的思想来编写程序解决问题才是最好的解决方式
作业量可以适当增多,多多给我们练习的机会,适当放慢一些课堂进度,给我们足够的时间去看书,去思考,实验课可以改成三周俩节,适当要宽恕留给我们写报告的时间。
对教师的意见:多多培养我们的动手能力,在课堂上,给我们多一些时间去写自己的代码,老师能多现场展示一些功能,我觉得效果比较好。
浙公网安备 33010602011771号