对JAVA(PTA-4~6)的简单总结()
JAVA Pta(4~6)的总结(第二次)
前言:
这一次的题目和上次比简直难多了,对于此次的题目,我感觉主要是对于继承的使用以及多态的使用。难度很大,认真学了一段时间才会,题目量比以前少,有几个题目集比较多但是比较简单。
我觉得啊,上课听老师说才感觉有点思路,而且感觉写法也会规范一点。
我觉得就这几点,我来总结一下这几次题目我的看法收获以及建议等等。
PTA4(7-2)

按照题目所给的类,进行代码的编写,主要是这个题目给出了类图,所以思路已经不用去想了,只要写一下main函数就可以了
这个题目中要注意的是运行超时的问题,我是这样解决的

只要检测到输入的值大于366就直接减去就行,比以前的那个少了366次的循环这就不容易超时了。
还有的是那个越界的问题,比如求前N天,我们只要考虑到不管是多少只要他天数改变我们就+1天,然后再去检测因为天数的增加而引发的结果,比如月数是否+1,
然后又因为月数的+1是否会影响到年数+1,就这几个点。求前N,后N天都是一个道理。
顺便说一下两个日期之间差了多少天,就是去比较两个数的大小,然后将大的数减一或者选出小的数循环加一,直到两个数相等,运行了多少次,就相隔多少天。
踩坑心得:
这个题目就是容易超时,我一开始还单纯得改了一下,发现改完以后天数对不上号,然后我就放弃使用年数*每年天数得算法,改为老老实实一天一天走,一年一年的减去。
这样想代码也简单,思路也简单,纠错也方便。
主要还是不够细节。
PTA5(7-5)

这个题目的要求和题目PTA4(7-2)是完全一样的,不同的在于,这个题目是将DateUtil作为一个YEAR,MONTH,DAY的共同父类
这个编写起来思路很清晰,而且创建YEAR类的时候不用去创建其他的几个类,这样的好处就是写起来快点,写的这个类不容易被其他类的错误操作而改变
但是坏处就是创建类的时候数据会有很多的多余,比如说创建YEAR时候会同时有其他month和day的相同方法。
对于题目PTA4(7-2)来说,创建year类的时候必须有其他的两个类,而且在对象的使用和改变时容易出错,操作起来很繁杂,容易不知道自己操作到哪里去了
开始我还是比较😵的,没看清楚类之间箭头的不同,还以为是同一个题目,随后看了一下,发现还是有很多的不同。
但是也只是在编写代码的结构不同,解题的思路都是相同的。
踩坑心得:同上
PTA4(7-3)
这一道,图像的编辑题目,我觉得就是按照前面的日期的类的创建一样,都是一个思路,
先找到关键次,然后对关键词进行类的创建,比如,圆,矩形之类的,然后洗一个Main对他们进行统一的编写,以及创建和调用。

类图太难搞了,我就直接粘贴代码的缩写上去了、
创建一个 SHPAE类,将其他的类的共同属性和方法放在里边,这样就减少了每个类的重复编写,还有对他们属性的封装,我直接就加了一个PRAVITE去保护他们
然后多态的体现就在于他们同属于shape类,但是却有ball,box,rectangle,等的区别
还有就是,对于每个子类他们的输出方法都是不同的,我就根据所需去编写,
对于这道题目思路的坑,
我说说我遇到的难点
踩坑心得:
比如说就是我已经输入了创建的图像个数和一些周长和半径的信息(但是还没有输入完),这个时候的控制台就已经打印出了Constructing Shape这句话
但是随后我输入的信息是错误的,所以控制台就只应该打印出Wrong Format,这时就矛盾了,所以我想了一下
我应该先把要输入的信息全部输入,然后再去创建种类的个数以及信息。而不是一边创建一边输入、
如此一来问题解决。
PTA6(7-5)
这个题目可以说是图像题目里边难度最大的一个
我先给出题目所给的类图
但是不完全

看起来很简单,看起来很少。、
再来看看题目的要求

这个题目的坑很多,我说一说,我写的类,就是一个shape类对其他类的统一管理,然后再对你所创建的shape类进行排序求和在排序。
先说解题思路,然后再统一说其他的收获(ex的点)
这个题目我是先创建一个SHPAE数组,将所创建的圆形数组,矩形数组,三角数组,放在里边,
也就是这样
然后flg就是一个标志位,一旦有错误,后边直接wrong format.
将他们一起放在shape里边,对于排序,求和也就是一些基础的问题。
最后就是打印信息的问题
踩坑心得:
我开始一直都是格式错误,然后改掉了但是还有一个测试点没有过

就这个,我仔细检查了半天,原来是没有数据的时候也要打印一行。

也就是这样
服了
感觉太讲究这个格式问题了。
PTA6(7-6)
这个题目就比较简单了,主要是写一个接口的问题
这是题目所给的类图

这道题的接口也就是GETAREA,我们只要写一下接口就完成
这个题目比前面两次都要温柔许多。没有什么特别难的地方
对于这三个题目的设计,我觉得第二题目对人的考验(折磨)比较大,三者的创建方式都差不多,多态的使用也没啥区别,主要是我们在输入以及输出的方式以及检测题目错误
的地方有些不同,然后就是我写完这三道题感觉就是在体验和思考这不同的方式完成同一件事情,并且当你写的越完善的时候,你就会对这个题目的思考和优化有更多的理解
踩坑心得:
同上。
。
PTA4(7-1)
水文检测,这道题目是这些题目里边难度最大的一个,我给出题目类图

这个题目就是麻烦在正则表达式的使用,
在这个题目里边,我说一下解题的思路和流程
我们先在一个字符串里边,添加我们的水文信息,然后将信息一条一条的在CHECKDATA里边检验
第一条进来时,数组行号是数组下标加一,因为在输出格式中列号是不会改变的,只用改变行号就行
检验规则是先用spilt “|”将他们分成几个部分,然后将第一部分的信息(也就是日期)去和正则表达式比较
又通过spilt的“/”将年月日分开,然后将年月日的信息送入MATCHER判断是否合法,随后来一个标志位进行标志是否合法,不合法就直接输出wrong。
随后将第二部分 也就是目标水位和实际水位,先是和正则表达式进行MATCHER判断,是否合法。
不合法的话
就输出行号和列号,最后输出错误的水文信息。
然后对字符串的目标开度和实际开度用spilt“/’分开,
分别放入MATCHER,如果不合法就打印出行号和列号,最后打印错误信息。
最后是对流量进行处理,先和正则MATCHER进行对比,不满足就直接打印行号和列号,
对于每一条信息最后是都是要每个环节都没有错才返回TRUE只要有一个错误,就打印这条信息
对于信息没有错误,然后创建一个水位信息数组,将每一条信息依次放入数组里边,
然后开始检查实际开度有无大于目标开度,就是将数组中每一条信息的实际和目的开度关系,如果有则打印数组下标+1的信息。
没有就下一条
求实际最大水位就是把数组的每个元素的最大实际水位进行比较,取个最大值
流量就是将数组的每个元素的流量按照公式计算最后加在一起。
这道题也就完成了,主要就是正则表达中Pattern和matcher的使用,这个我是去上网看了一下才理解的。
还有一些琐碎的打印我就不讲了,和上边的一样。
踩坑心得:
这道题目属实让人难受,我开始得时候没去注意那个小数点后几位得问题,还傻乎得去写了,没小数点和有小数点得情况
然后我突然看到“?”这个符号得意思然后就明白了,只用这样写
([1-9][0-9]{0,2}(\\.[0-9]{1,3})?)
就可区别出小数点得情况了。
还有就是在判断,文本是否合法得时候matcher的方法我写成了m1.find(),然后发现不能解决问题,感觉出现不合法的信息没有给我拦截下来,也米有报错。然后我又去网上找资料,
发现find()不能给你纠错,只能给我检测是否是数字。只有m1.mathces()才可以检测出来文本是否符合你所给的正则表达式。
PTA5(7-4)
这个题目就是正则表达式的普普通通检测了,将文本的信息放入字符串然后将其和关键字的数组进行比对和计数了。打印就行
这个题目也就是表达式的书写问题,其实只要我们想的情况多的话,问题就会越简单,代码也就越少。
不在赘述
对于PTA6里边的正则表达式我也不再赘述,最难的水文检测我也说太多饿了(有点懒),
踩坑心得
同上,主要问题我也说明了。
改进建议
统一说一下改进建议,其实检测的地方已经够细致了,主要是题目考察的特色不够鲜明,难度建议降低一点。
总结
我觉得还是自己太菜了,没有认真的学习知识,导致写题目的时候一些基本的语法和构造方式都不太会,还要去搜索,然后就是觉得老师很好
自己太拉了。以后我会加强对知识的学习。
此处附上源码(有几道题就没粘贴了)
7-2 日期问题面向对象设计(聚合一):
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int year=0,month=0,day=0,a,b;
a=input.nextInt();
year=input.nextInt();month= input.nextInt();day=input.nextInt();
if(year>=1900&&year<=2050&&month>=1&&month<=12&&day>=1&&day>=1&&day<=31){
DateUtil c=new DateUtil(year,month,day);
if(a==1){
b=input.nextInt();
if(!c.checkInputValidity()||b<0){
System.out.println("Wrong Format");
}
else {
System.out.println(c.getNextNDays(b).showDate());
}
}
else if(a==2){
b=input.nextInt();
if(!c.checkInputValidity()||b<0){
System.out.println("Wrong Format");
}
else {
System.out.println(c.getPreviousNDays(b).showDate());
}
}
else if(a==3){
int y1,m1,d1;
y1=input.nextInt();m1= input.nextInt();d1=input.nextInt();
DateUtil d=new DateUtil(y1,m1,d1);
if(!c.checkInputValidity()||!d.checkInputValidity()){
System.out.println("Wrong Format");
}
else {
System.out.println(c.getDaysofDates(d));
}
}
else {
System.out.println("Wrong Format");
}
}
else {
System.out.println("Wrong Format");
}
}
static class DateUtil {
Day day;
DateUtil() {
}
DateUtil(int y, int m, int d) {
this.day = new Day(y, m, d);
}
public Day getDay() {
return day;
}
public void setDay(Day day) {
this.day = day;
}
boolean checkInputValidity() {
if (day.vaildate() && this.getDay().getMonth().validate() && this.getDay().getMonth().getYear().vailDate()) {
return true;
} else {
return false;
}
}
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;
}
}
boolean equalTwoDates(DateUtil date) {
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 true;
} else {
return false;
}
}
String showDate() {
return this.getDay().getMonth().getYear().getValue() + "-" + this.getDay().getMonth().getValue() + "-" + this.getDay().getValue();
}
DateUtil getNextNDays(int n) {
int d = 0, m = 0, y = 0;
while (n > 0) {
this.getDay().dayIncrement();
if (!this.getDay().vaildate()) {
this.getDay().getMonth().monthIncrement();
if (!this.getDay().getMonth().validate()) {
this.getDay().getMonth().restMin();
this.getDay().getMonth().getYear().yearIncrement();
}
this.getDay().resetMin();
}
n--;
}
d = this.getDay().getValue();
m = this.getDay().getMonth().getValue();
y = getDay().getMonth().getYear().getValue();
return new DateUtil(y, m, d);
}
DateUtil getPreviousNDays(int n) {
int d = 0, m = 0, y = 0;
while (n > 0) {
this.getDay().datReduction();
if (!this.getDay().vaildate()) {
this.getDay().getMonth().monthReduction();
if (!this.getDay().getMonth().validate()) {
this.getDay().getMonth().restMax();
this.getDay().getMonth().getYear().yearReduction();
}
this.getDay().resetMax();
}
n--;
}
d = this.getDay().getValue();
m = this.getDay().getMonth().getValue();
y = getDay().getMonth().getYear().getValue();
return new DateUtil(y, m, d);
}
int getDaysofDates(DateUtil date){
DateUtil d1=this,d2=date;
int n=0;
if(compareDates(d2)){
d1=date;
d2=this;}
while(!d1.equalTwoDates(d2)){
d2.getDay().dayIncrement();
if (!d2.getDay().vaildate()) {
d2.getDay().getMonth().monthIncrement();
if (!d2.getDay().getMonth().validate()) {
d2.getDay().getMonth().restMin();
d2.getDay().getMonth().getYear().yearIncrement();
}
d2.getDay().resetMin();
}
n++;
}
return n;
}
}
static 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() {
if ((value % 4 == 0 && value % 100 != 0) || value % 400 == 0) {
return true;
}
return false;
}
public boolean vailDate() {
if (value >= 1900 && value <= 2050) {
return true;
}
return false;
}
void yearIncrement() {
value = value + 1;
}
void yearReduction() {
value = value - 1;
}
}
static 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 restMin() {
value = 1;
}
public void restMax() {
value = 12;
}
boolean validate() {
if (value <= 12 && value >= 1) {
return true;
} else {
return false;
}
}
public void monthIncrement() {
value = value + 1;
}
public void monthReduction() {
value = value - 1;
}
}
static class Day {
int value;
Month month;
int[] mon_maxmun = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public Day() {
}
public Day(int yearValue, int monthValue, int dayValue) {
this.value = dayValue;
this.month = new Month(yearValue, 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;
}
void resetMin() {
value = 1;
}
void resetMax() {
value = mon_maxmun[month.getValue() - 1];
}
boolean vaildate() {
mon_maxmun[1]=28;
if (month.year.isLeapYear()) {
mon_maxmun[1] = 29;
}
if (value >= 1 && value <= mon_maxmun[month.getValue() - 1]) {
return true;
} else {
return false;
}
}
void dayIncrement() {
value = value + 1;
}
void datReduction() {
value = value - 1;
}
}
}
7-5 日期问题面向对象设计(聚合二)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int year=0,month=0,day=0,a,b;
a=input.nextInt();
year=input.nextInt();month= input.nextInt();day=input.nextInt();
if(year>=1820&&year<=2020&&month>=1&&month<=12&&day>=1&&day>=1&&day<=31){
DateUtil c=new DateUtil(year,month,day);
if(a==1){
b=input.nextInt();
if(!c.checkInputValidity()||b<0){
System.out.println("Wrong Format");
}
else {
System.out.println(c.showDate()+" next "+b+" days is:"+c.getNextNDays(b).showDate());
}
}
else if(a==2){
b=input.nextInt();
if(!c.checkInputValidity()||b<0){
System.out.println("Wrong Format");
}
else {
System.out.println(c.showDate()+" previous "+b+" days is:"+c.getPreviousNDays(b).showDate());
}
}
else if(a==3){
int y1,m1,d1;
y1=input.nextInt();m1= input.nextInt();d1=input.nextInt();
DateUtil d=new DateUtil(y1,m1,d1);
if(!c.checkInputValidity()||!d.checkInputValidity()){
System.out.println("Wrong Format");
}
else {
System.out.println("The days between "+c.showDate()+" and "+d.showDate()+" are:"+c.getDaysofDates(d));
}
}
else {
System.out.println("Wrong Format");
}
}
else {
System.out.println("Wrong Format");
}
}
static class DateUtil {
Day day;
DateUtil() {
}
DateUtil(int y, int m, int d) {
this.day = new Day(y, m, d);
}
public Day getDay() {
return day;
}
public void setDay(Day day) {
this.day = day;
}
boolean checkInputValidity() {
if (day.vaildate() && this.getDay().getMonth().validate() && this.getDay().getMonth().getYear().vailDate()) {
return true;
} else {
return false;
}
}
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;
}
}
boolean equalTwoDates(DateUtil date) {
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 true;
} else {
return false;
}
}
String showDate() {
return this.getDay().getMonth().getYear().getValue() + "-" + this.getDay().getMonth().getValue() + "-" + this.getDay().getValue();
}
DateUtil getNextNDays(int n) {
int d = 0, m = 0, y = 0;
while(n>366){
if(this.getDay().getMonth().getYear().isLeapYear()){
n=n-366;
}
else{
n=n-365;
}
this.getDay().getMonth().getYear().yearIncrement();
}
while (n > 0) {
this.getDay().dayIncrement();
if (!this.getDay().vaildate()) {
this.getDay().getMonth().monthIncrement();
if (!this.getDay().getMonth().validate()) {
this.getDay().getMonth().restMin();
this.getDay().getMonth().getYear().yearIncrement();
}
this.getDay().resetMin();
}
n--;
}
d = this.getDay().getValue();
m = this.getDay().getMonth().getValue();
y = getDay().getMonth().getYear().getValue();
return new DateUtil(y, m, d);
}
DateUtil getPreviousNDays(int n) {
int d = 0, m = 0, y = 0;
while(n>366){
if(this.getDay().getMonth().getYear().isLeapYear()){
n=n-366;
}
else{
n=n-365;
}
this.getDay().getMonth().getYear().yearReduction();
}
while (n > 0) {
this.getDay().datReduction();
if (!this.getDay().vaildate()) {
this.getDay().getMonth().monthReduction();
if (!this.getDay().getMonth().validate()) {
this.getDay().getMonth().restMax();
this.getDay().getMonth().getYear().yearReduction();
}
this.getDay().resetMax();
}
n--;
}
d = this.getDay().getValue();
m = this.getDay().getMonth().getValue();
y = getDay().getMonth().getYear().getValue();
return new DateUtil(y, m, d);
}
int getDaysofDates(DateUtil date){
DateUtil d1=this,d2=date;
int n=0;
if(compareDates(d2)){
d1=date;
d2=this;
}
while(!d1.equalTwoDates(d2)){
d2.getDay().dayIncrement();
if (!d2.getDay().vaildate()) {
d2.getDay().getMonth().monthIncrement();
if (!d2.getDay().getMonth().validate()) {
d2.getDay().getMonth().restMin();
d2.getDay().getMonth().getYear().yearIncrement();
}
d2.getDay().resetMin();
}
n++;
}
return n;
}
}
static 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() {
if ((value % 4 == 0 && value % 100 != 0) || value % 400 == 0) {
return true;
}
return false;
}
public boolean vailDate() {
if (value >= 1820 && value <= 2020) {
return true;
}
return false;
}
void yearIncrement() {
value = value + 1;
}
void yearReduction() {
value = value - 1;
}
}
static 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 restMin() {
value = 1;
}
public void restMax() {
value = 12;
}
boolean validate() {
if (value <= 12 && value >= 1) {
return true;
} else {
return false;
}
}
public void monthIncrement() {
value = value + 1;
}
public void monthReduction() {
value = value - 1;
}
}
static class Day {
int value;
Month month;
int[] mon_maxmun = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public Day() {
}
public Day(int yearValue, int monthValue, int dayValue) {
this.value = dayValue;
this.month = new Month(yearValue, 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;
}
void resetMin() {
value = 1;
}
void resetMax() {
value = mon_maxmun[month.getValue() - 1];
}
boolean vaildate() {
mon_maxmun[1]=28;
if (month.year.isLeapYear()) {
mon_maxmun[1] = 29;
}
if (value >= 1 && value <= mon_maxmun[month.getValue() - 1]) {
return true;
} else {
return false;
}
}
void dayIncrement() {
value = value + 1;
}
void datReduction() {
value = value - 1;
}
}
}
7-3 图形继承
import javax.swing.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int chosen = input.nextInt();
double r1,r2,r3;
if(chosen==1||chosen==2||chosen==3||chosen==4)
{
switch (chosen) {
case 1:
r1=input.nextDouble();
if(r1>0) {
Circle c = new Circle();
c.setRadius(r1);
System.out.printf(String.format("Circle's area:%.2f",c.getArea(c.getRadius())));
}
else {
System.out.print("Wrong Format");
}
break;
case 2:
r1=input.nextDouble();
r2=input.nextDouble();
if(r1>0&&r2>0) {
Rectangle r = new Rectangle();
r.setLength(r1);
r.setWidth(r2);
System.out.print(String.format("Rectangle's area:%.2f",r.getArea(r.getWidth(),r.getLength())));
}
else {
System.out.print("Wrong Format");
}
break;
case 3:
r1=input.nextDouble();
if(r1>0) {
Ball b = new Ball();
b.setRadius(r1);
System.out.println(String.format("Ball's surface area:%.2f",b.getArea(b.getRadius())));
System.out.println(String.format("Ball's volume:%.2f",b.getVolume(b.getRadius())));
}
else {
System.out.print("Wrong Format");
}
break;
case 4:
r1=input.nextDouble();
r2=input.nextDouble();
r3=input.nextDouble();
if(r1>0&&r2>0&&r3>0) {
Box box = new Box();
box.setLength(r1);
box.setWidth(r2);
box.setHeight(r3);
System.out.println(String.format("Box's surface area:%.2f",box.getArea(box.getWidth(),box.getLength(),box.getHeight())));
System.out.println(String.format("Box's volume:%.2f",box.getVolume(box.getWidth(),box.getLength(),box.getHeight())));
}
else {
System.out.println("Wrong Format");
}
break;
default:
throw new IllegalStateException("Unexpected value: " + chosen);
}
}
else{
System.out.print("Wrong Format");
}
}
static class Shape {
public Shape() {
System.out.println("Constructing Shape");
}
public double getArea() {
return 0;
}
}
static class Circle extends Shape {
private double radius;
public Circle() {
System.out.println("Constructing Circle");
}
public double getArea(double radius) {
return Math.PI * radius * radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
}
static class Rectangle extends Shape {
private double width;
private double length;
public Rectangle() {
System.out.println("Constructing Rectangle");
}
public double getLength() {
return length;
}
public double getWidth() {
return width;
}
public void setLength(double length) {
this.length = length;
}
public void setWidth(double width) {
this.width = width;
}
public double getArea(double width, double length) {
return getWidth()*getLength();
}
}
static class Ball extends Circle {
public Ball() {
System.out.println("Constructing Ball");
}
@Override
public double getArea(double radius) {
return 4 * super.getArea(getRadius());
}
public double getVolume(double radius) {
return 4.0/3.0* Math.PI * getRadius() * getRadius() * getRadius();
}
}
static class Box extends Rectangle {
private double height;
public Box() {
System.out.println("Constructing Box");
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getArea(double width, double length, double height) {
return (getWidth() * getLength() + getLength() * getHeight() + getHeight() * getWidth()) * 2;
}
public double getVolume(double width, double length, double height) {
return getWidth() * getLength() * getHeight();
}
}
}
7-5 图形继承与多态
import java.util.Scanner;
/**
* @author 86173
*/
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a, b, c, n;
int i, j = 0, flg = 1;
double sum = 0;
Shape tmp = new Shape();
a = input.nextInt();
b = input.nextInt();
c = input.nextInt();
n = a + b + c;
if (a < 0 || b < 0 || c < 0) {
System.out.print("Wrong Format");
} else {
Circle[] C = new Circle[a];
Rectangle[] R = new Rectangle[b];
Triangle[] T = new Triangle[c];
Shape[] S = new Shape[n];
for (i = 0; i < a; i++) {
C[i] = new Circle();
S[j] = new Shape();
S[j] = C[i];
C[i].setRadius(input.nextDouble());
if (C[i].validate() && flg != 0) {
flg = 1;
} else {
flg = 0;
}
j++;
}
for (i = 0; i < b; i++) {
R[i] = new Rectangle();
S[j] = new Shape();
S[j] = R[i];
R[i].setLength(input.nextDouble());
R[i].setWidth(input.nextDouble());
if (R[i].validate() && flg != 0) {
flg = 1;
} else {
flg = 0;
}
j++;
}
for (i = 0; i < c; i++) {
T[i] = new Triangle();
S[j] = new Shape();
S[j] = T[i];
T[i].side1 = input.nextDouble();
T[i].side2 = input.nextDouble();
T[i].side3 = input.nextDouble();
if (T[i].vaildate() && flg != 0) {
flg = 1;
} else {
flg = 0;
}
j++;
}
if (flg == 0) {
System.out.print("Wrong Format");
} else { /*依次输出面积*/
System.out.println("Original area:");
for (i = 0; i < n; i++) {
System.out.printf("%.2f ", S[i].getArea());
}
System.out.println();
for (i = 0; i < n; i++) {
sum = S[i].getArea() + sum;
}
System.out.println("Sum of area:" + String.format("%.2f", sum));
System.out.println("Sorted area:");
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - 1; j++) {
if (S[j].getArea() > S[j + 1].getArea()) {
tmp = S[j];
S[j] = S[j + 1];
S[j + 1] = tmp;
}
}
}
for (i = 0; i < n; i++) {
System.out.printf("%.2f ",S[i].getArea());
}
System.out.println();
System.out.println("Sum of area:" + String.format("%.2f", sum));
}
}
}
static class Shape {
double getArea() {
return 1;
}
boolean validate() {
boolean validate = false;
return validate;
}
}
static class Rectangle extends Shape {
double width = 0;
double length = 0;
double getWidth() {
return this.width;
}
void setWidth(double width) {
this.width = width;
}
double getLength() {
return this.length;
}
void setLength(double length) {
this.length = length;
}
Rectangle(double width, double length) {
this.width = width;
this.length = length;
}
Rectangle() {
}
@Override
public double getArea() {
double area = 0;
area = this.length * this.width;
return area;
}
@Override
boolean validate() {
if (this.length > 0 && this.width > 0) {
return true;
} else {
return false;
}
}
}
static class Circle extends Shape {
double radius = 0;
double getRadius() {
return this.radius;
}
void setRadius(double radius) {
this.radius = radius;
}
Circle(double radius) {
this.radius = radius;
}
Circle() {
}
@Override
boolean validate() {
if (this.radius > 0) {
return true;
} else {
return false;
}
}
@Override
public double getArea() {
double area = 0;
area = Math.PI * this.radius * this.radius;
return area;
}
}
static class Triangle extends Shape {
double side1 = 0;
double side2 = 0;
double side3 = 0;
Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
Triangle() {
}
boolean vaildate() {
if (this.side1 + this.side2 > this.side3 && this.side1 + this.side3 > this.side2 && this.side2 + this.side3 > this.side1) {
return true;
} else {
return false;
}
}
@Override
double getArea() {
double area = 0;
double s = (this.side1 + this.side2 + this.side3) / 2.0;
area = Math.sqrt(s * (s - this.side1) * (s - this.side2) * (s - this.side3));
return area;
}
}
}
7-1 水文数据校验及处理
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.*;
class HydrologicalInfo {
private String measureDateTime;
private double objectWaterLevel;//目测水位
private double actualWaterLevel;
private double objectGateOpening;//闸门开度
private double actralGateOpening;
double waterFlow;
public String getMeasureDateTime() {
return measureDateTime;
}
public void setMeasureDateTime(String measureDateTime) {
this.measureDateTime = measureDateTime;
}
public double getObjectWaterLevel() {
return objectWaterLevel;
}
public void setObjectWaterLevel(double objectWaterLevel) {
this.objectWaterLevel = objectWaterLevel;
}
public double getActualWaterLevel() {
return actualWaterLevel;
}
public void setActualWaterLevel(double actualWaterLevel) {
this.actualWaterLevel = actualWaterLevel;
}
public double getObjectGateOpening() {
return objectGateOpening;
}
public void setObjectGateOpening(double objectGateOpening) {
this.objectGateOpening = objectGateOpening;
}
public double getActralGateOpening() {
return actralGateOpening;
}
public void setActralGateOpening(double actralGateOpening) {
this.actralGateOpening = actralGateOpening;
}
public double getWaterFlow() {
return waterFlow;
}
public void setWaterFlow(double waterFlow) {
this.waterFlow = waterFlow;
}
}
class DelaDate {
public DelaDate() {
boolean flg=true;;
ArrayList<String>list =new ArrayList<>();
Scanner inpute=new Scanner(System.in);
String data=inpute.nextLine();
if(data==null){
System.out.println("Wrong Format");
return;
}
while(!data.equals("exit")) {
list.add(data);
data=inpute.nextLine();
}
HydrologicalInfo arry[] =new HydrologicalInfo[list.size()];
for(int i=0;i<list.size();i++) {
flg=CheckData(list.get(i),i);
String [] arry1=list.get(i).split("\\|");
String [] arry2=arry1[3].split("\\/");
HydrologicalInfo H=new HydrologicalInfo();
H.setMeasureDateTime(arry1[0]);
H.setObjectWaterLevel(Double.parseDouble(arry1[1]));
H.setActualWaterLevel(Double.parseDouble(arry1[2]));
H.setObjectGateOpening(Double.parseDouble(arry2[0]));
H.setActralGateOpening(Double.parseDouble(arry2[1]));
H.setWaterFlow(Double.parseDouble(arry1[4]));
arry[i]=H;
}
if(flg) {
Checkopen(arry);
System.out.printf("Max Actual Water Level:%.2f",finMax(arry));
System.out.println();
System.out.printf("Total Water Flow:%.2f",calculate(arry));
}
}
public static boolean CheckData(String data,int row){
int flg=1;
row=row+1;
String[] arry=data.split("\\|");
if(arry.length<5){
System.out.println("Wrong Format");
flg=0;
}else{
String time1="(([1-9]{1}[0-9]{0,3})(\\/)(([1-9])|(1[0-2]))(\\/)(([1-9]{1})|([12][0-9])|(3[0-1])) (([02468])|(1[02468])|(2[024]))(:)([0]{2}))";
Pattern p1=Pattern.compile(time1);
Matcher m1=p1.matcher(arry[0]);
String[] arry1=arry[0].split("\\/");
int year=Integer.parseInt(arry1[0]);
int month=Integer.parseInt(arry1[1]);
String[] arry2=arry1[2].split(" ");
int day=Integer.parseInt(arry2[0]);
if(!m1.matches()||!judge(year,month,day)){
System.out.println("Row:"+row+",Column:1Wrong Format");
flg=0;
}
String waterline="([1-9][0-9]{0,2}((\\.)[0-9]{1,3})?)";
Pattern p2=Pattern.compile(waterline);
Matcher m2=p2.matcher(arry[1].replaceAll(" ",""));
Matcher m3=p2.matcher(arry[2].replaceAll(" ",""));
if(!m2.matches()){
System.out.println("Row:"+row+",Column:2Wrong Format");
flg=0;
}
if(!m3.matches()){
System.out.println("Row:"+row+",Column:3Wrong Format");
flg=0;
}
String[] arry3=arry[3].split("\\/");
String opendu="([1-9](\\.)[0-9]{2})";
Pattern p3=Pattern.compile(opendu);
Matcher m4=p3.matcher(arry3[0].replaceAll(" ",""));
Matcher m5=p3.matcher(arry3[1].replaceAll(" ",""));
if(!m4.matches()){
System.out.println("Row:"+row+",Column:4Wrong Format");
flg=0;
}
if(!m5.matches()){
System.out.println("Row:"+row+",Column:5Wrong Format");
flg=0;
}
Matcher m6=p2.matcher(arry[4].replaceAll(" ",""));
if(!m6.matches()){
System.out.println("Row:"+row+",Column:6Wrong Format");
flg=0;
}
}
if(flg==1){
return true;
}else{
System.out.println("Data:"+data);
return false;
}
}
public static double finMax(HydrologicalInfo arry[]) {
double max=0;
for(int i=0;i<arry.length;i++) {
if(arry[i].getActualWaterLevel()>max) {
max=arry[i].getActualWaterLevel();
}
}
return max;
}
public static double calculate(HydrologicalInfo arry[]) {
double sum=0;
for(int i=0;i<arry.length;i++) {
sum+=arry[i].getWaterFlow()*2*60*60;
}
return sum;
}
public static void Checkopen(HydrologicalInfo arry[]) {
for(int i=0;i<arry.length;i++) {
double num=arry[i].getActralGateOpening();
double num1=arry[i].getObjectGateOpening();
if(num>num1) {
System.out.println("Row:"+(i+1)+" GateOpening Warning");
}
}
}
public static boolean judge(int year,int month,int day) {
int arry[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeapYear(year)) {
arry[2]=29;
}
if(arry[month]<day) {
return false;
} else {
return true;
}
}
public static boolean isLeapYear(int year) {
if(year%400==0||(year%4==0&&year%100!=0)) {
return true;
} else {
return false;
}
}
}
public class Main{
public static void main(String [] args){
DelaDate a=new DelaDate();
}
}

浙公网安备 33010602011771号