第二阶段Blog作业

(1)前言

第四次作业

第一题水文数据及校验处理,该题难度较大,该题主要使用java中的字符串处理类以及正则表达式对输入字符串数据进行检验及计算,对字符串进行分割处理以及对字符串各部分进行合法判断;

第二题第二道题为日期问题面向对象设计(聚合一),考察对继承的运用,对重写的运用,实现类的继承,并定义相应类对象并进行测试。

第三题图形继承,主要考察类的继承和一些调用。

总的来说,题量相对较少,难度对我个人来说较大,前两题难度较大。

第五次作业

后两题为排序和和并两个有序数组为新的数组,较为简单

前两题日期问题面向对象设计(聚合二)和统计关键词出现的次数,对我来说难度较大

第六次作业

前四题为正则表达式和排序,较为简单,后边两题图形继承和多态,对我来说很难写

(2)设计与分析

7-2 日期问题面向对象设计(聚合一) (35 分)
 

参考题目7-2的要求,设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1900,2050] ,month∈[1,12] ,day∈[1,31] , 设计类图如下:

类图.jpg

应用程序共测试三个功能:

  1. 求下n天
  2. 求前n天
  3. 求两个日期相差的天数

注意:严禁使用Java中提供的任何与日期相关的类与方法,并提交完整源码,包括主类及方法(已提供,不需修改)

 

 


import java.util.Scanner;

class Year{
int value;

public Year(){

}// 默认构造方法

public Year(int value) {
this.value=value;
}// 带参构造方法

public int getValue() {
return value;
}//value getter

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>=1900&&value<=2050)
return true;
else
return false;
}

//+1
public void yearIncrement() {
value=value+1;
}

//-1
public void yearReduction() {
value=value-1;
}

}//calss类的结束

 

//math 类

class Month{
int value;
Year year;

public Month(){

}

public Month(int yearValue,int monthValue) {
this.year=new Year(yearValue);
this.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;
}
public void resetMin(){
value=1;
}
public void resetMax(){
value=12;
}
public boolean vaildate() {
if(value>=1&&value<=12)
return true;
else return false;
}
public void monthIncrement()
{
value=value+1;
}
public void monthReduction()
{
value=value-1;
}
}//----------------


class Day{
int value;
Month month;
int mon_maxnum[]= {31,28,31,30,31,30,31,31,30,31,30,31};

public Day() {

}
public Day(int yearValue,int monthValue,int dayValue) {
this.month=new Month(yearValue,monthValue);
this.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;
}

public void resetMin() {
value=1;
}
public void resetMax() {

value=mon_maxnum[month.getValue()-1];
}

public boolean validate(){
if(this.getMonth().getYear().isLeapYear())
mon_maxnum[1]=29;
if(value>=1&&value<=mon_maxnum[month.getValue()-1])
return true;
else
return false;
}

public void dayIncrement() {
value=value+1;
}

public void dayReduction() {
value=value-1;
}
}//------------------------------------

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(){
if(this.getDay().getMonth().getYear().validate()&&this.getDay().getMonth().vaildate()&&day.validate())
return true;
else
return false;
}
//比较两个日期大小
public boolean compareDates(DateUtil date) {
if(date.getDay().getMonth().getYear().getValue()<this.getDay().getMonth().getYear().getValue())
return false;
else if(date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue()&&date.getDay().getMonth().getValue()<this.getDay().getMonth().getValue())
return false;
else if(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 false;
else
return true;
}
//判定两个日期是否相等
public boolean equalTwoDates(DateUtil date){
if(this.getDay().getValue()==date.getDay().getValue()&&this.getDay().getMonth().getValue()==date.getDay().getMonth().getValue()&& this.getDay().getMonth().getYear().getValue()==date.getDay().getMonth().getYear().getValue())
return true;
else
return false;
}
//日期值格式化
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;
}


//求下n天
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){//该年剩余天数大于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){//如果n天后在本月
m=this.getDay().getMonth().getValue();
d=n+this.getDay().getValue();
}
else{//如果n天后不在本月
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=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);
}
//求前n天
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){//如果前n天在该年
y=this.getDay().getMonth().getYear().getValue();
int e=this.getDay().getValue();//本月已经过的天数
if(e>n){//如果前n天在本月
m=this.getDay().getMonth().getValue();
d=e-n;
}
else{//如果前n天不在本月
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=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;
}
else if(!this.compareDates(date)){//如果日期大小不对
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;
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+a[b1.getDay().getMonth().getValue()]-b1.getDay().getValue();//小日期在该月剩余的天数
ts=ts+b2.getDay().getValue();//大日期在该月已经过的天数
for(j=b1.getDay().getMonth().getValue()+1;j<=12;j++)//小日期在该年剩余的天数
ts=ts+a[j];
for(j=b2.getDay().getMonth().getValue()-1;j>0;j--)//大日期在该年已经过的天数
ts=ts+a[j];
if(b1.getDay().getMonth().getYear().isLeapYear()&&b1.getDay().getMonth().getValue()<=2)//如果小日期该年为闰年且该天在1月或2月
ts++;
if(b2.getDay().getMonth().getYear().isLeapYear()&&b2.getDay().getMonth().getValue()>2)//如果大日期该年为闰年且该天在1月或2月后
ts++;
}
return ts;
}
}//------------------------------------

//主类
public class Main {
public static void main(String[] args) {
Scanner x=new Scanner(System.in);
int year=0,month=0,day=0,a,b;
a=x.nextInt();//输入判断类型
year=x.nextInt();month= x.nextInt();day=x.nextInt();//输入年月日
DateUtil c=new DateUtil(year,month,day);
if(a==1){//求下n天
b=x.nextInt();//输入n
if(!c.checkInputValidity()||b<0){//如果数据不合法
System.out.println("Wrong Format");
System.exit(0);
}
else
System.out.println(c.getNextNDays(b).showDate());
}
else if(a==2){
b=x.nextInt();//输入n
if(!c.checkInputValidity()||b<0){//如果数据不合法
System.out.println("Wrong Format");
System.exit(0);
}
else
System.out.println(c.getPreviousNDays(b).showDate());
}
else if(a==3){
int y1,m1,d1;
y1=x.nextInt();m1= x.nextInt();d1=x.nextInt();//输入第二个年月日
DateUtil d=new DateUtil(y1,m1,d1);
if(!c.checkInputValidity()||!d.checkInputValidity()){//如果数据不合法
System.out.println("Wrong Format");
System.exit(0);
}
else
System.out.println(c.getDaysofDates(d));
}
else
System.out.println("Wrong Format");

}
}

 

 

7-3 图形继承 (15 分)
 

编写程序,实现图形类的继承,并定义相应类对象并进行测试。

  1. 类Shape,无属性,有一个返回0.0的求图形面积的公有方法public double getArea();//求图形面积
  2. 类Circle,继承自Shape,有一个私有实型的属性radius(半径),重写父类继承来的求面积方法,求圆的面积
  3. 类Rectangle,继承自Shape,有两个私有实型属性width和length,重写父类继承来的求面积方法,求矩形的面积
  4. 类Ball,继承自Circle,其属性从父类继承,重写父类求面积方法,求球表面积,此外,定义一求球体积的方法public double getVolume();//求球体积
  5. 类Box,继承自Rectangle,除从父类继承的属性外,再定义一个属性height,重写父类继承来的求面积方法,求立方体表面积,此外,定义一求立方体体积的方法public double getVolume();//求立方体体积
  6. 注意:
  • 每个类均有构造方法,且构造方法内必须输出如下内容:Constructing 类名
  • 每个类属性均为私有,且必须有getter和setter方法(可用Eclipse自动生成)
  • 输出的数值均保留两位小数

主方法内,主要实现四个功能(1-4): 从键盘输入1,则定义圆类,从键盘输入圆的半径后,主要输出圆的面积; 从键盘输入2,则定义矩形类,从键盘输入矩形的宽和长后,主要输出矩形的面积; 从键盘输入3,则定义球类,从键盘输入球的半径后,主要输出球的表面积和体积; 从键盘输入4,则定义立方体类,从键盘输入立方体的宽、长和高度后,主要输出立方体的表面积和体积;

假如数据输入非法(包括圆、矩形、球及立方体对象的属性不大于0和输入选择值非1-4),系统输出Wrong Format

 

 

import java.util.Scanner;

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() {
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;
private double lenth;

public double getWidth() {
return width;
}

public double getLength() {
return lenth;
}

public void setwidth() {
this.width=width;
}

public void setlenth() {
this.lenth=lenth;
}

public double getArea() {

return width*lenth;//重写父类的方法
}

}//------------

class Ball extends Circle{


public Ball()
{
System.out.println("Constructing Ball");
}


public double getArea() {

return 4.0*super.getArea();//方法的重载,super关键字
}

public double getVolume()
{
double r2=super.getArea();
return 4.0/3.0*r2*r2*r2*Math.PI;
}

}

class Box extends Rectangle{

public Box()
{
System.out.println("Constructing Box");
}

private double height;

public double getHeight() {
return height;
}

public void setHeight(double height) {
this.height = height;
}

public double getVolume()
{
return height*super.getArea();
}

public double getArea() {
// TODO Auto-generated method stub
double w2=super.getWidth();
double l2=super.getLength();
return 2*(w2*l2+w2*height+l2*height);
}
}

public class Main {

public static void main(String[] args) {
int inType;
Scanner scanner=new Scanner(System.in);
inType=scanner.nextInt();
switch(inType)
{
case 1:
double r=scanner.nextDouble();
if(r<0.0) {
System.out.println("Wrong Format");
}
else {
Circle circle=new Circle();
circle.setRadius();
System.out.println(String.format("Circle's area:%.2f",circle.getArea()));
}
break;

case 2:
double width=scanner.nextDouble();
double length=scanner.nextDouble();
if(width<0.0||length<0.0) {
System.out.println("Wrong Format");
}
else {
Rectangle rectangle=new Rectangle();
rectangle.setlenth();
rectangle.setwidth();
System.out.println(String.format("Rectangle's area:%.2f",rectangle.getArea()));
}
break;
case 3:
double r2=scanner.nextDouble();
if(r2<0.0) {
System.out.println("Wrong Format");
}
else {
Ball ball=new Ball();
ball.setRadius();
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 width2=scanner.nextDouble();
double length2=scanner.nextDouble();
double height=scanner.nextDouble();
if(width2<0.0||length2<0.0||height<0.0) {
System.out.println("Wrong Format");
}
else {
Box box=new Box();
box.setHeight(height);
box.setwidth();
box.setlenth();
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");
}
}
}

 

 

7-4 统计Java程序中关键词的出现次数 (25 分)
 

编写程序统计一个输入的Java源码中关键字(区分大小写)出现的次数。说明如下:

  • Java中共有53个关键字(自行百度)
  • 从键盘输入一段源码,统计这段源码中出现的关键字的数量
  • 注释中出现的关键字不用统计
  • 字符串中出现的关键字不用统计
  • 统计出的关键字及数量按照关键字升序进行排序输出
  • 未输入源码则认为输入非法

 

 

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Map;
import java.util.TreeMap;

public class Main {
public static void main(String[] args) {
Scanner x = new Scanner(System.in);
StringBuilder a = new StringBuilder();
Map map=new TreeMap();
String[] gjc = {"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"};
String kg,exit="exit";
int i,n,flag=0;

//输入
kg = x.nextLine();
while( !kg.equals(exit)) {
a.append(kg.replaceAll("//.*", " ").replaceAll("\".*\"", " "));//去掉"//"后和的内容以及双引号里的内容
kg = x.nextLine();
flag=1;
}
String b = a.toString().replaceAll("/\\*\\s*.*\\s*\\*/", " ");//去掉"/* */"里的内容,放入字符串b中
//System.out.println(b);

//如果没有内容
if(flag==0) {
System.out.println("Wrong Format");
}

// 循环找每个关键词出现的次数
for(i=0;i< gjc.length;i++) {
Pattern pattern = Pattern.compile("\\b"+gjc[i]+"\\b");//创建关键词的正则表达式
Matcher matcher = pattern.matcher(b);//字符串与关键词匹配
n=0;
while(matcher.find()) {//找到该关键词的话,记录该关键词的次数
n++;
//System.out.println(matcher.group());

}
if(n!=0){//把次数不是0的关键词替换为次数
map.put(gjc[i], n);
}
//System.out.println(map);
}
//System.out.println(map);
String map1= String.valueOf(map);//把map转化为字符串map1
//System.out.println(map1);
String map2=map1.replace("{","").replace("}","");//把map1里的"{""}"去掉存入字符串map2
//System.out.println(map2);
String[] map3=map2.split(", ");//把map2根据", "分开,存入字符串数组map3

//循环输出
for (i=0;i< map3.length;i++){
String[] map4=map3[i].split("=");//把每个字符串map3根据"="分开,存入字符串数组map4
System.out.println(map4[1]+"\t"+map4[0]);
}

}
}

参考题目7-3的要求,设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1820,2020] ,month∈[1,12] ,day∈[1,31] , 设计类图如下:

类图.jpg

应用程序共测试三个功能:

  1. 求下n天
  2. 求前n天
  3. 求两个日期相差的天数

注意:严禁使用Java中提供的任何与日期相关的类与方法,并提交完整源码,包括主类及方法(已提供,不需修改)

 

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int year = 0;
int month = 0;
int day = 0;

int choice = input.nextInt();

if (choice == 1) { // test getNextNDays method
int m = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

DateUtil date = new DateUtil(year, month, day);

if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}

m = input.nextInt();

if (m < 0) {
System.out.println("Wrong Format");
System.exit(0);
}

System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:");
System.out.println(date.getNextNDays(m).showDate());
} else if (choice == 2) { // test getPreviousNDays method
int n = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

DateUtil date = new DateUtil(year, month, day);

if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}

n = input.nextInt();

if (n < 0) {
System.out.println("Wrong Format");
System.exit(0);
}

System.out.print(
date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:");
System.out.println(date.getPreviousNDays(n).showDate());
} else if (choice == 3) { //test getDaysofDates method
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

int anotherYear = Integer.parseInt(input.next());
int anotherMonth = Integer.parseInt(input.next());
int anotherDay = Integer.parseInt(input.next());

DateUtil fromDate = new DateUtil(year, month, day);
DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);

if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
System.out.println("The days between " + fromDate.showDate() +
" and " + toDate.showDate() + " are:"
+ fromDate.getDaysofDates(toDate));
} else {
System.out.println("Wrong Format");
System.exit(0);
}
}
else{
System.out.println("Wrong Format");
System.exit(0);
}
}
}

class DateUtil {
private int year;
private int month;
private int day;

public DateUtil(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}

public DateUtil(){}

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

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

public void setDay(int day) {
this.day = day;
}

public int getYear() {
return year;
}

public int getMonth() {
return month;
}

public int getDay() {
return day;
}

private final int[] DAY_OF_MONTH = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

private int getDayOfMonth(int year, int month) {
int days = DAY_OF_MONTH[month - 1];
if (month == 2 && isLeapYear(year)) {
days = 29;
}
return days;
}

public boolean checkInputValidity()//检测输入的年、月、日是否合法
{
if (year < 1820 || year > 2020) return false;
if (month < 1 || month > 12) return false;
// int _day = this.getDayOfMonth(year, month);
// return day <= _day;
return day >= 1 && day <= 31;
}

public boolean isLeapYear(int year)//判断year是否为闰年
{
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}


public DateUtil getNextNDays(int n)//取得year-month-day的下n天日期
{
int year = this.year;
int month = this.month;
int day = this.day;
// day = Math.min(day, this.getDayOfMonth(year, month));
for (int i = 0; i < n; i++) {
day++;
if (day > getDayOfMonth(year, month)) {
day = 1;
month++;
if (month > 12) {
month = 1;
year++;
}
}
}
return new DateUtil(year, month, day);
}

public DateUtil getPreviousNDays(int n)//取得year-month-day的前n天日期
{
int year = this.year;
int month = this.month;
int day = this.day;
for (int i = 0; i < n; i++) {
day--;
while (day < 1) {
month--;
if (month < 1) {
month = 12;
year--;
}
day += getDayOfMonth(year, month);
}
}
return new DateUtil(year, month, day);
}

public boolean compareDates(DateUtil date)//比较当前日期与date的大小(先后)
{
if (this.year > date.year) return true;
if (this.year == date.year) {
if (this.month > date.month) return true;
if (this.month == date.month) {
if (this.day >= date.day) return true;
}
}
return false;
}

public boolean equalTwoDates(DateUtil date)//判断两个日期是否相等
{
if (date != null) {
if (year == date.year && month == date.month && day == date.day) {
return true;
}
}
return false;
}

private static final int[] mon = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
public int getDaysofDates(DateUtil date)//求当前日期与date之间相差的天数
{
DateUtil dateUtil1 = this; // 小
DateUtil dateUtil2 = date; // 大
if (this.compareDates(date)) {
dateUtil1 = date;
dateUtil2 = this;
}

int days;
int leapYearNum = 0;
for (int i = dateUtil1.getYear(); i < dateUtil2.getYear(); i++) {
if (isLeapYear(i)) {
leapYearNum++;
}
}

days = 365 * (dateUtil2.getYear() - dateUtil1.getYear()) + leapYearNum;

int d1 = mon[dateUtil1.getMonth() - 1] + dateUtil1.getDay() + (dateUtil1.getMonth() > 2 && isLeapYear(dateUtil1.getYear()) ? 1 : 0);
int d2 = mon[dateUtil2.getMonth() - 1] + dateUtil2.getDay() + (dateUtil2.getMonth() > 2 && isLeapYear(dateUtil2.getYear()) ? 1 : 0);
return days - d1 + d2;
}

public String showDate()//以“year-month-day”格式返回日期值
{
return year + "-" + month + "-" + day;
}
}

7-6 实现图形接口及多态性 (30 分)
 

编写程序,使用接口及类实现多态性,类图结构如下所示:

类图.jpg

其中:

  • GetArea为一个接口,无属性,只有一个GetArea(求面积)的抽象方法;
  • Circle及Rectangle分别为圆类及矩形类,分别实现GetArea接口
  • 要求:在Main类的主方法中分别定义一个圆类对象及矩形类对象(其属性值由键盘输入),使用接口的引用分别调用圆类对象及矩形类对象的求面积的方法,直接输出两个图形的面积值。(要求只保留两位小数)

 

 

 

 

 

import java.util.Scanner;
import java.util.Arrays;


//GetArea类
interface GetArea{
public double Mj();
}
//Circle类
class Circle implements GetArea{
private double radius;
//创建无参构造方法
public Circle(){
}
//创建带参构造方法
public Circle(double radius){
this.radius=radius;
}
//getter
public double getRadius(){
return radius;
}
//setter
public void setRadius(double radius){
this.radius = radius;
}
//计算圆面积
public double Mj(){
return Math.PI*radius*radius;
}
}
//Rectangle类
class Rectangle implements GetArea{
double width,length;
//创建无参构造方法
public Rectangle(){
}
//创建带参构造方法
public Rectangle(double width, double length){
this.width=width;
this.length=length;
}
//getter
public double A(){
return width;
}
public double B(){
return length;
}
//setter
public void setA(double a){
this.width=width;
}
public void setB(double b){
this.length=length;
}
//计算矩形面积
public double Mj(){
return width*length;
}
}

//主类
public class Main{
public static void main(String[] args){
Scanner x = new Scanner(System.in);
double ybj,jxc,jxk;
double yl,kp;
ybj=x.nextDouble();
jxc=x.nextDouble();
jxk=x.nextDouble();

if(ybj<=0||jxc<=0||jxk<=0){//如果不合法
System.out.println("Wrong Format");
System.exit(0);
}
GetArea yuanmianji=new Circle(ybj);
GetArea juxingmianji=new Rectangle(jxc,jxk);
yl=yuanmianji.Mj();
kp=juxingmianji.Mj();
System.out.printf("%.2f\n",yl);
System.out.printf("%.2f\n",kp);
}
}

  两种聚合的比较:第一种是用功能类当做聚集类,第二种是在外面写一类来当聚集类,两种都体现了可重用性,当相比之下,第二种更加的清晰明了点,关系不是太复杂

 

 

正则表达式是由普通字符(例如字符 a 到 z)以及特殊字符(称为"元字符")组成的文字模式。模式描述在搜索文本时要匹配的一个或多个字符串。正则表达式作为一个模板,将某个字符模式与所搜索的字符串进行匹配。

数字:^[0-9]*$
n位的数字:^\d{n}$
至少n位的数字:^\d{n,}$
m-n位的数字:^\d{m,n}$
零和非零开头的数字:^(0|[1-9][0-9]*)$
非零开头的最多带两位小数的数字:^([1-9][0-9]*)+(\.[0-9]{1,2})?$
带1-2位小数的正数或负数:^(\-)?\d+(\.\d{1,2})$
正数、负数、和小数:^(\-|\+)?\d+(\.\d+)?$
有两位小数的正实数:^[0-9]+(\.[0-9]{2})?$
有1~3位小数的正实数:^[0-9]+(\.[0-9]{1,3})?$
非零的正整数:^[1-9]\d*$ 或 ^([1-9][0-9]*){1,3}$ 或 ^\+?[1-9][0-9]*$
非零的负整数:^\-[1-9][]0-9"*$ 或 ^-[1-9]\d*$
非负整数:^\d+$ 或 ^[1-9]\d*|0$
非正整数:^-[1-9]\d*|0$ 或 ^((-\d+)|(0+))$
非负浮点数:^\d+(\.\d+)?$ 或 ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$
非正浮点数:^((-\d+(\.\d+)?)|(0+(\.0+)?))$ 或 ^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$
正浮点数:^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ 或 ^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$
负浮点数:^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ 或 ^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$
浮点数:^(-?\d+)(\.\d+)?$ 或 ^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$

汉字:^[\u4e00-\u9fa5]{0,}$

英文和数字:^[A-Za-z0-9]+$ 或 ^[A-Za-z0-9]{4,40}$
长度为3-20的所有字符:^.{3,20}$
由26个英文字母组成的字符串:^[A-Za-z]+$
由26个大写英文字母组成的字符串:^[A-Z]+$
由26个小写英文字母组成的字符串:^[a-z]+$
由数字和26个英文字母组成的字符串:^[A-Za-z0-9]+$
由数字、26个英文字母或者下划线组成的字符串:^\w+$ 或 ^\w{3,20}$
中文、英文、数字包括下划线:^[\u4E00-\u9FA5A-Za-z0-9_]+$
中文、英文、数字但不包括下划线等符号:^[\u4E00-\u9FA5A-Za-z0-9]+$ 或 ^[\u4E00-\u9FA5A-Za-z0-9]{2,20}$
  可以输入含有^%&',;=?$\"等字符:[^%&',;=?$\x22]+
  禁止输入含有~的字符:[^~\x22]+

 

 

题目集5(7-4)中Java集合框架应用的分析总结

 

Set和List对比:

Set:检索元素效率低下,删除和插入效率高,插入和删除不会引起元素位置改变。

List:和数组类似,List可以动态增长,查找元素效率高,插入删除元素效率低,因为会引起其他元素位置改变。

 

(3)采坑心得

 

 

 前n天后n天测试未通过

 

 4.改进建议:对相应题目的编码改进给出自己的见解,做到可持续改进

有很多地方应当可以更加简洁,下次注意,使得自己的代码可以更加的简单,更加的有用

学新东西的方式出现了问题,应该循序渐进,不能操之过急。

5.总结

学到了一些正则表达式的知识点以,学习到了类与类之间的关系,.对字符串有了更深的理解

 

posted on 2021-11-13 22:32  192072  阅读(26)  评论(0编辑  收藏  举报