一、前言:
本次博客是主要针对在java学习第二阶段中的PTA作业的总结性博客,第二阶段的作业的难度与第一次阶段相比有所提高,对java的知识点的考察主要集中在正则表达式的运用、类的继承,以及多态等。
二、设计与分析:
重点对题目的提交源码进行分析,可参考SourceMonitor的生成报表内容以及PowerDesigner的相应类图,要有相应的解释和心得(做到有图有真相),本次Blog必须分析的内容如下:
①题目集4(7-2)、题目集5(7-4)两种日期类聚合设计的优劣比较
7-2
package MAIN42; import java.util.Scanner; class DateUtil{ static int year; static int month; static int day; int[] monthdays = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31}; //年月日的构造方法 DateUtil(){ //无参构造器 } DateUtil(int year,int month,int day){ this.year = year; this.month = month; this.day = day; } public static int getYear() { return year; } public static void setYear(int year) { DateUtil.year = year; } public static int getMonth() { return month; } public static void setMonth(int month) { DateUtil.month = month; } public static int getDay() { return day; } public static void setDay(int day) { DateUtil.day = day; } /** * 闰年判断 */ public static boolean isLeapYear(int year) { boolean isLeapYear = (year%4==0 && year%100 !=0)||year%400 == 0; int []monthdays = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31}; return isLeapYear; } /** * 日期输入合法性校验 */ public static boolean checkInputValidity(int year,int month,int day) { int []monthdays = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31}; if (isLeapYear(year)){ monthdays[2] = 29; } boolean checkInputValidity = (year>=1900&&year<=2050&&month>=1&&month<=12&&day>=1&&day<=monthdays[month]); return checkInputValidity; } /** * 日期输入合法性校验,重载! */ public static boolean checkInputValidity(int year,int month,int day,int y1,int m1,int d1) { int []monthdays = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31}; if (isLeapYear(year)){ monthdays[2] = 29; } if (isLeapYear(y1)){ monthdays[2] = 29; } boolean checkInputValidity = (year>=1900&&year<=2050&&month>=1&&month<=12&&day>=1&&day<=monthdays[month]&&y1>=1900&&y1<=2050&&m1>=1&&m1<=12&&d1>=1&&d1<=monthdays[month]); return checkInputValidity; } /** * 求下n天 */ public static void afterrDate(int year,int month,int day,int n) { int[] monthdays = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (isLeapYear(year)) { monthdays[2] = 29; } if (checkInputValidity(year, month, day)) { day = day + n; if (n == 0) { System.out.println(year + "-" + month + "-" + day); return; } if (n > 0) { while (day > monthdays[month]) { day = day - monthdays[month]; month++; if (month == 13) { month = month - 12; year++; } } } if (n < 0) { while (day <= 0) { month--; if (month == 0) { month = month + 12; year--; } day = monthdays[month] + day; } } System.out.printf("%d-%d-%d\n", year, month, day); } else { System.out.println("Wrong Format"); } } /** * /求前n天/ */ public static void beforDate(int year,int month,int day,int n) { int []monthdays = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31}; if (isLeapYear(year)){ monthdays[2] = 29; } if (checkInputValidity(year, month, day)){ day = day -n; if (n==0){ System.out.println(year+"-"+month+"-"+day); return; } if (n>0){ while (day<=0){ month --; if (month==0){ month = month + 12; year--; } day = monthdays[month] + day; } }if (n<0){ while (day>monthdays[month]){ day = day - monthdays[month]; month++; if (month==13){ month = month - 12; year++; } } } System.out.printf("%d-%d-%d\n",year,month,day); }else { System.out.println("Wrong Format"); } } /** *测试两个日期之间相差的天数 */ public static void diffValueDate(int year,int month,int day,int y1,int m1,int d1 ) { int diff=0; //日期差值 int []monthdays = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31}; if (isLeapYear(year)){ monthdays[2] = 29; } if (checkInputValidity(year, month, day, y1, m1, d1)){ if (year == y1 && month == m1 && day == d1) { System.out.println(diff); return; } if (year==y1){ if (month==m1){ if (day!=d1){ diff = Math.abs(day-d1); System.out.println(diff); } } else { diff = (Math.abs(m1-month)-1)*61+Math.abs(day-d1); System.out.println(diff); } } System.out.printf(""); }else { System.out.println("Wrong Format"); } } } //主类 public class MAIN { //主方法 public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); //选择方式 int year = scanner.nextInt(); //年 int month = scanner.nextInt(); //月 int day = scanner.nextInt();//日 int n; //前n天或者下n天 switch (choice){ case 1: //测试输入日期的下n天 n = scanner.nextInt(); DateUtil.afterrDate(year,month,day,n); break; case 2: //测试输入日期的前n天 n = scanner.nextInt(); DateUtil.beforDate(year,month,day,n); break; case 3: //测试两个日期之间相差的天数 //输入第二个年月日 int y1 = scanner.nextInt(); int m1 = scanner.nextInt(); int d1 = scanner.nextInt(); DateUtil.diffValueDate(year,month,day,y1,m1,d1); break; default: System.out.println("Wrong Format"); } } } 7-4
package MAIN42; import java.util.Scanner; class Day extends Year { private int yearValue; private int monthValue; private int dayValue; public Day() { //无参构造器 } public Day(int yearValue,int monthValue,int dayValue) { this.yearValue = yearValue; this.monthValue = monthValue; this.dayValue = dayValue; } public void setYear(int yearValue) { this.yearValue = yearValue; } public void setMonth(int monthValue) { this.monthValue = monthValue; } public void setDay(int dayValue) { this.dayValue = dayValue; } public int getYear() { return yearValue; } public int getMonth() { return monthValue; } public int getDay() { return dayValue; } } class Month { private int yearValue; private int monthValue; private int dayValue; public Month() { //默认构造方法 } public Month(int yearValue,int monthValue,int dayValue) { this.yearValue = yearValue; this.monthValue = monthValue; this.dayValue = dayValue; } public void setYear(int yearValue) { this.yearValue = yearValue; } public void setMonth(int monthValue) { this.monthValue = monthValue; } public void setDay(int dayValue) { this.dayValue = dayValue; } public int getYear() { return yearValue; } public int getMonth() { return monthValue; } public int getDay() { return dayValue; } } class Year { private int yearValue; private int monthValue; private int dayValue; public Year() { //默认构造方法 } public Year(int yearValue,int monthValue,int dayValue) { this.yearValue = yearValue; this.monthValue = monthValue; this.dayValue = dayValue; } public void setYear(int yearValue) { this.yearValue = yearValue; } public void setMonth(int monthValue) { this.monthValue = monthValue; } public void setDay(int dayValue) { this.dayValue = dayValue; } public int getYear() { return yearValue; } public int getMonth() { return monthValue; } public int getDay() { return dayValue; } } class DateUtil { private long year; private long month; private long day; public DateUtil() { //默认构造方法 } public DateUtil(long year, long month, long day) { //带参构造方法 this.year = year; this.month = month; this.day = day; } public void setYear(long year) { this.year = year; } public void setMonth(long month) { this.month = month; } public void setDay(long day) { this.day = day; } public long getYear() { return year; } public long getMonth() { return month; } public long getDay() { return day; } private final long[] mon_maxnum=new long[]{31,28,31,30,31,30,31,31,30,31,30,31}; public static boolean isLeapYear(long year) { return (year%4==0 && year%100 !=0)||year%400 == 0; } private long getDayOfMonth(long year, long month) { long days = mon_maxnum[(int) (month - 1)]; if (month == 2 && isLeapYear(year)) { days = 29; } return days; } public boolean checkInputValidity() { //检查日期是否合法 if(isLeapYear(year)) { mon_maxnum[1]=29; } else { mon_maxnum[1]=28; } if(year < 1820 || year > 2020 || month < 1 || month > 12 || mon_maxnum[(int) (month - 1)] < day || day < 1) { return false; } else { return true; } } public DateUtil getNextNDays(long n) { //下n天 long year = this.year; long month = this.month; long 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(long n) { //前n天 long year = this.year; long month = this.month; long 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) { //比较当前日期与date的大小 if (this.year > date.year) { return true; } else { if (this.year == date.year) { if (this.month > date.month) { return true; } else { 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 long getDaysofDates(DateUtil date)//求当前日期与date之间相差的天数 { DateUtil dateUtil1 = this; // 小 DateUtil dateUtil2 = date; // 大 if (this.compareDates(date)) { dateUtil1 = date; dateUtil2 = this; } long days; int leapYearNum = 0; for (long i = dateUtil1.getYear(); i < dateUtil2.getYear(); i++) { if (isLeapYear(i)) { leapYearNum++; } } days = 365 * (dateUtil2.getYear() - dateUtil1.getYear()) + leapYearNum; long d1 = mon[(int) (dateUtil1.getMonth() - 1)] + dateUtil1.getDay() + (dateUtil1.getMonth() > 2 && isLeapYear(dateUtil1.getYear()) ? 1 : 0); long d2 = mon[(int) (dateUtil2.getMonth() - 1)] + dateUtil2.getDay() + (dateUtil2.getMonth() > 2 && isLeapYear(dateUtil2.getYear()) ? 1 : 0); return days - d1 + d2; } public String showDate()//以“year-month-day”格式返回日期值 { String str=year + "-" + month + "-" + day; return str; } } public class MAIN{ public static void main(String[] args) { Scanner in = new Scanner(System.in); long year; long month; long day; long year2; long month2; long day2; int number; number = in.nextInt(); if(number == 0) { System.out.println("Wrong Format"); System.exit(0); } if (number == 1) { //下n天 long m; year = in.nextLong(); month = in.nextLong(); day = in.nextLong(); DateUtil date = new DateUtil(year, month, day); if (date.checkInputValidity()) { //检查日期是否合法 System.out.println("Wrong Format"); System.exit(0); } m = in.nextLong(); if (m < 0) { System.out.println("Wrong Format"); System.exit(0); } System.out.print(date.showDate()+" next "+m+" days is:"+date.getNextNDays(m).showDate()); date = null; System.exit(0); } else if (number == 2) { //前n天 long n ; year = in.nextLong(); month = in.nextLong(); day = in.nextLong(); DateUtil date = new DateUtil(year, month, day); if (date.checkInputValidity()) { //检查日期是否合法 System.out.println("Wrong Format"); System.exit(0); } n = in.nextLong(); if (n < 0) { System.out.println("Wrong Format"); System.exit(0); } System.out.print(date.showDate()+" previous "+n+" days is:"+date.getPreviousNDays(n).showDate()); date = null; System.exit(0); } else if (number == 3) { //两天之间相差的天数 year = in.nextLong(); month = in.nextLong(); day = in.nextLong(); year2 = in.nextLong(); month2 = in.nextLong(); day2 = in.nextLong(); DateUtil fromDate = new DateUtil(year, month, day); DateUtil toDate = new DateUtil(year2, month2, day2); if (fromDate.checkInputValidity() && toDate.checkInputValidity()) { //检查日期的合法性 System.out.println("The days between "+year+"-"+month+"-"+day+" and "+year2+"-"+month2+"-"+day2+" are:"+fromDate.getDaysofDates(toDate)); System.exit(0); } else { System.out.println("Wrong Format"); System.exit(0); } } else { System.out.println("Wrong Format"); System.exit(0); } } }
Metrics Details For File 'MAIN.java'
--------------------------------------------------------------------------------------------
Parameter Value
========= =====
Project Directory E:\IntelliJ_IDEA\arrary3\javapta作业\src\MAIN42\
Project Name 7-4分析结构
Checkpoint Name Baseline
File Name MAIN.java
Lines 384
Statements 215
Percent Branch Statements 14.9
Method Call Statements 53
Percent Lines with Comments 4.9
Classes and Interfaces 5
Methods per Class 8.00
Average Statements per Method 3.90
Line Number of Most Complex Method 286
Name of Most Complex Method MAIN.main()
Maximum Complexity 13
Line Number of Deepest Block 236
Maximum Block Depth 6
Average Block Depth 2.00
Average Complexity 1.95
--------------------------------------------------------------------------------------------
Most Complex Methods in 5 Class(es): Complexity, Statements, Max Depth, Calls
DateUtil.checkInputValidity() 10, 8, 3, 2
DateUtil.compareDates() 7, 9, 6, 0
DateUtil.DateUtil() 1, 2, 2, 0
DateUtil.DateUtil() 1, 0, 0, 0
DateUtil.getDay() 1, 1, 2, 0
DateUtil.getDayOfMonth() 3, 4, 3, 2
DateUtil.getMonth() 1, 1, 2, 0
DateUtil.getNextNDays() 4, 11, 5, 1
DateUtil.getPreviousNDays() 4, 11, 5, 1
DateUtil.getYear() 1, 1, 2, 0
DateUtil.isLeapYear() 3, 1, 2, 0
DateUtil.setDay() 1, 1, 2, 0
DateUtil.setMonth() 1, 1, 2, 0
DateUtil.setYear() 1, 1, 2, 0
Day.Day() 1, 3, 2, 0
Day.Day() 1, 0, 0, 0
Day.getDay() 1, 1, 2, 0
Day.getMonth() 1, 1, 2, 0
Day.getYear() 1, 1, 2, 0
Day.setDay() 1, 1, 2, 0
Day.setMonth() 1, 1, 2, 0
Day.setYear() 1, 1, 2, 0
MAIN.main() 13, 56, 4, 35
Month.getDay() 1, 1, 2, 0
Month.getMonth() 1, 1, 2, 0
Month.getYear() 1, 1, 2, 0
Month.Month() 1, 3, 2, 0
Month.Month() 1, 0, 0, 0
Month.setDay() 1, 1, 2, 0
Month.setMonth() 1, 1, 2, 0
Month.setYear() 1, 1, 2, 0
Year.getDay() 1, 1, 2, 0
Year.getMonth() 1, 1, 2, 0
Year.getYear() 1, 1, 2, 0
Year.setDay() 1, 1, 2, 0
Year.setMonth() 1, 1, 2, 0
Year.setYear() 1, 1, 2, 0
Year.Year() 1, 3, 2, 0
Year.Year() 1, 0, 0, 0
--------------------------------------------------------------------------------------------
Block Depth Statements
0 21
1 57
2 69
3 45
4 17
5 5
6 1
7 0
8 0
9+ 0
--------------------------------------------------------------------------------------------
②题目集4(7-3)、题目集6(7-5、7-6)三种渐进式图形继承设计的思路与技术运用(封装、继承、多态、接口等)
题目集4(7-3)
import java.util.Scanner; /** * 继承 */ public class Main4 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); switch (n){ case 1: double r = input.nextDouble(); if (r<=0){ System.out.println("Wrong Format"); }else { Circle a = new Circle(); a.setRadius(r); System.out.printf("Circle's area:%.2f",a.getArea()); } break; case 2: double W = input.nextDouble(); double L = input.nextDouble(); if (W<=0||L<=0){ System.out.println("Wrong Format"); }else { Rectangle b = new Rectangle(); b.setLength(L); b.setWidth(W); System.out.printf("Rectangle's area:%.2f",b.getArea()); } break; case 3: double R = input.nextDouble(); if (R<=0){ System.out.println("Wrong Format"); }else { Ball c = new Ball(); c.setRadius(R); System.out.printf("Ball's surface area:%.2f",c.getRadius()); System.out.println(); System.out.printf("Ball's volume:%.2f",c.getVolume()); } break; case 4: double C = input.nextDouble(); double K = input.nextDouble(); double G = input.nextDouble(); if (C<=0||K<=0||G<=0){ System.out.println("Wrong Format"); }else { BOx d = new BOx(); d.setLength(C); d.setHeight(G); d.setWidth(K); System.out.printf("Box's surface area:%.2f",d.getArea()); System.out.println(); System.out.printf("Box's volume:%.2f",d.getVolume()); } break; default: System.out.println("Wrong Format"); } } } //求图形面积的共有方法 class Shape{ public Shape(){ System.out.println("Constructing Shape"); } public double getArea(){ return 0.0; } } //圆类 class Circle extends Shape{ //继承Shape private double radius; public Circle(){ System.out.println("Constructing Circle"); } @Override public double getArea() { //重写getArea方法 return Math.PI*radius*radius; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } } //矩形类 class Rectangle extends Shape{ private double width; private double length; public Rectangle(){ System.out.println("Constructing Rectangle"); } @Override public double getArea() {//方法的重写 return width*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; } } // 球类 class Ball extends Circle{ //球继承圆 public Ball(){ System.out.println("Constructing Ball"); } @Override public double getRadius() {//重写父类方法 return 4*super.getArea(); } public double getVolume(){// 球体积 return super.getArea()*super.getRadius()*4/3.0; } } //立方体类 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; } @Override public double getArea() { return (super.getArea()+height*getWidth()+height*getLength())*2; } public double getVolume(){ return super.getArea()*height; } }
题目集6(7-5)
Metrics Details For File 'Main.java'
--------------------------------------------------------------------------------------------
Parameter Value
========= =====
Project Directory E:\IntelliJ_IDEA\arrary3\javapta作业\src\第六次作业\
Project Name
Checkpoint Name Baseline
File Name Main.java
Lines 183
Statements 105
Percent Branch Statements 8.6
Method Call Statements 25
Percent Lines with Comments 7.1
Classes and Interfaces 2
Methods per Class 9.50
Average Statements per Method 2.89
Line Number of Most Complex Method 7
Name of Most Complex Method Main.main()
Maximum Complexity 1
Line Number of Deepest Block 24
Maximum Block Depth 3
Average Block Depth 1.27
Average Complexity 1.00
--------------------------------------------------------------------------------------------
Most Complex Methods in 2 Class(es): Complexity, Statements, Max Depth, Calls
Main.main() 1, 6, 2, 5
Triangle.area() 1, 3, 2, 2
Triangle.getSide1() 1, 1, 2, 0
Triangle.getSide2() 1, 1, 2, 0
Triangle.getSide3() 1, 1, 2, 0
Triangle.setSide1() 1, 1, 2, 0
Triangle.setSide2() 1, 1, 2, 0
Triangle.setSide3() 1, 1, 2, 0
Triangle.Triangle() 1, 3, 2, 0
--------------------------------------------------------------------------------------------
Block Depth Statements
0 20
1 40
2 42
3 3
4 0
5 0
6 0
7 0
8 0
9+ 0
--------------------------------------------------------------------------------------------
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//y圆,矩形,三角形个数
int x = input.nextInt();
int y = input.nextInt();
int z = input.nextInt();
//校验输入创建数量合法性
if (x<0||y<0||z<0){
System.out.println("Wrong Format");
System.exit(0);
}
//根据要求创建图像数量
int flag = 1; //数据合法标志位,1合法;0非法;
double[] areaSum = new double[x+y+z];//所有面积仍在这个数组里面,便于排序
int n = 0; //数组个数
double totalArea = 0; //总面积
//圆
Shape[] C = new Circle[x];
for (int i=0;i<x;i++){ //圆
double R = input.nextDouble();
if (R<=0) {
flag = 0;
}
C[i] = new Circle(R);
totalArea += C[i].area();
areaSum[n] = C[i].area();
n++;
}
//矩形
Shape[] R = new Rectangle[y];
for (int i=0;i<y;i++){
double L = input.nextDouble();
double W = input.nextDouble();
if (L<=0||W<=0) {
flag = 0;
}
R[i] = new Rectangle(W,L);
totalArea += R[i].area();
areaSum[n] = R[i].area();
n++;
}
//三角形
Shape[] T = new Triangle[z];
for (int i=0;i<z;i++){
double S1 = input.nextDouble();
double S2 = input.nextDouble();
double S3 = input.nextDouble();
if (S1+S2<=S3||S1+S3<=S2||S2+S3<=S1||S2<=0||S1<=0||S3<=0){
flag = 0;
}
T[i] = new Triangle(S1,S2,S3);
totalArea += T[i].area();
areaSum[n] = T[i].area();
n++;
}
if (flag==0){
System.out.println("Wrong Format");
System.exit(0);
}
System.out.println("Original area:");
for (int i=0;i<(x+y+z);i++){
System.out.printf("%.2f ",areaSum[i]);
}
System.out.printf("\nSum of area:%.2f",totalArea);
System.out.println("\nSorted area:");
Arrays.sort(areaSum);
for (int i=0;i<(x+y+z);i++){
System.out.printf("%.2f ",areaSum[i]);
}
System.out.println();
System.out.printf("Sum of area:%.2f",totalArea);
}
}
//Shape 类
abstract class Shape{
public abstract double area();
}
//圆 类
class Circle extends Shape{
private double radius;
Circle(double radius){
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public double area() {
return Math.PI*radius*radius;
}
}
//矩形类
class Rectangle extends Shape{
private double width;
private double length;
Rectangle(double width,double length){
this.length = length;
this.width = width;
}
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;
}
@Override
public double area() {
return length*width;
}
}
//三角形类
class Triangle extends Shape{
private double side1;
private double side2;
private double side3;
Triangle(double side1,double side2,double side3){
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double getSide1() {
return side1;
}
public void setSide1(double side1) {
this.side1 = side1;
}
public double getSide2() {
return side2;
}
public void setSide2(double side2) {
this.side2 = side2;
}
public double getSide3() {
return side3;
}
public void setSide3(double side3) {
this.side3 = side3;
}
@Override
public double area() {
double p = 0;
p = (side3+side2+side1)/2;
return Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));
}
}
题目集6(7-6)
package 第六次作业; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); double r = input.nextDouble(); double y = input.nextDouble(); double z = input.nextDouble(); //校验输入合法性 if (r <= 0 || y <= 0 || z <= 0) { System.out.println("Wrong Format"); System.exit(0); } GetArea C = new Circle(r); GetArea R = new Rectangle(y,z); double circleArea = C.area(); double rectangleArea = R.area(); System.out.printf("%.2f",circleArea); System.out.println(); System.out.printf("%.2f",rectangleArea); } } //GetArea 类 interface GetArea{ public double area(); } //圆 类 class Circle implements GetArea{ private double radius; Circle(double radius){ this.radius = radius; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } @Override public double area() { return Math.PI*radius*radius; } } //矩形类 class Rectangle implements GetArea{ private double width; private double length; Rectangle(double width,double length){ this.length = length; this.width = width; } 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; } @Override public double area() { return length*width; } }
Metrics Details For File 'Main.java'
--------------------------------------------------------------------------------------------
Parameter Value
========= =====
Project Directory E:\IntelliJ_IDEA\arrary3\javapta作业\src\第六次作业\
Project Name 测试67-6
Checkpoint Name Baseline
File Name Main.java
Lines 82
Statements 43
Percent Branch Statements 0.0
Method Call Statements 11
Percent Lines with Comments 4.9
Classes and Interfaces 1
Methods per Class 11.00
Average Statements per Method 2.73
Line Number of Most Complex Method 6
Name of Most Complex Method Main.main()
Maximum Complexity 1
Line Number of Deepest Block 7
Maximum Block Depth 2
Average Block Depth 0.72
Average Complexity 1.00
--------------------------------------------------------------------------------------------
Most Complex Methods in 1 Class(es): Complexity, Statements, Max Depth, Calls
Main.main() 1, 6, 2, 5
--------------------------------------------------------------------------------------------
Block Depth Statements
0 18
1 19
2 6
3 0
4 0
5 0
6 0
7 0
8 0
9+ 0
--------------------------------------------------------------------------------------------
③对三次题目集中用到的正则表达式技术的分析总结
详解Pattern类和Matcher类
java正则表达式通过java.util.regex包下的Pattern类与Matcher类实现(建议在阅读本文时,打开java API文档,当介绍到哪个方法时,查看java API中的方法说明,效果会更佳).
Pattern类用于创建一个正则表达式,也可以说创建一个匹配模式,它的构造方法是私有的,不可以直接创建,但可以通过Pattern.complie(String regex)简单工厂方法创建一个正则表达式,
Pattern p=Pattern.compile("\\w+");
p.pattern();//返回 \w+
pattern() 返回正则表达式的字符串形式,其实就是返回Pattern.complile(String regex)的regex参数
1.Pattern.split(CharSequence input)
Pattern有一个split(CharSequence input)方法,用于分隔字符串,并返回一个String[],我猜String.split(String regex)就是通过Pattern.split(CharSequence input)来实现的.
Pattern p=Pattern.compile("\\d+");
String[] str=p.split("我的QQ是:456456我的电话是:0532214我的邮箱是:aaa@aaa.com");
结果:str[0]="我的QQ是:" str[1]="我的电话是:" str[2]="我的邮箱是:aaa@aaa.com"
2.Pattern.matcher(String regex,CharSequence input)是一个静态方法,用于快速匹配字符串,该方法适合用于只匹配一次,且匹配全部字符串.
Pattern.matches("\\d+","2223");//返回true
Pattern.matches("\\d+","2223aa");//返回false,需要匹配到所有字符串才能返回true,这里aa不能匹配到
Pattern.matches("\\d+","22bb23");//返回false,需要匹配到所有字符串才能返回true,这里bb不能匹配到
3.Pattern.matcher(CharSequence input)
说了这么多,终于轮到Matcher类登场了,Pattern.matcher(CharSequence input)返回一个Matcher对象.
Matcher类的构造方法也是私有的,不能随意创建,只能通过Pattern.matcher(CharSequence input)方法得到该类的实例.
Pattern类只能做一些简单的匹配操作,要想得到更强更便捷的正则匹配操作,那就需要将Pattern与Matcher一起合作.Matcher类提供了对正则表达式的分组支持,以及对正则表达式的多次匹配支持.
Pattern p=Pattern.compile("\\d+");
Matcher m=p.matcher("22bb23");
m.pattern();//返回p 也就是返回该Matcher对象是由哪个Pattern对象的创建的
4.Matcher.matches()/ Matcher.lookingAt()/ Matcher.find()
Matcher类提供三个匹配操作方法,三个方法均返回boolean类型,当匹配到时返回true,没匹配到则返回false
matches()对整个字符串进行匹配,只有整个字符串都匹配了才返回true
Pattern p=Pattern.compile("\\d+");
Matcher m=p.matcher("22bb23");
m.matches();//返回false,因为bb不能被\d+匹配,导致整个字符串匹配未成功.
Matcher m2=p.matcher("2223");
m2.matches();//返回true,因为\d+匹配到了整个字符串
我们现在回头看一下Pattern.matcher(String regex,CharSequence input),它与下面这段代码等价
Pattern.compile(regex).matcher(input).matches()
lookingAt()对前面的字符串进行匹配,只有匹配到的字符串在最前面才返回true
④题目集5(7-4)中Java集合框架应用的分析总结
写好方法类,只需要在main中进行创建对象然后引用
package MAIN42; import java.util.Arrays; public class Main1 { public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); int size = input.nextInt(); int[] list = new int[size]; for (int i = 0; i < list.length; i++) { list[i] = input.nextInt(); } int choice = input.nextInt(); switch(choice){ case 1:bubbleSort(list);break; case 2:insertionSort(list);break; case 3:selectionSort(list);break; default: break; } } //Insert your code /** * m冒泡排序 */ public static void bubbleSort(int[] list){ //交换媒介 int temp; //定义一个布尔类型变量,标记数组是否已达到有序状态 boolean flage = true; for (int i=0;i<list.length;i++){ for (int j=0;j<list.length-i-1;j++){ if(list[j]>list[j+1]){ temp = list[j]; list[j] = list[j+1]; list[j+1] = temp; flage = false;//本趟发生了交换,表明该数组在本趟处于无序状态,需要继续排序。 } //System.out.println("第"+i+"趟,第"+j+"次排序"+Arrays.toString(v)); } //算法优化 if(flage){ //System.out.println("排序完成!"); break; }else { flage = true; } } for (int k=0;k<list.length;k++){ System.out.print(list[k]+" "); } } //插入排序 public static void insertionSort(int[] list){ int i,j,temp; for(i=0;i<list.length;i++){ j=i; while(j!=0&&list[j]<list[j-1]){ temp=list[j]; list[j]=list[j-1]; list[j-1]=temp; j--; } } for(i=0;i<list.length;i++){//循环输出 System.out.print(list[i]+" "); } } //选择排序 public static void selectionSort(int[] list){ int i,j,temp,min; for(i=0;i<list.length-1;i++){ min=i; for(j=i;j<list.length;j++){//循环找出最小数的位置 if(list[j]<list[min]){ min=j; } } //最小数与原数组该位置的数进行交换 temp=list[min]; list[min]=list[i]; list[i]=temp; } for(i=0;i<list.length;i++){//循环输出 System.out.print(list[i]+" "); } } }
三、采坑心得:对源码的提交过程中出现的问题及心得进行总结,务必做到详实,拿数据、源码及测试结果说话,切忌假大空
1.面向对象编程不能注重于按步骤一步一步解决问题,而应该注重于将问题中的部分分成各个对象,各司其职,谁做了什么事,谁又做了什么事,他们共同作用的结果解决了一件事。
可以说,面向对象编程更接近现实世界。程序的对象就像现实世界中的个体,他们通过发送消息告知彼此所要做的(方法的调用)。OO设计的主要任务是将设计尽量和现实世界的事物一致化。尽量使程序设计的思路符合现实世界事物的规律。类和对象是OO编程中最重要最基本的两个元素,在设计时,先将找到要处理的各个对象,再找到各个对象的共同的祖先,逐层抽象。最后确立各个类之间的继承关系。
2.编写类时需要充分利用类的单一职责的原则,通过类图编写类的属性和方法的作用域,类与类之间的关系,也要注意类的继承关系,关注子类对父类方法的重写,子类对父类属性的使用。
四、改进建议:对相应题目的编码改进给出自己的见解,做到可持续改进
在冒泡排序算法中,我觉得可以加上算法的优化,
下面是我自己写的优化后的冒泡排序,大家可以互相学习一下,
1 package com.itbaizhan; 2 3 import java.util.Arrays; 4 5 /** 6 * m冒泡排序 7 */ 8 public class Test07BubbleSort { 9 public static void main(String[] args) { 10 int[] v ={3,1,6,2,9,0,7,4,8,5}; 11 BubbleSort(v); 12 } 13 14 public static void BubbleSort(int[] v){ 15 16 //交换媒介 17 int temp; 18 19 //定义一个布尔类型变量,标记数组是否已达到有序状态 20 boolean flag = true; 21 22 for (int i=0;i<v.length;i++){ 23 for (int j=0;j<v.length-i-1;j++){ 24 if(v[j]>v[j+1]){ 25 temp = v[j]; 26 v[j] = v[j+1]; 27 v[j+1] = temp; 28 29 flag = false;//本趟发生了交换,表明该数组在本趟处于无序状态,需要继续排序。 30 } 31 System.out.println("第"+i+"趟,第"+j+"次排序"+Arrays.toString(v)); 32 } 33 34 //算法优化 35 if(flag){ 36 System.out.println("排序完成!"); 37 break; 38 }else { 39 flag = true; 40 } 41 42 } 43 System.out.println(Arrays.toString(v)); 44 45 } 46 }
五、总结:对本阶段三次题目集的综合性总结,学到了什么,哪些地方需要进一步学习及研究,对教师、课程、作业、实验、课上及课下组织方式等方面的改进建议及意见。
面向对象设计的基本原理理解
1、单一功能原则:认为对象应该仅具有一种单一功能的概念
2、开闭原则:认为软件体应该是对于扩展开放的,但是对于修改封闭的概念
3、里氏替换原则:认为“程序中的对象应该是可以在不改变程序正确性的前提下被它的子类所替换的”概念
4、接口隔离原则:认为“多个特定客户端接口要好于一个宽泛用途的接口”
5、依赖反转原则:认为一个方法应该遵从“依赖于抽象而不是一个实例”的概念,依赖注入所以该原则的一种实现方式
类只能继承extends一个类,但可以实现implements多个接口,子类需要实现抽象类或接口中的所有不完整的方法,也就是说,多个子类继承于一个父类或实现一个接口,这些子类中会有相同的方法,而子类和父类可以在一定条件下相互转化,每个子类都是父类的实例,若为一个父对象赋值不同的子对象,则父对象可使用不同子对象的相同方法,即父对象就可以根据当前赋值给它的子对象的特性以不同的方式运作,也就是面向对象的多态性。
对课程的建议
1.每次作业截止之后可以出一下不计分的补题,可以让没有及时解决问题的同学继续尝试。