Java题目集1~3相关作业总结

(1)前言:

 第一次作业:

  题量:8道

  难度:简单

  知识点:

  •  实现简单计算的Java程序;
  •    使用Scanner类从控制台获取输入;
  •    使用标识符命名变量
  •    学会使用数值数据类型并进行正确操作
  •    对选择语句和循环语句的灵活运用
  • 一维数组的使用

 第二次作业:

  题量:5道

  难度:稍难

  知识点:

  •    字符串的使用
  •    学习使用数组存储字符串中数据
  •    学习二进制数与十进制数的转换
  •    多维数组的灵活整合
  •    学会使用带组合条件的选择语句进行编程
  •    利用boolean数据变量,并结合关系操作符编写布尔表达式检验数据是否合乎要求
  •    学习构造方法并使用方法

 第三次作业:

  题量:3

  难度:困难

  知识点:

  •    学习对类的创建并使用
  •    构造私有属性,实现类的封装
  •    学习使用无参构造法和带参构造法
  •    LocalDate类的运用
  •    学习使用getter和setter方法对私有属性进行获取和修改
  •    利用boolean数据变量,并结合关系操作符编写布尔表达式检验数据是否合乎要求
  •   学习并使用正则表达式
  •    利用数组存储String中数据信息

(2)设计与分析:

第一次作业第七题:

问题描述:对多个整数进行排序 ,先从键盘输入一个整数n,之后回车 再从键盘输入n个整数,整数之间用一个或多个空格分隔

提交源码:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        int n = reader.nextInt();
        int a[];
        a = new int[n];
        for (int i = 0; i < a.length; i++)
            a[i] = reader.nextInt();
        for (int i = 0; i < a.length; i++) {
            for (int j=a.length - 1;j > i;j--) {
                if (a[i] > a[j]) {
                    int temp;
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
            }
            }
        }
            System.out.print("The sorted numbers are:");
            for (int j = 0; j < a.length; j++)
                System.out.print(a[j] + " ");
    }
}

SourceMonitor生成报表:

报表分析:

在Kiviat Graph图中,可以显示出,代码数据平均依赖时间还是较长的,平均复杂度也较大 

在Block Histogram中,块依赖差距较大

作业分析:

在看到题目时,由于此前所学过一点C语言,遇到过类似的题目,便仿照用C语言做类似题的方法,利用for循环和数组存储数据,再采用冒泡排序将其进行从小到大排列。

改进建议:

采用冒泡排序还是比较基础的,对于排序的多种方式可以都去尝试一下,然后采取最优化的排序方式。

第一次作业第八题:

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

提交源码:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        double a,b,c;
        Scanner reader = new Scanner(System.in);
        a = reader.nextDouble();
        b = reader.nextDouble();
        c = reader.nextDouble();
        if(a >= 1 && a <= 200 && b >= 1 && b <= 200 && c >= 1 && c <= 200 ){
            if((a + b) > c && (b + c) > a && (a + c) > b) {
                int i;
                if (a == b && b == c){
                    i = 1;//等边三角形
                }
                else if (a == b || a == c || b == c) {
                    //等腰三角形
                    i = 2;
                    if(a * a + b * b - c * c < 0.0011 || b * b + c * c - a * a < 0.0011 || a * a + c * c - b * b < 0.0011){
                        i = 3;//等腰直角三角形
                    }
                }
                else if (a * a + b * b - c * c < 0.0011 || b * b + c * c - a * a < 0.0011|| a * a + c * c - b * b < 0.0011) {
                    //直角三角形
                    i = 4;
                }
                else
                    i = 5;//普通三角形
                switch (i){
                    case 1:
                        System.out.println("Equilateral triangle");break;//等边三角形
                    case 2:
                        System.out.println("Isosceles triangle");break;//等腰三角形
                    case 3:
                        System.out.println("Isosceles right-angled triangle");break;//等腰直角三角形
                    case 4:
                        System.out.println("Right-angled triangle");break;//直角三角形
                    case 5:
                        System.out.println("General triangle");break;//普通三角形
                }
            }
            else
                System.out.println("Not a triangle");
        }
        else
            System.out.println("Wrong Format");
    }
}

  SourceMonitor生成报表:

 

 

 报表分析:

在Kiviat Graph图中,可以显示出,代码数据在平均依赖时间、最大依赖时间、平均法中较突出,同时配上了相应的注释。

在Block Histogram中,各块依赖相当,个别依赖较大和较小。

作业分析:

考虑到三角形的满足条件和基本类型,首先对三角形进行判断是否属于三角形,在此基础上利用边与边的关系对三角形进行等边、等腰、直角、等腰直角及普通三角的判断。在此,我定义整型 i ,若满足条件的各个结果,则得到 i 的相应值,再利用switch语句对 i 进行判断输出相应的结果。

踩坑历程:在判断直角三角形时,一开始只考虑到直角三角形满足勾股定理,但在输入边长数据时,对于像带根号的数据,此语句不能对此做出正确的判断,导致此测试点提交错误,系统未能对其正确判断,反复测试数据后,才明白,对直角三角形的测试不单纯利用勾股定理中的等式关系,应将等式改为相应的可取精度。于是,经过一系列精度数取值,最终我的结果确定在0.0011。

改进建议:

在判断三角形时,也可以直接将判断的结果直接输出相对应的三角形满足类型,可不必另设计 i 对其取值,再利用switch语句判断输出。

第二次作业第四题:

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

提交源码:

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

    }//主方法
}

 SourceMonitor生成报表:

 报表分析:

在Kiviat Graph图中,可以显示出,代码的注释部分很少,几乎没有,总体运行占时还是比较大,平均的复杂性、最大复杂性和平均法也较突出。

在Block Histogram中,各块依赖还是比较紧密。

作业分析:采用布尔加方法对是否为闰年和日期正确与否进行判断,再定义方法获得下一天的日期,并在main中调用,输入日期,调用方法,对日期进行判断,若日期合乎要求,在进行下一步求下一天,最终输出下一天的日期。

踩坑历程:由于做题的粗心,在输出错误时,将Wrong Format后加了一个点,导致在测试时,凡是判断错误的测试点均未通过。同时一开始,代码出现运行超时的现象,将代码能简化的简化后,可以进行正常测试了。

改进建议:对于代码的简化,注意一下在必要的时候适当备注一下,当简化后,有些地方容易忘了是什么含义,注释也是写代码是一个助手。

 

第二次作业第五题:

问题描述: 求前N天,输入年月日的值(均为整型数),同时输入一个取值范围在[-10,10] 之间的整型数n,输出该日期的前n天(当n > 0时)、该日期的后n天(当n<0时)。
其中年份取值范围为 [1820,2020] ,月份取值范围为[1,12] ,日期取值范围为[1,31] 。

提交源码:

import java.util.Scanner;
public class Main {
    public static boolean checknum(int num) {/*判断数字是否合法*/
        if (num >= -10 && num <= 10) {
            return true;
        } else
            return false;
    }
    public static boolean isleapyear(int year) {/*判断闰年*/
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            return true;
        } else
            return false;
    }

    public static boolean checkdata(int year, int month, int day) {/*判断年月日*/
        if (year >= 1820 && year <= 2020 && month >= 1 && month <= 12 && day >= 1 && day <= 31) {
            if (month == 2) {
                if (isleapyear(year)) {
                    if (day >= 1 && day <= 29) {
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    if (day >= 1 && day <= 28) {
                        return true;
                    } else {
                        return false;
                    }
                }
            } else if (month == 4 || month == 6 || month == 9 || month == 11) {
                if (day >= 1 && day <= 30) {
                    return true;
                } else {
                    return false;
                }
            } else {
                if (day >= 1 && day <= 31) {
                    return true;
                } else {
                    return false;
                }
            }
        } else
            return false;
    }

    public static void beforeday(int year, int month, int day, int num) {
        day = day - num;
        if (month == 4 || month == 6 || month == 9 || month == 11) {
            if (day <= 0) {
                month = month - 1;
                day = 31 + day;
            } else if (day > 30) {
                month = month + 1;
                day = day - 30;
            }
        } else if (month == 5 || month == 10) {
            if (day <= 0) {
                month = month - 1;
                day = 30 + day;
            } else if (day > 31) {
                month = month + 1;
                day = day - 31;
            }
        }
        if (month == 12) {
            if (day <= 0) {
                month = month - 1;
                day = 30 + day;
            } else if (day > 31) {
                year = year + 1;
                month = 1;
                day = day - 31;
            }
        }
        if (month == 2) {
            if (isleapyear(year))
            {
                if (day <= 0) {
                    month = 1;
                    day = 31 + day;
                }
                else if (day > 29) {
                    month = 3;
                    day = day - 29;
                }
            }
            else
            {
                if (day <= 0) {
                    month = 1;
                    day = 31 + day;
                }
                else if (day > 28) {
                    month = 3;
                    day = day - 28;
                }
            }
        }
        if (month == 3) {
            if (day <= 0) {
                month = 2;
                if (isleapyear(year)) {
                    day = 29 + day;
                } else if(! isleapyear(year)){
                    day = 28 + day;}
            } else if (day > 31) {
                month = 4;
                day = day - 31;
            }
        }

        if (month == 7) {
            if (day <= 0) {
                month = 6;
                day = 30 + num;
            } else if (day > 31) {
                month = 8;
                day = day - 31;
            }
        }
        if (month == 8) {
            if (day <= 0) {
                month = 7;
                day = 31 + day;
            } else if (day > 31) {
                month = 9;
                day = day - 31;
            }
        }
        if (month == 1) {
            if (day <= 0) {
                year = year - 1;
                month = 12;
                day = 31 + day;
            } else if (day > 31) {
                month = 2;
                day = day - 31;
            }
        }
        if(checkdata(year,month,day)==true){
            System.out.println(num + " days ago is:" + year + "-" + month + "-" + day);
        }else{
            System.out.println("Wrong Format");
        }

    }
    public static void main(String[] args){
        int num,year,month,day;
        Scanner rd = new Scanner(System.in);
        year = rd.nextInt();
        month = rd.nextInt();
        day = rd.nextInt();
        num = rd.nextInt();
        if(checknum(num)==true&&checkdata(year,month,day)==true){
            beforeday(year,month,day,num);
        }
        else {
            System.out.println("Wrong Format");
        }
    }
}

 SourceMonitor生成报表:

报表分析:

在Kiviat Graph图中,可以显示出,总体平均运行和平均法占时较小,平均的复杂性、最大复杂性和注释较突出。

在Block Histogram中,各块依赖较紧密,出现一个峰值,但相对较整齐。

 作业分析:此题和前一题颇具相似,在前一题的基础上,它改变的是将后一天改变成输入数字n,并求出输入日期的前n天,因此,在前一题的基础上,增加了对输入数字的判断和将后一天的方法改编为求前n天。

踩坑历程:在对闰年的二月和四月的前n天出现测试点错误,在跨年变化环节上也出现了测试点错误,经检查将跨年时年份的改变加上,对闰年四月和二月检查改正后,运行正常,测试点通过。

改进建议:对于代码的简化还有待加强,在进行前n天的日期变化改变过程中,也许可以尝试用switch方法,就是试试用多种方法,对于前面的代码,基本用的都是if条件语句,感觉还是很单一。

第三次作业第二题:

问题描述:

定义日期类:定义一个类Date,包含三个私有属性年(year)、月(month)、日(day),均为整型数,其中:年份的合法取值范围为[1900,2000] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。

提交源码:

//import java.lang.reflect.Constructor;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Date data = new Date();
        int year1,month1,day1;
        Scanner rd = new Scanner(System.in);
        year1 = rd.nextInt();
        month1 = rd.nextInt();
        day1 = rd.nextInt();
        data.setDay(day1);
        data.setMonth(month1);
        data.setYear(year1);
        data.getNextDate();
    }
}
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};
    //constructor的使用
//    public Constructor Date(int year1,int month1,int day1){
//        return getNextDate(year1,month1,day1);
//    }
    public int getYear(){
        return year;
    }
    public void setYear(int year1){
        year = year1;
    }
    public int getMonth(){
        return month;
    }
    public void setMonth(int month1){
        month = month1;
    }
    public int getDay(){
        return day;
    }
    public void setDay(int day1){
        day = day1;
    }
    public boolean IsLeapyear(int year){
        if((year%4==0&&year%100!=0)||(year%400==0))
            return true;
        else
            return false;
    }
    public boolean checkInputValidity() {
        boolean i=true;
        if ((year >= 1900) && (year <= 2000)&&(month >= 1) &&( month <= 12)&&(day>=1)&&(day<=31)) {
            if (month == 4 || month == 6 || month == 9 || month == 11) {
                i = day <= 30;
            }
            if (month == 2) {
                if(IsLeapyear(year)){
                    i = day <= 29;
                }
                else {
                    i= day <= 28;
                }
            }
        }
        else {
            i = false;
        }
        return i;
    }
    public void getNextDate(){
        if(checkInputValidity()){
        if (month == 4 || month == 6 || month == 9 || month == 11) {
            if (day != 30) {
                day = day + 1;
            } else if(day == 30){
                month = month + 1;
                day=1;
            }
        }
        else if (month==1||month==3||month==5||month==7||month==8||month==10){
            if(day!=31){
                day = day+1;
            }else{
                month=month+1;
                day=1;}
        }
        else if(month==12){
            if(day!=31){
                day = day+1;
            }else {
                year=year+1;
                month=1;
                day=1;
            }
        }
        else if(month==2){
            if(IsLeapyear(year)){
                if(day!=29){
                    day=day+1;
                }else {
                    month=month+1;
                    day=1;}
            }
            else {
                if(day!=28){
                    day=day+1;
                }else {
                    month = month+1;
                    day=1;
                }
            }
        }
        if(checkInputValidity()) {
            System.out.println("Next day is:" + year + "-" + month + "-" + day);
        }else {
            System.out.println("Date Format is Wrong");
        }
        }else{
             System.out.println("Date Format is Wrong");
        }
    }
}

 SourceMonitor生成报表:

 报表分析:

在Kiviat Graph图中,可以显示出,总体平均运行时间、最大运行时间、平均法、平均的复杂性和最大复杂性较突出。

在Block Histogram中,各块依赖参差不齐,对于不同的需求有不同的依赖。

 作业分析:此题和前一次作业颇具相似,但是这次的作业是设计一个data类,将后一天的日期输出,对于前面一次的作业,仅是在一个类中实现获得后一天。于是,我定义一个Data类,在此类中存放私有的属性,同时,将判断日期是否正确和年份判断,各年份、月份和天数的获得和建立定义共有方法供其使用。

踩坑历程:由于前一次的作业与之类似的缘故,在这次的作业中,只是在最开始是不太会用set和get获取私有属性,熟悉后,成功通过测试点。

改进建议:这里是通过set和get获得类中的私有属性,但是,应该还有其他的方法取得私有属性,建议尝试一下。

第三次作业第三题:

问题描述:一元多项式求导(类设计)

提交源码:

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.lang.StringBuffer;
public class Main {
    public static void main(String[] args) {
        String str;
        Scanner rd = new Scanner(System.in);
        str = rd.nextLine();
        str = str.replaceAll(" ", "");
        InputZhengshu Zheng = new InputZhengshu();
        InputSingleFormula Formula = new InputSingleFormula();
        InputFormula Formula1 = new InputFormula();
        if (str.matches(Zheng.regex1)) {
            System.out.println("0");
        } else if (str.matches(Formula.regex2)) {
            StringBuffer sBuffer = new StringBuffer();
            String flag = "(\\W|-)?[1-9]([\\d]{1,})?\\W[^a-wy-zA-Z]\\W(\\W|-)?[1-9]([\\d]{1,})?";
            Pattern pattern = Pattern.compile(flag);
            Matcher matcher = pattern.matcher(str);
            while (matcher.find()) {
                String temp = new String(matcher.group());
                String[] n = new String[3];
                int i = 0;
                String number = "((\\-)?[\\d]{1,})";
                Pattern pattern1 = Pattern.compile(number);
                Matcher matcher1 = pattern1.matcher(temp);
                while (matcher1.find()) {
                    n[++i] = matcher1.group();
                }
                int a, b;
                a = Integer.parseInt(n[1]);
                b = Integer.parseInt(n[2]);
                a *= b;
                b--;
                n[1] = String.valueOf(a);
                n[2] = String.valueOf(b);
                if (a > 0 && !sBuffer.isEmpty()) {
                    temp = "+" + n[1] + "*x^" + n[2];
                } else {
                    temp = n[1] + "*x^" + n[2];
                }
                sBuffer.append(temp);
            }
            System.out.println(sBuffer.toString());
        } else if (str.matches(Formula1.regex3)) {
            StringBuffer sBuffer = new StringBuffer();
            String flag = "(\\W|-)?[1-9]([\\d]{1,})?\\W[^a-wy-zA-Z]\\W(\\W|-)?[1-9]([\\d]{1,})?";
            Pattern pattern = Pattern.compile(flag);
            Matcher matcher = pattern.matcher(str);
            while (matcher.find()) {
                String temp = new String(matcher.group());
                String[] n = new String[3];
                int i = 0;
                String number = "((\\-)?[\\d]{1,})";
                Pattern pattern1 = Pattern.compile(number);
                Matcher matcher1 = pattern1.matcher(temp);
                while (matcher1.find()) {
                    n[++i] = matcher1.group();
                }
                int a, b;
                a = Integer.parseInt(n[1]);
                b = Integer.parseInt(n[2]);
                a *= b;
                b--;
                n[1] = String.valueOf(a);
                n[2] = String.valueOf(b);
                if (a > 0 && !sBuffer.isEmpty()) {
                    temp = "+" + n[1] + "*x^" + n[2];
                } else {
                    temp = n[1] + "*x^" + n[2];
                }
                sBuffer.append(temp);
            }
            System.out.println(sBuffer.toString());

        }
    }
}
        class InputZhengshu<regex1> {//整数测试(含整数加整数)
            String regex1 = "((\\W|-)?[1-9]([\\d]{1,})?)+";
        }
        class InputSingleFormula<regex2> {//多个算术式测试(含一个算数)
            String regex2 = "((\\W|-)?[1-9]([\\d]{1,})?\\W[^a-wy-zA-Z]\\W(\\W|-)?[1-9]([\\d]{1,})?)+";
        }
        class InputFormula<regex3> {//算式加整数
            String regex3 = "(((\\W|-)?[1-9]([\\d]{1,})?)+)?((((\\W|-)?[1-9]([\\d]{1,})?\\W[^a-wy-zA-Z]\\W(\\W|-)?[1-9]([\\d]{1,})?))+)?(((\\W|-)?[1-9]([\\d]{1,})?)+)?";
        }
        class InputFormulaA<regex4> {//算式加整数混合
        }

 

SourceMonitor生成报表:

 

 报表分析:

在Kiviat Graph图中,可以显示出,总体平均运行时间、最大运行时间、平均法、平均的复杂性和最大复杂性都较突出。

在Block Histogram中,各块依赖也是参差不齐,对于不同的需求有不同的依赖。

 作业分析:此题需运用正则表达式判别输入的等式是否符合需求,据此,先在等式上分析等式得组成,对于含未知数和不含未知数的等式进行分类处理判断,利用正则,在此需求上判断。对于符合条件的式子再利用正则表达式中的pattern、matcher进行处理,式子中符合要求的整数进行求导。

踩坑历程:一开始,由于对scanner类的用法不对,输入的整行数据中,对空格忽略处理,直接进行空格前最先输入的数据识别,发现问题后,改正用法,可以对其进行整行处理识别,但是由于对正则和数组用法掌握度有限,最终我只能通过输入格式不对和输入整数的求导及对单个式子的求导。

改进建议:熟练掌握正则的用法,同时,对于数组的使用,还有待加强。


(3)总结

  在三次的作业中,我学会了从控制台中进行读取输入操作,学会对基本数据类型的简单操作,学会对常量和变量的使用和了解一些基本的运算符、表达式与语句,同时,在作业中学会定义类和相关属性和方法并在适当的地方学会调用类的属性及其方法。在完成作业的过程中虽然时有出现错误,但是,也正是这些错误,让自己对一些数据类型的转换和类的调用方法有了更清晰的了解。也因为这些作业,我明白自己在Java的学习中对数组,字符串的运用还是略显不足,在数据转换过程中还不能灵活运用。

  另外,对于课堂上的学习,感觉只是Java学习中的一点点,课后还需及时的广泛的学习,不然没法做出布置的作业。

 

 

 

        

posted @ 2021-04-04 14:34  WWppyy  阅读(119)  评论(0)    收藏  举报