ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

题目集4~6的总结性Blog

2021-05-02 15:01:14  阅读:165  来源: 互联网

标签:总结性 题目 int System month Blog year else day


前言:

总结前三次作业的经验,对于java整体来说有了一个比较简单的认识,也是为后面这三次的作业做好了一定的铺垫。对于后面三次作业,第四次作业一共有三道题目,第五次作业共有五道小题,第六次作业则是有六道题目。下面则是对题目的分析总结:

对于第四次作业来说:大概就是学会Java中的字符串处理类以及正则表达式对输入字符串数据进行合法性校验及计算,以及对一些聚合知识的使用,整体难度感觉还是挺难的,相对于其他的来说,还是有一点难度的。

第五次作业则是学会对一些字符串和数组的处理的学习,比如对字符串的长度判断,对一些数组的合并排序处理,同时再上一次作业条件下对聚合的二次学习使用,整体来说是感觉是第四题有一点难度。

对于第六次作业来说则是对正则表达式的一个基本熟悉,同时对一些字符串的排序,以及对接口多态的初次认识了解并使用,这次题目整体还是相对简单些,前两次有个吧题目有点难度。

总的来说这几次作业,相对来说对于一些字符串的操作还是挺复杂的个人感觉也是相对有一些难度.

设计与分析:

下面为所分析题集:

①题目集4(7-2)、题目集5(7-4)两种日期类聚合设计的优劣比较

②题目集4(7-3)、题目集6(7-5、7-6)三种渐进式图形继承设计的思路与技术运用(封装、继承、多态、接口等)

③对三次题目集中用到的正则表达式技术的分析总结

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

一:题目集4(7-2)、题目集5(7-4)两种日期类聚合设计的优劣比较:

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

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

类图.jpg

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

  1. 求下n天
  2. 求前n天
  3. 求两个日期相差的天数
7-5 日期问题面向对象设计(聚合二) (40 分)  

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

类图.jpg

比较分析如下:

对于  7-2  结构清晰,在功能类里使用的方法较少,更容易让人理清。但同时,它的缺点也很明显,功能类无法直接调用每一个类,想要调用一个类需要先调用上一个类,多了很多不必要的麻烦。

  分析圈复杂度发现在功能类及main类中的复杂度较高,7这种类型的聚合虽然看起来简单明了,能将月日年的各种判断在各自的类中直接完成,但是调用起来十分繁杂,需要一层层的进行调用,实际上是降低了程序运行的效率。

          7-4 聚合设计虽然在功能类中多了很多方法,但是他可以直接对月日年进行引用,更加方便类图与题目给出的类图基本一致,从圈复杂度来看聚合二中的复杂度要更低一些,说明聚合二的效率要更高。总体来说,还是聚合二的结构更加合理,效率也更高。

  

源代码:


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,n=0,m=0;
int year1,year2,month1,month2,day1,day2;
int num2=0;
int num3=0;
boolean boo;
int j=input.nextInt();


if(j==1){

year=input.nextInt();
month=input.nextInt();
day=input.nextInt();
m=input.nextInt();
Month month3=new Month(m,year,month,day);
month3.sty();
}
if(j==2){
n=input.nextInt();
year=input.nextInt();
month=input.nextInt();
day=input.nextInt();
Year year3=new Year(n,year,month,day);
year3.sty();

}
if(j==3){
year1=input.nextInt();
month1=input.nextInt();
day1=input.nextInt();
year2=input.nextInt();
month2=input.nextInt();
day2=input.nextInt();

DateUtil date=new DateUtil(year1, year2, month1, month2, day1, day2);
date.sty();

}
else {
// System.out.println("Wrong Format");
System.exit(1);
}}
}


class DateUtil {
int year1;
int month1;
int day1;
int year2;
int month2;
int day2;
DateUtil(int year1,int year2,int month1,int month2,int day1,int day2){
this.year1=year1;
this.year2=year2;
this.month1=month1;
this.month2=month2;
this.day1=day1;
this.day2=day2;
}
public void sty() {
Scanner input = new Scanner(System.in);

int num2=0;
int num3=0;
if(checkInputValidity(year1,month1,day1)==true&&checkInputValidity(year2,month2,day2)==true)
{
num2=numOfDays(year1,month1,day1);
num3=numOfDays(year2,month2,day2);
System.out.println(num2);
System.out.println(num3);
System.out.println(Math.abs(num2-num3));
}
else System.out.println("Wrong Format");
}

public static int numOfDays(int year,int month,int day)
{ int days=0;
for(int i =1;i<year;i++){
if(i%4==0&&i%100!=0||i%400==0){ days=days+366;
}else{
days=days+365; } }
for(int month1=1;month1<=month;month1++){
if(month==3){
if(year%4==0&&year%100!=0||year%400==0){
days=days+29; }
else{
days=days+28; } }

if (month==5||month==7||month==10||month==12){
days=days+30;
}
else if (month==2||month==4||month==6||month==8||month==9||month==11){
days=days+31;
}
if(month==1){
days=days+day; }
};
return days;}
public static boolean checkInputValidity(int year,int month,int day) {

boolean boo=false;
if(1900<=year&&year<=2050)
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{
if(1<=day&&day<=31)
boo= true;
else
boo=false;
}
else if(month==4||month==6||month==9||month==11)
{
if(1<=day&&day<=30)
boo= true;
else
boo=false;
}
else if(month==2&&isLeapYear(year)==true)
{
if(1<=day&&day<=29)
boo= true;
else
boo=false;
}
else if(month==2&&isLeapYear(year)==false)
{
if(1<=day&&day<=28)
boo= true;
else
boo=false;
}
else
boo=false;
}
else
boo=false;
return boo;

}

public static boolean isLeapYear(int year) {

if(year%4==0&&year%100!=0||year%400==0)
{
return true;
}
else
{
return false;
}
}
}
class Month {
int year, month, day, m;
public Month(int m, int year, int month, int day) {
// TODO 自动生成的构造函数存根
this.year=year;
this.day=day;
this.m=m;
this.month=month;
}
public void sty() {
Scanner input = new Scanner(System.in);
int n;
boolean boo;

n=-m;
boo=checkInputValidity(year,month,day,n);

if(boo==true)
{
getDate(year,month,day,n);
}
else
System.out.println("Wrong Format");
}

public static boolean isLeapYear(int year) {

if(year%4==0&&year%100!=0||year%400==0)
{
return true;
}
else
{
return false;
}
}
public static boolean checkInputValidity(int year,int month,int day,int n)//判断输入日期是否合法,返回布尔值
{
boolean boo=false;
if(1900<=year&&year<=2050)
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{
if(1<=day&&day<=31)
boo= true;
else
boo=false;
}
else if(month==4||month==6||month==9||month==11)
{
if(1<=day&&day<=30)
boo= true;
else
boo=false;
}
else if(month==2&&isLeapYear(year)==true)
{
if(1<=day&&day<=29)
boo= true;
else
boo=false;
}
else if(month==2&&isLeapYear(year)==false)
{
if(1<=day&&day<=28)
boo= true;
else
boo=false;
}
else
boo=false;
}
else
boo=false;
return boo;

}
public static void getDate(int year,int month,int day,int n)
{
while(day<20) {
if(n>0)
{
if(day>n)
{
day=day-n;
System.out.println(year+month+day);
}
else if(day<n&&(month==2||month==4||month==6||month==8||month==9||month==11))
{
month=month-1;
day=31-n+day;
System.out.println(year+month+day);
}
else if(day<n&&(month==5||month==7||month==10||month==12))
{
month=month-1;
day=30-n+day;
System.out.println(year+month+day);
}
else if(day<n&&isLeapYear(year)==true&&month==3)
{
month=month-1;
day=29-n+day;
System.out.println(year+month+day);
}
else if(day<n&&isLeapYear(year)==false&&month==3)
{
month=month-1;
day=28-n+day;
System.out.println(year+month+day);
}
else
{
year=year-1;
month=12;
day=31-n+day;
System.out.println(year+month+day);
}

}
else
{
if(day+Math.abs(n)<=28)
{
day=day+Math.abs(n);
System.out.println(year+month+day);
}
else if(day+Math.abs(n)>31&&month==1||month==3||month==5||month==7||month==8||month==10)
{
month=month+1;
day=Math.abs(n)+day-31;
System.out.println(year+month+day);
}
else if(day+Math.abs(n)>31&&month==4||month==6||month==9||month==11)
{
month=month+1;
day=Math.abs(n)+day-30;
}
else if(day+Math.abs(n)>29&&isLeapYear(year)==true&&month==2)
{
month=month+1;
day=Math.abs(n)+day-29;
System.out.println(year+month+day);
}
else if(day+Math.abs(n)>28&&isLeapYear(year)==false&&month==2)
{
month=month+1;
day=Math.abs(n)+day-28;
System.out.println(year+month+day);
}
else
{
year=year+1;
month=1;
day=Math.abs(n)+day-31;
System.out.println(year+month+day);
}
}

}}
}
class Year {
int n, year, month, day;
public Year(int n, int year, int month, int day) {
// TODO 自动生成的构造函数存根
this.year=year;
this.day=day;
this.n=n;
this.month=month;
}
private int m=0;
public void sty() {

boolean boo;

boo=checkInputValidity(year,month,day,n);
if(boo==true)
{
getDate(year,month,day,n);

}
else
System.out.println("Wrong Format");
}

public int getM() {
return m;
}

public void setM(int m) {
this.m = m;
}

public static boolean isLeapYear(int year) {

if(year%4==0&&year%100!=0||year%400==0)
{
return true;
}
else
{
return false;
}
}
public static boolean checkInputValidity(int year,int month,int day,int n)//判断输入日期是否合法,返回布尔值
{
boolean boo=false;
if(1820<=year&&year<=2020)
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{
if(1<=day&&day<=31)
boo= true;
else
boo=false;
}
else if(month==4||month==6||month==9||month==11)
{
if(1<=day&&day<=30)
boo= true;
else
boo=false;
}
else if(month==2&&isLeapYear(year)==true)
{
if(1<=day&&day<=29)
boo= true;
else
boo=false;
}
else if(month==2&&isLeapYear(year)==false)
{
if(1<=day&&day<=28)
boo= true;
else
boo=false;
}
else
boo=false;
}
else
boo=false;
return boo;

}
public static void getDate(int year,int month,int day,int n)
{
if(n>0)
{
if(day>n)
{
day=day-n;
System.out.println(year+month+day);
}
else if(day<n&&(month==2||month==4||month==6||month==8||month==9||month==11))
{
month=month-1;
day=31-n+day;
System.out.println(year+month+day);
}
else if(day<n&&(month==5||month==7||month==10||month==12))
{
month=month-1;
day=30-n+day;
System.out.println(year+month+day);
}
else if(day<n&&isLeapYear(year)==true&&month==3)
{
month=month-1;
day=29-n+day;
System.out.println(year+month+day);
}
else if(day<n&&isLeapYear(year)==false&&month==3)
{
month=month-1;
day=28-n+day;
System.out.println(year+month+day);
}
else
{
year=year-1;
month=12;
day=31-n+day;
System.out.println(year+month+day);
}

}
else
{
if(day+Math.abs(n)<=28)
{
day=day+Math.abs(n);
System.out.println(year+month+day);
}
else if(day+Math.abs(n)>31&&month==1||month==3||month==5||month==7||month==8||month==10)
{
month=month+1;
day=Math.abs(n)+day-31;
System.out.println(year+month+day);
}
else if(day+Math.abs(n)>31&&month==4||month==6||month==9||month==11)
{
month=month+1;
day=Math.abs(n)+day-30;
}
else if(day+Math.abs(n)>29&&isLeapYear(year)==true&&month==2)
{
month=month+1;
day=Math.abs(n)+day-29;
System.out.println(year+month+day);
}
else if(day+Math.abs(n)>28&&isLeapYear(year)==false&&month==2)
{
month=month+1;
day=Math.abs(n)+day-28;
System.out.println(year+month+day);
}
else
{
year=year+1;
month=1;
day=Math.abs(n)+day-31;
System.out.println(year+month+day);
}
}

}
}


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,n=0,m=0;
int year1,year2,month1,month2,day1,day2;
int num2=0;
int num3=0;
boolean boo;
int j=input.nextInt();


if(j==1){

year=input.nextInt();
month=input.nextInt();
day=input.nextInt();
m=input.nextInt();
Month month3=new Month(m,year,month,day);
month3.sty();
}
if(j==2){
n=input.nextInt();
year=input.nextInt();
month=input.nextInt();
day=input.nextInt();
Year year3=new Year(n,year,month,day);
year3.sty();

}
if(j==3){
year1=input.nextInt();
month1=input.nextInt();
day1=input.nextInt();
year2=input.nextInt();
month2=input.nextInt();
day2=input.nextInt();

DateUtil date=new DateUtil(year1, year2, month1, month2, day1, day2);
date.sty();

}
else {
// System.out.println("Wrong Format");
System.exit(1);
}}
}


class DateUtil {
int year1;
int month1;
int day1;
int year2;
int month2;
int day2;
DateUtil(int year1,int year2,int month1,int month2,int day1,int day2){
this.year1=year1;
this.year2=year2;
this.month1=month1;
this.month2=month2;
this.day1=day1;
this.day2=day2;
}
public void sty() {
Scanner input = new Scanner(System.in);

int num2=0;
int num3=0;
if(checkInputValidity(year1,month1,day1)==true&&checkInputValidity(year2,month2,day2)==true)
{
num2=numOfDays(year1,month1,day1);
num3=numOfDays(year2,month2,day2);
System.out.println(num2);
System.out.println(num3);
System.out.println(Math.abs(num2-num3));
}
else System.out.println("Wrong Format");
}

public static int numOfDays(int year,int month,int day)
{ int days=0;
for(int i =1;i<year;i++){
if(i%4==0&&i%100!=0||i%400==0){ days=days+366;
}else{
days=days+365; } }
for(int month1=1;month1<=month;month1++){
if(month==3){
if(year%4==0&&year%100!=0||year%400==0){
days=days+29; }
else{
days=days+28; } }

if (month==5||month==7||month==10||month==12){
days=days+30;
}
else if (month==2||month==4||month==6||month==8||month==9||month==11){
days=days+31;
}
if(month==1){
days=days+day; }
};
return days;}
public static boolean checkInputValidity(int year,int month,int day) {

boolean boo=false;
if(1900<=year&&year<=2050)
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{
if(1<=day&&day<=31)
boo= true;
else
boo=false;
}
else if(month==4||month==6||month==9||month==11)
{
if(1<=day&&day<=30)
boo= true;
else
boo=false;
}
else if(month==2&&isLeapYear(year)==true)
{
if(1<=day&&day<=29)
boo= true;
else
boo=false;
}
else if(month==2&&isLeapYear(year)==false)
{
if(1<=day&&day<=28)
boo= true;
else
boo=false;
}
else
boo=false;
}
else
boo=false;
return boo;

}

public static boolean isLeapYear(int year) {

if(year%4==0&&year%100!=0||year%400==0)
{
return true;
}
else
{
return false;
}
}
}
class Month {
int year, month, day, m;
public Month(int m, int year, int month, int day) {
// TODO 自动生成的构造函数存根
this.year=year;
this.day=day;
this.m=m;
this.month=month;
}
public void sty() {
Scanner input = new Scanner(System.in);
int n;
boolean boo;

n=-m;
boo=checkInputValidity(year,month,day,n);

if(boo==true)
{
getDate(year,month,day,n);
}
else
System.out.println("Wrong Format");
}

public static boolean isLeapYear(int year) {

if(year%4==0&&year%100!=0||year%400==0)
{
return true;
}
else
{
return false;
}
}
public static boolean checkInputValidity(int year,int month,int day,int n)//判断输入日期是否合法,返回布尔值
{
boolean boo=false;
if(1900<=year&&year<=2050)
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{
if(1<=day&&day<=31)
boo= true;
else
boo=false;
}
else if(month==4||month==6||month==9||month==11)
{
if(1<=day&&day<=30)
boo= true;
else
boo=false;
}
else if(month==2&&isLeapYear(year)==true)
{
if(1<=day&&day<=29)
boo= true;
else
boo=false;
}
else if(month==2&&isLeapYear(year)==false)
{
if(1<=day&&day<=28)
boo= true;
else
boo=false;
}
else
boo=false;
}
else
boo=false;
return boo;

}
public static void getDate(int year,int month,int day,int n)
{
while(day<20) {
if(n>0)
{
if(day>n)
{
day=day-n;
System.out.println(year+month+day);
}
else if(day<n&&(month==2||month==4||month==6||month==8||month==9||month==11))
{
month=month-1;
day=31-n+day;
System.out.println(year+month+day);
}
else if(day<n&&(month==5||month==7||month==10||month==12))
{
month=month-1;
day=30-n+day;
System.out.println(year+month+day);
}
else if(day<n&&isLeapYear(year)==true&&month==3)
{
month=month-1;
day=29-n+day;
System.out.println(year+month+day);
}
else if(day<n&&isLeapYear(year)==false&&month==3)
{
month=month-1;
day=28-n+day;
System.out.println(year+month+day);
}
else
{
year=year-1;
month=12;
day=31-n+day;
System.out.println(year+month+day);
}

}
else
{
if(day+Math.abs(n)<=28)
{
day=day+Math.abs(n);
System.out.println(year+month+day);
}
else if(day+Math.abs(n)>31&&month==1||month==3||month==5||month==7||month==8||month==10)
{
month=month+1;
day=Math.abs(n)+day-31;
System.out.println(year+month+day);
}
else if(day+Math.abs(n)>31&&month==4||month==6||month==9||month==11)
{
month=month+1;
day=Math.abs(n)+day-30;
}
else if(day+Math.abs(n)>29&&isLeapYear(year)==true&&month==2)
{
month=month+1;
day=Math.abs(n)+day-29;
System.out.println(year+month+day);
}
else if(day+Math.abs(n)>28&&isLeapYear(year)==false&&month==2)
{
month=month+1;
day=Math.abs(n)+day-28;
System.out.println(year+month+day);
}
else
{
year=year+1;
month=1;
day=Math.abs(n)+day-31;
System.out.println(year+month+day);
}
}

}}
}
class Year {
int n, year, month, day;
public Year(int n, int year, int month, int day) {
// TODO 自动生成的构造函数存根
this.year=year;
this.day=day;
this.n=n;
this.month=month;
}
private int m=0;
public void sty() {

boolean boo;

boo=checkInputValidity(year,month,day,n);
if(boo==true)
{
getDate(year,month,day,n);

}
else
System.out.println("Wrong Format");
}

public int getM() {
return m;
}

public void setM(int m) {
this.m = m;
}

public static boolean isLeapYear(int year) {

if(year%4==0&&year%100!=0||year%400==0)
{
return true;
}
else
{
return false;
}
}
public static boolean checkInputValidity(int year,int month,int day,int n)//判断输入日期是否合法,返回布尔值
{
boolean boo=false;
if(1820<=year&&year<=2020)
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{
if(1<=day&&day<=31)
boo= true;
else
boo=false;
}
else if(month==4||month==6||month==9||month==11)
{
if(1<=day&&day<=30)
boo= true;
else
boo=false;
}
else if(month==2&&isLeapYear(year)==true)
{
if(1<=day&&day<=29)
boo= true;
else
boo=false;
}
else if(month==2&&isLeapYear(year)==false)
{
if(1<=day&&day<=28)
boo= true;
else
boo=false;
}
else
boo=false;
}
else
boo=false;
return boo;

}
public static void getDate(int year,int month,int day,int n)
{
if(n>0)
{
if(day>n)
{
day=day-n;
System.out.println(year+month+day);
}
else if(day<n&&(month==2||month==4||month==6||month==8||month==9||month==11))
{
month=month-1;
day=31-n+day;
System.out.println(year+month+day);
}
else if(day<n&&(month==5||month==7||month==10||month==12))
{
month=month-1;
day=30-n+day;
System.out.println(year+month+day);
}
else if(day<n&&isLeapYear(year)==true&&month==3)
{
month=month-1;
day=29-n+day;
System.out.println(year+month+day);
}
else if(day<n&&isLeapYear(year)==false&&month==3)
{
month=month-1;
day=28-n+day;
System.out.println(year+month+day);
}
else
{
year=year-1;
month=12;
day=31-n+day;
System.out.println(year+month+day);
}

}
else
{
if(day+Math.abs(n)<=28)
{
day=day+Math.abs(n);
System.out.println(year+month+day);
}
else if(day+Math.abs(n)>31&&month==1||month==3||month==5||month==7||month==8||month==10)
{
month=month+1;
day=Math.abs(n)+day-31;
System.out.println(year+month+day);
}
else if(day+Math.abs(n)>31&&month==4||month==6||month==9||month==11)
{
month=month+1;
day=Math.abs(n)+day-30;
}
else if(day+Math.abs(n)>29&&isLeapYear(year)==true&&month==2)
{
month=month+1;
day=Math.abs(n)+day-29;
System.out.println(year+month+day);
}
else if(day+Math.abs(n)>28&&isLeapYear(year)==false&&month==2)
{
month=month+1;
day=Math.abs(n)+day-28;
System.out.println(year+month+day);
}
else
{
year=year+1;
month=1;
day=Math.abs(n)+day-31;
System.out.println(year+month+day);
}
}

}}

2.题目集4(7-3)、题目集6(7-5、7-6)三种渐进式图形继承设计的思路与技术运用(封装、继承、多态、接口等)

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自动生成)
  • 输出的数值均保留两位小数
  • 7-5 图形继承与多态 (50 分)  

    掌握类的继承、多态性及其使用方法。具体需求参见作业指导书。

    2021-OO第06次作业-5指导书V1.0.pdf

    输入格式:

    从键盘首先输入三个整型值(例如a b c),分别代表想要创建的Circle、Rectangle及Triangle对象的数量,然后根据图形数量继续输入各对象的属性值(均为实型数),数与数之间可以用一个或多个空格或回车分隔。

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

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

    类图.jpg

    其中:

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

    对于第四次的7-3设计:首先则是想到每一个图形都得求面积,这就也是意味着都得用一个getArea的方法,则就所有的图形类都继承着这个父类,然后在各自的类中都继承该方法,返回相应的面积计算公式

    对于后面两题,则一个是得用到多态,先建立相关的对象,输入对象的相关的属性值之后,调用对象的求面积或者体积的方法求得相应的面积和体积,最后输出。最后一个则是这次老师要求要用到接口,于是就把这次中的求面积的方法单独做成了一个接口,

    以实现求他功能。

    比较如下:

    首先对于对于第四次作业中的7-3知识简单的继承关系,用起来还是比较简单的,但是再第五次作业中则是用到了继承和多态,在之前的基础上则提高了代码的维护性(继承保证);提高了代码的扩展性。如果在后续还得添加一些图形求面积,则会方便一些。

    同时第六题中则是想对于之前多用到了一个接口,也是方便直接在各自的类中直接重写方法。也是提高了代码的延展性。

    关键代码:

    7-5

    import java.text.DecimalFormat;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Scanner;

    public class Main {

    public static void main(String[] args) {
    // TODO 自动生成的方法存根

    Scanner input = new Scanner(System.in);
    int num=input.nextInt();
    int num1=input.nextInt();
    int num2=input.nextInt();
    double sum=0;
    if(num1<0||num2<0||num<0){
    System.out.print("Wrong Format");
    System.exit(0);
    }
    // if(num1==0&&num2==0&&num==0){
    // System.out.print("Wrong Format");
    // System.exit(0);
    // }
    ArrayList<Double> areas=new ArrayList<>();
    // double radius=sc.nextDouble();
    // ArrayList<Circle> c=new ArrayList<>();
    for(int i=0;i<num;i++) {
    double radius=input.nextDouble();
    Circle c1=new Circle(radius);
    // c.add(c1);
    areas.add(c1.getArea());
    if(radius<=0)
    {
    System.out.print("Wrong Format");
    System.exit(0);
    }
    }

    for (int i=0;i<num1;i++){
    double length=input.nextDouble();
    double width=input.nextDouble();
    Rectangle rectangle=new Rectangle(length,width);
    areas.add(rectangle.getArea());
    if(length<=0||width<=0)
    {
    System.out.print("Wrong Format");
    System.exit(0);
    }
    }

    for (int i=0;i<num2;i++){
    double side1=input.nextDouble();
    double side2=input.nextDouble();
    double side3=input.nextDouble();
    Triangle triangle=new Triangle(side1,side2,side3);
    areas.add(triangle.getArea());
    if(side1+side2>side3&&side1+side3>side2&&side2+side3>side1&&side1>0&&side2>0&&side3>0)
    {}
    else{
    System.out.print("Wrong Format");
    System.exit(0);
    }


    }

    System.out.println("Original area:");
    for (Double area : areas) {
    DecimalFormat df = new DecimalFormat("#.00");
    System.out.print(df.format(area)+" ");
    }
    for (Double area : areas) {
    sum=sum+area;
    }
    System.out.println();
    System.out.print("Sum of area:");
    DecimalFormat df = new DecimalFormat("#.00");
    System.out.println(df.format(sum));
    System.out.println("Sorted area:");
    Collections.sort(areas);
    for (Double area : areas) {
    DecimalFormat df3 = new DecimalFormat("#.00");
    System.out.print(df3.format(area)+" ");
    }
    System.out.println();
    System.out.print("Sum of area:");
    DecimalFormat df4 = new DecimalFormat("#.00");
    System.out.print(df4.format(sum));


    if(num<0||num1<0||num2<0)
    {
    System.out.println("Wrong Format");
    }

    }
    }
    class Circle extends Shape{
    private double radius;

    public Circle() {
    }

    public Circle(double radius) {
    this.radius = radius;
    }

    public double getArea(){
    return radius*radius*Math.PI;
    }

    public double getRadius() {
    return radius;
    }

    public void setRadius(double radius) {
    this.radius = radius;
    }
    }
    class Rectangle extends Shape{
    private double length;
    private double width;

    public Rectangle() {
    }

    public Rectangle(double length, double width) {
    this.length = length;
    this.width = width;
    }

    public double getArea(){
    return length*width;

    }

    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;
    }
    }
    abstract class Shape {
    public abstract double getArea();
    }
    class Triangle extends Shape {
    private double side1;
    private double side2;
    private double side3;

    public double getArea(){
    double p=(side1+side2+side3)/2;
    return Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));
    }

    public Triangle() {
    }

    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;
    }
    }

    7-6

    import java.text.DecimalFormat;
    import java.util.Scanner;

    public class Main {

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    double radius=input.nextDouble();
    double length=input.nextDouble();
    double width=input.nextDouble();
    Circle c1=new Circle(radius);
    Rectangle rectangle=new Rectangle(length,width);

    if(length<=0||width<=0)
    {
    System.out.print("Wrong Format");
    System.exit(0);
    }
    if(radius<=0)
    {
    System.out.print("Wrong Format");
    System.exit(0);
    }
    DecimalFormat df = new DecimalFormat("#.00");
    System.out.println(df.format(c1.getArea()));
    // System.out.print(c1.getArea()+" ");
    DecimalFormat df2 = new DecimalFormat("#.00");
    System.out.print(df2.format(rectangle.getArea()));
    // System.out.print(rectangle.getArea());
    }
    }class Circle implements GetArea{
    private double radius;

    public Circle() {
    }

    public Circle(double radius) {
    this.radius = radius;
    }

    public double getArea(){
    return radius*radius*Math.PI;
    }

    public double getRadius() {
    return radius;
    }

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


    interface GetArea {
    public double getArea();
    }
    class Rectangle implements GetArea{
    private double length;
    private double width;

    public Rectangle() {
    }

    public Rectangle(double length, double width) {
    this.length = length;
    this.width = width;
    }

    public double getArea(){
    return length*width;

    }

    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;
    }
    }

    3、对三次题目集中用到的正则表达式技术的分析总结:

    正则表达式因为平时也是自己用的不多,所以开始应用起来还是比较不熟悉。前两次的正则表达式的使用只是简单的一个词的判断,在数关键词题目中,关键词可以用循环+判断字符串是否相等来判断是否为关键字,所以也没有大面积地

    使用正则表达式。而后三次的正则表达式训练题,是专门为了训练正则表达式的使用,这三道题目相对还算简单,大多还只是字母与数字夹杂在一起来识别,需要额外拿出来的例子相对较少。使用正则表达式,关键在于pattern的书写上,

    pattern的书写很多时候都是表示大多种情况,部分情况则需要单独出来判断。对于题目中所给出的限制条件,可以是直接写一个完整的式子来比价判断,当然还有谢谢陌生的方法也是忘记怎么使用的了。比如Matcher.matches():

    匹配Pattern实例,只有完全匹配时才会返回true等等。也可以是写多个式子一个一个限制比较从而得出结论。不过对于一些简便的语句代替还是不熟悉,只能一个一个字符进行判定,从而达到目的。

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

        这道题集合框架的运用就在于ArrayLIst<>的使用,他并不同于普通的数组,他的内容顺序等是可变的,且他的使用方法也不同与普通的数组,他的用途比起数组就更加的有实用性与可变性。同时对于关键字的提取与出现次数的计算,

        使用正则表达式可以很灵活的处理。输入的代码首先肯定是要根据空格、括号等符号进行划分,使用list进行操作。不过list是按对象存入的顺序保存对象,不做排序和编辑操作,因此后期需要对关键字次数另行计算与排序。但是这道题目自己感觉还是同复杂的

        由于自己的原因,前面知识自己的分析,该题自己并没有完成,

      三、采坑心得

        总体来说第四次题目自我感觉还是挺麻烦的,自己第二题交了,同学不小心拿我的号交了次,粉笔我高,还被查重就是很离谱。这算的pta的坑嘛。随后这次作业的第三题,面积在输出是忘记‘s也是浪费了我很多时间去寻找这个错误,最可气的就是自己感觉明明对了

却找不出错误在哪

 

好比这个地方,自己想着明明球面积能对,为啥圆测试过不了。

其次则是第五次题目中的错误,那个日期的求下一天倒还行,对于一些数字大的求个前n天以及相差天数,个人感觉这种花时间难计算的题目还是够算的,需要太久时间来梳理了,还是挺麻烦的

 

随后则是最后一次题目中在第四题对学号的判定时,自己忘记04号判断了

 

 以至于后面位数判定错误,原来时自己在加上一个0即可

 

同时

 

 对于这题,至今也不知道怎么样的才是正确答案。

 

四、改进建议:

希望老师可以把正确答案发出来。要不然感觉写了也没一个正确的答案,不会的还是不会。

五、总结:

这几次作业,自己对类的掌握更加深刻,让我更加明白类的定义、与使用,并且还掌握了关于类的继承与多态,让我了解使用了抽象类和接口。这次习题集更加综合地训练我对类的使用方法,

继承、多态有了一定的深入了解,还有接口,集合,对它们的理解个更深刻。也明白了代码可以同时高效使用,体会到了代码高效使用的好处,同时自己对这一些知识的应用有了进一步的认识。

希望自己在以后在写代码时能够多多运用以上知识,使得代码更加完美,自己还得多多学习

 

 

 

 

 

 

 

标签:总结性,题目,int,System,month,Blog,year,else,day
来源: https://www.cnblogs.com/yuzz123/p/14725705.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有