OO第一次博客作业

OO第一次博客作业

 第一次博客,写的有些仓促也不完美,主要是希望能与大家一起分享交流互相学习,望谅解QAQ。这次PTA作业考察的知识点有:基本的函数运用(if,for,switch等), 构造对象,构造对象方法,正则表达式等。

难度:容易~中等

目录:

1.PTA题目集1的7-8(判断三角形类型)

2.PTA题目集2的7-4(求下一天)

3.PTA题目集2的7-5(求前N天)

4.PTA题目集3的7-2(定义日期类)

5.PTA题目集3的7-3(一元多项式求导(类设计))


1.PTA题目集1的7-8(判断三角形类型)

要求:输入三角形三条边,判断该三角形为什么类型的三角形。

输入格式:在一行中输入三角形的三条边的值(实型数),可以用一个或多个空格或回车分隔,其中三条边的取值范围均为[1,200]。

输出格式:(1)如果输入数据非法,则输出“Wrong Format”;(2)如果输入数据合法,但三条边不能构成三角形,则输出“Not a triangle”; (3)如果输入数据合法且能够成等边三角形,则输出“Equilateral triangle”; (3)如果输入数据合法且能够成等腰直角三角形,则输出“Isosceles right-angled triangle”; (5)如果输入数据合法且能够成等腰三角形,则输出“Isosceles triangle”; (6)如果输入数据合法且能够成直角三角形,则输出“Right-angled triangle”; (7)如果输入数据合法且能够成一般三角形,则输出“General triangle”。

输入样例1:

在这里给出一组输入。例如:

50 50 50.0

输出样例1:

在这里给出相应的输出。例如:

Equilateral triangle

输入样例2:

在这里给出一组输入。例如:

60.2 60.2 80.56

输出样例2:

在这里给出相应的输出。例如:

Isosceles triangle

输入样例3:

在这里给出一组输入。例如:

0.5 20.5 80

输出样例3:

在这里给出相应的输出。例如:

Wrong Format
 
import java.util.Scanner;
public class Main 
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        double a = input.nextDouble();
        double b = input.nextDouble();
        double c = input.nextDouble();
        double x,y,z;
        if(a < 1 || a > 200 || b < 1 || b > 200 || c < 1 || c > 200)
        {
            System.out.println("Wrong Format");
            System.exit(0);
        }
        if(a > b && a > c)
        {
            x = a;
            if(b > c)
            {
                y = b;
                z = c;
            }
            else
            {
                y = c;
                z = b;
            }
        }
        else if(b > c)
        {
            x = b;
            if(a > c)
            {
                y = a;
                z = c;
            }
            else
            {
                y = c;
                z = a;
            }
        }
        else
        {
            x = c;
            if(a > b)
            {
                y = a;
                z = b;
            }
            else
            {
                y = b;
                z = a;
            }
        }
        if(y + z > x)
        {
            if (x == y && y == z)
            {
                System.out.println("Equilateral triangle");
                System.exit(0);
            }
            else if ((x == y || y == z || x == z) && ((y * y + z * z - x * x < 0.00000001 && x * y + z * z - x * x > -0.00000001)))
            {
                System.out.println("Isosceles right-angled triangle");
                System.exit(0);
            }
            else if ((x == y || y == z || x == z))
            {
                System.out.println("Isosceles triangle");
                System.exit(0);
            }
            else if (y * y + z * z - x * x < 0.00000001 && y * y + z * z - x * x > -0.00000001)
            {
                System.out.println("Right-angled triangle");
                System.exit(0);
            }
            else
            {
                System.out.println("General triangle");
                System.exit(0);
            }
        }
        else
        {
            System.out.println("Not a triangle");
            System.exit(0);
        }
        input.close();
    }
}

分析:这题不难,就是去运用if-else嵌套来判断各个边的关系,但是在做的过程中,测试的时候老有一个测试点过不了,最后才想明白是精度值的问题,因为键盘想要输入根号2这样的无理数时,只能输入一定的精度值,导致程序刚开始时无法判断直角三角形。

心得:感觉对各种数据类型的数据还不敏感,没有充分考虑到,要加强对各种输入数据的分析强度,这道题难度不大,但要注重细节。

改进建议:三边长度的排序方法可以优化,可以构建一个三角形类来使main函数更简洁。


2.PTA题目集2的7-4(求下一天)

输入年月日的值(均为整型数),输出该日期的下一天。 其中:年份的合法取值范围为[1820,2020] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。 

注意:不允许使用Java中和日期相关的类和方法。

要求:Main类中必须含有如下方法,签名如下:

public static void main(String[] args);//主方法 
public static boolean isLeapYear(int year) ;//判断year是否为闰年,返回boolean类型 
public static boolean checkInputValidity(int year,int month,int day);//判断输入日期是否合法,返回布尔值
public static void nextDate(int year,int month,int day) ; //求输入日期的下一天

输入格式:

在一行内输入年月日的值,均为整型数,可以用一到多个空格或回车分隔。

输出格式:

  1. 当输入数据非法及输入日期不存在时,输出“Wrong Format”;
  2. 当输入日期合法,输出下一天,格式如下:Next date is:年-月-日

输入样例1:

在这里给出一组输入。例如:

2020 3 10

输出样例1:

在这里给出相应的输出。例如:

Next date is:2020-3-11

输入样例2:

在这里给出一组输入。例如:

2025 2 10

输出样例2:

在这里给出相应的输出。例如:

Wrong Format
 
import java.util.Scanner;
public class Main 
{
    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);
        int y, m, d;
        y = input.nextInt();
        m = input.nextInt();
        d = input.nextInt();
        if (checkInputValidty(y, m, d) == false)
        {
            System.out.print("Wrong Format");
            System.exit(1);
        }
        nextDate(y, m, d);
        input.close();
    }
    
    public static boolean isLeapYear(int year)
    {
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public static boolean checkInputValidty(int year,int month,int day)
    {
        boolean i = false;
        if ((year < 1820 || year > 2020) || (month < 1 || month > 12) || (day < 1 || day > 31))
        {
            i = false;
            return i;
        }
        switch(month)
        {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
            {
                if (day > 31)
                {
                    i = false;
                }
                else
                {
                    i = true;
                }
                break;
            }
            case 4:
            case 6:
            case 9:
            case 11:
            {
                if (day > 30)
                {
                    i = false;
                }
                else
                {
                    i = true;
                }
                break;
            }
            case 2:
            {
                if (isLeapYear(year) == true)
                {
                    if (day > 29)
                    {
                        i = false;
                    }
                    else
                    {
                        i = true;
                    }
                }
                else
                {
                    if (day > 28)
                    {
                        i = false;
                    }
                    else
                    {
                        i = true;
                    }
                }
                break;
            }
        }
        return i;
    }
    
    public static void nextDate(int year,int month,int day)
    {
        switch(month)
        {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
            {
                if (day == 31)
                {
                    if (month == 12)
                    {
                        year++;
                        month = 1;
                        day = 1;
                    }
                    else
                    {
                        month++;
                        day = 1;
                    }
                }
                else
                {
                    day++;
                }
                break;
            }
            case 4:
            case 6:
            case 9:
            case 11:
            {
                if (day == 30)
                {
                    month++;
                    day = 1;
                }
                else
                {
                    day++;
                }
                break;
            }
            case 2:
            {
                if (isLeapYear(year) == true)
                {
                    if (day == 29)
                    {
                        month++;
                        day = 1;
                    }
                    else
                    {
                        day++;
                    }
                }
                else
                {
                    if (day == 28)
                    {
                        month++;
                        day = 1;
                    }
                    else
                    {
                        day++;
                    }
                }
                break;
            }
                
        }
        System.out.print("Next date is:" + year + "-" + month + "-" + day);
        System.exit(0);
    }
}

分析:偏简单的题目,但因为要自己设计方法不能有自带的方法,导致代码冗长。

心得:主要是二月份要判断的条件相对其他月比较多,总体上也不难。

改进建议:限于题目要求以及做题时的水平,没有用上类相关的东西,设计一个类可以让代码更简洁。


3.PTA题目集2的7-5(求前N天)

要求:输入年月日的值(均为整型数),同时输入一个取值范围在[-10,10] 之间的整型数n,输出该日期的前n天(当n > 0时)、该日期的后n天(当n<0时)。
其中年份取值范围为 [1820,2020] ,月份取值范围为[1,12] ,日期取值范围为[1,31] 。
注意:不允许使用Java中任何与日期有关的类或方法。

输入格式:

在一行中输入年月日的值以及n的值,可以用一个或多个空格或回车分隔。

输出格式:

  1. 当输入的年、月、日以及n的值非法时,输出“Wrong Format”;
  2. 当输入数据合法时,输出“n days ago is:年-月-日”

输入样例1:

在这里给出一组输入。例如:

2018  6 19 8 

输出样例1:

在这里给出相应的输出。例如:

8 days ago is:2018-6-11

输入样例2:

在这里给出一组输入。例如:

2018  6 19 -8 

输出样例2:

在这里给出相应的输出。例如:

-8 days ago is:2018-6-27
 
import java.util.Scanner;
public class Main 
{
    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);
        int y, m, d, n;
        y = input.nextInt();
        m = input.nextInt();
        d = input.nextInt();
        n = input.nextInt();
        if (checkInputValidty(y, m, d, n) == false)
        {
            System.out.print("Wrong Format");
            System.exit(1);
        }
        whatDate(y, m, d, n);
        input.close();
    }
    
    public static boolean isLeapYear(int year)
    {
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public static boolean checkInputValidty(int year, int month, int day, int n)
    {
        boolean i = false;
        if (Math.abs(n) > 10)
        {
            i = false;
            return i;
        }
        if ((year < 1820 || year > 2020) || (month < 1 || month > 12) || (day < 1 || day > 31))
        {
            i = false;
            return i;
        }
        switch(month)
        {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
            {
                if (day > 31)
                {
                    i = false;
                }
                else
                {
                    i = true;
                }
                break;
            }
            case 4:
            case 6:
            case 9:
            case 11:
            {
                if (day > 30)
                {
                    i = false;
                }
                else
                {
                    i = true;
                }
                break;
            }
            case 2:
            {
                if (isLeapYear(year) == true)
                {
                    if (day > 29)
                    {
                        i = false;
                    }
                    else
                    {
                        i = true;
                    }
                }
                else
                {
                    if (day > 28)
                    {
                        i = false;
                    }
                    else
                    {
                        i = true;
                    }
                }
                break;
            }
        }
        return i;
    }
    
    public static void whatDate(int year, int month, int day, int i)
    {
        int j = Math.abs(i);
        if (i > 0)
        {
            for (; j > 0; j--)
            {
                if (day == 1)
                {
                    if (month == 1)
                    {
                        year--;
                        month = 12;
                        day = 31;
                    }
                    else
                    {
                        month--;
                        switch(month)
                        {
                            case 1:
                            case 3:
                            case 5:
                            case 7:
                            case 8:
                            case 10:
                            case 12:
                            {
                                day = 31;
                                break;
                            }
                            case 4:
                            case 6:
                            case 9:
                            case 11:
                            {
                                day = 30;
                                break;
                            }
                            case 2:
                            {
                                if (isLeapYear(year) == true)
                                {
                                    day = 29;
                                }
                                else
                                {
                                    day = 28;
                                }
                                break;
                            }
                        }
                    }
                }
                else
                {
                    day--;
                }
            }
        }
        else
        {
            for (; j > 0; j--)
            {
                switch(month)
                {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                    {
                        if (day == 31)
                        {
                            if (month == 12)
                            {
                                year++;
                                month = 1;
                                day = 1;
                            }
                            else
                            {
                                month++;
                                day = 1;
                            }
                        }
                        else
                        {
                            day++;
                        }
                        break;
                    }
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                    {
                        if (day == 30)
                        {
                            month++;
                            day = 1;
                        }
                        else
                        {
                            day++;
                        }
                        break;
                    }
                    case 2:
                    {
                        if (isLeapYear(year) == true)
                        {
                            if (day == 29)
                            {
                                month++;
                                day = 1;
                            }
                            else
                            {
                                day++;
                            }
                        }
                        else
                        {
                            if (day == 28)
                            {
                                month++;
                                day = 1;
                            }
                            else
                            {
                                day++;
                            }
                        }
                        break;
                    }
                }
            }
        }
        System.out.print(i + " days ago is:" + year + "-" + month + "-" + day);
        System.exit(0);
    }
}

分析:这题跟上面那道题目异曲同工,只不过是将+1天改为了+n天或+-n天其实都差不多,偏易。

心得:感觉到上题没有区别,一般 ^ w ^。

改进建议:能用java中与日期相关的类和方法就好了。


 4.PTA题目集3的7-2(定义日期类)

定义一个类Date,包含三个私有属性年(year)、月(month)、日(day),均为整型数,其中:年份的合法取值范围为[1900,2000] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。 注意:不允许使用Java中和日期相关的类和方法,否则按0分处理。

要求:Date类结构如下图所示:

输入格式:

在一行内输入年月日的值,均为整型数,可以用一到多个空格或回车分隔。

输出格式:

  • 当输入数据非法及输入日期不存在时,输出“Date Format is Wrong”;
  • 当输入日期合法,输出下一天,格式如下:Next day is:年-月-日

输入样例1:

在这里给出一组输入。例如:

1912 12 25

输出样例1:

在这里给出相应的输出。例如:

Next day is:1912-12-26

输入样例2:

在这里给出一组输入。例如:

2001 2 30

输出样例2:

在这里给出相应的输出。例如:

Date Format is Wrong
 
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        Date date1 = new Date();
        date1.setYear(input.nextInt());
        date1.setMonth(input.nextInt());
        date1.setDay(input.nextInt());
        if (date1.checkInputValidity() == false)
        {
            System.out.println("Date Format is Wrong");
            System.exit(0);
        }
        else
        {
            date1.getNextDate();
        }
        System.out.println("Next day is:" + date1.getYear() + "-" + date1.getMonth() + "-" + date1.getDay());
        input.close();
    }
}

class Date
{
    private int year;
    private int month;
    private int day;
    private int[] mon_maxnum = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    public Date()
    {
    }
    
    public Date(int year, int month, int day)
    {
        this.year = year;
        this.month = month;
        this.day = day;
    }
    
    public int getYear()
    {
        return year;
    }
    
    public void setYear(int year)
    {
        this.year = year;
    }
    
    public int getMonth()
    {
        return month;
    }
    
    public void setMonth(int month)
    {
        this.month = month;
    }
    
    public int getDay()
    {
        return day;
    }
    
    public void setDay(int day)
    {
        this.day = day;
    }

    public boolean isLeapYear(int year)
    {
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public boolean checkInputValidity()
    {
        if ((year < 1900 || year > 2000) || (month < 1 || month > 12) || (day < 1 || day > 31))
        {
            return false;
        }
        if (isLeapYear(year))
        {
            mon_maxnum[2] = 29;
            if (mon_maxnum[month] < day)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        else
        {
            if (mon_maxnum[month] < day)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
    
    public void getNextDate()
    {
        switch(month)
        {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
            {
                if (day == 31)
                {
                    if (month == 12)
                    {
                        year++;
                        month = 1;
                        day = 1;
                    }
                    else
                    {
                        month++;
                        day = 1;
                    }
                }
                else
                {
                    day++;
                }
                break;
            }
            case 4:
            case 6:
            case 9:
            case 11:
            {
                if (day == 30)
                {
                    month++;
                    day = 1;
                }
                else
                {
                    day++;
                }
                break;
            }
            case 2:
            {
                if (isLeapYear(year) == true)
                {
                    if (day == 29)
                    {
                        month++;
                        day = 1;
                    }
                    else
                    {
                        day++;
                    }
                }
                else
                {
                    if (day == 28)
                    {
                        month++;
                        day = 1;
                    }
                    else
                    {
                        day++;
                    }
                }
                break;
            }
        }
    }
}

分析:这题就是考察如何设计类以及类中的方法,入门级并不难。

心得:认识到类的作用了,让main函数更简洁更明了。

改进建议:直接用java自带的与日期相关的类与方法。


 

5.PTA题目集3的7-3(一元多项式求导(类设计))

要求:编写程序性,实现对简单多项式的导函数进行求解。详见作业指导书。  OO作业3-3题目说明.pdf

输入格式:

在一行内输入一个待计算导函数的表达式,以回车符结束。

输出格式:

  1. 如果输入表达式不符合上述表达式基本规则,则输出“Wrong Format”。
  2. 如果输入合法,则在一行内正常输出该表达式的导函数,注意以下几点: 结果不需要排序,也不需要化简;
  • 当某一项为“0”时,则该项不需要显示,但如果整个导函数结果为“0”时,则显示为“0”;
  • 当输出结果第一项系数符号为“+”时,不输出“+”;
  • 当指数符号为“+”时,不输出“+”;
  • 当指数值为“0”时,则不需要输出“x^0”,只需要输出其系数即可。

输出格式见输入输出示例。

输入样例1:

在这里给出一组输入。例如:

-2*     x^-2+  5*x^12-4*x+       12

输出样例1:

在这里给出相应的输出。例如:

4*x^-3+60*x^11-4

输入样例2:

在这里给出一组输入。例如:

2*x^6-0*x^7+5

输出样例2:

在这里给出相应的输出。例如:

Wrong Format

这道题受限于目前能力,当前无法写出,以后会回过头来重新写的。
分析:这道题目主要考察正则表达式,用正则表达式判断输入的格式是否正确,同时要考虑如何把每个多项式提取出来并判断是否符合要求格式。
心得:感觉到了正则表达式的强大作用,以后要再把这个吃透。

第一次写博客,还不太会用,同时因为能力所限,个别作业也没有写出来,以后会加强学习的!!! 

 

posted @ 2021-04-04 23:50  -Hush-  阅读(313)  评论(0)    收藏  举报