高抬贵手

导航

题目集4~6的总结

题目集四 7-2

代码:

import java.util.Scanner;

 

public class Main {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int choice= in.nextInt();

        int year,month,day;

        switch (choice){

            case 1:

                year = Integer.parseInt(in.next());

                month = Integer.parseInt(in.next());

                day = Integer.parseInt(in.next());

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

                if (!date.checkInputValidity()) {

                    System.out.print("Wrong Format");

                    System.exit(0);

                }

                int m = in.nextInt();

                if (m < 0) {

                    System.out.print("Wrong Format");

                    System.exit(0);

                }

                System.out.print(date.getNextNDays(m).showDate());

                break;

            case 2:

                year = Integer.parseInt(in.next());

                month = Integer.parseInt(in.next());

                day = Integer.parseInt(in.next());

 

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

 

                if (!data.checkInputValidity()) {

                    System.out.print("Wrong Format");

                    System.exit(0);

                }

                int n = in.nextInt();

                if (n < 0) {

                    System.out.print("Wrong Format");

                    System.exit(0);

                }

                System.out.print(data.getPreviousNDays(n).showDate());

                break;

            case 3:

                year = Integer.parseInt(in.next());

                month = Integer.parseInt(in.next());

                day = Integer.parseInt(in.next());

 

                int anotherYear = Integer.parseInt(in.next());

                int anotherMonth = Integer.parseInt(in.next());

                int anotherDay = Integer.parseInt(in.next());

 

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

                DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);

 

                if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {

                    System.out.print(fromDate.getDaysofDates(toDate));

                } else {

                    System.out.print("Wrong Format");

                    System.exit(0);

                }

                break;

            default:

                    System.out.print("Wrong Format");

                    System.exit(0);

                    break;

        }

    }

}

 

class DateUtil {

    Day day;

    public DateUtil() {

 

    }

 

    public DateUtil(int d, int m, int y) {

        this.day = new Day(d, m, y);

    }

 

    public Day getDay() {

        return day;

    }

 

    public void setDay(Day d) {

        this.day = d;

    }

 

    public boolean checkInputValidity() {//检测输入的年、月、日是否合法

        if (this.getDay().getMonth().getYear().validate() && this.getDay().getMonth().validate() && day.validate())

            return true;

        return false;

    }

 

    public boolean compareDates(DateUtil date) {//比较当前日期与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;

 

        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;

 

        return true;

    }

 

    public boolean equalTwoDates(DateUtil date) {//判断两个日期是否相等

        if (this.getDay().getValue() == date.getDay().getValue()

                && this.getDay().getMonth().getYear().getValue() == date.getDay().getMonth().getYear().getValue()

                && this.getDay().getMonth().getValue() == date.getDay().getMonth().getValue())

            return true;

 

        return false;

    }

 

    public String showDate() {//“year-month-day”格式返回日期值

        return this.getDay().getMonth().getYear().getValue() + "-" + this.getDay().getMonth().getValue() + "-" + this.getDay().getValue();

 

    }

 

    public DateUtil getNextNDays(int n) {//取得year-month-day的下n天日期

        int[] arr = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        int year, month, day;

        int rest = restday(this);

        if (rest > n) {//本年

            year = this.getDay().getMonth().getYear().getValue();

            int Mday = arr[this.getDay().getMonth().getValue()];

            if (this.getDay().getMonth().getYear().isLeapYear() && this.getDay().getMonth().getValue() == 2) {

                Mday++;

            }

            Mday -= this.getDay().getValue();//本月剩余的日期

            if (Mday >= n) {    //本月

                month = this.getDay().getMonth().getValue();

                day = n + this.getDay().getValue();

            } else {    //其他月

                n -= Mday;

                month = this.getDay().getMonth().getValue() + 1;

                int k = month;

                while (n - arr[k] > 0) {

                    n -= arr[k];

                    month++;

                    k++;

                }

                day = n;

            }

        } else {

            n -= rest;

            year = this.getDay().getMonth().getYear().getValue() + 1;

            int y = 365;

            if (new Year(year).isLeapYear()) {

                y++;

            }

            while (n - y > 0) {

                n -= y;

                year++;

                y = 365;

                if (new Year(year).isLeapYear())

                    y++;

            }

            int k = 1;

            while (n - arr[k] > 0) {

                n -= arr[k];

                k++;

            }

            month = k;

            day = n;

        }

        return new DateUtil(year, month, day);

    }

 

    public int restday(DateUtil d) {

        int n = 0;

        int[] arr = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        for (int i = d.getDay().getMonth().getValue() + 1; i <= 12; i++) {

            n += arr[i];

        }

        n += arr[d.getDay().getMonth().getValue()] - d.getDay().getValue();

        if (d.getDay().getMonth().getYear().isLeapYear() && d.getDay().getMonth().getValue() <= 2)

            n++;

        return n;

    }

 

    public DateUtil getPreviousNDays(int n) {//取得year-month-day的前n天日期

        int[] arr = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        int year, month, day;

        int rest = 365 - restday(this);

        if (this.getDay().getMonth().getYear().isLeapYear()) {

            rest++;

        }

        if (rest > n) {//本年

            year = this.getDay().getMonth().getYear().getValue();

            int mday = this.getDay().getValue();//本月剩余的日期

            if (mday > n) {    //本月

                month = this.getDay().getMonth().getValue();

                day = mday - n;

            } else {    //其他月

                n -= mday;

                month = this.getDay().getMonth().getValue() - 1;

                if (month == 0) {

                    month = 12;

                    year = this.getDay().getMonth().getYear().getValue() - 1;

                }

                int k = month;

                while (n - arr[k] > 0) {

                    n -= arr[k];

                    month--;

                    k--;

                }

                day = arr[k] - n;

                if (new Year(year).isLeapYear() && month == 2) {

                    day++;

                }

            }

        } else {

            n -= rest;

            year = this.getDay().getMonth().getYear().getValue() - 1;

            int y = 365;

            if (new Year(year).isLeapYear()) {

                y++;

            }

            while (n - y > 0) {

                n -= y;

                year--;

                y = 365;

                if (new Year(year).isLeapYear())

                    y++;

            }

            int k = 12;

            while (n - arr[k] > 0) {

                n -= arr[k];

                k--;

            }

            month = k;

            day = arr[k] - n;

            if (new Year(year).isLeapYear() && month == 2) {

                day++;

            }

        }

        return new DateUtil(year, month, day);

    }

 

    public int getDaysofDates(DateUtil date) {//求当前日期与date之间相差的天数

        DateUtil pred = this;

        DateUtil nextd = date;

        if (this.equalTwoDates(date)) {

            return 0;

        } else if (!this.compareDates(date)) {

            pred = date;

            nextd = this;

        }

        int[] arr = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        int i, j, d = 0;

        for (i = pred.getDay().getMonth().getYear().getValue() + 1; i < nextd.getDay().getMonth().getYear().getValue(); i++) {

            d = d + 365;

            if (new Year(i).isLeapYear())

                d++;

        }

        if (pred.getDay().getMonth().getYear().getValue() != nextd.getDay().getMonth().getYear().getValue()) {

            for (j = pred.getDay().getMonth().getValue() + 1; j <= 12; j++)

                d = d + arr[j];

            d += arr[pred.getDay().getMonth().getValue()] - pred.getDay().getValue();

            for (j = 1; j < nextd.getDay().getMonth().getValue(); j++)

                d += arr[j];

            d += nextd.getDay().getValue();

            if (pred.getDay().getMonth().getYear().isLeapYear() && pred.getDay().getMonth().getValue() <= 2)

                d++;

            if (nextd.getDay().getMonth().getYear().isLeapYear() && nextd.getDay().getMonth().getValue() > 2) {

                d++;

            }

        } else if (pred.getDay().getMonth().getYear().getValue() == nextd.getDay().getMonth().getYear().getValue() && pred.getDay().getMonth().getValue() != nextd.getDay().getMonth().getValue()) {

            for (j = pred.getDay().getMonth().getValue() + 1; j <= nextd.getDay().getMonth().getValue() - 1; j++)

                d += arr[j];

            d += arr[pred.getDay().getMonth().getValue()] - pred.getDay().getValue();

            d += nextd.getDay().getValue();

            if (pred.getDay().getMonth().getYear().isLeapYear() && pred.getDay().getMonth().getValue() <= 2)

                d++;

        } else if (pred.getDay().getMonth().getYear().getValue() == nextd.getDay().getMonth().getYear().getValue() && pred.getDay().getMonth().getValue() == nextd.getDay().getMonth().getValue()) {

            d = nextd.getDay().getValue() - pred.getDay().getValue();

        }

        return d;

    }

}

 

class Day{

    int value;

    Month month;

    int[] mon_maxnum ={31,28,31,30,31,30,31,31,30,31,30,31};

    public Day() {

 

    }

    public Day(int yearValue,int monthValue,int dayValue) {

        this.month = new Month(yearValue,monthValue);

        this.value = dayValue;

    }

    public int getValue() {

        return value;

    }

    public void setValue(int value) {

        this.value = value;

    }

    public Month getMonth() {

        return month;

    }

    public void setMonth(Month value) {

        this.month = value;

    }

    public void resetMin() {

        value=1;

    }

    public void resetMax() {

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

    }

    public boolean validate() {

        if(this.getMonth().getYear().isLeapYear())

            mon_maxnum[1]++;

        if(value>=1 && mon_maxnum[month.getValue()-1]>=value)

            return true;

        return false;

    }

    public void dayIncrement() {

        value++;

    }

    public void dayReduction() {

        value--;

    }

}

 

class Month{

    int value;

    Year year;

    public Month() {

 

    }

    public Month(int yearValue,int monthValue) {

        this.year = new Year(yearValue);

        this.value = monthValue;

    }

    public int getValue() {

        return value;

    }

    public void setValue(int value) {

        this.value = value;

    }

    public Year getYear() {

        return year;

    }

    public void setYear(Year year) {

        this.year = year;

    }

    public void resetMin() {

        value=1;

    }

    public void resetMax() {

        value=12;

    }

    public boolean validate() {

        if(value>=1 && value<=12)

            return true;

        return false;

    }

    public void monthIncrement() {

        value++;

    }

    public void monthReduction() {

        value--;

    }

}

 

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(){//判断year是否为闰年

        boolean result;

        result=(value%4 == 0 && value%100 != 0)||value%400 == 0;

        if(result)

            return true;

        else

            return false;

    }

    public boolean validate() {

        if(value>=1900 && value<=2050)

            return true;

        return false;

    }

    public void yearIncrement() {

        value++;

    }

    public void yearReduction() {

        value--;

    }

}

 

题目集五7-5

代码:

import java.util.Scanner;

 

public class Main {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int choice= in.nextInt();

        int year,month,day;

        switch (choice){

            case 1:

                year = Integer.parseInt(in.next());

                month = Integer.parseInt(in.next());

                day = Integer.parseInt(in.next());

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

                if (!date.checkInputValidity()) {

                    System.out.print("Wrong Format");

                    System.exit(0);

                }

                int m = in.nextInt();

                if (m < 0) {

                    System.out.print("Wrong Format");

                    System.exit(0);

                }

                System.out.print(date.showDate() + " next " + m + " days is:"+date.getNextNDays(m).showDate());

                break;

            case 2:

                year = Integer.parseInt(in.next());

                month = Integer.parseInt(in.next());

                day = Integer.parseInt(in.next());

 

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

 

                if (!data.checkInputValidity()) {

                    System.out.print("Wrong Format");

                    System.exit(0);

                }

                int n = in.nextInt();

                if (n < 0) {

                    System.out.print("Wrong Format");

                    System.exit(0);

                }

                System.out.print(data.showDate() + " previous " + n + " days is:"+data.getPreviousNDays(n).showDate());

                break;

            case 3:

                year = Integer.parseInt(in.next());

                month = Integer.parseInt(in.next());

                day = Integer.parseInt(in.next());

 

                int anotherYear = Integer.parseInt(in.next());

                int anotherMonth = Integer.parseInt(in.next());

                int anotherDay = Integer.parseInt(in.next());

 

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

                DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);

 

                if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {

                    System.out.print("The days between " + fromDate.showDate() + " and " + toDate.showDate() + " are:"

                            + fromDate.getDaysofDates(toDate));

                } else {

                    System.out.print("Wrong Format");

                    System.exit(0);

                }

                break;

            default:

                    System.out.print("Wrong Format");

                    System.exit(0);

                    break;

        }

    }

}

 

class DateUtil {

    Year year;

    Month month;

    Day day;

    int[] mon_maxnum ={31,28,31,30,31,30,31,31,30,31,30,31};

    public DateUtil() {

 

    }

    public DateUtil(int y, int m, int d) {

        this.day = new Day(d);

        this.month = new Month(m);

        this.year = new Year(y);

    }

    public Year getYear(){

        return year;

    }

    public void setYear(Year year){

        this.year=year;

    }

    public Month getMonth(){

        return month;

    }

    public void setMonth(Month month){

        this.month=month;

    }

    public Day getDay() {

        return day;

    }

 

    public void setDay(Day day) {

        this.day = day;

    }

    public void setDayMin(){

        this.day.value=1;

    }

    public void getDayMax(){

        if(this.year.isLeapYear())

            mon_maxnum[1]++;

        this.day.value=mon_maxnum[this.month.value-1];

    }

    public boolean checkInputValidity() {//检测输入的年、月、日是否合法

        if (this.getYear().validate() && this.getMonth().validate()){

            if(!new Year(this.getYear().getValue()).isLeapYear()){

                if(this.getDay().getValue()<=mon_maxnum[this.getMonth().getValue()-1])

                    return true;

            }

            else if(this.getDay().getValue()<=mon_maxnum[this.getMonth().getValue()-1] || this.getDay().getValue()==29)

                return true;

        }

        return false;

    }

    public DateUtil getNextNDays(int n) {//取得year-month-day的下n天日期

        int[] arr = {0,31,28,31,30,31,30,31,31,30,31,30,31};

        int year, month, day;

        int rest = restday(this);

        if (rest>n) {//本年

            year=this.getYear().getValue();

            int mday = arr[this.getMonth().getValue()];

            if (this.getYear().isLeapYear()&&this.getMonth().getValue()==2) {

                mday++;

            }

            mday-=this.getDay().getValue();//本月剩余的日期

            if (mday>=n) {    //本月

                month = this.getMonth().getValue();

                day = n+this.getDay().getValue();

            }

            else{    //其他月

                n-=mday;

                month = this.getMonth().getValue()+1;

                int k = month;

                while(n-arr[k]>0){

                    n -= arr[k];

                    month++;

                    k++;

                }

                day = n;

            }

        }

        else {

            n-=rest;

            year = this.getYear().getValue()+1;

            int y = 365;

            if (new Year(year).isLeapYear()) {

                y++;

            }

            while(n-y>0){

                n-=y;

                year++;

                y=365;

                if (new Year(year).isLeapYear())

                    y++;

            }

            int k = 1;

            while(n-arr[k]>0){

                n -= arr[k];

                k++;

            }

            month = k;

            day = n;

        }

        return new DateUtil(year, month, day);

    }

 

    public DateUtil getPreviousNDays(int n) {//取得year-month-day的前n天日期

        int arr[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};

        int year, month, day;

        int rest = 365-restday(this);

        if (this.getYear().isLeapYear()) {

            rest++;

        }

        if (rest>n) {//本年

            year=this.getYear().getValue();

            int mday=this.getDay().getValue();//本月剩余的日期

            if (mday>n) {    //本月

                month = this.getMonth().getValue();

                day = mday-n;

            }

            else{    //其他月

                n-=mday;

                month = this.getMonth().getValue()-1;

                if (month==0) {

                    month = 12;

                    year=this.getYear().getValue()-1;

                }

                int k = month;

                while(n-arr[k]>0){

                    n -= arr[k];

                    month--;

                    k--;

                }

                day = arr[k]-n;

                if (new Year(year).isLeapYear()&&month==2) {

                    day++;

                }

            }

        }

        else {

            n-=rest;

            year = this.getYear().getValue()-1;

            int y = 365;

            if (new Year(year).isLeapYear()) {

                y++;

            }

            while(n-y>0){

                n-=y;

                year--;

                y=365;

                if (new Year(year).isLeapYear())

                    y++;

            }

            int k = 12;

            while(n-arr[k]>0){

                n -= arr[k];

                k--;

            }

            month = k;

            day = arr[k]-n;

            if (new Year(year).isLeapYear()&&month==2) {

                day++;

            }

        }

        return new DateUtil(year, month, day);

    }

 

    public boolean compareDates(DateUtil date) {//比较当前日期与date的大小(先后)

        if (date.getYear().getValue() < this.getYear().getValue())

            return false;

 

        else if (date.getYear().getValue() == this.getYear().getValue()

                && date.getMonth().getValue() < this.getMonth().getValue())

            return false;

 

        if (date.getYear().getValue() == this.getYear().getValue()

                && date.getMonth().getValue() == this.getMonth().getValue()

                && date.getDay().getValue() < this.getDay().getValue())

            return false;

        return true;

    }

 

    public int restday(DateUtil d) {

        int n = 0;

        int arr[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};

        for (int i = d.getMonth().getValue()+1; i <=12; i++) {

            n+=arr[i];

        }

        n+=arr[d.getMonth().getValue()]-d.getDay().getValue();

        if(d.getYear().isLeapYear()&&d.getMonth().getValue()<=2)

            n++;

        return n;

    }

 

    public boolean equalTwoDates(DateUtil date) {//判断两个日期是否相等

        if (this.getDay().getValue() == date.getDay().getValue()

                && this.getMonth().getValue() == date.getMonth().getValue()

                && this.getYear().getValue() == date.getYear().getValue())

            return true;

        return false;

    }

    public int getDaysofDates(DateUtil date) {//求当前日期与date之间相差的天数

        DateUtil pred = this;

        DateUtil nextd = date;

        if (this.equalTwoDates(date)) {

            return 0;

        } else if (!this.compareDates(date)){

            pred = date;

            nextd = this;

        }

        int[] arr = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        int i, d = 0;

        for (i = pred.getYear().getValue() + 1; i < nextd.getYear().getValue(); i++) {

            d = d + 365;

            if (new Year(i).isLeapYear())

                d++;

        }

        for(i=pred.getMonth().getValue()+1;i<=12;i++){

            if(i==2 && new Year(pred.getYear().getValue()).isLeapYear())

                d++;

            d+=arr[i];

        }

        d+=arr[pred.getMonth().getValue()]-pred.getDay().getValue();

        for(i=1;i<nextd.getMonth().getValue();i++){

            d+=arr[i];

        }

        if(new Year(nextd.getYear().getValue()).isLeapYear() && (nextd.getMonth().getValue()>2 || nextd.getDay().getValue()==29))

            d++;

        d+=nextd.getDay().getValue();

        return d;

    }

 

    public String showDate() {//“year-month-day”格式返回日期值

        return this.getYear().getValue() + "-" + this.getMonth().getValue() + "-" + this.getDay().getValue();

    }

}

class Day{

    int value;

    public Day() {

 

    }

    public Day(int value) {

        this.value =value;

    }

    public int getValue() {

        return value;

    }

    public void setValue(int value) {

        this.value = value;

    }

    public void dayIncrement() {

        value++;

    }

    public void dayReduction() {

        value--;

    }

}

 

class Month{

    int value;

    public Month() {

 

    }

    public Month(int value) {

        this.value =value;

    }

    public int getValue() {

        return value;

    }

    public void setValue(int value) {

        this.value = value;

    }

    public void resetMin() {

        value=1;

    }

    public void resetMax() {

        value=12;

    }

    public boolean validate() {

        if(value>=1 && value<=12)

            return true;

        return false;

    }

    public void monthIncrement() {

        value++;

    }

    public void monthReduction() {

        value--;

    }

}

 

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(){//判断year是否为闰年

        boolean result;

        result=(value%4 == 0 && value%100 != 0)||value%400 == 0;

        if(result)

            return true;

        else

            return false;

    }

    public boolean validate() {

        if(value>=1820 && value<=2020)

            return true;

        return false;

    }

    public void yearIncrement() {

        value++;

    }

    public void yearReduction() {

        value--;

    }

}

 

比较:

题目集四7-2类图

 

 

 

题目集五7-5类图:

 

 

 

比较:可以看出,这两题几乎一模一样,但在构建类的思想上有所不同,第一种采用分开年月日,层层依赖,牵一发而动全身,而第二种就有所不同了,在一个类DateUtil里囊括年月日的调用思想,如果后续要加入其他功能的话,第二种应该是要比第一种更适合编辑修改。

 

题目集四 7-3

代码:

import java.util.Scanner;

 

public class Main {

    public static void main(String[] args) {

        Scanner in=new Scanner(System.in);

        int choice=in.nextInt();

        switch (choice){

            case 1 :{

                double r = in.nextDouble();

                if (r <= 0){

                    System.out.print("Wrong Format");

                    System.exit(-1);//输入错误,直接结束;

                }

                Circle circle = new Circle();

                circle.setRadius(r);

                System.out.printf("Circle's area:%.2f",circle.getArea());

                break;

            }

            case 2 :{

                double length = in.nextDouble();

                double width = in.nextDouble();

                if (length <= 0 || width <= 0){

                    System.out.print("Wrong Format");

                    System.exit(-1);

                }

                Rectangle rectangle = new Rectangle();

                rectangle.setLength(length);

                rectangle.setWidth(width);

                System.out.printf("Rectangle's area:%.2f",rectangle.getArea());

                break;

            }

            case 3 :{

                double radius = in.nextDouble();

                if (radius <= 0){

                    System.out.print("Wrong Format");

                    System.exit(-1);

                }

                Ball ball = new Ball();

                ball.setRadius(radius);

                System.out.printf("Ball's surface area:%.2f\n",ball.getArea());

                System.out.printf("Ball's volume:%.2f",ball.getVolume());

                break;

            }

            case 4 :{

                double length = in.nextDouble();

                double width = in.nextDouble();

                double height = in.nextDouble();

                if (length <= 0 || width <= 0 || height <= 0){

                    System.out.print("Wrong Format");

                    System.exit(-1);

                }

                Box box = new Box();

                box.setLength(length);

                box.setWidth(width);

                box.setHeight(height);

                System.out.printf("Box's surface area:%.2f\n",box.getArea());

                System.out.printf("Box's volume:%.2f",box.getVolume());

                break;

            }

            default :

                System.out.print("Wrong Format");

                System.exit(-1);

        }

    }

}

 

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 double getRadius(){

        return radius;

    }

    public void setRadius(double radius){

        this.radius = radius;

    }

    public double getArea(){

        return Math.pow(radius,2)* Math.PI;

    }

}

 

class Rectangle extends Shape{//求矩形面积

    private double width;

    private double length;

    public Rectangle(){

        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 double getArea(){

        return 4 * Math.pow(super.getRadius(),2) * Math.PI;

    }

    public double getVolume(){

        return 4.0 / 3.0 * Math.PI * Math.pow(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 (super.getArea() + height * super.getWidth() + height * super.getLength()) * 2;

    }

    public double getVolume(){

        return height * super.getArea();

    }

}

 

 

 

 

分析:这是典型的继承题目,球继承了圆形的各项属性和方法,立方体继承了矩形的各项属性和方法,这种继承的好处在于,在实现想要获取的图形不同时,比如圆和球体都需要,采用继承,可以分别获取,也避免了球体重复圆形的方法,减少了冗余度,整个代码工整。

 

题目集六 7-5

代码:

 

import java.util.Scanner;

import java.util.Arrays;

 

public class Main {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int a=in.nextInt();

        int b=in.nextInt();

        int c=in.nextInt();

        if (a<0 || b<0 ||c<0){

            System.out.print("Wrong Format");

            System.exit(-1);

        }

        double[] A=new double[a+b+c];

        int j=0;

        for(int i=0;i<a;i++){

            double radius=in.nextDouble();

            if (radius <= 0){

                System.out.print("Wrong Format");

                System.exit(-1);//输入错误,直接结束;

            }

            Circle circle = new Circle();

            circle.setRadius(radius);

            A[j]=circle.getArea();

            j++;

        }

        for(int i=0;i<b;i++){

            double width=in.nextDouble();

            double length=in.nextDouble();

            if (length <= 0 || width <= 0){

                System.out.print("Wrong Format");

                System.exit(-1);

            }

            Rectangle rectangle = new Rectangle();

            rectangle.setLength(length);

            rectangle.setWidth(width);

            A[j]=rectangle.getArea();

            j++;

        }

        for(int i=0;i<c;i++){

            double side1=in.nextDouble();

            double side2=in.nextDouble();

            double side3=in.nextDouble();

            if (side1+side2<=side3 ||side1+side3<=side2 || side2+side3<=side1 || side1<=0 || side2<=0 || side3<=0){

                System.out.print("Wrong Format");

                System.exit(-1);

            }

            Triangle triangle= new Triangle();

            triangle.setSide1(side1);

            triangle.setSide2(side2);

            triangle.setSide3(side3);

            A[j]=triangle.getArea();

            j++;

        }

        System.out.println("Original area:");

        double sum=0;

        for(int k=0;k<a+b+c;k++){

            System.out.printf("%.2f ",A[k]);

            sum+=A[k];

        }

        System.out.print("\n");

        System.out.printf("Sum of area:%.2f\n",sum);

        Arrays.sort(A);

        System.out.println("Sorted area:");

        for(int k=0;k<a+b+c;k++){

            System.out.printf("%.2f ",A[k]);

        }

        System.out.print("\n");

        System.out.printf("Sum of area:%.2f",sum);

    }

}

 

class Shape{

    public double getArea(){

        return 0.0;

    }

}

 

class Circle extends Shape{//求圆面积

    private double radius;

    public double getRadius(){

        return radius;

    }

    public void setRadius(double radius){

        this.radius = radius;

    }

    public boolean validate(double radius){

        return radius>0;

    }

    public double getArea(){

        return Math.pow(radius,2)* Math.PI;

    }

}

 

class Rectangle extends Shape{//求矩形面积

    private double width;

    private double length;

    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 Triangle extends Shape{//求三角形面积

    private double side1;

    private double side2;

    private double 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(double side1,double side2,double side3){

        return side1+side2>side3;

    }

    public double getArea(){

        return (1.0/4.0)*Math.sqrt((side1+side2+side3)*(side1+side2-side3)*(side1+side3-side2)*(side2+side3-side1));

    }

}

 

分析:这题和上题一样,用到的继承方法如出一辙,不同的只是在主函数调用的地方用了一些循环,在中心思想上差不多。

 

题目集六7-6

代码:

 

import java.util.Scanner;

 

public class Main {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        double radius=in.nextDouble();

        double width=in.nextDouble();

        double length=in.nextDouble();

        if (radius <= 0){

            System.out.print("Wrong Format");

            System.exit(-1);//输入错误,直接结束;

        }

        Circle circle = new Circle();

        circle.setRadius(radius);

        if (length <= 0 || width <= 0){

            System.out.print("Wrong Format");

            System.exit(-1);

        }

        Rectangle rectangle = new Rectangle();

        rectangle.setLength(length);

        rectangle.setWidth(width);

        System.out.printf("%.2f\n",circle.getArea());

        System.out.printf("%.2f",rectangle.getArea());

    }

}

 

class Shape{

    public double getArea(){

        return 0.0;

    }

}

 

class Circle extends Shape{//求圆面积

    private double radius;

    public double getRadius(){

        return radius;

    }

    public void setRadius(double radius){

        this.radius = radius;

    }

    public boolean validate(double radius){

        return radius>0;

    }

    public double getArea(){

        return Math.pow(radius,2)* Math.PI;

    }

}

 

class Rectangle extends Shape{//求矩形面积

    private double width;

    private double length;

    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 Triangle extends Shape{//求三角形面积

    private double side1;

    private double side2;

    private double 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(double side1,double side2,double side3){

        return side1+side2>side3;

    }

    public double getArea(){

        return (1.0/4.0)*Math.sqrt((side1+side2+side3)*(side1+side2-side3)*(side1+side3-side2)*(side2+side3-side1));

    }

}

分析:这题也是一样的继承,不过少了三角形的类,每个图形继承自形状的特点。

 

题目集五 7-4

代码:

import java.util.*;

import java.util.regex.*;

 

public class Main {

 

    public static void main(String[] args) {

        Scanner in=new Scanner(System.in);

        String important="abstract  assert  boolean  break  byte  case  catch  char  class  const  continue  default  do " +

                " double  else  enum  extends  final  finally  float  for  goto  if  implements  import  instanceof  int  " +

                "interface  long  native  new  package  private  protected  public  return  strictfp  short  static  super  " +

                "switch  synchronized  this  throw  throws  transient  try  void  volatile  while  true  false  null";

        String []end=important.split("  ");

        Map<String,Integer> mp= new TreeMap<>();

        for(String e:end)  mp.put(e,0);

 

        String s;

        int flag=0;

        StringBuilder t=new StringBuilder();

 

        while(!(s=in.nextLine().trim()).equals("exit")){

            if(s.matches(".*//.*")) continue;

            t.append(s).append(" ");

        }

        s=t.toString();

        s=change(s);

        Pattern p=Pattern.compile("/\\*(.*)?\\*/");

        Matcher m=p.matcher(s);

        while(m.find()){

            s=s.replace(m.group()," ");

            m=p.matcher(s);

        }

 

        p=Pattern.compile("\"(.*?)\"");

        m=p.matcher(s);

        while(m.find()){

            s=s.replace(m.group()," ");

            m=p.matcher(s);

        }

 

        if(s.length()==0){ System.out.print("Wrong Format");System.exit(0);}

        s=s.replaceAll("\\p{P}"," ");

 

        String[] temp=s.split("\\s+");

        for(String e:temp)

            if(mp.containsKey(e)) { mp.put(e,mp.get(e)+1);flag=1; }

 

        if(flag==0) System.exit(0);

 

        List<Map.Entry<String, Integer>> list= new ArrayList<>(mp.entrySet());

        list.stream().sorted(Map.Entry.comparingByKey()).filter(u-> u.getValue()!=0).forEach(u-> System.out.printf("%d\t%s\n",u.getValue(),u.getKey()));

 

 

    }

    static String []special = {"\\$","_","int\\(\\)","boolean\\(\\)","double\\(\\)","float\\(\\)","byte\\(\\)","long\\(\\)","short\\(\\)","\\*"};

    public static String change(String s){

        if (s.length()<800) return s;

        for (String e: special)  s = s.replaceAll(e, "CYQ");

        return  s;

    }

}

分析:这道题先用正则表达式处理输入的字符串,然后模式匹配java关键字,表面处理起来没啥问题,但是细究会发现没那么简单,题目测试点有很多没发现的地方,让我一头雾水,最后在同学的帮助下完成了这道题,总的来说,这道题还是很有借鉴思路的。

 

总结:这三次作业集,明显地可以看出难度上来了,也意味着我们向着更高深的java知识迈进,学到了很多,这次的作业分析,希望能够帮助了大家。

posted on 2021-11-13 20:05  高抬贵手  阅读(35)  评论(0编辑  收藏  举报