题目集4~6的总结性Blog
知识点涉及:基本程序的设计,选择结构,字符和字符串,循环,基本方法调用,数组,对象和类。
难度:一般~中等
题目集目录: 1.PTA题目集4(7-2)
2.PTA题目集4(7-3)
3.PTA题目集5(7-4)
4.PTA题目集6(7-5)
5.PTA题目集6(7-6)
正文:
参考题目7-2的要求,设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1900,2050] ,month∈[1,12] ,day∈[1,31] , 设计类图如下:

应用程序共测试三个功能:
- 求下n天
- 求前n天
- 求两个日期相差的天数
注意:严禁使用Java中提供的任何与日期相关的类与方法,并提交完整源码,包括主类及方法(已提供,不需修改)
输入格式:
有三种输入方式(以输入的第一个数字划分[1,3]):
- 1 year month day n //测试输入日期的下n天
- 2 year month day n //测试输入日期的前n天
- 3 year1 month1 day1 year2 month2 day2 //测试两个日期之间相差的天数
输出格式:
- 当输入有误时,输出格式如下:
Wrong Format - 当第一个数字为1且输入均有效,输出格式如下:
year-month-day - 当第一个数字为2且输入均有效,输出格式如下:
year-month-day - 当第一个数字为3且输入均有效,输出格式如下:
天数值
输入样例1:
在这里给出一组输入。例如:
3 2014 2 14 2020 6 14
输出样例1:
在这里给出相应的输出。例如:
2312
输入样例2:
在这里给出一组输入。例如:
2 1935 2 17 125340
输出样例2:
在这里给出相应的输出。例如:
1591-12-17
输入样例3:
在这里给出一组输入。例如:
1 1999 3 28 6543
输出样例3:
在这里给出相应的输出。例如:
2017-2-24
输入样例4:
在这里给出一组输入。例如:
0 2000 5 12 30
输出样例4:
在这里给出相应的输出。例如:
Wrong Format
题目分析:各个类之间用的聚合关系进行联系,代码主要难点实现在各数据的边界值需根据是否是闰年等条件进行判断。
代码部分:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
String num=input.nextLine();
}
}
class Year
{
private 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(int year)
{
if((year%400==0)||(year%4==0&&year%100!=0))
return true;
else
return false;
}
public boolean validate(int year)
{
if (year>=1900&&year<=2050)
return true;
else
return false;
}
public void yearIncrement()
{
value++;
}
public void yearReduction()
{
value--;
}
}
class Month
{
private int value;
private Year year;
public Month()
{
;
}
public Month(int yearValue,int monthValue)
{
Year year=new Year();
value=monthValue;
year.setValue(yearValue);
}
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;
year.yearIncrement();
}
public void resetMax()
{
value = 12;
year.yearReduction();
}
public boolean Validate()
{
if (value>=1&&value<=12)
return true;
else
return false;
}
public void monthIncrement()
{
value++;
}
public void monthReduction()
{
value--;
}
}
class Day
{
private int value;
private Month month;
private int []mon_maxnum= {31,28,31,30,31,30,31,31,30,31,30,31};
public int[] getMon_maxnum() {
return mon_maxnum;
}
public void setMon_maxnum(int[] mon_maxnum) {
this.mon_maxnum = mon_maxnum;
}
public Day()
{
;
}
public Day(int yearValue,int monthValue,int dayValue)
{
Month month=new Month();
value=dayValue;
month.getYear().setValue(yearValue);
month.setValue(monthValue);
}
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()
{
if (month.getYear().isLeapYear(month.getYear().getValue())==true)
mon_maxnum[1]=29;
else
mon_maxnum[1]=28;
value= 1;
month.monthIncrement();
}
public void resetMax()
{
if (month.getYear().isLeapYear(month.getYear().getValue())==true)
mon_maxnum[1]=29;
else
mon_maxnum[1]=28;
value = mon_maxnum[month.getValue()-1];
month.monthReduction();
}
public boolean validate()
{
if (getMonth().getYear().getValue()>=1900&&getMonth().getYear().getValue()<=2050)
{
if (getMonth().getValue()>=1&&getMonth().getValue()<=12)
{
if (getValue()>=1&&getValue()<=mon_maxnum[getMonth().getValue()])
{
return true;
}
}
}
return false;
}
public void dayIncrement()
{
value++;
}
public void dayReduction()
{
value--;
}
}
class DateUtil
{
private Day day;
public DateUtil()
{
}
public DateUtil(int d, int m, int y)
{
this.setDay(new Day(y, m ,d));
}
public Day getDay()
{
return this.day;
}
public void setDay(Day day)
{
this.day = day;
}
/*public boolean cheakInputValidity()
{
if (this.day.getMonth().getYear().validate() && this.day.getMonth().validate() && this.day.validate())
{
return true;
}
else
{
return false;
}
}*/
public boolean compareDates(DateUtil date)
{
if (this.day.getMonth().getYear().getValue() > date.getDay().getMonth().getYear().getValue())
{
return false;
}
else if (this.day.getMonth().getYear().getValue() == date.getDay().getMonth().getYear().getValue())
{
if (this.day.getMonth().getValue() > date.getDay().getMonth().getValue())
{
return false;
}
else if (this.day.getMonth().getValue() == date.getDay().getMonth().getValue())
{
if (this.day.getValue() > date.getDay().getValue())
{
return false;
}
else
{
return true;
}
}
else
{
return true;
}
}
else
{
return true;
}
}
public boolean equalTwoDates(DateUtil date)
{
if (this.day.getMonth().getYear().getValue() == date.getDay().getMonth().getYear().getValue())
{
if (this.day.getMonth().getValue() == date.getDay().getMonth().getValue())
{
if (this.day.getValue() == date.getDay().getValue())
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
}
public String showDate()
{
String sD;
sD = this.day.getMonth().getYear().getValue()+"-" + this.day.getMonth().getValue() + "-" + this.day.getValue();
return sD;
}
public DateUtil getNextNDays(int n)
{
while(n > 0)
{
this.day.dayIncrement();
if (this.day.getMonth().getYear().isLeapYear(this.day.getMonth().getYear().getValue()))
{
if (this.day.getMonth().getValue() == 2)
{
if (this.day.getValue() > this.day.getMon_maxnum()[this.day.getMonth().getValue() - 1] + 1)
{
this.day.getMonth().monthIncrement();
if (this.day.getMonth().getValue() > 12)
{
this.day.getMonth().getYear().yearIncrement();
this.day.getMonth().resetMin();
}
this.day.resetMin();
}
}
else
{
if (this.day.getValue() > this.day.getMon_maxnum()[this.day.getMonth().getValue() - 1])
{
this.day.getMonth().monthIncrement();
if (this.day.getMonth().getValue() > 12)
{
this.day.getMonth().getYear().yearIncrement();
this.day.getMonth().resetMin();
}
this.day.resetMin();
}
}
}
else
{
if (this.day.getValue() > this.day.getMon_maxnum()[this.day.getMonth().getValue() - 1])
{
this.day.getMonth().monthIncrement();
if (this.day.getMonth().getValue() > 12)
{
this.day.getMonth().getYear().yearIncrement();
this.day.getMonth().resetMin();
}
this.day.resetMin();
}
}
n--;
}
return this;
}
public DateUtil getPreviousNDays(int n)
{
while(n > 0)
{
this.day.dayReduction();
if (this.day.getValue() < 1)
{
this.day.getMonth().monthReduction();
if (this.day.getMonth().getValue() < 1)
{
this.day.getMonth().getYear().yearReduction();
this.day.getMonth().resetMax();
}
this.day.resetMax();
}
n--;
}
return this;
}
public int getDaysofDates(DateUtil date)
{
int count = 0;
while(true)
{
if (equalTwoDates(date))
{
break;
}
date.day.dayReduction();
if (date.day.getValue() < 1)
{
date.day.getMonth().monthReduction();
if (date.day.getMonth().getValue() < 1)
{
date.day.getMonth().getYear().yearReduction();
date.day.getMonth().resetMax();
}
date.day.resetMax();
}
count ++;
}
return count;
}
}
编写程序,实现图形类的继承,并定义相应类对象并进行测试。
- 类Shape,无属性,有一个返回0.0的求图形面积的公有方法
public double getArea();//求图形面积 - 类Circle,继承自Shape,有一个私有实型的属性radius(半径),重写父类继承来的求面积方法,求圆的面积
- 类Rectangle,继承自Shape,有两个私有实型属性width和length,重写父类继承来的求面积方法,求矩形的面积
- 类Ball,继承自Circle,其属性从父类继承,重写父类求面积方法,求球表面积,此外,定义一求球体积的方法
public double getVolume();//求球体积 - 类Box,继承自Rectangle,除从父类继承的属性外,再定义一个属性height,重写父类继承来的求面积方法,求立方体表面积,此外,定义一求立方体体积的方法
public double getVolume();//求立方体体积 - 注意:
- 每个类均有构造方法,且构造方法内必须输出如下内容:
Constructing 类名 - 每个类属性均为私有,且必须有getter和setter方法(可用Eclipse自动生成)
- 输出的数值均保留两位小数
主方法内,主要实现四个功能(1-4): 从键盘输入1,则定义圆类,从键盘输入圆的半径后,主要输出圆的面积; 从键盘输入2,则定义矩形类,从键盘输入矩形的宽和长后,主要输出矩形的面积; 从键盘输入3,则定义球类,从键盘输入球的半径后,主要输出球的表面积和体积; 从键盘输入4,则定义立方体类,从键盘输入立方体的宽、长和高度后,主要输出立方体的表面积和体积;
假如数据输入非法(包括圆、矩形、球及立方体对象的属性不大于0和输入选择值非1-4),系统输出Wrong Format
输入格式:
共四种合法输入
- 1 圆半径
- 2 矩形宽、长
- 3 球半径
- 4 立方体宽、长、高
输出格式:
按照以上需求提示依次输出
输入样例1:
在这里给出一组输入。例如:
1 1.0
输出样例1:
在这里给出相应的输出。例如:
Constructing Shape
Constructing Circle
Circle's area:3.14
输入样例2:
在这里给出一组输入。例如:
4 3.6 2.1 0.01211
输出样例2:
在这里给出相应的输出。例如:
Constructing Shape
Constructing Rectangle
Constructing Box
Box's surface area:15.26
Box's volume:0.09
输入样例3:
在这里给出一组输入。例如:
2 -2.3 5.110
输出样例2:
在这里给出相应的输出。例如:
Wrong Format
代码分析:
![]()
代码:
import java.util.Scanner;
import java.lang.Math;
public class Main
{
public static void main(String[] args )
{
Scanner input=new Scanner(System.in);
int num=input.nextInt();
if(num==1)
{
double a=input.nextDouble();
if(a<=0)
System.out.println("Wrong Format");
else
{
Circle circle=new Circle(a);
System.out.printf("Circle's area:%.2f\n",circle.getArea());
}
}
else if(num==2)
{
double b=input.nextDouble();
double c=input.nextDouble();
if(b<=0||c<=0)
System.out.println("Wrong Format");
else
{
Rectangle rectangle=new Rectangle(b,c);
System.out.printf("Rectangle's area:%.2f\n",rectangle.getArea());
}
}
else if(num==3)
{
double d=input.nextDouble();
if(d<=0)
System.out.println("Wrong Format");
else
{
Ball ball=new Ball(d);
System.out.printf("Ball's surface area:%.2f\nBall's volume:%.2f\n",ball.getArea(),ball.getVolume());
}
}
else if(num==4)
{
double a=input.nextDouble();
double b=input.nextDouble();
double c=input.nextDouble();
if(a<=0||b<=0||c<=0)
System.out.println("Wrong Format");
else
{
Box box=new Box(a,b,c);
System.out.printf("Box's surface area:%.2f\nBox's volume:%.2f\n",box.getArea(),box.getVolume());
}
}
else
{
System.out.println("Wrong Format");
}
}
}
class Shape
{
public Shape()
{
System.out.println("Constructing Shape");
}
public double getArea()
{
return 0.0;
}
}
class Circle extends Shape
{
private double radius;
public Circle()
{
System.out.println("Constructing Circle");
}
public Circle(double radius)
{
setter(radius);
System.out.println("Constructing Circle");
}
public double getArea()
{
return (radius*radius*Math.PI);
}
public double getter()
{
return radius;
}
public void setter(Double radius)
{
this.radius=radius;
}
}
class Rectangle extends Shape
{
private double width;
private double length;
public Rectangle()
{
System.out.println("Constructing Rectangle");
}
public Rectangle(double width,double length)
{
setLength(length);
setWidth(width);
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 double getArea()
{
return width*length;
}
}
class Ball extends Circle
{
public Ball()
{
System.out.println("Constructing Ball");
}
public Ball(double radius)
{
setter(radius);
System.out.println("Constructing Ball");
}
public double getArea()
{
return (4*Math.PI*super.getter()*super.getter());
}
public double getVolume()
{
return (4/3.0*Math.PI*super.getter()*super.getter()*super.getter());
}
}
class Box extends Rectangle
{
private double height;
public Box()
{
System.out.println("Constructing Box");
}
public Box(double height,double width,double length)
{
this.height=height;
setLength(length);
setWidth(width);
System.out.println("Constructing Box");
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getArea()
{
return 2*(height*super.getLength()+height*super.getWidth()+super.getLength()*super.getWidth());
}
public double getVolume()
{
return height*super.getLength()*super.getWidth();
}
}
掌握类的继承、多态性及其使用方法。具体需求参见作业指导书。
输入格式:
从键盘首先输入三个整型值(例如a b c),分别代表想要创建的Circle、Rectangle及Triangle对象的数量,然后根据图形数量继续输入各对象的属性值(均为实型数),数与数之间可以用一个或多个空格或回车分隔。
输出格式:
- 如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边关系),则输出
Wrong Format。 - 如果输入合法,则正常输出,输出内容如下(输出格式见输入输出示例):
- 各个图形的面积;
- 所有图形的面积总和;
- 排序后的各个图形面积;
- 再次所有图形的面积总和。
输入样例1:
在这里给出一组输入。例如:
1 1 1 2.3 3.2 3.2 6.5 3.2 4.2
输出样例1:
在这里给出相应的输出。例如:
Original area:
16.62 10.24 5.68
Sum of area:32.54
Sorted area:
5.68 10.24 16.62
Sum of area:32.54
输入样例2:
在这里给出一组输入。例如:
0 2 2 2.3 2.5 56.4 86.5 64.3 85.6 74.6544 3.2 6.1 4.5
输出样例2:
在这里给出相应的输出。例如:
Original area:
5.75 4878.60 2325.19 7.00
Sum of area:7216.54
Sorted area:
5.75 7.00 2325.19 4878.60
Sum of area:7216.54
输入样例3:
在这里给出一组输入。例如:
0 0 1 3 3 6
输出样例3:
在这里给出相应的输出。例如:
Wrong Format
代码分析:
![]()
代码:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
int num1=input.nextInt();
int num2=input.nextInt();
int num3=input.nextInt();
if(num1<0||num2<0||num3<0)
{
System.out.println("Wrong Format");
System.exit(0);
}
Circle []a=new Circle[num1];
Rectangle []b=new Rectangle[num2];
Triangle []c=new Triangle[num3];
int j=0;
double sum=0;
double []d=new double[num1+num2+num3];
if(num1>=0&&num2>=0&&num3>=0)
{
for(int i=0;i<a.length;i++,j++)
{
a[i]=new Circle();
a[i].setRadius(input.nextDouble());
if(a[i].validate()==false)
{
System.out.println("Wrong Format");
System.exit(0);
}
//System.out.println("Original area:");
//System.out.printf("%.2f ",a[i].getArea());
d[j]=a[i].getArea();
}
for(int i=0;i<b.length;i++,j++)
{
b[i]=new Rectangle();
b[i].setLength(input.nextDouble());
b[i].setWidth(input.nextDouble());
if(b[i].validate()==false)
{
System.out.println("Wrong Format");
System.exit(0);
}
//System.out.printf("%.2f ",b[i].getArea());
d[j]=b[i].getArea();
}
for(int i=0;i<c.length;i++,j++)
{
c[i]= new Triangle();
c[i].setSide1(input.nextDouble());
c[i].setSide2(input.nextDouble());
c[i].setSide3(input.nextDouble());
if(c[i].validate()==false)
{
System.out.println("Wrong Format");
System.exit(0);
}
//System.out.printf("%.2f ",c[i].getArea());
d[j]=c[i].getArea();
}
//System.out.print("\n");
for(int i=0;i<d.length;i++)
sum+=d[i];
//System.out.printf("Sum of area:%.2f\nSorted area:\n",sum);
Arrays.sort(d);
//System.out.print("\n");
//System.out.printf("Sum of area:%.2f",sum);
}
else
{
System.out.println("Wrong Format");
System.exit(0);
}
System.out.println("Original area:");
for(int i=0;i<a.length;i++,j++)
System.out.printf("%.2f ",a[i].getArea());
for(int i=0;i<b.length;i++,j++)
System.out.printf("%.2f ",b[i].getArea());
for(int i=0;i<c.length;i++,j++)
System.out.printf("%.2f ",c[i].getArea());
System.out.print("\n");
System.out.printf("Sum of area:%.2f\nSorted area:\n",sum);
for(int i=0;i<d.length;i++)
System.out.printf("%.2f ",d[i]);
System.out.print("\n");
System.out.printf("Sum of area:%.2f",sum);
input.close();
}
}
abstract class Shape
{
abstract public double getArea();
abstract public boolean validate();
@Override
public String toString()
{
return "";
}
}
class Circle extends Shape
{
private double radius;
public Circle()
{
;
}
public Circle(double radius)
{
this.radius=radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea()
{
double s=radius*radius*Math.PI;
return s;
}
public boolean validate()
{
boolean a=true;
if(radius<=0)
{
a=false;
}
return a;
}
public String toString()
{
return "Area is"+this.getArea();
}
}
class Rectangle
{
private double width;
private double length;
public Rectangle()
{
;
}
public Rectangle(double width,double length)
{
this.length=length;
this.width=width;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getArea()
{
double s=length*width;
return s;
}
public boolean validate()
{
boolean a=true;
if(length<0||width<0)
{
a=false;
}
return a;
}
public String toString()
{
return "Area is"+this.getArea();
}
}
class Triangle
{
private double side1;
private double side2;
private double side3;
public Triangle()
{
;
}
public Triangle(double side1,double side2,double side3)
{
this.side1=side1;
this.side2=side2;
this.setSide3(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;
}
public double getArea()
{
double s = (side1 + side2 + side3)/2f;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
public boolean validate()
{
boolean a=true;
if(side1<0||side2<0||side3<0||(side1+side2<=side3)||(side1+side3<=side2)||(side3+side2<=side1))
{
a=false;
}
return a;
}
public String toString()
{
return "Area is"+this.getArea();
}
}
编写程序,使用接口及类实现多态性,类图结构如下所示:

其中:
- GetArea为一个接口,无属性,只有一个GetArea(求面积)的抽象方法;
- Circle及Rectangle分别为圆类及矩形类,分别实现GetArea接口
- 要求:在Main类的主方法中分别定义一个圆类对象及矩形类对象(其属性值由键盘输入),使用接口的引用分别调用圆类对象及矩形类对象的求面积的方法,直接输出两个图形的面积值。(要求只保留两位小数)
输入格式:
从键盘分别输入圆的半径值及矩形的宽、长的值,用空格分开。
输出格式:
- 如果输入的圆的半径值及矩形的宽、长的值非法(≤0),则输出
Wrong Format - 如果输入合法,则分别输出圆的面积和矩形的面积值(各占一行),保留两位小数。
输入样例1:
在这里给出一组输入。例如:
2 3.6 2.45
输出样例1:
在这里给出相应的输出。例如:
12.57
8.82
输入样例2:
在这里给出一组输入。例如:
9 0.5 -7.03
输出样例2:
在这里给出相应的输出。例如:
Wrong Format
代码总结性分析:
题目集4(7-2)、题目集5(7-4)两种日期类聚合设计的优劣比较:
第一种设计简单易懂但是代码相互联系太紧密不易修改,第二种易于修改
正则表达式技术的分析总结
实例 描述
[Pp]ython 匹配 “Python” 或 “python”。
rub[ye] 匹配 “ruby” 或 “rube”。
[abcdef] 匹配中括号内的任意一个字母。
[0-9] 匹配任何数字。类似于 [0123456789]。
[a-z] 匹配任何小写字母。
[A-Z] 匹配任何大写字母。
[a-zA-Z0-9] 匹配任何字母及数字。
[^au] 除了au字母以外的所有字符。
[^0-9] 匹配除了数字外的字符。
实例 描述
. 匹配除 “\n” 之外的任何单个字符。要匹配包括 ‘\n’ 在内的任何字符,请使用象 ‘[.\n]’ 的模式。
? 匹配一个字符零次或一次,另一个作用是非贪婪模式
+ 匹配1次或多次
* 匹配0次或多次
\b 匹配一个长度为0的子串
\d 匹配一个数字字符。等价于 [0-9]。
\D 匹配一个非数字字符。等价于 [^0-9]。
\s 匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]。
\S 匹配任何非空白字符。等价于 [^ \f\n\r\t\v]。
\w 匹配包括下划线的任何单词字符。等价于’[A-Za-z0-9_]’。
\W 匹配任何非单词字符。等价于 ‘[^A-Za-z0-9_]‘。
\b 匹配一个长度为0的子串


浙公网安备 33010602011771号