第二次博客作业
一、前言
题目的量还行,难度的话每次作业都有一道难的,其他题难度适中吧,以下是我对这三次作业的一些心得分享和体会,本人水平有限,希望大家多加指正我的错误与不足!
二、设计与分析
题目集4的7-2
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); int year,month,day,choice,a; choice=in.nextInt(); year=in.nextInt(); month=in.nextInt(); day=in.nextInt(); DateUtil d=new DateUtil(year,month,day); switch(choice) { case 1: { a=in.nextInt(); if(d.checkInputValidity()==true&&a>=0) { System.out.println(d.getNextNDays(a).showDate()); } else System.out.println("Wrong Format"); }break; case 2: { a=in.nextInt(); if(d.checkInputValidity()==true&&a>=0) { System.out.println(d.getPreviousNDays(a).showDate()); } else System.out.println("Wrong Format"); }break; case 3: { int y2=in.nextInt(); int m2=in.nextInt(); int d2=in.nextInt(); DateUtil dd=new DateUtil(y2,m2,d2); if(d.checkInputValidity()==true&&dd.checkInputValidity()==true) { System.out.println(d.getDaysofDates(dd)); } else System.out.println("Wrong Format"); }break; default:System.out.println("Wrong Format"); } } } class DateUtil { Day day; public DateUtil() { } public DateUtil(int d,int m,int y) { this.day=new Day(d,m,y); } public Day getDay() { return day; } public void setDay(Day d) { this.day=d; } public boolean checkInputValidity() { boolean checkInputValidity; checkInputValidity=(this.getDay().getMonth().getYear().validate()&&this.getDay().getMonth().validate()&&this.getDay().validate()); return checkInputValidity; } public boolean compareDates(DateUtil date) { boolean date1; date1=(date.getDay().getMonth().getYear().getValue()>=this.getDay().getMonth().getYear().getValue()||date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue()&&date.getDay().getMonth().getValue()>=this.getDay().getMonth().getValue()||date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue()&&date.getDay().getMonth().getValue()==this.getDay().getMonth().getValue()&&date.getDay().getValue()<this.getDay().getValue()); return date1; } public boolean equalTwoDates(DateUtil date) { boolean date2; date2=(date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue()&&date.getDay().getMonth().getValue()==this.getDay().getMonth().getValue()&&date.getDay().getValue()==this.getDay().getValue()); return date2; } public String showDate() { return this.getDay().getMonth().getYear().getValue()+"-"+this.getDay().getMonth().getValue()+"-"+this.getDay().getValue(); } public int syts(DateUtil d){ int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int b=0,i; for(i=d.getDay().getMonth().getValue()+1;i<=12;i++){ b=b+a[i]; } b=b+a[d.getDay().getMonth().getValue()]-d.getDay().getValue(); if(d.getDay().getMonth().getYear().isLeapYear()&&d.getDay().getMonth().getValue()<=2)//闰年 b++; return b; } public DateUtil getNextNDays(int n){ int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int y=0,m=0,d=0; int i,j; int b=syts(this); if(b>n){ y=this.getDay().getMonth().getYear().getValue(); if(this.getDay().getMonth().getYear().isLeapYear()){ a[2]=29; } int e=a[this.getDay().getMonth().getValue()]; e=e-this.getDay().getValue(); if(e>=n){ m=this.getDay().getMonth().getValue(); d=n+this.getDay().getValue(); } else{ n=n-e; m=this.getDay().getMonth().getValue()+1; i=m; while(n-a[i]>0&&i<=12){ n=n-a[i]; m++; i++; } d=n; } } else{ n=n-b; y=this.getDay().getMonth().getYear().getValue()+1; int c=365; if(new Year(y).isLeapYear()){ c++; } while(n-c>0){ n=n-c; y++; c=365; if(new Year(y).isLeapYear()) c++; } i=1; while(n-a[i]>0&&i<=12){ n=n-a[i]; i++; } m=i; d=n; } return new DateUtil(y, m, d); } public DateUtil getPreviousNDays(int n){ int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int y=0,m=0,d=0; int i,b; b=365-syts(this); if(this.getDay().getMonth().getYear().isLeapYear()){ b++; } if (b>n){ y=this.getDay().getMonth().getYear().getValue(); int e=this.getDay().getValue(); if(e>n){ m=this.getDay().getMonth().getValue(); d=e-n; } else{ n=n-e; m=this.getDay().getMonth().getValue()-1; i=m; while(n-a[i]>0&&i>=0){ n=n-a[i]; m--; i--; } d=a[i]-n; if(new Year(y).isLeapYear()&&m==2){ d++; } } } else{ n=n-b; y=this.getDay().getMonth().getYear().getValue()-1; int f=365; if(new Year(y).isLeapYear()){ f++; } while(n-f>0){ n=n-f; y--; f=365; if(new Year(y).isLeapYear()) f++; } i=12; while(n-a[i]>0&&i>=0){ n=n-a[i]; i--; } m=i; d=a[i]-n; if(new Year(f).isLeapYear()&&m==2){ d++; } } return new DateUtil(y, m, d); } public int getDaysofDates(DateUtil date) { DateUtil b1=this; DateUtil b2=date; if(this.equalTwoDates(date)) { return 0; } if(this.compareDates(date)==true) { b1=this; b2=date; } else { b1=date; b2=this; } int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int i,j,ts=0; for(i=b1.getDay().getMonth().getYear().getValue()+1;i<b2.getDay().getMonth().getYear().getValue();i++) { ts=ts+365; if(new Year(i).isLeapYear()) ts++; } if(b1.getDay().getMonth().getYear().getValue()==b2.getDay().getMonth().getYear().getValue()&&b1.getDay().getMonth().getValue()==b2.getDay().getMonth().getValue()) { ts=b2.getDay().getValue()-b1.getDay().getValue(); } else if(b1.getDay().getMonth().getYear().getValue()==b2.getDay().getMonth().getYear().getValue()&&b1.getDay().getMonth().getValue()!=b2.getDay().getMonth().getValue()) { if(b1.getDay().getMonth().getYear().isLeapYear()) a[2]=29; else a[2]=28; ts=ts+a[b1.getDay().getMonth().getValue()]-b1.getDay().getValue(); ts=ts+b2.getDay().getValue(); for(j=b1.getDay().getMonth().getValue()+1;j<=b2.getDay().getMonth().getValue()-1;j++) ts+=a[j]; } else if(b1.getDay().getMonth().getYear().getValue()!=b2.getDay().getMonth().getYear().getValue()){ ts=ts+b2.getDay().getValue(); if(b2.getDay().getMonth().getYear().isLeapYear()) a[2]=29; else a[2]=28; for(j=b2.getDay().getMonth().getValue()-1;j>0;j--) ts=ts+a[j]; if(b1.getDay().getMonth().getYear().isLeapYear()) a[2]=29; else a[2]=28; ts=ts+a[b1.getDay().getMonth().getValue()]-b1.getDay().getValue(); for(j=b1.getDay().getMonth().getValue()+1;j<=12;j++) ts=ts+a[j]; } return ts; } } class Year { int value; public Year() { } public Year(int value) { this.value=value; } public int getValue() { return value; } public void setValue(int value) { this.value=value; } public boolean isLeapYear() { boolean isLeapYear; isLeapYear=(value % 4 == 0 && value % 100 !=0 ||value % 400 == 0); return isLeapYear; } public boolean validate() { boolean validate; validate=(value>=1900&&value<=2050); return validate; } void yearIncrement() { value+=1; } void yearReduction() { value-=1; } } class Month { int value; Year year; public Month() { } public Month(int yearValue,int monthValue) { year=new Year(yearValue); value=monthValue; } public int getValue() { return value; } public void setValue(int value) { this.value=value; } public Year getYear() { return year; } public void setYear(Year year) { this.year=year; } void resetMin() { value=1; } void resetMax() { value=12; } public boolean validate() { boolean validate; validate=(value>=1&&value<=12); return validate; } void monthIncrement() { value+=1; } void monthReduction() { value-=1; } } class Day { int value; Month month; int []a= {31,28,31,30,31,30,31,31,30,31,30,31}; public Day() { } public Day(int yearValue,int monthValue,int dayValue) { month=new Month(yearValue,monthValue); value=dayValue; } public int getValue() { return value; } public void setValue(int value) { this.value=value; } public Month getMonth() { return month; } public void setMonth(Month value) { this.month=value; } void resetMin() { value=1; } void restMax() { value=a[month.getValue()-1]; } boolean validate() { boolean validate; if(this.getMonth().getYear().isLeapYear()) { a[1]=29; } validate=(value>=1&&value<=a[month.getValue()-1]); return validate; } void dayIncrement() { value+=1; } void dayReduction() { value-=1; } }
大体思路
1、先根据类图把四个类里面的内容都写好,包括方法的有参构造,无参构造,get,set方法,检验日期合法性等等
2、再去想如何求下n天,前n天和两个日期相差的天数
3、求两个日期相差天数时先要去求两个日期之间相差的年份,再去求小的那年总的天数减去已过完的天数,最后求大的那年已过完的天数,用定义的ts变量相加得到天数。
4、求下n天,先去求该年剩余的天数,再去判断下n天是否在该年,如果不是,判断下n天是否在该月,确定出月份后根据该月有几天就能判断出下n天的日期。
5、求前n天,方法和求n天一样,一个小小的区别就是要拿365减去该年剩余的天数,因为我们需要知道该年已过完的天数,其他方法同第4条。
容易出错的地方
1、用this的时候要想清楚后面跟的是什么东西,因为方法有点多,很容易混淆
2、求两个日期相差天数时,要先判断下是哪个日期在前,这就要回到主类去看,传进来的参数是谁,这道题date是后面输入的日期,this是前面输入的日期,很容易写反掉。
题目集5的7-5
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); int year,month,day,choice,a; choice=in.nextInt(); year=in.nextInt(); month=in.nextInt(); day=in.nextInt(); DateUtil d=new DateUtil(year,month,day); switch(choice) { case 1: { a=in.nextInt(); if(d.checkInputValidity()==true&&a>=0) { System.out.println(year+"-"+month+"-"+day+" next "+a+" days "+"is:"+d.getNextNDays(a).showDate()); } else System.out.println("Wrong Format"); }break; case 2: { a=in.nextInt(); if(d.checkInputValidity()==true&&a>=0) { System.out.println(year+"-"+month+"-"+day+" previous "+a+" days "+"is:"+d.getPreviousNDays(a).showDate()); } else System.out.println("Wrong Format"); }break; case 3: { int y2=in.nextInt(); int m2=in.nextInt(); int d2=in.nextInt(); DateUtil dd=new DateUtil(y2,m2,d2); if(d.checkInputValidity()==true&&dd.checkInputValidity()==true) { System.out.println("The days between "+year+"-"+month+"-"+day+" and "+y2+"-"+m2+"-"+d2+" are:"+d.getDaysofDates(dd)); } else System.out.println("Wrong Format"); }break; default:System.out.println("Wrong Format"); } } } class DateUtil { Day day; public DateUtil() { } public DateUtil(int d,int m,int y) { this.day=new Day(d,m,y); } public Day getDay() { return day; } public void setDay(Day d) { this.day=d; } public boolean checkInputValidity() { boolean checkInputValidity; checkInputValidity=(this.getDay().getMonth().getYear().validate()&&this.getDay().getMonth().validate()&&this.getDay().validate()); return checkInputValidity; } public boolean compareDates(DateUtil date) { boolean date1; date1=(date.getDay().getMonth().getYear().getValue()>=this.getDay().getMonth().getYear().getValue()||date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue()&&date.getDay().getMonth().getValue()>=this.getDay().getMonth().getValue()||date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue()&&date.getDay().getMonth().getValue()==this.getDay().getMonth().getValue()&&date.getDay().getValue()<this.getDay().getValue()); return date1; } public boolean equalTwoDates(DateUtil date) { boolean date2; date2=(date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue()&&date.getDay().getMonth().getValue()==this.getDay().getMonth().getValue()&&date.getDay().getValue()==this.getDay().getValue()); return date2; } public String showDate() { return this.getDay().getMonth().getYear().getValue()+"-"+this.getDay().getMonth().getValue()+"-"+this.getDay().getValue(); } public int syts(DateUtil d){ int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int b=0,i; for(i=d.getDay().getMonth().getValue()+1;i<=12;i++){ b=b+a[i]; } b=b+a[d.getDay().getMonth().getValue()]-d.getDay().getValue(); if(d.getDay().getMonth().getYear().isLeapYear()&&d.getDay().getMonth().getValue()<=2)//闰年 b++; return b; } public DateUtil getNextNDays(int n){ int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int y=0,m=0,d=0; int i,j; int b=syts(this); if(b>n){ y=this.getDay().getMonth().getYear().getValue(); if(this.getDay().getMonth().getYear().isLeapYear()){ a[2]=29; } int e=a[this.getDay().getMonth().getValue()]; e=e-this.getDay().getValue(); if(e>=n){ m=this.getDay().getMonth().getValue(); d=n+this.getDay().getValue(); } else{ n=n-e; m=this.getDay().getMonth().getValue()+1; i=m; while(n-a[i]>0&&i<=12){ n=n-a[i]; m++; i++; } d=n; } } else{ n=n-b; y=this.getDay().getMonth().getYear().getValue()+1; int c=365; if(new Year(y).isLeapYear()){ c++; } while(n-c>0){ n=n-c; y++; c=365; if(new Year(y).isLeapYear()) c++; } i=1; while(n-a[i]>0&&i<=12){ n=n-a[i]; i++; } m=i; d=n; } return new DateUtil(y, m, d); } public DateUtil getPreviousNDays(int n){ int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int y=0,m=0,d=0; int i,b; b=365-syts(this); if(this.getDay().getMonth().getYear().isLeapYear()){ b++; } if (b>n){ y=this.getDay().getMonth().getYear().getValue(); int e=this.getDay().getValue(); if(e>n){ m=this.getDay().getMonth().getValue(); d=e-n; } else{ n=n-e; m=this.getDay().getMonth().getValue()-1; i=m; while(n-a[i]>0&&i>=0){ n=n-a[i]; m--; i--; } d=a[i]-n; if(new Year(y).isLeapYear()&&m==2){ d++; } } } else{ n=n-b; y=this.getDay().getMonth().getYear().getValue()-1; int f=365; if(new Year(y).isLeapYear()){ f++; } while(n-f>0){ n=n-f; y--; f=365; if(new Year(y).isLeapYear()) f++; } i=12; while(n-a[i]>0&&i>=0){ n=n-a[i]; i--; } m=i; d=a[i]-n; if(new Year(f).isLeapYear()&&m==2){ d++; } } return new DateUtil(y, m, d); } public int getDaysofDates(DateUtil date) { DateUtil b1=this; DateUtil b2=date; if(this.equalTwoDates(date)) { return 0; } if(this.compareDates(date)==true) { b1=this; b2=date; } else { b1=date; b2=this; } int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int i,j,ts=0; for(i=b1.getDay().getMonth().getYear().getValue()+1;i<b2.getDay().getMonth().getYear().getValue();i++) { ts=ts+365; if(new Year(i).isLeapYear()) ts++; } if(b1.getDay().getMonth().getYear().getValue()==b2.getDay().getMonth().getYear().getValue()&&b1.getDay().getMonth().getValue()==b2.getDay().getMonth().getValue()) { ts=b2.getDay().getValue()-b1.getDay().getValue(); } else if(b1.getDay().getMonth().getYear().getValue()==b2.getDay().getMonth().getYear().getValue()&&b1.getDay().getMonth().getValue()!=b2.getDay().getMonth().getValue()) { if(b1.getDay().getMonth().getYear().isLeapYear()) a[2]=29; else a[2]=28; ts=ts+a[b1.getDay().getMonth().getValue()]-b1.getDay().getValue(); ts=ts+b2.getDay().getValue(); for(j=b1.getDay().getMonth().getValue()+1;j<=b2.getDay().getMonth().getValue()-1;j++) ts+=a[j]; } else if(b1.getDay().getMonth().getYear().getValue()!=b2.getDay().getMonth().getYear().getValue()){ ts=ts+b2.getDay().getValue(); if(b2.getDay().getMonth().getYear().isLeapYear()) a[2]=29; else a[2]=28; for(j=b2.getDay().getMonth().getValue()-1;j>0;j--) ts=ts+a[j]; if(b1.getDay().getMonth().getYear().isLeapYear()) a[2]=29; else a[2]=28; ts=ts+a[b1.getDay().getMonth().getValue()]-b1.getDay().getValue(); for(j=b1.getDay().getMonth().getValue()+1;j<=12;j++) ts=ts+a[j]; } return ts; } } class Year { int value; public Year() { } public Year(int value) { this.value=value; } public int getValue() { return value; } public void setValue(int value) { this.value=value; } public boolean isLeapYear() { boolean isLeapYear; isLeapYear=(value % 4 == 0 && value % 100 !=0 ||value % 400 == 0); return isLeapYear; } public boolean validate() { boolean validate; validate=(value>=1820&&value<=2020); return validate; } void yearIncrement() { value+=1; } void yearReduction() { value-=1; } } class Month { int value; Year year; public Month() { } public Month(int yearValue,int monthValue) { year=new Year(yearValue); value=monthValue; } public int getValue() { return value; } public void setValue(int value) { this.value=value; } public Year getYear() { return year; } public void setYear(Year year) { this.year=year; } void resetMin() { value=1; } void resetMax() { value=12; } public boolean validate() { boolean validate; validate=(value>=1&&value<=12); return validate; } void monthIncrement() { value+=1; } void monthReduction() { value-=1; } } class Day { int value; Month month; int []a= {31,28,31,30,31,30,31,31,30,31,30,31}; public Day() { } public Day(int yearValue,int monthValue,int dayValue) { month=new Month(yearValue,monthValue); value=dayValue; } public int getValue() { return value; } public void setValue(int value) { this.value=value; } public Month getMonth() { return month; } public void setMonth(Month value) { this.month=value; } void resetMin() { value=1; } void restMax() { value=a[month.getValue()-1]; } boolean validate() { boolean validate; if(this.getMonth().getYear().isLeapYear()) { a[1]=29; } validate=(value>=1&&value<=a[month.getValue()-1]); return validate; } void dayIncrement() { value+=1; } void dayReduction() { value-=1; } }
大体思路
1、我这道题因为算法的设计可以直接过测试点,所以只是改了输出,所以就不做过多赘述,具体思路上一题已经写了。
容易出错的地方
1、输入合法性的判断,年份的区间和上一题的不一样,当时提交的时候没注意到这个,所以错了。
题目集4的7-3
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); int func=in.nextInt(); switch(func) { case 1: { double r=in.nextDouble(); if(r<=0.0) { System.out.println("Wrong Format"); } else { Circle circle=new Circle(); circle.setRadius(r); System.out.println(String.format("Circle's area:%.2f",circle.getArea())); } }break; case 2: { double w=in.nextDouble(); double l=in.nextDouble(); if(w<=0.0||l<=0.0) { System.out.println("Wrong Format"); } else { Rectangle rectangle=new Rectangle(); rectangle.setLength(l); rectangle.setWidth(w); System.out.println(String.format("Rectangle's area:%.2f",rectangle.getArea())); } }break; case 3: { double r4=in.nextDouble(); if(r4<=0.0) { System.out.println("Wrong Format"); } else { Ball ball=new Ball(); ball.setRadius(r4); System.out.println(String.format("Ball's surface area:%.2f",ball.getArea())); System.out.println(String.format("Ball's volume:%.2f",ball.getVolume())); } }break; case 4: { double w4=in.nextDouble(); double l4=in.nextDouble(); double h4=in.nextDouble(); if(w4<=0.0||l4<=0.0||h4<=0.0) { System.out.println("Wrong Format"); } else { Box box=new Box(); box.setHeight(h4); box.setLength(l4); box.setWidth(w4); System.out.println(String.format("Box's surface area:%.2f",box.getArea())); System.out.println(String.format("Box's volume:%.2f",box.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 { public Circle() { System.out.println("Constructing Circle"); } private double radius; public double getRadius() { return radius; } public void setRadius(double radius) { this.radius=radius; } public double getArea() { return Math.PI*radius*radius; } } class Rectangle extends Shape { public Rectangle() { System.out.println("Constructing Rectangle"); } private double 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; } public double getArea() { return width*length; } } class Ball extends Circle { public Ball() { System.out.println("Constructing Ball"); } public double getArea() { return 4*super.getArea(); } public double getVolume() { double r2=getRadius(); return 4.0/3.0*super.getArea()*r2; } } class Box extends Rectangle { public Box() { System.out.println("Constructing Box"); } public double height; public double getHeight() { return height; } public void setHeight(double height) { this.height=height; } public double getArea() { double w2=getWidth(); double l2=getLength(); return 2*(l2*height+height*w2+w2*l2); } public double getVolume() { double w3=getWidth(); double l3=getLength(); return height*l3*w3; } }
大体思路
1、先看题目要求,这道题是需要用到继承的,Shape类为父类,用来求图形的面积,Circle类和Rectangle类继承Shape类。Ball类和Box类分别继承Circle类和Rectangle类。
2、将上述五个类创建完之后,根据题目要求,在主方法中实现4个功能,用一个switch语句再合适不过了。然后非法输入的话输出Wrong Format。
容易出错的地方
1、这道题的框架还是比较清晰的,容易出错的点个人觉得是继承这块,第一次做这方面的题,也是去查阅了资料后才懂这方面的知识,然后这题要用到方法的重写,听老师提到过方法的重载,然后我就去查了一下他们两个的区别,方法的重写是对父类的方法进行一下重写,使得它可以满足子类的需要,这道题中每个图形求面积的方法都是不一样的,有父类的好处就是我再去求别的图形的面积的时候只需要创建一个子类,再在主方法中多加一个case,不用去动别的代码,便于提出新的需求时的修改。
2、输出的时候要求保留两位小数,我用了String format 输出,这个我之前不知道,也是遇到了才去查资料学的。感觉这道题对小白不是很友好,当时很多知识还没学,看的时候还是很懵的,现在学了更深一层的东西看着就简单很多。
题目集6的7-5
import java.util.Scanner; import java.util.Arrays; //Shape类 abstract class Shape{ public abstract double Mj(); } //Circle类 class Circle extends Shape{ private double r; public Circle(double r){ super(); this.r=r; } //getter public double getR(){ return r; } //setter public void setR(double r){ this.r = r; } public double Mj(){//计算圆面积 return Math.PI*r*r; } } //Rectangle类 class Rectangle extends Shape{ private double a,b; public Rectangle(double a, double b){ super(); this.a=a; this.b=b; } //getter public double A(){ return a; } public double B(){ return b; } //setter public void setA(double a){ this.a=a; } public void setB(double b){ this.b=b; } public double Mj(){//计算矩形面积 return a*b; } } //Triangle类 class Triangle extends Shape{ private double a; private double b; private double c; public Triangle(double a, double b, double c){ this.a = a; this.b = b; this.c = c; } //getter public double A(){ return a; } public double B(){ return b; } public double C(){ return c; } //setter public void setA(double a){ this.a=a; } public void setB(double b){ this.b=b; } public void setC(double c){ this.c=c; } public double Mj(){//计算三角形面积 double s1,s2; s1=(a+b+c)/2; s2=Math.sqrt(s1*(s1-a)*(s1-b)*(s1-c)); return s2; } } //主类 public class Main{ public static void main(String[] args){ Scanner x = new Scanner(System.in); int q,w,e,gs=0,h;//个数 q=x.nextInt();w=x.nextInt();e=x.nextInt(); if(q<0||w<0||e<0){//如果不合法 System.out.println("Wrong Format"); System.exit(0); } h=q+w+e; double[] he=new double [q+w+e]; double zh=0; int i,flag=0; Shape []c=new Circle[q]; Shape []r=new Rectangle[w]; Shape []t=new Triangle[e]; for(i=0;i<q;i++){//三角形 double bj; bj=x.nextDouble();//输入半径 if(bj<=0){//判断是否合法 flag=1; } c[i]=new Circle(bj);//把半径传入Circle类 zh=zh+c[i].Mj();//计算总面积 he[gs]=c[i].Mj();//把面积传入数组he gs++; } for(i=0;i<w;i++){//矩形 double db; double cb; db=x.nextDouble();cb=x.nextDouble();//输入边长 if(db<0||cb<0){//判断是否合法 flag=1; } r[i]=new Rectangle(db,cb);//把边长传入Rectangle类 zh=zh+r[i].Mj();//计算总面积 he[gs]=r[i].Mj();//把面积传入数组he gs++; } for(i=0;i<e;i++){//三角形 double a; double b; double d; double[] three=new double[3]; three[0]=x.nextDouble();three[1]=x.nextDouble();three[2]=x.nextDouble();//输入边长 Arrays.sort(three);//三边从小到大排序 a=three[0];b=three[1];d=three[2]; if(a<0||b<0||d<0||a+b<=d){//判断是否合法 flag=1; } t[i]=new Triangle(a,b,d);//把边长传入Triangle类 zh=zh+t[i].Mj();//计算总面积 he[gs]=t[i].Mj();//把面积传入数组he gs++; } if(flag==1){//如果输入的不合法 System.out.println("Wrong Format"); System.exit(0); } System.out.println("Original area:"); for (i=0;i<h;i++){ System.out.printf("%.2f ",he[i]); } System.out.printf("\nSum of area:%.2f\n",zh); System.out.println("Sorted area:"); Arrays.sort(he); for(i=0;i<h;i++){ System.out.printf("%.2f ",he[i]); } System.out.printf("\nSum of area:%.2f",zh); } }
大体思路
1、深入理解继承与多态的原理及用法
2、ArrayList常用方法及和数组的关系
容易出错的地方
1、如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边关系),则输出Wrong Format
题目集6的7-6
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); double r=in.nextDouble(); double w=in.nextDouble(); double l=in.nextDouble(); if(r<=0||w<=0||l<=0) { System.out.println("Wrong Format"); } else { Circle circle=new Circle(); circle.setRadius(r); System.out.println(String.format("%.2f",circle.getArea())); Rectangle rectangle=new Rectangle(); rectangle.setLength(l); rectangle.setWidth(w); System.out.println(String.format("%.2f",rectangle.getArea())); } } } interface GetArea { public double getArea(); } class Circle implements GetArea { public Circle() { } private double radius; public double getRadius() { return radius; } public void setRadius(double radius) { this.radius=radius; } public double getArea() { return Math.PI*radius*radius; } } class Rectangle implements GetArea { public Rectangle() { } private double 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; } public double getArea() { return width*length; } }
大体思路
1、和题目集4(7-3)挺相似的,把父类改成接口,然后这题要求的是圆和矩形,更简单。
容易出错的地方
1、输出结果要求保留两位小数
正则表达式
1、 \d\w\s 匹配第一个字符为数字,第二个字符为字母或数字、或下划线或汉字,第三字符为空格的字符串 例如:11 ,2a , 1_
2、
3、\d* 匹配重复0次或多次数字 例如:可能为空 或 任意数字 (2,3。。。。)
4、\d+ 匹配重复1次或多次数字 例如:可能为1个或多个数字 1,23,234,2345,........
5、\d? 匹配重复次个或者一次数字 例如:可能为空或者任意的一个数字(1,2,。。。)
6、\d{8}匹配重复8次数字 例如:123456768
7、\d{4,}匹配重复至少4次数字 例如:1234,12345,124244,。。。。。
8、^\d{8,11}$ 匹配重复8-11次数字 例如:12345678,123456789,1234567890,12345678901
9、 需要匹配qq邮箱 \d{8,}+qq+\.+com 在这里的. 就需要加斜杠
10、\d{3}-\d{8}|\d{4}-\d{8} 可以匹配两种不同长度区号的固定电话
题目集5的7-4
import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); String a; StringBuilder ss=new StringBuilder(); Map<String, Integer> map=new HashMap<String, Integer>(); String []key= { "abstract","assert","boolean","break","byte","case","catch", "char","class","const","continue","default","do","double","else", "enum","extends","false","final","finally","float", "for","goto","if","implements","import","instanceof", "int","interface","long","native","new","null","package", "private","protected","public","return","short","static", "strictfp","super","switch","synchronized","this","throw", "throws","transient","true","try","void","volatile","while" }; int j=0; for(int i=0;;i++) { a=input.nextLine(); if(a.equals("exit")) break; if(a.matches("(.*)//(.*)")) {String b[]=a.split("//"); ss.append(b[0]+" "); //ss.append('\n'); } else {ss.append(a+" "); //ss.append('\n'); } } int count=0; String s=ss.toString(); // System.out.println(s); Pattern p=Pattern.compile("\"(.*?)\""); Matcher m=p.matcher(s); while(m.find()){ s=s.replace(m.group()," "); p=Pattern.compile("\"(.*?)\""); m=p.matcher(s); } p=Pattern.compile("/\\**(.*?)/"); m=p.matcher(s); while(m.find()){ s=s.replace(m.group()," "); // p=Pattern.compile("/\\*(.*?)\\*/"); m=p.matcher(s); } // System.out.println(s); if(s.isEmpty()) {System.out.println("Wrong Format"); System.exit(0); } s=s.replace("["," "); s=s.replace("]"," "); s=s.replace("-","a"); s=s.replace("*","a"); s=s.replace("/","a"); s=s.replace("+","a"); s=s.replace(">","a"); s=s.replace("=","a"); s=s.replace("!","a"); s=s.replace(":","a"); s=s.replace("\\","a"); s= s.replaceAll("[^a-zA-Z]", " "); String []s1=s.split("[ ' ']"); for(int i=0;i<s1.length;i++) {//System.out.println(s1[i]); for( j=0;j<key.length;j++) if(s1[i].equals(key[j])) { map.put(key[j], 0); } } for( int i = 0;i<s1.length;i++) { for( j=0;j<key.length;j++) if(s1[i].equals(key[j])) { count=map.get(key[j]); map.put(key[j], count+1); } } Set set=map.keySet(); Object[] arr=set.toArray(); Arrays.sort(arr); for(Object k:arr){ System.out.println(map.get(k)+"\t"+k); } } }
这道题我不太懂,以上代码是网上找的,就不发表意见了。
三、总结
这三次的作业我觉得自己的java水平又提升了一个台阶,会根据提供类图写出代码,构造出一个类,懂得了继承,接口,抽象类,方法的重写,正则表达式等等,这些都让我对java更掌握了。在写类图的时候对this的用法更了解了,通过this可以调用各个类里的变量,很方便。继承也是一个很重要的结构,如果用户提出新的需求时,更方便去改进,不需要改动父类,只要修改子类的东西就可以了。正则表达式这个的话有点复杂,我只是会用最基本的一些,掌握的不太好,还需要进一步学习吧。对这三次作业还算基本满意吧,每次作业都花了挺多时间去做的。希望在做下次作业时能做的更好一点,好好学习java挺有用的。

浙公网安备 33010602011771号