Blog2

一:前言

1.这三次的题目集是上一次的进阶,其实也就题目集四变难了,因为后面同学们向老师反馈了题目变难了很多,所以老师在题目集五和题目集六的难度有一点下降(多了几个小题目让我们拿分,其实我感觉这挺好,可能完成了这些小题目我们可能更有信心),所以总体来说,题目集四的难度大,题目量少;题目集五的难度较题目集四有所下降,但个别题目还是有难度的,题目量也变多了;题目集六较题目集五难度又有所下降,但题目量又变多了。

2.知识点考察:

题目集四:

(1):水文数据校验及处理,这题也是该题目集最难的题目,它要求使用Java中的字符串处理类以及正则表达式对输入字符串数据进行合法性校验及计算,考察的是逻辑思路以及正则表达式的学习程度以及一些基本的Java运算语法。

(2):日期问题面向对象设计,这题的难度适中,它要求设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1900,2050] ,month∈[1,12] ,day∈[1,31] ,考察的是逻辑思维、对类的应用依赖聚合,它注明了几个类其实难度也并未变大,所以来说这道题目就是属于一般难度。

(3):图形继承,这道题也是该题目集中最简单的,它要求实现图形类的继承,并定义相应类对象并进行测试,输出值均保留两位小数,考察的是尽量使代码可复用、例如使用this及super关键字观察构造方法链的调用过程、保留小数格式可采用String.format、重写方法建议使用标注@Override、思考继承可以为程序结构带来什么不一样的特性,所以说这道题考查的知识点并不难。

题目集五:

(1):找出最长的单词,合并两个有序数组为新的有序数组,这两道题也是该题目集最简单的了,考察的知识点也就是split以及一些语法和逻辑思维。

(2):对整型数据排序,要求分别使用三种排序方法,插入排序法、选择排序法以及冒泡排序法三种算法对数据进行排序(升序),如下是我的排序方法,

顺序分别为插入排序法、选择排序法和冒泡排序法:

  private static void insertionSort(int[] list) {
	  int i,j,temp;
	  int k = list.length;
	  for(i = 1;i < k;i ++) {
		  temp = list[i];
		  for(j = i - 1;j >= 0;j --) {
			  if(temp < list[j]) {
				  list[j + 1] = list[j];
				  if(j == 0) {
					  list[j] = temp;
					  break;
				  }
			  }
			  else {
				  list[j + 1] = temp;
				  break;
			  }
		  }
	  }
	  for(int n = 0;n < k;n ++) {
		  System.out.print(list[n] + " ");
	  }
  }
  private static void selectionSort(int[] list) {
	  int i,j,m,temp = 0;
	  int k = list.length;
	  for(i = 0;i < k -1;i ++) {
		  temp = i;
		  for(j = i + 1;j < k;j ++) {
			  if(list[temp] > list[j]){
				  temp = j;
			  }			  
		  }
		  if(i != temp) {
			  m = list[i];
		      list[i] = list[temp];
			  list[temp] = m;
		  }
	  }
	  for(int n = 0;n < k;n ++) {
		  System.out.print(list[n] + " ");
	  }
  }
  private static void bubbleSort(int[] list) {
	  int i,j,temp;
	  int k = list.length;
	  for(i = 0;i < k - 1;i ++) {
		  for(j = i + 1;j < k;j ++) {
			  if(list[i] > list[j]) {
				  temp = list[i];
				  list[i] = list[j];
				  list[j] = temp;
			  }
		  }
	  }
	  for(int n = 0;n < k;n ++) {
		  System.out.print(list[n] + " ");
	  }
  }
}   

(3):统计Java程序中关键词的出现次数,这道题是该题目集中难度最大的,要求统计输入源码中出现关键字的数量,主要考察的知识点是正则表达式和一些语法;

(4):日期问题面向对象设计,这道题与题目集四中的7-2很相似,但有一点不同的是它类中的方法不同;所以题目难度变大了一点点,但只要会题目集四中的7-2,就能写出该题目,考察点是类的聚合依赖。

题目集六:

(1):前四道(7-1——7-4)都是正则表达式的训练:QQ号检验、字符排序、验证码检验和学号检验,这些都是为了提升正则表达式的使用能力,所以都是基础。

(2):图形继承与多态,这道题是这里最难的,在这里,有一个新的知识点需要自己学习,就是ArrayList,以及排序Collections.sort,,除了这里,也就是考察的是getter和setter以及一些基本语法和逻辑。

(3):实现图形接口及多态性,使用接口及类实现多态性,其实这道题只要懂接口就会变得不在困难,考察的知识点其实是、深入理解继承与多态的原理及用法、ArrayList常用方法及和数组的关系、泛型的应用、Arrays及Collections的简单应用。

二:设计与分析

1.题目集四7-2和题目集五的7-6这两道题感觉上差不多,接下来先看一下源码,先看题目集四的7-2:

 

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int a = input.nextInt();
		int year = input.nextInt();
		int month = input.nextInt();
		int day1 = input.nextInt();
		DateUtil date = new DateUtil(day1,month,year);
		if (!date.checkInputValidity()) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
		if(a > 3||a < 1) {
			System.out.println("Wrong Format");
			System.exit(0);
		}
        switch(a) {
        case '1':  	
        	int n = input.nextInt();
        	System.out.print(date.getNextNDays(n).showDate());
        	break;
        case 2:
        	int m = input.nextInt();
        	System.out.print(date.getPreviousNDays(m).showDate());       
        	break;
        }
	}

}
class DateUtil{
	Day day;
	int day1;
    int month;
    int year;
	public DateUtil() {
		super();
		// TODO 自动生成的构造函数存根
	}
	

	public DateUtil(int day1, int month, int year) {
		super();
		this.day1 = day1;
		this.month = month;
		this.year = year;
	}
	public Day getDay() {
		return day;
	}
	public void setDay(Day day) {
		this.day = day;
	}
	

	public int getDay1() {
		return day1;
	}

	public void setDay1(int day1) {
		this.day1 = day1;
	}


	public int getMonth() {
		return month;
	}


	public void setMonth(int month) {
		this.month = month;
	}


	public int getYear() {
		return year;
	}


	public void setYear(int year) {
		this.year = year;
	}


	public boolean checkInputValidity() {
		if (year < 1820 || year > 2020) return false;
        if (month < 1 || month > 12) return false;

        return day1 >= 1 && day1 <= 31;
	}
	public boolean compareDates(DateUtil date) {
		if(year > date.year)
			return true;
		else if(year < date.year) 
			return false;
		else if(year == date.year){
			if(month > date.month)
			    return true;
			else if(month < date.month)
				return false;
			else if(month == date.month){
				if(day.getValue() >= date.day.getValue())
					return true;
				else if(day.getValue() < date.day.getValue())
					return false;
			}
		}
		return false;
	}
	public boolean equalTwoDates(DateUtil date) {
		if(year == date.year&&month == date.month&&day.getValue() == date.day.getValue())
			return true;
		else return false;
	}
	public String showDate() {
		return year + "-" + month + "-" + day1;
	}
	 public DateUtil getNextNDays(int n)//取得year-month-day的下n天日期
	    {
	        int year = this.year;
	        int month = this.month;
	        int day1 = this.day1;
	        for (int i = 0; i < n; i++) {
	            day1 ++;
	            if (day1 > day.resetMax()) {
	                day1 = 1;
	                month++;
	                if (month > 12) {
	                    month = 1;
	                    year++;
	                }
	            }
	        }
	        return new DateUtil(day1,month, year);
	    }
	public DateUtil getPreviousNDays(int n) {
		for(int i = 1;i <= n;i++) {
			day.dayReducion();
			if(day.validate()) {
				continue;
			}
			else {
				month --;
				day.resetMax();
				if(month < 1) {
					year --;
					month = 12;
				}
			}
		}
		return new DateUtil(day1,month,year);
	}
 
}
class Day{
	int value;
	Month month;
	int[] mon_maxnum = {31,28,31,30,31,30,31,31,30,31,30,31};
	public Day() {
		super();
		// TODO 自动生成的构造函数存根
	}
	public Day(int value, Month month) {
		super();
		this.value = value;
		this.month = month;
	}
	public int getValue() {
		return value;
	}
	public void setValue(int value) {
		this.value = value;
	}
	public Month getMonth() {
		return month;
	}
	public void setMonth(Month month) {
		this.month = month;
	}
    public void resetMin() {
    	value = 1;
    }
    public int resetMax() {
    	if(month.year.isLeapYear())
    		mon_maxnum[1] ++; 	
    	value = mon_maxnum[month.getValue() - 1];
    	return value;
    }
	public boolean validate() {
		if(month.getValue() == 2) {
			if(month.year.isLeapYear())
				if(value < 1||value >29)
				    return false;
			    else return true;
		}
		if(value > mon_maxnum[month.getValue() - 1]||value < 1)
			    return false;
		else return true;		    
	}
	public void dayIncrement() {
		value ++;
	}
	public void dayReducion() {
		value --;
	}
}
class Month{
	int value;
	Year year = new Year();
	public Month() {
		super();
	}
public Month(int value, Year year) {
		super();
		this.value = value;
		this.year = year;
	}
	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;
	}
	public void resetMin() {
		value = 1;
	}
	public void restMax() {
		value = 12;
	}
	public boolean validate() {
		if(value < 1||value > 12)
			return false;
		else return true;
	}
	public void monthIncrement() {
		value ++;
	}
	public void monthReducion() {
		value --;
	}
}
class Year{
	int value;
	public Year() {
		super();
		// TODO 自动生成的构造函数存根
	}
	public Year(int value) {
		super();
		this.value = value;
	}
	public int getValue() {
		return value;
	}
	public void setValue(int value) {
		this.value = value;
	}
	public boolean isLeapYear() {
		if((value % 4 == 0&&value % 100 != 0)||value % 400 == 0)
            return true;
		else return false;
	}
	public boolean validate() {
		if(value > 2050||value < 1900)
			return false;
		else return true;
	}
	public void yearIncrement() {
		value ++;
	}
	public void yearReduction() {
		value --;
	}
}

 

 这道题,之前我是没做出来的,但是大概思路我都是有的,所以题目集五我就做的很快,接下来再看一下题目集五7-5:

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int a = input.nextInt();
		if(a > 3||a < 1) {
			System.out.println("Wrong Format");
			System.exit(0);
		}
		int y = input.nextInt();
		int m = input.nextInt();
		int d = input.nextInt();
		Day day = new Day(d);
		Month month = new Month(m);
		Year year = new Year(y);		
		DateUtil date = new DateUtil();
		date.setDay(day);
		date.setMonth(month);
		date.setYear(year);
		if (!date.checkInputValidity()) {
            System.out.println("Wrong Format");
            System.exit(0);
        }	
        switch(a) {
        case 1: 
        	long n = input.nextLong();   
        	date.getNextNDays(n);
        	System.out.print(y + "-" + m + "-" + d + " next " +  n + " days is:" + date.showDate());
        	break;
        case 2:
        	long m1 = input.nextLong();
        	date.getPreviousNDays(m1);
        	System.out.print(y + "-" + m + "-" + d + " previous " + m1 + " days is:" + date.showDate());       
        	break;
        case 3:
        	int y1 = input.nextInt();
        	int m2 = input.nextInt();
        	int d1 = input.nextInt();
        	Day day1 = new Day(d1);
    		Month month1 = new Month(m2);
    		Year year1 = new Year(y1);		
    		DateUtil date1 = new DateUtil();
    		date1.setDay(day1);
    		date1.setMonth(month1);
    		date1.setYear(year1);
        	System.out.print("The days between " + y + "-" + m + "-" + d + " and " + y1 + "-" + m2+ "-" + d1 + " are:" + date.getDaysofDates(date1));
        }
	}
}
class Year{
	int value;
	public Year() {
		super();
	}
	public Year(int value) {
		super();
		this.value = value;
	}
	public int getValue() {
		return value;
	}
	public void setValue(int value) {
		this.value = value;
	}
	public boolean isLeapYear() {
		if((value % 4 == 0&&value % 100 != 0)||value % 400 == 0)
            return true;
		else return false;
	}
	public boolean validate() {
		if(value > 2050||value < 1900)
			return false;
		else return true;
	}
	public void yearIncrement() {
		value ++;
	}
	public void yearReduction() {
		value --;
	}
}
class Month{
	int value;
	public Month() {
		super();
	}
    public Month(int value) {
		super();
		this.value = value;
	}
	public int getValue() {
		return value;
	}
	public void setValue(int value) {
		this.value = value;
	}
	public void resetMin() {
		value = 1;
	}
	public void restMax() {
		value = 12;
	}
	public boolean validate() {
		if(value < 1||value > 12)
			return false;
		else return true;
	}
	public void monthIncrement() {
		value ++;
	}
	public void monthReducion() {
		value --;
	}
}
class Day{
	int value;
	public Day() {
		super();
	}
	public Day(int value) {
		super();
		this.value = value;
	}
	public int getValue() {
		return value;
	}
	public void setValue(int value) {
		this.value = value;
	}
	public void dayIncrement() {
		value ++;
	}
	public void dayReducion() {
		value --;
	}
}
class DateUtil{
    Year year;
    Month month ;
    Day day ;
    int[] mon_maxnum = {0,31,28,31,30,31,30,31,31,30,31,30,31};
	public DateUtil() {
		super();		
	}
	public DateUtil(int y, int m, int d) {
	}
	public Day getDay() {
		return day;
	}
	public void setDay(Day day) {
		this.day = day;
	}
	public Month getMonth() {
		return month;
	}
	public void setMonth(Month month) {
		this.month = month;
	}
	public Year getYear() {
		return year;
	}
	public void setYear(Year year) {
		this.year = year;
	}
    public void setDayMin() {
    	day.setValue(1);
    }
    public int setDayMax() {
    	day.setValue(mon_maxnum[month.getValue() - 1]);
    	if(month.getValue() == 2&&year.isLeapYear())
    		mon_maxnum[1] = 29;
    	return day.getValue();
    }
	public boolean checkInputValidity() {
		if (year.getValue() < 1820 || year.getValue() > 2020 || month.getValue() < 1 || month.getValue() > 12) {
			return false;
		} else if (year.getValue() >= 1820 && year.getValue() <= 2020 && month.getValue() >= 1 && month.getValue() <= 12) {
			if (month.getValue() == 1 || month.getValue() == 3 || month.getValue() == 5 || month.getValue() == 7 || month.getValue() == 8 
					|| month.getValue() == 10 || month.getValue() == 12) {
				if (day.getValue() < 1 || day.getValue() > 31) {
					return false;
				} else
					return true;
			}else if(month.getValue() == 4 || month.getValue() == 6 || month.getValue() == 9 || month.getValue() == 11) {
				if (day.getValue() < 1 || day.getValue() > 30) {
					return false;
				} else
					return true;
			} else if (month.getValue() == 2) {
				if (year.isLeapYear()) {
					if (day.getValue() < 1 || day.getValue() > 29) {
						return false;
					} else
						return true;
				} else {
					if (day.getValue() < 1 || day.getValue() > 28) {
						return false;
					} else
						return true;
				}
			}
		}
		return true;
	}
	public boolean compareDates(DateUtil date) {
		if(year.getValue() > date.year.getValue())
			return true;
		else if(year.getValue() < date.year.getValue()) 
			return false;
		else if(year.getValue() == date.year.getValue()){
			if(month.getValue() > date.month.getValue())
			    return true;
			else if(month.getValue() < date.month.getValue())
				return false;
			else if(month.getValue() == date.month.getValue()){
				if(day.getValue() >= date.day.getValue())
					return true;
				else if(day.getValue() < date.day.getValue())
					return false;
			}
		}
		return false;
	}
	public boolean equalTwoDates(DateUtil date) {
		if(year.getValue() == date.year.getValue()&&month.getValue() == date.month.getValue()&&day.getValue() == date.day.getValue())
			return true;
		else return false;
	}
	public String showDate() {
		return year.getValue() + "-" + month.getValue() + "-" + day.getValue();
	}
	 public DateUtil getNextNDays(long n){
		 int m;
	        for (int i = 0; i < n; i++) {
	            day.dayIncrement();     
	            m=mon_maxnum[month.getValue()];
	            if(year.isLeapYear()&&month.getValue()==2)
	            	 m=mon_maxnum[month.getValue()]+1;
	            if (day.getValue() > m) {
	                day.setValue(1);
	                month.monthIncrement();
	                if (month.getValue() > 12) {
	                    month.setValue(1);
	                    year.yearIncrement();
	                }
	            }
	        }
	        return new DateUtil(year.getValue(),month.getValue(),day.getValue());
	    }
	public DateUtil getPreviousNDays(long n) {
		int m1;
		for(int i = 0;i < n;i++) {
			day.dayReducion();	
			if(day.getValue() == 0){
				month.monthReducion();
				if(month.getValue() < 1) {
					year.yearReduction();
					month.setValue(12);
				}
				m1 = mon_maxnum[month.getValue()];
	            if(year.isLeapYear()&&month.getValue() == 2)
	            	 m1 = mon_maxnum[month.getValue()]+1;
				day.setValue(m1);
			}
		}
		return new DateUtil(year.getValue(),month.getValue(),day.getValue());
	}
	public int getDaysofDates(DateUtil date) {
        int n;       
        DateUtil date1 = this; 
        DateUtil date2 = date;
        if (this.compareDates(date)) {
        	date1 = date;
            date2 = this;
        }
        for(n = 0;!date1.equalTwoDates(date2);n ++) {
        	date1.getNextNDays(1);		
        }       
		return n;
       }
}

  

 

要求差不多,主要是要求的方法稍微有一些是不一样的(“聚合”也有一些不一样),这也就得出来它们中的优劣势,接下来让我们看一下这两道题的类图和复杂度,类图如下:

 

 

 这个是题目集四7-2的类图,在Day,Month,Year中有它们各自的方法,如:判断合法性、加一天(一个月\一年)、减一天(一个月\一年)、getter和setter等,然后主要功能是在DateUtil这个类中实现的,在该类中,它也有判断年月日合法性,接下来看一下题目集五的类图,如下:

 

 

   这个类图它与上一个类图差不多,都是Day、Month、Year和DateUtil这些类,但它们不同的是类中的方法有一些不同,就比如Day类中的区别,题目集五中的Day里面就少了加减一天以及判断数据合法性的功能,还少了一个数组,在DateUtil类中,它比上一个中多了加减一天的功能(也就是说功能移除到这里来了),其他功能也就差不多了,但这里是DateUtil类与其他类(Day,Month,Year)分别聚合,而题目集四中的却是一个聚合一个(DateUtil与Day单一聚合,Day与Month单一聚合,Month与Year单一聚合),这是它们之间的差异,所以优劣性我也认为是在这里体现的,题目集五7-6的聚合我认为更简便于题目集四7-2,但题目集四7-2任何两个类都能有关联,这是题目集四7-2好于题目集五7-6的地方,接下来看一下这两个图的生成报表内容:

 

 

 

 这是题目集四7-2的生成报表类图;

 

 

 就两副的生成报表内容来看:复杂度一样,但这可能是因为题目集四没写好的缘故,因为我认为聚合二的方法简便于聚合一;但可能是我在日期的合法性校验使用多了if语句,所以就会显得很复杂,如果完善以下,聚合二的效率肯定是比聚合一高,并且他不是一个聚合一个而是都是聚合DateUtil,所以这个可能是他简便于第一个的原因,总体来说,题目集五聚合二好于题目集四聚合一。

2.题目集4(7-3)、题目集6(7-5、7-6),这是从继承到多态再到接口,先看这几道题的源码:

题目集4(7-3):

import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int i = input.nextInt();
		if(i >=1&&i <=4) {					
		    switch(i){
		        case 1:{
		    	    double radius = input.nextDouble();			    	    
		    	    if(radius <=  0) {
		    	    	System.out.println("Wrong Format");
		        		System.exit(0);
		    	    }
		    	    else{
		    	    	Circle circle = new Circle();
		    	    	circle.setRadius(radius);
		    	    	 System.out.println("Circle's area:" +  String.format("%.2f",circle.getArea()));
		    	    }
		        	}
		        
		        case 2:{
		        	double width = input.nextDouble();
		        	double length = input.nextDouble();
		        	if(width > 0&&length > 0) {
		        	    Rectangle rectangle = new Rectangle();
		        	    rectangle.setLength(length);
		        	    rectangle.setWidth(width);
		        	    System.out.println("Rectangle's area:" +  String.format("%.2f",rectangle.getArea()));
		        	}
		        	else {
		        		System.out.println("Wrong Format");
		        		System.exit(0);
		        	}
		        }
		        case 3:{
		        	double radius = input.nextDouble();
		        	if(radius >0) {
		        		Ball ball = new Ball();
		        		ball.setRadius(radius);
		        		System.out.println("Ball's surface area:" +  String.format("%.2f",ball.getArea()));
		        	    System.out.println("Ball's volume:" +  String.format("%.2f",ball.getVolume()));
		        	}
		        	else{
		        		System.out.println("Wrong Format");
		        		System.exit(0);
		        	}
		        }
		        
		        case 4:{
		        	double width = input.nextDouble();
		        	double length = input.nextDouble();
		        	double height = input.nextDouble();
		        	if(height > 0&&width > 0&&length > 0) {
		        		Box box = new Box();
	        	        box.setWidth(width);
		        	    box.setLength(length);
		        	    box.setHeight(height);
		        	    System.out.println("Box's surface area:" +  String.format("%.2f",box.getArea()));
		        	    System.out.println("Box's volume:" +  String.format("%.2f",box.getVolume()));
		        	}
		        	else{
		        		System.out.println("Wrong Format");
		        		System.exit(0);
		        	}
		        }
		    }
	    }
		else System.out.println("Wrong Format");
	}	
}

class Shape{
	public Shape() {
		super();
		System.out.println("Constructing Shape");
	}
	public double getArea() {
		return 0;
	}
}

class Circle extends Shape{
	protected double radius;
	public Circle() {
		super();
		System.out.println("Constructing Circle");
	}
	
	public double getRadius() {
		return radius;
	}

	public void setRadius(double radius) {
		this.radius = radius;
	}

	public Circle(double radius) {
		super();
		this.radius = radius;
	}
	public double getArea() {
		return Math.PI * radius * radius;
	}
}

class Rectangle extends Shape{
	protected double width,length;
	public Rectangle() {
		super();
		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 Rectangle(double width, double length) {
		super();
		this.width = width;
		this.length = length;
	}
	public double getArea() {
		return width * length;
	}
}
class Ball extends Circle{
	double radius = super.getRadius();
	
	public Ball() {
		super();
		System.out.println("Constructing Ball");
	}
	
	public Ball(double radius) {
		super();
		this.radius = radius;
	}

	public double getRadius() {
		return radius;
	}

	public void setRadius(double radius) {
		this.radius = radius;
	}

	public double getArea() {
		return 4 * Math.PI * radius *radius;
	}
	public double getVolume() {
		return 4.0 * Math.PI * radius *radius * radius / 3.0;
	}
}
class Box extends Rectangle{
	private double height;
	double length = super.getLength();
	double width = super.getWidth();
	public Box() {
		super();
		System.out.println("Constructing Box");
	}
	

	public Box(double width, double length, double height) {
		super();
		this.width = width;
		this.length = length;
		this.height = height;
	}


	public double getLength() {
		return length;
	}


	public void setLength(double length) {
		this.length = length;
	}


	public double getWidth() {
		return width;
	}


	public void setWidth(double width) {
		this.width = width;
	}


	public double getHeight() {
		return height;
	}
	public void setHeight(double height) {
		this.height = height;
	}
	public double getArea() {
		return 2 * (height * width + height * length + width * length);
	}
	public double getVolume() {
		return height * width * length;
	}
}

 题目集6(7-5):

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Main{
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int a = input.nextInt();
		int b = input.nextInt();
		int c = input.nextInt();
		double n = 0;
		if(a < 0||b < 0||c < 0) {
			System.out.println("Wrong Format");
			System.exit(0);
		}
		Shape []A = new Shape[a];
		ArrayList<Double> list = new ArrayList<Double>();
		for(int i = 0;i < a;i ++) {
			double radius = input.nextDouble();
			Circle circle = new Circle(radius);
			if(circle.validate())
			   A[i] = new Circle(radius);
			else {
				System.out.println("Wrong Format");
				System.exit(0);
			}
			n = n + A[i].getArea();
			list.add(A[i].getArea());
		}
		Shape []B = new Shape[b];
		for(int j = 0;j < b;j ++) {
			double width = input.nextDouble();
			double length = input.nextDouble();
			Rectangle rectangle = new Rectangle(width,length);
			if(rectangle.validate())
			    B[j] = new Rectangle(width,length);
			else {
				System.out.println("Wrong Format");
				System.exit(0);
			}
			n = n + B[j].getArea();
			list.add(B[j].getArea());
		}
		Shape []C = new Shape[c]; 
		for(int m = 0;m < c;m ++) {
			double side1 = input.nextDouble();
			double side2 = input.nextDouble();
			double side3 = input.nextDouble();
			Triangle triangle = new Triangle(side1,side2,side3);
			if(triangle.validate())
			    C[m] = new Triangle(side1,side2,side3);
			else {
				System.out.println("Wrong Format");
				System.exit(0);
			}
			n = n + C[m].getArea();
			list.add(C[m].getArea());
		}
		System.out.println("Original area:");
		for(int i = 0;i < a;i ++) {
			System.out.print(String.format("%.2f",A[i].getArea()) + " ");
		}
		for(int j = 0;j < b;j ++) {
			System.out.print(String.format("%.2f",B[j].getArea()) + " ");
		}
		for(int m = 0;m < c;m ++) {
			System.out.print(String.format("%.2f",C[m].getArea()) + " ");
		}
		System.out.println("");
		System.out.println("Sum of area:" + String.format("%.2f",n));
		Collections.sort(list);
		System.out.println("Sorted area:");
		for(Double k:list)
		    System.out.print(String.format("%.2f",k) + " ");
		System.out.println("");
		System.out.println("Sum of area:" + String.format("%.2f",n));
	}
}
abstract class Shape{
	public abstract double getArea();
	public abstract boolean validate();
}

class Circle extends Shape{
	public double radius;
	public Circle(double radius) {
		this.radius = radius;
	}
	public double getRadius() {
		return radius;
	}
	public void setRadius(double radius) {
		this.radius = radius;
	}
	@Override
	public double getArea() {
		return Math.PI * radius * radius;
	}
	public boolean validate() {
		if(radius > 0)
			return true;
		else return false;
	}
}
class Rectangle extends Shape{
	public double width;
	public double length;
	
	public Rectangle(double width, double length) {
		super();
		this.width = width;
		this.length = 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;
	}

	@Override
	public double getArea(){
		return width * length;
	}
	public boolean validate() {
		if(width > 0&&length > 0)
			return true;
		else return false;
	}
}
class Triangle extends Shape{
	public double side1;
	public double side2;
	public double side3;
	
	public 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 getArea() {		
		return  Math.sqrt((side1 + side2 + side3) * (side1 + side2 - side3)
				* (side1 + side3 -side2) * (side2 + side3 - side1)) / 4;
	}
	public boolean validate() {
		if(side1 + side2 > side3&&side1 + side3 > side2&&side2 + side3 > side1
				&&side1 > 0&&side2 > 0&&side3 > 0)
			return true;
		else return false;
	}
}

题目集6(7-6):

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		double radius = input.nextDouble();
		double width = input.nextDouble();
		double length = input.nextDouble();
		if(radius <= 0||width <= 0||length <= 0) {
			System.out.println("Wrong Format");
			System.exit(0);
		}
		GetArea getarea = new Circle(radius);
		System.out.println(String.format("%.2f", getarea.getArea()));
		GetArea getArea = new Retangle(width,length);
		System.out.println(String.format("%.2f",getArea.getArea()));
	}

}
class Circle implements GetArea{
	double radius;
	public double getRadius() {
		return radius;
	}
	public void setRadius(double radius) {
		this.radius = radius;
	}
	public Circle() {
	}
	public Circle(double radius) {
		super();
		this.radius = radius;
	}
	public double getArea() {
		return Math.PI * radius * radius;
	}
}
class Retangle implements GetArea{
	double width;
	double length;
	public Retangle() {
		super();
		// TODO 自动生成的构造函数存根
	}
	public Retangle(double width, double length) {
		super();
		this.width = width;
		this.length = 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;
	}
}
interface GetArea{
	public double getArea();
}

 这三道题(题目集4(7-3)、题目集6(7-5、7-6))使用了三种渐进式图形继承设计的思路与技术运用(封装、继承、多态、接口等),第一个使用的是继承(例如:class Circle extends Shape),首先定义一个父类Sheep,它是Circle和Rectangle都继承Sheep,类Ball继承Circle,类Box继承Rectangle,继承的特性:子类拥有父类非priavte的属性、方法;子类可以拥有自己的属性和方法、即子类可以对父类进行扩展;子类可以用自己的方式实现父类的方法;java的继承是单继承,但是可以多重继承;单继承:一个子类只可以继承一个父类;多重继承:A类继承 B类,B类继承C类,即C类是B类的父类,B类是A类的父类;继承提高了类之间的耦合性(继承的缺点,提高了代码间的联系);第二个使用的是继承与多态(一个对象具有多种形态),首先定义一个抽象类Sheep,它也是Circle和Rectangle都继承抽象类Sheep,还有Triangle也继承抽象类Sheep;第三个使用的是接口与多态性,我们需要先定义一个接口,使用interface关键字,然后再定义两个类Circle和Rectangle,然后在这两个类使用implements接入这个接口,接口就相当于一个招牌的作用; 接下来我们看一下这个类图和生成报表内容:

题目集4(7-3):

 

 

 

 

 3.在这三次题目集中,难免避免不了使用正则表达式,对三次题目集中的正则表达式进行分析,在题目集四中只有那道最难的题目需要使用到正则表达式,不出所料,我不会做这道题目,所以这道题目的正则表达式我也做不了过多的分析。在我们眼中,正则表达式能干什么,正则表达式的主要应用对象是文本,使用正则表达式可以指定想要匹配的字符串规则,然后通过这个规则来匹配、查找、替换或切割那些符合指定规则的文本。总体来讲,正则表达式可以对指定的文本实现以下功能:

  • 匹配验证: 判断给定的字符串是否符合正则表达式所指定的过滤规则,从而可以判断某个字符串的内容是否符合特定的规则(如email地址、手机号码等);当正则表达式用于匹配验证时,通常需要在正则表达式字符串的首部和尾部加上^和$,以匹配整个待验证的字符串。
  • 查找与替换: 判断给定字符串中是否包含满足正则表达式所指定的匹配规则的子串,如查找一段文本中的所包含的IP地址。另外,还可以对查找到的子串进行内容替换。
  • 字符串分割与子串截取: 基于子串查找功能还可以以符合正则表达式所指定的匹配规则的字符串作为分隔符对给定的字符串进行分割。

 题目集五:7-4需要使用正则表达式,但这也是需要高级一点的正则表达式,但是我只学了一些皮毛,所以并没有得到多少分这道题,主要还是使用正则表达式的匹配校验;

而在题目集六当中,有四道题目是需要用到正则表达式,这也是因为它考验的是基础,所以这里就是拿了满分,举几个题目的例子,如下:

 

7-4 正则表达式训练-学号校验 (7 分)
 

 

对软件学院2020级同学学号进行校验,学号共八位,规则如下:

  • 1、2位:入学年份后两位,例如20年
  • 3、4位:学院代码,软件学院代码为20
  • 5位:方向代码,例如1为软件工程,7为物联网
  • 6位:班级序号
  • 7、8位:学号(序号)

要求如下:

  • 只针对2020级
  • 其中软件工程专业班级分别为:202011~17、61,物联网工程专业班级为202071~202073,数据科学与大数据专业班级为202081~82
  • 每个班级学号后两位为01~40

输入格式:

在一行输入一个字符串。

输出格式:

若符合规则的学号,输出”正确“,若不符合,输出”错误“。

输入样例1:

在这里给出一组输入。例如:

20201536
 

输出样例1:

在这里给出相应的输出。例如:

正确
 

输入样例2:

在这里给出一组输入。例如:

20201541
 

输出样例2:

在这里给出相应的输出。例如:

错误


源码如下:

 

import java.util.Scanner;
public class Main{
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		String a = input.nextLine();
		String b = "(2020)((1[1-7])|61|7[1-3]|8[1-2])(0[1-9]|[1-3][0-9]|4[0])";
		boolean c = a.matches(b);
		if(c)
			System.out.println("正确");
		else
			System.out.println("错误");
	}

}

  

所以正则还是要从基础开始变牢,这也是老师常常在课堂上念叨的,所以我需要打好基础,

正则表达式就是由几个字符构成的,例如:                                        

                   

正则表达式主要是认识好那些匹配的符号,学好这个就能解决好许多基础问题。

 

4.题目集五7-5这道题目只要也是考察正则表达式,知道题目的难度其实就是这样,不简单也不难,所以还是要尽量的学习好正则表达式。

三:采坑心得

 

1.在许多地方你会发现用到了if-else语句,但这些都只是增加复杂度的语法,所以在日期类型题目,能不用if-else语句就不用,多使用swith语句,他的复杂程度会下降很多,所以if-else语句是一种增加复杂程度的语句,毕竟每家公司对复杂度的要求有明确的规定,所以按这些要求来,if-else语句必是不可以用的,所以以后都少用if-else语句。

 

2.因为代码都是在eclipse中写的,所以每次报错都能看到,也就能及时的修改,但提交源码是有时候还有些测试点是不会过的,所以要学会根据测试点来优化自己的代码。

 

3.在对待代码时自己要养成很多好的习惯,例如:在+号前后都加“  ”,所以这都是一个好的习惯,提前养好这些习惯,更能让自己未来的代码路变得更通畅。

 

四:改进建议

 

1.于题目集五和题目集六我认为可以加强代码的优化,让代码的复杂度降低,同样题目集四除了第一题,其他的也可以加强代码的优化,降低复杂度;但第三题我应该通过更多的时间来学习有关正则的知识,用来更顺畅的写代码,然后以便做出更难的题目,也可以更好方便地优化代码。由于第一题的难度太大,所以加强正则表达式的能力迫切需要提升。

 

2.题目集总结四和题目集五都有日期类题目,所以在这块可以加强代码的优化,就是把if-else语句改成swith语句,这里就是在检验日期的合法性是修改,在其他使用if-else的地方,可以直接return 判断语句,这样就能简便不少。

 

五:总结

 

这三次的题目集主要还是题目集四有难度,后面两次还行,而且难度在于题目集四的7-1,这道题也就没有多少人会写,他需要很强的正则表达式功底,所以我找到自己的缺陷,这就是进步的第一步—发现问题。最后的题目让我了解到Java的学习之路并不是一帆风顺,也是磕磕绊绊的,但这并不是放弃的理由,所以需要我更加的努力学习,希望在下次作业中继续进步努力学习。教师、课程、作业、实验、课上及课下组织方式都很好。

 

                                 

posted @ 2021-05-02 00:07  浅夏侯青  阅读(99)  评论(0)    收藏  举报