题目集4~6的总结性Blog

前言

知识点:这几次pta作业主要关于java的三大特性:继承,多态,封装。以及java正则表达式的

使用。

题量:这几次pta的题量不大。

难度:个别题目难度较大,比如水文数据校验及处理和统计Java程序中关键词的出现次数。

设计与分析

第四次作业7-2 日期问题面向对象设计

 

 

这道题目难度不大,直接根据类图进行各个类的完善。再写题过程中出了一些问题年越界测试,月越界等

 

测试点过不了,代码会出现数组的越界的问题,idea会给予提示 。

 

 

如上图中

 

 进行天数的合法性检查时,如果没有

this.month.getValue()<=12

这段代码我们输入2000 13  27这个数据时idea就会报错提示数组越界,因为再建立月份天数数组时只给了13个容量如下

 

 

 这题其他部分没啥问题,就是比较繁琐。

源代码如下

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int flag= input.nextInt();

if(flag==1)
{
int year= input.nextInt();
int month= input.nextInt();
int day= input.nextInt();
int n= input.nextInt();

DateUtil dateUtil=new DateUtil(year,month,day);
if(dateUtil.checkInputValidity())
{ dateUtil.getNextNDays(n);
String s=dateUtil.showDate();
System.out.print(s);}
else
System.out.print("Wrong Format");
}
else if(flag==2)
{
int year= input.nextInt();
int month= input.nextInt();
int day= input.nextInt();
int n= input.nextInt();
DateUtil dateUtil=new DateUtil(year,month,day);
if(dateUtil.checkInputValidity())
{ dateUtil.getPreviousNDays(n);
String s=dateUtil.showDate();
System.out.print(s);}
else
System.out.print("Wrong Format");
}
else if(flag==3)
{
int year= input.nextInt();
int month= input.nextInt();
int day= input.nextInt();
int year1= input.nextInt();
int month1= input.nextInt();
int day1= input.nextInt();
DateUtil dateUtil=new DateUtil(year,month,day);
DateUtil dateUti1=new DateUtil(year1,month1,day1);
if(dateUtil.checkInputValidity()&&dateUti1.checkInputValidity())
{ int c=dateUtil.getDaysofDates(dateUti1);
System.out.print(c);}
else
System.out.print("Wrong Format");
}
else
System.out.print("Wrong Format");

}
}

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(){
if(value%4==0&&value%100!=0)
return true;
else if(value%400==0)
return true;
else
return false;
}
public boolean validate(){
if(value>=1900&&value<=2050)
return true;
else
return false;
}
public void yearIncrement(){
value=value+1;
}
public void yearReduction(){
value=value-1;
}
}

class Month{
private int value;
private Year year;
public Month(){}
public Month(int yearValue,int monthValue){
this.value=monthValue;
this.year=new Year(yearValue);
}

public void setValue(int value) {
this.value = value;
}

public int getValue() {
return value;
}

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

public Year getYear() {
return year;
}
public void raseMin(){
value=1;
}
public void raseMax(){
value=12;
}

public boolean vaildate(){
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;
int t[] = new int[]{0,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;
month = new Month(yearValue, monthValue);
}

public void setValue(int value) {
this.value = value;
}

public int getValue() {
return value;
}

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

public Month getMonth() {
return month;
}

public void raseMin() {
value = 1;
}

public void raseMax() {
value =t[month.getValue()];
}
public boolean validate(){
if(this.month.getYear().isLeapYear())
t[2]=29;
else
t[2]=28;
if(value>=1&&value<=31)
{
if(this.month.getValue()<=12&&this.value>t[this.month.getValue()])
return false;
else
return true;
}
else
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){
day=new Day(d,m,y);
}

public Day getDay() {
return day;
}

public void setDay(Day day) {
this.day = day;
}
public boolean checkInputValidity(){
if(this.day.validate()&&this.day.getMonth().vaildate()&&this.day.getMonth().getYear().validate())
return true;
else
return false;
}
public boolean compareDates(DateUtil date){
if(this.day.getMonth().getYear().getValue()>date.day.getMonth().getYear().getValue())
return true;

else if(this.day.getMonth().getYear().getValue()==date.day.getMonth().getYear().getValue()&&this.day.getMonth().getValue()>date.day.getMonth().getValue())
return true;

else if(this.day.getMonth().getYear().getValue()==date.day.getMonth().getYear().getValue()&&this.day.getMonth().getValue()==date.day.getMonth().getValue()&&this.day.getValue()>date.day.getValue())
return true;
else
return false;
}
public boolean equalTwoDates(DateUtil date){
if(this.day.getMonth().getYear().getValue()==date.day.getMonth().getYear().getValue())
{
if(this.day.getMonth().getValue()==date.day.getMonth().getValue())
{
if(this.day.getValue()==date.day.getValue())
return true;
else
return false;
}
else
return false;
}
else
return false;
}
public String showDate(){
return this.day.getMonth().getYear().getValue()+"-"+this.day.getMonth().getValue()+"-"+this.day.getValue();
}
public void getNextNDays(int n){
while(n>0){
this.day.validate();
if((this.day.getValue() + 1) <= this.day.t[this.day.getMonth().getValue()])
this.day.dayIncrement();
else
{
if((this.day.getMonth().getValue()+1)<=12)
{
this.day.getMonth().monthIncrement();
this.day.raseMin();
}
else
{
this.day.getMonth().getYear().yearIncrement();
this.day.getMonth().raseMin();
this.day.raseMin();
}
}
n--;
}

}
public void getPreviousNDays(int n){
while(n>0){
this.day.validate();
if((this.day.getValue() - 1) >= 1)
this.day.dayReduction();
else
{
if((this.day.getMonth().getValue()-1)>=1)
{
this.day.getMonth().monthReduction();
this.day.raseMax();
}
else
{
this.day.getMonth().getYear().yearReduction();
this.day.getMonth().raseMax();
this.day.raseMax();
}
}
n--;
}
}
public int getDaysofDates(DateUtil date){
int days=0;
if(this.equalTwoDates(date))
return days;
else
{
if(this.compareDates(date))
{
while(this.day.getValue()!=date.day.getValue()||this.day.getMonth().getValue()!=date.day.getMonth().getValue()||this.day.getMonth().getYear().getValue()!=date.day.getMonth().getYear().getValue())
{
date.day.validate();
if((date.day.getValue() + 1) <= date.day.t[date.day.getMonth().getValue()])
date.day.dayIncrement();
else
{
if((date.day.getMonth().getValue()+1)<=12)
{
date.day.getMonth().monthIncrement();
date.day.raseMin();
}
else
{
date.day.getMonth().getYear().yearIncrement();
date.day.getMonth().raseMin();
date.day.raseMin();
}
}
days++;
}
}
else
{
while(this.day.getValue()!=date.day.getValue()||this.day.getMonth().getValue()!=date.day.getMonth().getValue()||this.day.getMonth().getYear().getValue()!=date.day.getMonth().getYear().getValue())
{
this.day.validate();
if((this.day.getValue() + 1) <= this.day.t[this.day.getMonth().getValue()])
this.day.dayIncrement();
else
{
if((this.day.getMonth().getValue()+1)<=12)
{
this.day.getMonth().monthIncrement();
this.day.raseMin();
}
else
{
this.day.getMonth().getYear().yearIncrement();
this.day.getMonth().raseMin();
this.day.raseMin();
}
}
days++;
}
}

}
return days;
}

}

第四次作业7-3图形继承
这题与前面的一次作业十分相似,主要是运用了Java三大特性之一的继承来完成。
子类Circle,Rectangle,Ball,Box都是继承父类Shape在父类中有如下方法

然后由子类重写父类方法,来解决代码量大且臃肿,而且维护性不高(维护性主要是后期
需要修改的时候,就需要修改很多的代码,容易出错)的问题。这题难度也不大,一些具
体方法与之前的作业相似。
源代码如下
import java.util.Scanner;

public class Main {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int flag;
flag=input.nextInt();
if(flag==1)
{
double radius=input.nextDouble();
if(radius<=0)
System.out.print("Wrong Format");
else
{
Circle circle=new Circle();

circle.setRadius(radius);
System.out.print("Circle's area:"+ String.format("%.2f",circle.getArea()));
}

}
else if(flag==2)
{
double width=input.nextDouble();
double length=input.nextDouble();
if(width<=0||length<=0)
System.out.print("Wrong Format");
else
{
Rectangle rectangle=new Rectangle();

rectangle.setLength(length);
rectangle.setWidth(width);
System.out.print("Rectangle's area:"+ String.format("%.2f",rectangle.getArea()));
}
}
else if(flag==3)
{
double radius=input.nextDouble();
if(radius<=0)
System.out.print("Wrong Format");
else
{
Ball ball=new Ball();
ball.setRadius(radius);

System.out.println("Ball's surface area:"+ String.format("%.2f",ball.getArea()));
System.out.print("Ball's volume:"+ String.format("%.2f",ball.getVolume()));
}
}
else if(flag==4)
{
double width=input.nextDouble();
double length=input.nextDouble();
double height=input.nextDouble();
if(width<=0||length<=0||height<=0)
System.out.print("Wrong Format");
else
{
Box box=new Box();

box.setLength(length);
box.setHeight(height);
box.setWidth(width);
System.out.println("Box's surface area:"+ String.format("%.2f",box.getArea()));
System.out.print("Box's volume:"+ String.format("%.2f",box.getVolume()));
}
}
else
System.out.print("Wrong Format");
}

}
class Shape{
public double getArea(){
return 0.0;

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

class Circle extends Shape{
private double radius;
public Circle(){
System.out.println("Constructing Circle");
}
public void setRadius(double radius){
this.radius=radius;
}

public double getRadius() {
return radius;
}

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

class Rectangle extends Shape{
private double width;
private double length;
public Rectangle(){
System.out.println("Constructing Rectangle");
}
public void setWidth(double width){
this.width=width;
}

public double getWidth() {
return 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 double getArea(){
return 4 * super.getRadius() * super.getRadius() * Math.PI;
}
public double getVolume(){
return (4*Math.PI*super.getRadius()*super.getRadius()*super.getRadius())/3;
}
}

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() {
return 2*height*super.getLength()+2*height*super.getWidth()+2*super.getLength()*super.getWidth();
}
public double getVolume(){
return height*super.getLength()*super.getWidth();
}
}
 
第五次作业7-4统计Java程序中关键词的出现次数 
该题难度较大,运用到了正则表达式有些测试点并没有通过

 


 

源代码如下
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-5 日期问题面向对象设计(聚合二)

 


这次题目与之前的第四作业7-2相似,相比与第四次作业7-2这次题目类设计更加简洁,没有7-2


设计的繁琐。


在这个题目中我刚刚开始采用了与之前题目相似的方法然后就在下n天,前n天等测试点出现运行超时


的问题。因为在之前的getNextNDays等方法我采用了最简单的方法,让天数一天天的加,循环次数


太多导致运行超时,然后我采用了一年一年的加,然后不足一年再进行一天一天的加如图


 

 

源代码如下

 

 

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int flag= input.nextInt();

if(flag==1)
{
int year= input.nextInt();
int month= input.nextInt();
int day= input.nextInt();
int n= input.nextInt();

DateUtil dateUtil=new DateUtil(year,month,day);
if(dateUtil.checkInputValidity())
{
dateUtil.getNextNDays(n);
String s=dateUtil.showDate();
System.out.print(year+"-"+month+"-"+day+" next "+n+" days is:"+s);
}
else
System.out.print("Wrong Format");
}
else if(flag==2)
{
int year= input.nextInt();
int month= input.nextInt();
int day= input.nextInt();
int n= input.nextInt();
DateUtil dateUtil=new DateUtil(year,month,day);
if(dateUtil.checkInputValidity())
{ dateUtil.getPreviousNDays(n);
String s=dateUtil.showDate();
System.out.print(year+"-"+month+"-"+day+" previous "+n+" days is:"+s);
}
else
System.out.print("Wrong Format");
}
else if(flag==3)
{
int year= input.nextInt();
int month= input.nextInt();
int day= input.nextInt();
int year1= input.nextInt();
int month1= input.nextInt();
int day1= input.nextInt();
DateUtil dateUtil=new DateUtil(year,month,day);
DateUtil dateUti1=new DateUtil(year1,month1,day1);
if(dateUtil.checkInputValidity()&&dateUti1.checkInputValidity())
{

int c=dateUtil.getDaysofDates(dateUti1);
System.out.print("The days between "+year+"-"+month+"-"+day+" and "+year1+"-"+month1+"-"+day1+" are:"+c);
}
else
System.out.print("Wrong Format");
}
else
System.out.print("Wrong Format");

}
}

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(){
if(value%4==0&&value%100!=0)
return true;
else if(value%400==0)
return true;
else
return false;
}
public boolean validate(){
if(value>=1820&&value<=2020)
return true;
else
return false;
}
public void yearIncrement(){
value=value+1;
}
public void yearReduction(){
value=value-1;
}
}

class Month{
private int value;
private Year year;
public Month(){}
public Month(int yearValue,int monthValue){
this.value=monthValue;
this.year=new Year(yearValue);
}

public void setValue(int value) {
this.value = value;
}

public int getValue() {
return value;
}

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

public Year getYear() {
return year;
}
public void raseMin(){
value=1;
}
public void raseMax(){
value=12;
}

public boolean vaildate(){
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;
int t[] = new int[]{0,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;
month = new Month(yearValue, monthValue);
}

public void setValue(int value) {
this.value = value;
}

public int getValue() {
return value;
}

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

public Month getMonth() {
return month;
}

public void raseMin() {
value = 1;
}

public void raseMax() {
value =t[month.getValue()];
}
public boolean validate(){
if(this.month.getYear().isLeapYear())
t[2]=29;
else
t[2]=28;
if(value>=1&&value<=31)
{
if(this.month.getValue()<=12&&this.value>t[this.month.getValue()])
return false;
else
return true;
}
else
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){
day=new Day(d,m,y);
}

public Day getDay() {
return day;
}

public void setDay(Day day) {
this.day = day;
}
public boolean checkInputValidity(){
if(this.day.validate()&&this.day.getMonth().vaildate()&&this.day.getMonth().getYear().validate())
return true;
else
return false;
}
public boolean compareDates(DateUtil date){
if(this.day.getMonth().getYear().getValue()>date.day.getMonth().getYear().getValue())
return true;

else if(this.day.getMonth().getYear().getValue()==date.day.getMonth().getYear().getValue()&&this.day.getMonth().getValue()>date.day.getMonth().getValue())
return true;

else if(this.day.getMonth().getYear().getValue()==date.day.getMonth().getYear().getValue()&&this.day.getMonth().getValue()==date.day.getMonth().getValue()&&this.day.getValue()>date.day.getValue())
return true;
else
return false;
}
public boolean equalTwoDates(DateUtil date){
if(this.day.getMonth().getYear().getValue()==date.day.getMonth().getYear().getValue())
{
if(this.day.getMonth().getValue()==date.day.getMonth().getValue())
{
if(this.day.getValue()==date.day.getValue())
return true;
else
return false;
}
else
return false;
}
else
return false;
}
public String showDate(){
return this.day.getMonth().getYear().getValue()+"-"+this.day.getMonth().getValue()+"-"+this.day.getValue();
}
public void getNextNDays(int n) {
while (n > 0) {
if (this.day.getMonth().getYear().isLeapYear()) {
if (n >= 366) {
this.day.getMonth().getYear().yearIncrement();
n = n - 366;
}
else {
this.day.validate();
if ((this.day.getValue() + 1) <= this.day.t[this.day.getMonth().getValue()])
this.day.dayIncrement();
else {
if ((this.day.getMonth().getValue() + 1) <= 12) {
this.day.getMonth().monthIncrement();
this.day.raseMin();
} else {
this.day.getMonth().getYear().yearIncrement();
this.day.getMonth().raseMin();
this.day.raseMin();
}
}
n--;
}
}
else {
if (n >= 365) {
this.day.getMonth().getYear().yearIncrement();
n = n - 365;
} else {
this.day.validate();
if ((this.day.getValue() + 1) <= this.day.t[this.day.getMonth().getValue()])
this.day.dayIncrement();
else {
if ((this.day.getMonth().getValue() + 1) <= 12) {
this.day.getMonth().monthIncrement();
this.day.raseMin();
} else {
this.day.getMonth().getYear().yearIncrement();
this.day.getMonth().raseMin();
this.day.raseMin();
}
}
n--;
}

}

}
}
public void getPreviousNDays(int n){

while (n > 0) {
if (this.day.getMonth().getYear().isLeapYear()) {
if (n >= 366) {
this.day.getMonth().getYear().yearReduction();
n = n - 366;
}
else {
this.day.validate();
if((this.day.getValue() - 1) >= 1)
this.day.dayReduction();
else
{
if((this.day.getMonth().getValue()-1)>=1)
{
this.day.getMonth().monthReduction();
this.day.raseMax();
}
else
{
this.day.getMonth().getYear().yearReduction();
this.day.getMonth().raseMax();
this.day.raseMax();
}
}
n--;
}
}
else {
if (n >= 365) {
this.day.getMonth().getYear().yearReduction();
n = n - 365;
} else {
this.day.validate();
if((this.day.getValue() - 1) >= 1)
this.day.dayReduction();
else
{
if((this.day.getMonth().getValue()-1)>=1)
{
this.day.getMonth().monthReduction();
this.day.raseMax();
}
else
{
this.day.getMonth().getYear().yearReduction();
this.day.getMonth().raseMax();
this.day.raseMax();
}
}
n--;}
}

}

}


public int getDaysofDates(DateUtil date){
int days=0;
if(this.equalTwoDates(date))
return days;
else
{
if(this.compareDates(date))
{
while(this.day.getValue()!=date.day.getValue()||this.day.getMonth().getValue()!=date.day.getMonth().getValue()||this.day.getMonth().getYear().getValue()!=date.day.getMonth().getYear().getValue())
{
date.day.validate();
if((date.day.getValue() + 1) <= date.day.t[date.day.getMonth().getValue()])
date.day.dayIncrement();
else
{
if((date.day.getMonth().getValue()+1)<=12)
{
date.day.getMonth().monthIncrement();
date.day.raseMin();
}
else
{
date.day.getMonth().getYear().yearIncrement();
date.day.getMonth().raseMin();
date.day.raseMin();
}
}
days++;
}
}
else
{
while(this.day.getValue()!=date.day.getValue()||this.day.getMonth().getValue()!=date.day.getMonth().getValue()||this.day.getMonth().getYear().getValue()!=date.day.getMonth().getYear().getValue())
{
this.day.validate();
if((this.day.getValue() + 1) <= this.day.t[this.day.getMonth().getValue()])
this.day.dayIncrement();
else
{
if((this.day.getMonth().getValue()+1)<=12)
{
this.day.getMonth().monthIncrement();
this.day.raseMin();
}
else
{
this.day.getMonth().getYear().yearIncrement();
this.day.getMonth().raseMin();
this.day.raseMin();
}
}
days++;
}
}

}
return days;
}

}
第六次作业7-5图形继承与多态
这题与之前的相似,不同的是在该题中我应用到了Arrays 类,Collections 类。
掌握了一些基本方法。我将创建的各个图形对象均存储在 ArrayList类型的列表中,
然后通过Collections类进行排序,排序过程中运用到了comparable接口来自定义
排序规则如下

在定义排序时有如下规则

 

 

这次作业让我学到了挺多东西。

 

源代码如下

 

 

import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
int i = 0;
if(a<0||b<0||c<0)
{
System.out.print("Wrong Format");
System.exit(0);
}
ArrayList<Shape> list = new ArrayList<>();
for (i = 0; i < a; i++)
{
double radius = input.nextDouble();
Circle cc = new Circle(radius);
list.add(cc);
}
for(i=0;i<b;i++)
{
double width= input.nextDouble();
double length= input.nextDouble();
Rectangle rr=new Rectangle(width,length);
list.add(rr);
}
for(i=0;i<c;i++)
{
double side1= input.nextDouble();
double side2= input.nextDouble();
double side3= input.nextDouble();
Triangle tt=new Triangle(side1,side2,side3);
list.add(tt);
}
for(i=0;i<list.size();i++)
{
if(list.get(i).validate())
{}
else
{
System.out.print("Wrong Format");
System.exit(0);
}
}
System.out.println("Original area:");
for(i=0;i<list.size();i++)
{
System.out.print(String.format("%.2f",list.get(i).getArea())+" ");
}
double sum=0;
for(i=0;i<list.size();i++)
{
sum=sum+list.get(i).getArea();
}
System.out.println();
System.out.print("Sum of area:"+String.format("%.2f",sum));
Collections.sort(list, new Comparator<Shape>() {
@Override
public int compare(Shape o1, Shape o2) {
if(o1.getArea()>o2.getArea())
return 1;
else
return -1;
}
});
System.out.println();
System.out.print("Sorted area:");
System.out.println();
for(i=0;i<list.size();i++)
{
System.out.print(String.format("%.2f",list.get(i).getArea())+" ");
}
sum=0;
for(i=0;i<list.size();i++)
{
sum=sum+list.get(i).getArea();
}
System.out.println();
System.out.print("Sum of area:"+String.format("%.2f",sum));
}


}


class Shape{
public double getArea(){
return 0.0;
}
public boolean validate(){
return true;
}

}

class Circle extends Shape{
private double radius;
public Circle(){

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

public double getRadius() {
return radius;
}
public boolean validate(){
if(this.radius<=0)
return false;
else
return true;
}
public double getArea(){
return radius*radius*Math.PI;
}
}

class Rectangle extends Shape{
private double width;
private double length;
public Rectangle(){

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

public double getWidth() {
return width;
}

public double getLength() {
return length;
}

public void setLength(double length) {
this.length = length;
}
public boolean validate(){
if(this.width<0||this.length<0)
return false;
else
return true;
}
public double getArea(){
return width*length;
}
}

class Triangle extends Circle{
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.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;
}

public boolean validate(){
if(this.side1<0||this.side2<0||this.side3<0)
return false;
else
{
if((side1+side2)>side3&&(side1+side3)>side2&&(side3+side2)>side1&&(side1-side2)<side3&&(side1-side3)<side2&&(side3-side2)<side1&&(side2-side1)<side3&&(side3-side1)<side2&&(side2-side3)<side1)
return true;
else
return false;
}
}
public double getArea(){
double p;
p=(side1+side3+side2)/2;
return Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));
}

}
第六次作业7-6实现图形接口及多态性

 


该题与之前相似难度不大。


源代码如下


 

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Circle circle=new Circle();
Rectangle rectangle=new Rectangle();
double radius= in.nextDouble();
double width= in.nextDouble();
double length= in.nextDouble();
if(radius>0&&width>0&&length>0)
{
circle.setRadius(radius);
rectangle.setWidth(width);
rectangle.setLength(length);
System.out.println(String.format("%.2f", circle.getArea()));
System.out.print(String.format("%.2f", rectangle.getArea()));
}
else
System.out.print("Wrong Format");

}
}
class GetArea{

}
class Circle extends GetArea{
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(){
return radius*radius*Math.PI;
}

}



class Rectangle extends GetArea {
private double width;
private double length;
public Rectangle(){}
public Rectangle(double width,double length){
this.width=width;
this.length=length;
}

public double getWidth() {
return width;
}

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

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

public double getLength() {
return length;
}
public double getArea(){
return length*width;
}

}
踩坑心得
这几次作业主要是出现了数组越界,运行超时的问题。数组越界主要是数组开
了指定的空间以后,在调用数组时使用了超越数组空间的方式如

 

 如果把this.month.getvalue()<=12去掉以后,然后获得2000.13.4的后5天就会出现以下问题

 

idea会提示13超过了数组的长度所以在运用数组的时候要注意数组空间的大小,切忌数组的越界。

 

 改进建议

在这几次作业中在日期的题目中,在获得下n天的日期的方法采用了一天天的加的方法,该法会让程序循环
多次,从而多次造成运行超时的问题。所以在之后方法的构建的过程中,应该采用合适的方法来减少循环的
次数。
总结
通过这几次作业让我对Java的三大特性继承,封装,多态有了初步了解。继承是java面向对象编程技术的一块基石,因为它允许创建分等级层次的类
继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法,使得子类具有父类相同的行为。继承
可以解决代码量大且臃肿,而且维护性不高(维护性主要是后期需要修改的时候,就需要修改很多的代码,容易出错)。
在面向对象程式设计方法中,封装是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法封装可以被认为是一个保护屏障,防止该类的代码
和数据被外部类定义的代码随机访问。要访问该类的代码和数据,必须通过严格的接口控制。封装最主要的功能在于我们能修改自己的实现代码,而不用
修改那些调用我们代码的程序片段。适当的封装可以让程式码更容易理解与维护,也加强了程式码的安全性。
多态可以消除类型之间的耦合关系,灵活,简化。
在今后的学习过程中要多使用Java的三大特性,让我能够多Java有更多的了解。


 

posted @ 2021-11-13 19:11  万劫生  阅读(46)  评论(0)    收藏  举报