第一次博客作业

第一次博客作业

前言:第一次写博客心里有点小紧张,很多东西不太懂,排版问题还望多多见谅。这学期才接触Java,所以对一些题目的掌握不是很快,不过我感觉第一次作业的知识点和大一接触的c语言有很大程度上是类似的,而第二次作业难点在于作业的复杂程度,第三次作业是关于构建类的,第三题也是最难的一题,关于这题我也没有拿到满分。

设计与分析:

题目集一:

7-8 判断三角形类型

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

输入格式:

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

输出格式:

(1)如果输入数据非法,则输出“Wrong Format”;

(2)如果输入数据合法,但三条边不能构成三角形,则输出“Not a triangle”;

(3)如果输入数据合法且能够成等边三角形,则输出“Equilateral triangle”;

(4)如果输入数据合法且能够成等腰直角三角形,则输出“Isosceles right-angled triangle”;

(5)如果输入数据合法且能够成等腰三角形,则输出“Isosceles triangle”;

(6)如果输入数据合法且能够成直角三角形,则输出“Right-angled triangle”;

(7)如果输入数据合法且能够成一般三角形,则输出“General triangle”。

 7-8 代码:

import java.util.Scanner;

import java.util.Arrays;

public class Main {

 

public static void main(String[] args) {

// TODO 自动生成的方法存根

    Scanner in = new Scanner(System.in);

    double a[] = new double[3];

    int i;

    for(i = 0; i < 3; i++){

    a[i] = in.nextDouble();

    }

    Arrays.sort(a);

    for(i = 0; i < 3; i++){

     if(a[i] < 1 || a[i] > 200){

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

     return;

     }

     }

    if(a[0] + a[1] > a[2])

    {

     if(a[0] == a[1] && a[1] == a[2])

     {

         System.out.println("Equilateral triangle");

     }

     else if(a[0] == a[1] && a[0] * a[0] + a[1] * a[1] - a[2] * a[2] < 0.000001)

     System.out.println("Isosceles right-angled triangle");

     else if(a[2] == a[1] || a[2] == a[0] || a[0] == a[1])

     System.out.println("Isosceles triangle");

     else if(a[0] * a[0] + a[1] * a[1] - a[2] * a[2] < 0.000001)

     System.out.println("Right-angled triangle");

     else{

     System.out.println("General triangle");

     }

    }

    else

     System.out.println("Not a triangle");

    

}

 

}

类图分析:

 

 

 

 

文字分析:这道题目一共七种情况,可以用if-else嵌套来做,我们第一步要先判断输入的数据是否合法,题目中已经给出了三条边的取值范围,那么我们得判断输入三条边是否在指定的区间,不在的话就要输出“Wrong Format”,除此之外我们还要判断三条边是否能构成三角形,利用三角形两边之和大于第三边这条定理来进行判断。

第二步我们还要判断三角形类型,可以利用勾股定理与对三角形的定义来判断,大体上来看跟C语言方面的知识没有太大的区别。

题目集二:

7-4 求下一天

输入年月日的值(均为整型数),输出该日期的下一天。 其中:年份的合法取值范围为[1820,2020] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。 注意:不允许使用Java中和日期相关的类和方法。

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

输入格式:

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

输出格式:

当输入数据非法及输入日期不存在时,输出“Wrong Format”;

当输入日期合法,输出下一天,格式如下:Next date is:年-月-日

7-4 代码:

import java.util.Scanner;

public class Main {

public static boolean isLeapYear(int year)

{

boolean isLeapYear = (year % 4 == 0&&year % 100 != 0)||year % 400 == 0;

return isLeapYear;

}

public static boolean checkInputValidity(int year,int month,int day)

{

int a = 0;

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

    int []mon2 = new int[] {0,31,29,31,30,31,30,31,31,30,31,30,31};

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

    {

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

     {

     if(isLeapYear(year))

     {

     if(day <= mon2[month]&&day >= 1)

     a = 1;

     }

     else

     {

     if(day <= mon1[month]&&day >= 1)

     a = 1;

     }

     }

    }

boolean s = a == 1;

return s;

}

public static void nextDate(int year,int month,int day)

{

int i = 0;

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

if(year % 4 == 0&&year % 100 != 0||year % 400 == 0)

{

a[2] = 29;

}

if(checkInputValidity( year, month, day))

{

if(month == 12)

{

if(day == a[month])

{

day = 1;

year = year + 1;

month = 1;

}

else

day = day + 1;

}

else if(day == a[month])

{

day = 1;

month = month + 1;

}

else

day = day + 1;

System.out.println("Next date is:"+year+"-"+month+"-"+day);

}

else

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

}

 

public static void main(String[] args) {

// TODO 自动生成的方法存根

    Scanner input = new Scanner(System.in);

  

    int year = input.nextInt();

    int month = input.nextInt();

    int day = input.nextInt();

    nextDate( year, month, day);

    

}

}

类图分析:

 

 

 

 

文字分析:这道题涉及到了Java的方法,好在题目开始已经给出了大概的框架,我们只需要跟着题目给出的框架构建方法就好了,前面两个判断是否是闰年以及判断合法的方法比较简单,我认为比较困难的是第三个方法,需要调用之前的两个方法并且输出下一天,这个方法的复杂度是比较高的,需要对各种情况都考虑到,花了我很多时间。

 

7-5 求前N天 (30 分)

输入年月日的值(均为整型数),同时输入一个取值范围在[-10,10] 之间的整型数n,输出该日期的前n天(当n > 0时)、该日期的后n天(当n<0时)。

其中年份取值范围为 [1820,2020] ,月份取值范围为[1,12] ,日期取值范围为[1,31] 。

注意:不允许使用Java中任何与日期有关的类或方法。

输入格式:

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

输出格式:

当输入的年、月、日以及n的值非法时,输出“Wrong Format”;

当输入数据合法时,输出“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 boolean isLeapYear(int year)

{

boolean isLeapYear = (year % 4 == 0&&year % 100 != 0)||year % 400 == 0;

return isLeapYear;

}

public static boolean checkInputValidity(int year,int month,int day,int n)

{

 

int a = 0;

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

    int []mon2 = new int[] {0,31,29,31,30,31,30,31,31,30,31,30,31};

if(year >= 1820&&year <= 2020&&n <= 10&&n >=-10 )

    {

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

     {

     if(isLeapYear(year))

     {

     if(day <= mon2[month]&&day >= 1)

     a = 1;

     }

     else

     {

     if(day <= mon1[month]&&day >= 1)

     a = 1;

     }

     }

    }

boolean s = a == 1;

return s;

}

public static void nextDate(int year,int month,int day,int n)

{

//int i = 0;

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

if(year % 4 == 0&&year % 100 != 0||year % 400 == 0)

{

a[2] = 29;

}

        

 

if(checkInputValidity( year, month, day,n))

{

            day -= n;

if(n > 0)

{

if(day <= 0)

{

month--;

if(month == 0)

{

month += 12;

year --;

if(year % 4 == 0&&year % 100 != 0||year % 400 == 0)

{

a[2] = 29;

}

else

a[2] = 28;

}

day += a[month];

}

}

else if(n < 0)

{

if(year % 4 == 0&&year % 100 != 0||year % 400 == 0)

{

a[2] = 29;

}

else

a[2] = 28;

if(day > a[month])

{

day -= a[month];

month++;

if(month > 12)

{

month -= 12;

year ++;

}

}

}

System.out.println(n+" days ago is:"+year+"-"+month+"-"+day);

}

else

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

}

 

public static void main(String[] args) {

// TODO 自动生成的方法存根

    Scanner input = new Scanner(System.in);

    int year = input.nextInt();

    int month = input.nextInt();

    int day = input.nextInt();

    int n = input.nextInt();

    nextDate( year, month, day,n);

    

}

 

}

类图分析:

 

 

 

 

文字分析:

这道题本来是比较复杂的,但已经有了之前那道题的基础,所以我直接在上一题的基础代码上进行了修改,就是增添了判断前几天与后几天的循环,在判断的时候需要注意不能把条件初始化忘了,我一开始就是忘了初始化日期,导致一直报错。

题目集三:

 

7-2 定义日期类 (28 分)

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

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

 

 

 

输入格式:

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

输出格式:

当输入数据非法及输入日期不存在时,输出“Date Format is Wrong”;

当输入日期合法,输出下一天,格式如下:Next day is:年-月-日

代码:

 

 

import java.util.Scanner;

 

public class Main {

 

public static void main(String[] args) {

// TODO 自动生成的方法存根

Scanner input = new Scanner(System.in);

int year = input.nextInt();

int month = input.nextInt();

int day = input.nextInt();

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

if(date.checkinoutValidity())

{

date.getNextDate();

//System.out.println("Next day is:"+date.getYear()+"-"+date.getMonth()+"-"+date.getDay());

System.out.printf("Next day is:%d-"+"%d-"+"%.0f\n",date.getYear(),date.getMonth(),date.getDay());

}

}

 

}

class Date{

private int year;

    private    int month;

    private int day;

private int panduan;

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

int[] month1=new int[]{0,31,29,31,30,31,30,31,31,30,31,30,31};

 public static boolean isLeapYear(int year){

         if((year%4==0&&year%100!=0)||year%400==0) {

             return true;

         }

        else

              return false;

      }

public  boolean checkinoutValidity() {

 if(month>0&&month<=12){

      if(isLeapYear(year))

        {

            if(day<=month1[month]&&day>0)

                panduan=1;

        }

              else

              {

                if(day<=month2[month]&&day>0)

                panduan=1;

                

              }

 }

 if(year<1900||year>2000||month<1||month>12||day<1||day>31||panduan==0)

 {

            System.out.println("Date Format is Wrong");

            return false;

 }

            return true;

    }

public Date(int a ,int b,int c)

{

         this.year = a;

         this.month = b;

         this.day = c;

    }

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

  return day;

}

    public void setDay(int day) {

  this.day = day;

    }

    public void getNextDate() {

            this.day+=1;

            if(isLeapYear(this.year)==true ) {

               

                  if(day>month1[month]) {

                   this.day=this.day-month1[month];

                   this.month+=1;

                   if(this.month>12) {

                       this.month -=12;

                       this.year+=1;

                   }

               }

          }

          else {

              

              if(day>month2[month]) {

                  this.day=this.day-month2[month];

                  this.month+=1;

                  if(this.month>12) {

                      this.month -=12;

                      this.year+=1;

                  }

              }      

           }

    }

}

类图分析:

 

 

 

 

 

文字分析:

这道题主要考察类的构建,其中许多方法可以根据题目集二中构建的来完成,我把关于创建对象的操作放进了类里面,这样会让我的代码看起来没有那么乱,提高了代码的整洁度。

 

7-3 一元多项式求导(类设计) (50 分)

编写程序性,实现对简单多项式的导函数进行求解。详见作业指导书。

输入格式:

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

输出格式:

如果输入表达式不符合上述表达式基本规则,则输出“Wrong Format”。

如果输入合法,则在一行内正常输出该表达式的导函数,注意以下几点: 结果不需要排序,也不需要化简;

当某一项为“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

代码如下:

//package pta3;

import java.util.*;

import java.util.regex.*;

import java.util.ArrayList;

import java.util.List;

 

public class Main {

    public static void main(String[] args) {

        String regex = "^(([-+]([1-9][0-9]*)(\\*x(\\^[+-]?([1-9][0-9]*))?))|(([1-9][0-9]*)\\*(x(\\^[+-]?([1-9][0-9]*))?))|([-+](x(\\^[+-]?([1-9][0-9]*))?))|([-+]([1-9][0-9]*))|(([1-9][0-9]*))|((x(\\^[+-]?([1-9][0-9]*))?)))+$";

        String regex2 = "-?([1-9][0-9]*)\\*x(\\^[+-]?([1-9][0-9]*))?";

        String regex1 = "(\\*x\\^)|(\\*x)";

        String regex3 = "-?[1-9][0-9]*";

        Scanner in = new Scanner(System.in);

        String s = in.nextLine();

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

        ArrayList<String> list = new ArrayList<>();

        if(s.matches(regex3))

        {

            System.out.print(0);

        }

        

        if(s.matches(regex))

        {

            Pattern pattern = Pattern.compile(regex2);

            Matcher matcher = pattern.matcher(s);

            while(matcher.find())

            {

                list.add(matcher.group());

            }

            int sum = 0;

            int x = 0,y = 0,i = 0;

            while(i  < list.size())

            {

                if(i == 1)

                {

                    String[] stt= list.get(i).split(regex1);

                    if(stt.length != 1)  

                    {

                        x = Integer.parseInt(stt[0]);

                        y = Integer.parseInt(stt[1]);

                        x *= y;

                        y --;

                        if(y == 1)

                        {

                            System.out.print("+" + x + "*x");

                        }else

                        {

                            System.out.print("+" + x + "*x^" +y);

                        }

                    }

                    else

                    {

                        sum += Integer.parseInt(stt[0]);

                    }

                }else

                {

                    String[] stt= list.get(i).split(regex1);

                    if(stt.length != 1)

                    {

                        x = Integer.parseInt(stt[0]);

                        y = Integer.parseInt(stt[1]);

                        x *= y;

                        y --;

                        if(y == 1)

                        {

                            System.out.print(x + "*x^" + y);

                        }else

                        {

                            System.out.print(x + "*x^" +y);

                        }

                    }

                    else

                    {

                        sum += Integer.parseInt(stt[0]);

                    }

                }

                if(sum < 0)

                {

                    System.out.print(sum);

                }

                else if(sum > 0)

                {

                    System.out.print(sum);

                }

                i++;

            }

        }else

        {

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

        }

 

    }

}

类图分析:

 

 

 

 

 

 

文字分析:这道题我有几个测试点没有过去,这道题需要我们利用java.util.regex中的类来进行匹配,再用Deri类中的divide()方法分割出每一项,还要保留符号。在根据不同的格式来进行求导运算,最重要的就是要灵活运用正则表达式。由于对正则表达式的掌握不深,所以我这道题没有完全实现题目要求。

踩坑心得

在题目集一的7-8中对于直角三角形的判定不能完全使用勾股定理判定,我百度发现Java中会产生一定误差,所以需要用两边平方和减去第三边平方的差来进行判定,

总结

通过这三次题目集的练习,让我快速的了解了Java的语法,以及Java中各种类的功能的强大,对于面向对象这个概念也有了一种比较直观的认识,让我对之后的学习有了一定的信心,在今后的学习中我也一定会加倍努力的。

 

posted @ 2021-10-15 20:08  6t6yuu  阅读(81)  评论(0)    收藏  举报