题目集4~6的总结性Blog
题目集4~6的总结性Blog
前言:
题目集4主要考察了java的聚合,继承,和字符串的知识点,题量为7-1最大,7-2与7-3次之,题目难度也是从7-1到7-3逐级递减。
题目集5主要考察了字符串和数组以及聚合的知识点,题量为7-5最大,其余的次之,题目难度也是7-5最大,然后其他的略简单一些。
题目集6主要且集中地考察了正则表达式的知识点,还考察了继承和多态以及接口的知识点,题量情况为7-5.7-6题量最大,其余不如这两道题题量大,题目难度情况也是7-5.7-6难道最大,其余难道较小,只需掌握正则表达式的知识即可做题。
设计与分析:
题目集4,7--2题的分析:
类图:

代码以及分析:
① 题目集4(7-2)、题目集5(7-4)两种日期类聚合设计的优劣比较
1. 题目集4(7-2)
import java.util.Scanner;
class Year{
int value;
boolean checkleapyear=false;
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(int year){
if(year%4==0&&year%100!=0||year%400==0)
checkleapyear=true;
return true;
}
public void validate(){
if(value<1900||value>2050)
check=false;
}
public void yearIncrement(){
value++;
}
public void yearReduction(){
value--;
}
}
class Month{
int value;
Year year=new Year();
boolean check=true;
Month(){
}
Month(int yearValue,int monthValue){
this.value=monthValue;
this.year.value=yearValue;
}
public int getValue(){
return value;
}
public void setValue(int value){
this.value=value;
}
public Year getYear(){
return year.value;
}
public void setYear(int value){
year.value=value;
}
public void resetMin(){
value=1;
}
public void resetMax(){
value=12;
}
public void validate(){
if(value<1||value>12)
check=false;
}
public void monthIncrement(){
value++;
}
public void monthReduction(){
value--;
}
}
class Day{
int value;
Month month=new month();
Year year=new year();
int[] month_maxnum={31,28,31,30,31,30,31,31,30,31,30,31};
boolean check=true;
Day(){
}
Day(int yearValue,int monthValue,int dayValue){
value=dayValue;
month.value=monthValue;
year.value=yearValue;
}
public int getValue(){
return value;
}
public void setValue(int value){
this.value=value;
}
public Month getMonth(){
return month.value;
}
public void setMonth(int value){
month.value=value;
}
public void resetMin(){
value=1;
}
public void resetMax(){
if(checkleapyear==true&&month.value==2)
value=29;
else
value=month_maxnum[month.value];
}
public void validate(){
if(value<1||checkleapyear==false&&value>month_maxnum[month.value]||(checkleapyear==true&&month.value==2&&value>29)||(checkleapyear==true&&month.value!=2&&value>month_maxnum[month.value]))
check=false;
}
public void dayIncrement(){
value++;
}
public void dayReduction(){
value--;
}
}
class DateUtil{
Day day=new Day();
Month month=new month();
Year year=new year();
boolean check=true;
DateUtil(){
}
DateUtil(int d,int m,int y){
day.value=d;
month.value=m;
year.value=y;
}
public Day getDay(){
return day.value;
}
public void setDay(Day d){
day.value=d;
}
public void checkInputValidity()
{
if(year.check==false||month.check==false||day.check==false)
check=false;
}
public boolean compareDates(DateUtil date){
if (this.year > date.year||(this.year == date.year&&this.month > date.month)||(this.year == date.year&&this.month==date.month&&this.day >= date.day)) return true;
else
return false;
}
public boolean equalTwoDates(DateUtil date){
if (date != null) {
if (year == date.year && month == date.month && day == date.day) {
return true;
}
}
return false;
}
public String showDate()
{
return year.value + "-" + month.value + "-" + day.value;
}
public DateUtil getNextNDays(int n)
{
for (int i = 0; i < n; i++) {
day.value++;
if (day.value > month_maxnum[month.value]) {
day = 1;
month++;
if (month > 12) {
month = 1;
year++;
}
}
}
return new DateUtil(year.value, month.value, day.value);
}
public DateUtil getPreviousNDays(int n)//取得year-month-day的前n天日期
{
for (int i = 0; i < n; i++) {
day.value--;
while (day.value < 1) {
month.value--;
if (month.value < 1) {
month.value = 12;
year.value--;
}
day.value = month_maxnum[month.value]
}
}
return new DateUtil(year.value, month.value, day.value);
}
public int getDaysofDates(DateUtil date)//求当前日期与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 class Main {
public static void main(String[] args) throws Exception{
Scanner input = new Scanner(System.in);
int day,month,year;
int choice = input.nextInt();
switch(choice){
case 1:
year=input.nextInt();
month=input.nextInt();
day=input.nextInt();
int m=input.nextInt();
DateUtil date = new DateUtil(year, month, day);
if (date.check=false||m<0) {
System.out.println("Wrong Format");
//System.exit(0);
}
System.out.println(date.getNextNDays(m).showDate());
case 2:
year=input.nextInt();
month=input.nextInt();
day=input.nextInt();
DateUtil date = new DateUtil(year, month, day);
int n=input.nextInt();
if (date.check=false||n<0) {
System.out.println("Wrong Format");
//System.exit(0);
}
System.out.println(date.getPreviousNDays(n).showDate());
case 3:
year1=input.nextInt();
month1=input.nextInt();
day1=input.nextInt();
year2=input.nextInt();
month2=input.nextInt();
day2=input.nextInt();
DateUtil date1 = new DateUtil(year1, month1, day1);
DateUtil date2 = new DateUtil(year2, month2, day2);
if (date1.check=false || date2.check=false)
System.out.println("Wrong Format");
else
System.out.println(date1.getDaysofDates(date2))
default:
System.out.println("Wrong Format");
}
}
}
2. 题目集5(7-4)
import java.util.Scanner;
class Year{
long int value;
boolean checkleapyear=false;
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(int year){
if(year%4==0&&year%100!=0||year%400==0)
checkleapyear=true;
return true;
}
public void validate(){
if(value<1900||value>2050)
check=false;
}
public void yearIncrement(){
value++;
}
public void yearReduction(){
value--;
}
}
class Month{
long int value;
Year year=new Year();
boolean check=true;
Month(){
}
Month(int yearValue,int monthValue){
this.value=monthValue;
this.year.value=yearValue;
}
public int getValue(){
return value;
}
public void setValue(int value){
this.value=value;
}
public Year getYear(){
return year.value;
}
public void setYear(int value){
year.value=value;
}
public void resetMin(){
value=1;
}
public void resetMax(){
value=12;
}
public void validate(){
if(value<1||value>12)
check=false;
}
public void monthIncrement(){
value++;
}
public void monthReduction(){
value--;
}
}
class Day{
long int value;
Month month=new month();
Year year=new year();
int[] month_maxnum={31,28,31,30,31,30,31,31,30,31,30,31};
boolean check=true;
Day(){
}
Day(int yearValue,int monthValue,int dayValue){
value=dayValue;
month.value=monthValue;
year.value=yearValue;
}
public int getValue(){
return value;
}
public void setValue(int value){
this.value=value;
}
public Month getMonth(){
return month.value;
}
public void setMonth(int value){
month.value=value;
}
public void resetMin(){
value=1;
}
public void resetMax(){
if(checkleapyear==true&&month.value==2)
value=29;
else
value=month_maxnum[month.value];
}
public void validate(){
if(value<1||checkleapyear==false&&value>month_maxnum[month.value]||(checkleapyear==true&&month.value==2&&value>29)||(checkleapyear==true&&month.value!=2&&value>month_maxnum[month.value]))
check=false;
}
public void dayIncrement(){
value++;
}
public void dayReduction(){
value--;
}
}
class DateUtil{
Day day=new Day();
Month month=new month();
Year year=new year();
boolean check=true;
DateUtil(){
}
DateUtil(int d,int m,int y){
day.value=d;
month.value=m;
year.value=y;
}
public Day getDay(){
return day.value;
}
public void setDay(Day d){
day.value=d;
}
public void checkInputValidity()
{
if(year.check==false||month.check==false||day.check==false)
check=false;
}
public boolean compareDates(DateUtil date){
if (this.year > date.year||(this.year == date.year&&this.month > date.month)||(this.year == date.year&&this.month==date.month&&this.day >= date.day)) return true;
else
return false;
}
public boolean equalTwoDates(DateUtil date){
if (date != null) {
if (year == date.year && month == date.month && day == date.day) {
return true;
}
}
return false;
}
public String showDate()
{
return year.value + "-" + month.value + "-" + day.value;
}
public DateUtil getNextNDays(int n)
{
for (int i = 0; i < n; i++) {
day.value++;
if (day.value > month_maxnum[month.value]) {
day = 1;
month++;
if (month > 12) {
month = 1;
year++;
}
}
}
return new DateUtil(year.value, month.value, day.value);
}
public DateUtil getPreviousNDays(int n)//取得year-month-day的前n天日期
{
for (int i = 0; i < n; i++) {
day.value--;
while (day.value < 1) {
month.value--;
if (month.value < 1) {
month.value = 12;
year.value--;
}
day.value = month_maxnum[month.value]
}
}
return new DateUtil(year.value, month.value, day.value);
}
public int getDaysofDates(DateUtil date)//求当前日期与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 class Main {
public static void main(String[] args) throws Exception{
Scanner input = new Scanner(System.in);
int day,month,year;
int choice = input.nextInt();
switch(choice){
case 1:
year=input.nextInt();
month=input.nextInt();
day=input.nextInt();
int m=input.nextInt();
DateUtil date = new DateUtil(year, month, day);
if (date.check=false||m<0) {
System.out.println("Wrong Format");
//System.exit(0);
}
System.out.println(date.getNextNDays(m).showDate());
case 2:
year=input.nextInt();
month=input.nextInt();
day=input.nextInt();
DateUtil date = new DateUtil(year, month, day);
int n=input.nextInt();
if (date.check=false||n<0) {
System.out.println("Wrong Format");
//System.exit(0);
}
System.out.println(date.getPreviousNDays(n).showDate());
case 3:
year1=input.nextInt();
month1=input.nextInt();
day1=input.nextInt();
year2=input.nextInt();
month2=input.nextInt();
day2=input.nextInt();
DateUtil date1 = new DateUtil(year1, month1, day1);
DateUtil date2 = new DateUtil(year2, month2, day2);
if (date1.check=false || date2.check=false)
System.out.println("Wrong Format");
else
System.out.println(date1.getDaysofDates(date2))
default:
System.out.println("Wrong Format");
}
}
}
解释与心得:
题目集4 7-2通过编写定义不同的类,进行对象的嵌套,实现了多个相关类之间的相互调用。Year类为最底层的对象,依次嵌套在月,日类中,实现的日期的聚合,这样的结构使得类与类的嵌套与调用非常繁琐和复杂,当代码出现bug时难以定位,即使是debug也难以快速找到问题点。
题目集5 7-4的类设计显而易见的Year、Month、Day类相互独立,没有7-2的嵌套与绑定,当代码出现bug时可以快速找到问题点,并且这样的结构更加清晰明了,便于他人阅读。
②题目集4(7-3)、题目集6(7-5、7-6)三种渐进式图形继承设计的思路与技术运用(封装、继承、多态、接口等)
题目集4 7-3
import java.util.Scanner;
import java.text.DecimalFormat;
class Shape{
public Shape(){
}
public void hehe(){
System.out.println("Constructing Shape");
}
}
class Circle{
private double radius;
public Circle(){
}
public void xixi(){
System.out.println("Constructing Circle");
}
public void CircleSetter(double radius){
this.radius=radius;
}
public double getCircleArea(){
return (3.14159*radius*radius);
}
}
class Rectangle{
private double width,length;
public Rectangle(){
}
public void www(){
System.out.println("Constructing Rectangle");
}
public void RectangleSetter(double width,double length){
this.width=width;
this.length=length;
}
public double getRectangleArea(){
return (width*length);
}
}
class Ball{
private double rradius;
public Ball(){
}
public void vvv(){
System.out.println("Constructing Circle\nConstructing Ball");
}
public void BallSetter(double rradius){
this.rradius=rradius;
}
public double getBallArea(){
return (4*Math.PI*rradius*rradius);
}
public double getVolume(){
return (4.0/3*Math.PI*rradius*rradius*rradius);
}
}
class Box{
private double wwidth,llength,height;
public Box(){
}
public void kk(){
System.out.println("Constructing Rectangle\nConstructing Box");
}
public void BallSetter(double wwidth,double llength,double height){
this.wwidth=wwidth;
this.llength=llength;
this.height=height;
}
public double getBallArea(){
return (2*(wwidth*llength+llength*height+height*wwidth));
}
public double getVVolume(){
return (wwidth*llength*height);
}
}
public class Main{
public static void main(String[] args) throws Exception{
DecimalFormat df=new DecimalFormat("0.00");
Scanner input=new Scanner(System.in);
int kind=input.nextInt();
switch(kind){
case 1:
double r=input.nextDouble();
if(r<0)
{
System.out.print("Wrong Format");break;
}
else
{
Shape q=new Shape();
q.hehe();
Circle w=new Circle();
w.xixi();
w.CircleSetter(r);
System.out.print("Circle's area:"+df.format(w.getCircleArea()));break;
}
case 2:
double ku=input.nextDouble();
double ch=input.nextDouble();
if(ku<0 || ch<0)
{
System.out.print("Wrong Format");break;
}
else
{
Shape e=new Shape();
e.hehe();
Rectangle t=new Rectangle();
t.www();
t.RectangleSetter(ku,ch);
System.out.print("Rectangle's area:"+df.format(t.getRectangleArea()));break;
}
case 3:
double rr=input.nextDouble();
if(rr<0)
{
System.out.print("Wrong Format");break;
}
else
{
Shape y=new Shape();
y.hehe();
Ball u=new Ball();
u.vvv();
u.BallSetter(rr);
System.out.print("Ball's surface area:"+df.format(u.getBallArea())+"\nBall's volume:"+df.format(u.getVolume()));break;
}
case 4:
double kua=input.nextDouble();
double cha=input.nextDouble();
double gao=input.nextDouble();
if(kua<0 || cha<0 || gao<0)
{
System.out.print("Wrong Format");break;
}
else
{
Shape i=new Shape();
i.hehe();
Box o=new Box();
o.kk();
o.BallSetter(kua,cha,gao);
System.out.print("Box's surface area:"+df.format(o.getBallArea())+"\nBox's volume:"+df.format(o.getVVolume()));break;
}
default:
System.out.print("Wrong Format");break;
}
}
}
题目集6 7-5
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import java.text.DecimalFormat;
class gongjv implements Comparator<Double>{
@Override
public int compare(Double o1, Double o2) {
if(o1>o2) {
return 1;
}else if(o1<o2) {
return -1;
}else
return 0;
}
}
class Triangle{
double a,b,c;
int check=1;
public Triangle(double a, double b, double c) {
super();
this.a = a;
this.b = b;
this.c = c;
}
public double getArea(){
return Math.sqrt((a+b+c)*(a+b-c)*(a+c-b)*(b+c-a))/4;
}
public boolean hefasanbian() {
double a[] = new double[3];
a[0] = this.a;
a[1] = this.b;
a[2] = this.c;
Arrays.sort(a);
if ((a[0] + a[1] > a[2]) && a[0]>0 && a[1]>0 && a[2]>0&&(a[2]-a[0]<a[1])) {
return true;
} else {
return false;
}
}
}
class Rectangle{
double chang,kuan;
int check=1;
public boolean hefa() {
if(this.chang>0&this.kuan>0) {//???&
return true;
}else {
return false;
}
}
public Rectangle(double chang, double kuan) {
super();
this.chang = chang;
this.kuan = kuan;
}
public double getArea(){
return chang*kuan;
}
}
class Circle{
double radius;
int check=1;
public boolean hefa() {
if(this.radius>0) {
return true;
}else {
return false;
}
}
public double getArea(){
return Math.PI*radius*radius;
}
public Circle(double radius) {
super();
this.radius = radius;
}
}
public class Main {
public static void main(String[] args) {
DecimalFormat df=new DecimalFormat("0.00");
Scanner se = new Scanner(System.in);
int ynum = se.nextInt();
int cnum = se.nextInt();
int snum = se.nextInt();
if (ynum >= 0 && cnum >= 0 && snum >= 0) {
Rectangle r[] = new Rectangle[cnum];
Triangle t[] = new Triangle[snum];
Circle c[] = new Circle[ynum];
ArrayList <Double> mjb=new ArrayList<Double>();
for (int i = 0; i < ynum; i++) {
c[i] = new Circle(se.nextDouble());
if (c[i].hefa() == false)
c[i].check=0;
if(c[i].check==0) {
System.out.println("Wrong Format");
System.exit(0);
}
mjb.add(c[i].getArea());
}
for (int i = 0; i < cnum; i++) {
r[i] = new Rectangle(se.nextDouble(), se.nextDouble());
if (r[i].hefa() == false)
r[i].check=0;
if(r[i].check==0) {
System.out.println("Wrong Format");
System.exit(0);
}
mjb.add(r[i].getArea());
}
for (int i = 0; i < snum; i++) {
t[i] = new Triangle(se.nextDouble(), se.nextDouble(), se.nextDouble());
if (t[i].hefasanbian() == false)
t[i].check=0;
if(t[i].check==0)
{
System.out.println("Wrong Format");
System.exit(0);
}
mjb.add(t[i].getArea());
}
System.out.println("Original area:");
shuchu(mjb);
System.out.println("Sum of area:"+df.format(qiu(mjb)));
Collections.sort(mjb, new gongjv());
System.out.println("Sorted area:");
for(double i:mjb) {
System.out.print(df.format(i)+" ");
}
System.out.println();
System.out.printf("Sum of area:"+df.format(qiu(mjb)));
}
else {
System.out.println("Wrong Format");
}
}
public static double qiu(ArrayList <Double> mjb){
double a = 0;
for (double i:mjb) {
a=a+i;
}
return a;
}
public static void shuchu(ArrayList <Double> mjb) {
DecimalFormat df=new DecimalFormat("0.00");
for (double i:mjb) {
System.out.print(df.format(i)+" ");
}
System.out.println();
}
}
解释和心得:
题目集4(7-3)是继承类的结构,其运用的主要结构为通过子类继承父类并且重写父类的方式,并且对各个图形类进行方法的构造,从而实现对相同方法的不同体现,其充分体现了继承类的优点。
题目集6(7-5、7-6)继承的实现是通过抽象类和接口来实现的,抽象类中的成员变量可以是各种类型的,而接口中的成员变量只能是public static final类型的,因为一个类可以实现多个接口,通过继承可以精简代码,充分体现其独立可维护性和可读性,多态就是不同的对象对同一消息做出的不同响应。
③对三次题目集中用到的正则表达式技术的分析总结:
三次题目中使用正则表达式的难度各有不同,其中最考验技术的是题目集4 7-1的水文数据的检测。需要对字符串进行分割后对每一个分割后的字符串进行检验,然后根据检验结果进行输,利用类的单一职责将每一部分封装成一个类,且一个类只负责该类的职责,通过类与类之间的聚合来实现整个过程,字符串分割后产生的五部分还需要指出错误行以及列数,当数据不合法时进行输出操作。其余题目的正则表达式都只需要做一个简单的匹配,把需要检验的数据分析和拆开利用正则表达式进行检验即可完成要求。
题目集06 7-1
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String qq = sc.nextLine();
boolean b = checkQQ2(qq);
if(b==true)
System.out.println("你输入的QQ号验证成功");
else
System.out.println("你输入的QQ号验证失败");
}
public static boolean checkQQ2(String qq){
String regex = "[1-9][0-9]{4,14}";
return qq.matches(regex);
}
}
④题目集5(7-4)中Java集合框架应用的分析总结
本题主要通过TreeMap,即KEY-VALUE的集合,借此来完成NavigableMap接口,然后返回有序的key集合。同时它也可以被克隆、序列化,因为它有Cloneable接口、java.io.Serializable接口。它的iterator 方法返回的迭代器是fail-fastl的,因此Tree Map是非同步的。排序基于使用的构造方法。Map中的每一个元素包含“一个key”和“key对应的value”。
(3)采坑心得:利用 DecimalFormat df=new DecimalFormat("0.00");进行控制小数点精确度时,只在public class Main中定义了一次,而没有在下面自定义的方法中再声明一次,导致出错找不到df这个变量。在输出时格式不对,检查发现输出时没有加空格,后来改为System.out.print(df.format(i)+" ");。
(4)改进建议:在控制小数点后几位输出时,不用每次都先引进包 import java.text.DecimalFormat;,然后定义DecimalFormat df=new DecimalFormat("0.00");进行控制输出这么麻烦,直接使用printf(“%.2f”,i)模仿C语言,以格式化的形式输出更加方便,另外,养成加上throws Exception和System.exit(0);的好习惯可以减少出错。
(5)总结:
通过这几次题目集的完成,我也更深入地渗透了面向对象的编程思维模式,其更具独立性和可维护性。这次我也掌握了正则表达式的知识和实践,对聚合和多态也通过自己打代码有了更好的了解,提高了代码的多次利用性质。本次题目集的难度由易到难非常适合我们初学者,建议以后都可以延续从易到难,循序渐进的出题风格。

浙公网安备 33010602011771号