Blog 题目集1-3总结
目录
(1)前言。
(2)设计与分析。
(3)踩坑新得。
(4)改进建议。
(5)总结。
1.前言
该学期正式学习java的过程中做了许多练习,因此在这里做个分析与总结。以及个人体会。
3次题目里中的问题关键:
第一次题目集
本次题目集中要求较为简单。主要考核知识点包括1.基础程序设计从控制台读取输入。2.简单数字运算。3.for循环if循环do.....while循环的使用。
4.switch语句的使用。5.字符串的输入与检测。
第二次题目集
本次题目集考核知识点为1.字符串与整形的相互转化。2.支付串的输入与读取。
第三次题目集
本次题目集考核知识点为1.类的创建与使用。2.类的聚合。
个人体会
由于是本学期才开始学习的java,且前期是自学的原因导致开始写是有些不熟练,但随着不断练习开始能运用。对几个知识点谈谈自己的感受。
1.String数组
数组的创建与c语言相比有所不同,且数组中某个数值调用不能单纯用arring【i】要用arring.charAt(i)进行调用。
2.类
在java中新学到的一个全新知识点,类有三大特性
封装性
将数据和操作封装为一个有机的整体,由于类中私有成员都是隐藏的,只向外部提供有限的接口,所以能够保证内部的高内聚性和与外部的低耦合性。用者不必了解具体的实现细节,而只是要通过外部接口,以特定的访问权限来使用类的成员,能够增强安全性和简化编程。
继承性
继承性更符合认知规律,使程序更易于理解,同时节省不必要的重复代码。
多态性
同一操作作用于不同对象,可以有不同的解释,产生不同的执行结果。在运行时,可以通过指向父类(基类)的指针,来调用实现子类(派生类)中的方法。
2.设计与心得
题目集2 7/2

代码
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner leix = new Scanner(System.in); String a=leix.nextLine(); int mu=0; int i=0,n=0,x=1,t=0,y=0; if(a.length()<11) { y=1; System.out.println("null data");} for(i=0;i<a.length();i++){ if(a.charAt(i)=='0'&&(i+10)<=a.length()) { y=1; t=0; for(n=i+1;n<=i+9;n++) { if(a.charAt(n)=='1') t++; } t=t%2; if(a.charAt(i+10)!='1'&&t!=1) { System.out.println(x+":"+"validate error"); x++; } else if(a.charAt(i+10)=='1'&&t!=1) {System.out.println(x+":"+"parity check error");x++;} else if(a.charAt(i+10)!='1'&&t==1) {System.out.println(x+":"+"validate error");x++;} else if(a.charAt(i+10)=='1'&&t==1) { System.out.print(x+":"); for(n=i+1;n<i+9;n++) { System.out.print(a.charAt(n)); if(n==i+8) {System.out.print("\n");} } x++; } i=i+10; } } if(y==0) { System.out.print("null data"); } } }
设计分析
该题目中要求输入一串字符串并获取正确的输出方式,我通过逐步获取字节只到获取正确的目标在通过获取对应的字符来判断正误;在这道题目中我逐渐熟练了对于字符串数组的运用。
踩坑心得
1.未正确读取需要字符串。
2.单组数据特殊值获取错误。
题目集 3 7-1

代码
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner input = new Scanner(System.in); double a = Double.parseDouble(input.next()); double b = Double.parseDouble(input.next()); double c = Double.parseDouble(input.next()); if(a == 0){ System.out.println("Wrong Format"); System.exit(0); } //create a QuadraticEquation object QuadraticEquation equation = new QuadraticEquation(a, b, c); //get value of b * b - 4 * a * c double discriminant = equation.getDiscriminant(); System.out.println("a=" + equation.getA() + ",b=" + equation.getB() + ",c=" + equation.getC()+":"); if (discriminant < 0) { System.out.println("The equation has no roots."); } else if (discriminant == 0) { System.out.println("The root is " + String.format("%.2f", equation.getRoot1())); } else // (discriminant >= 0) { System.out.println("The roots are " + String.format("%.2f", equation.getRoot1()) + " and " + String.format("%.2f", equation.getRoot2())); } } } class QuadraticEquation{ private double a; private double b; private double c; public QuadraticEquation(Double a,Double b,Double c){ this.a=a; this.b=b; this.c=c; } public double getA(){ return a; } public void setA(double a){ this.a=a; } public double getB(){ return b; } public void setB(double b){ this.b=b; } public double getC(){ return c; } public void setC(double c){ this.c=c; } double result=b*b-4*(a*c); public double getDiscriminant(){ double result=b*b-4*(a*c); return result; } public double getRoot1(){ double result=b*b-4*(a*c); Double Root1=(-b+Math.sqrt(result))/(2*a); return Root1; } public double getRoot2(){ double result=b*b-4*(a*c); Double Root2=(-b-Math.sqrt(result))/(2*a); return Root2; } }
设计分析
该题目要求用类的方法来解一元二次方程,因此在本题目中要创建类使用类。
错误心得
1.对于类的使用不熟练。
2.未能正确创建类并未使用get 和set。
题目集3 7-2

代码
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int year = 0; int month = 0; int day = 0; int choice = input.nextInt(); if (choice == 1) { // test getNextNDays method int m = 0; year = Integer.parseInt(input.next()); month = Integer.parseInt(input.next()); day = Integer.parseInt(input.next()); DateUtil date = new DateUtil(year, month, day); if (!date.checkInputValidity()) { System.out.println("Wrong Format"); System.exit(0); } m = input.nextInt(); if (m < 0) { System.out.println("Wrong Format"); System.exit(0); } System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:"); System.out.println(date.getNextNDays(m).showDate()); } else if (choice == 2) { // test getPreviousNDays method int n = 0; year = Integer.parseInt(input.next()); month = Integer.parseInt(input.next()); day = Integer.parseInt(input.next()); DateUtil date = new DateUtil(year, month, day); if (!date.checkInputValidity()) { System.out.println("Wrong Format"); System.exit(0); } n = input.nextInt(); if (n < 0) { System.out.println("Wrong Format"); System.exit(0); } System.out.print( date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:"); System.out.println(date.getPreviousNDays(n).showDate()); } else if (choice == 3) { //test getDaysofDates method year = Integer.parseInt(input.next()); month = Integer.parseInt(input.next()); day = Integer.parseInt(input.next()); int anotherYear = Integer.parseInt(input.next()); int anotherMonth = Integer.parseInt(input.next()); int anotherDay = Integer.parseInt(input.next()); DateUtil fromDate = new DateUtil(year, month, day); DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay); if (fromDate.checkInputValidity() && toDate.checkInputValidity()) { System.out.println("The days between " + fromDate.showDate() + " and " + toDate.showDate() + " are:" + fromDate.getDaysofDates(toDate)); } else { System.out.println("Wrong Format"); System.exit(0); } } else{ System.out.println("Wrong Format"); System.exit(0); } } } class DateUtil{ private int year; private int month; private int day; public DateUtil() { super(); } DateUtil(int year, int month, int day){ this.year=year; this.month=month; this.day=day; } public void setYear(int year){ this.year=year; } public int getYear(){ return 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){ boolean isLeapYear= (year%4==0&&year%100!=0)||year%400==0; return isLeapYear; } public boolean checkInputValidity(){ int t=0; int[] m1=new int[]{0,31,29,31,30,31,30,31,31,30,31,30,31}; int[] m2=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31}; if(year>=1820&&year<=2020) { if(month>0&&month<=12) { if(isLeapYear(year)) { if(day<=m1[month]&&day>0) t=1; } else { if(day<=m2[month]&&day>0) t=1; } } } boolean checkInputValidity=t==1; return checkInputValidity; } public DateUtil getNextNDays(int n){ long x = 0; int i; int j; int t=0; int[] m = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; for (i = 1; i < year; i++) { if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) x += 366; else x += 365;} if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) m[2] = 29; for (j = 1; j < month; j++) { x += m[j]; } x += day; x=x+n; int newyear=1,newmonth=1,newday=1; for(i=1;x>0;i++,newyear++) { if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) x -= 366; else x -= 365; } newyear=i-1; if (newyear % 4 == 0 && newyear % 100 != 0 || newyear % 400 == 0) x += 366; else x += 365; if (((newyear)% 4 == 0 && newyear % 100 != 0) || newyear % 400 == 0) m[2] = 29; for (j = 1; x>m[j]; j++,newmonth++) { t=j; x-= m[j]; } newday = (int)x; year=newyear;month=newmonth;day=newday; return this; } public DateUtil getPreviousNDays(int n){ int x = 0; int i; int j; int t=0; int[] m = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; for (i = 1; i < year; i++) { if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) x += 366; else x += 365;} if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) m[2] = 29; for (j = 1; j < month; j++) { x += m[j]; } x += day; x=x-n; int newyear=1,newmonth=1,newday=1; for(i=1;x>0;i++,newyear++) { if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) x -= 366; else x -= 365; } newyear=i-1; if (newyear % 4 == 0 && newyear % 100 != 0 || newyear % 400 == 0) x += 366; else x += 365; if (((newyear)% 4 == 0 && newyear % 100 != 0) || newyear % 400 == 0) m[2] = 29; for (j = 1; x>m[j]; j++,newmonth++) { t=j; x-= m[j]; } newday = x; year=newyear;month=newmonth;day=newday; return this; } public int getDaysofDates(DateUtil date){ int x = 0; int i; int j; int[] m = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; for (i = 1; i < date.year; i++) { if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) x += 366; else x += 365;} if ((date.year % 4 == 0 && date.year % 100 != 0) || date.year % 400 == 0) m[2] = 29; for (j = 1; j < date.month; j++) { x += m[j]; } x += date.day; int x1=0; for (i = 1; i < this.year; i++) { if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) x1 += 366; else x1 += 365;} if ((this.year % 4 == 0 && this.year % 100 != 0) || this.year % 400 == 0) m[2] = 29; for (j = 1; j < this.month; j++) { x1 += m[j]; } x1+=this.day; int r=Math.abs(x1-x); return r; } public String showDate(){ return year + "-" + month + "-" + day; } }
设计分析
该题目相较于上一题,对于类的运用难度明显提高,并且要求也显得更加复杂。
错误心得

1.下n天:整型数最大值测试不通过,需要修改自己的整形为长整形后该回。
2.前n天:整型数最大值测试不通过。
题目集3 7/3

代码
import java.util.Scanner; 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(int year){ boolean isLeapYear= (year%4==0&&year%100!=0)||year%400==0; return isLeapYear; } public boolean validate(){ boolean validate=(value<=2050&&value>=1900); return validate; } public void yearIncrement(){ value=value+1; } public void yearReduction(){ value=value-1; } } 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 Year getYear(){ return year; } public void setValue(int value){ this.value=value; } public void setYear(Year year){ this.year=year; } public void resetMin(){ value=1; } public void resetMax(){ value=12; } public boolean validate(){ boolean validate=(value>=1&&value<=12); return validate; } public void dayIncrement(){ value=value+1; } public void dayReduction(){ value=value-1; } } class Day{ int value; Month month; int m[]={0,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 Month getMonth(){ return month; } public void setValue(int value){ this.value=value; } public void setMonth(Month value){ this.month=value; } public void resetMin(){ value=1; } public void resetMax(){ value=m[month.getValue()]; } public boolean validate(){ if(this.getMonth().getYear().isLeapYear(this.getMonth().getYear().getValue())==true) m[2]=29; if(value>=1&&value<=m[month.getValue()]) return true; else return false; } public void dayIncrement() { value=value+1; } public void dayReduction() { value=value-1; } } public class Main { public static void main(String[] args) { int year=0,month=0,day=0,b; Scanner input = new Scanner(System.in); int a = input.nextInt(); year=input.nextInt();month= input.nextInt();day=input.nextInt(); DateUtil c=new DateUtil(year,month,day); if(a==1){ b = input.nextInt(); if(!c.checkInputValidity()||b<0){ System.out.println("Wrong Format"); System.exit(0); } else System.out.println(c.getNextNDays(b).showDate()); } else if(a==2){ b=input.nextInt(); if(!c.checkInputValidity()||b<0){ System.out.println("Wrong Format"); System.exit(0); } else System.out.println(c.getPreviousNDays(b).showDate()); } else if(a==3){ int y1,m1,d1; y1=input.nextInt();m1= input.nextInt();d1=input.nextInt(); DateUtil d=new DateUtil(y1,m1,d1); if(!c.checkInputValidity()||!d.checkInputValidity()){ System.out.println("Wrong Format"); System.exit(0); } else System.out.println(c.getDaysofDates(d)); } else System.out.println("Wrong Format"); } } class DateUtil{ Day day; public DateUtil(){} public DateUtil(int day,int month,int year){ this.day=new Day(day,month,year); } public Day getDay(){ return day; } public void setDay(Day day){ this.day=day; } public boolean checkInputValidity(){ int t=0; int[] m1=new int[]{0,31,29,31,30,31,30,31,31,30,31,30,31}; int[] m2=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31}; if(getDay().getMonth().getYear().getValue()>=1820&&getDay().getMonth().getYear().getValue()<=2500) { if(getDay().getMonth().getValue()>0&&getDay().getMonth().getValue()<=12) { if((getDay().getMonth().getYear().getValue()%4==0&&getDay().getMonth().getYear().getValue()%100!=0)||getDay().getMonth().getYear().getValue()%400==0) { if(getDay().getValue()<=m1[getDay().getMonth().getValue()]&&getDay().getValue()>0) t=1; } else { if(getDay().getValue()<=m2[getDay().getMonth().getValue()]&&getDay().getValue()>0) t=1; } } } boolean checkInputValidity=t==1; return checkInputValidity; } public boolean compareDates(DateUtil 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; else 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; else return true; } public boolean equalTwoDates(DateUtil date){ if(this.getDay().getValue()==date.getDay().getValue()&&this.getDay().getMonth().getValue()==date.getDay().getMonth().getValue()&& this.getDay().getMonth().getYear().getValue()==date.getDay().getMonth().getYear().getValue()) return true; else return false; } public String showDate(){ return this.getDay().getMonth().getYear().getValue()+"-"+this.getDay().getMonth().getValue()+"-"+this.getDay().getValue(); } public DateUtil getNextNDays(int n){ long x = 0; int i; int j; int t=0; int[] m = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; for (i = 1; i < this.getDay().getMonth().getYear().getValue(); i++) { if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) x += 366; else x += 365;} if ((this.getDay().getMonth().getYear().getValue() % 4 == 0 && this.getDay().getMonth().getYear().getValue() % 100 != 0) || this.getDay().getMonth().getYear().getValue() % 400 == 0) m[2] = 29; for (j = 1; j < this.getDay().getMonth().getValue(); j++) { x += m[j]; } x += this.getDay().getValue(); x=x+n; int newyear=1,newmonth=1,newday=1; for(i=1;x>0;i++,newyear++) { if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) x -= 366; else x -= 365; } newyear=i-1; if (newyear % 4 == 0 && newyear % 100 != 0 || newyear % 400 == 0) x += 366; else x += 365; if (((newyear)% 4 == 0 && newyear % 100 != 0) || newyear % 400 == 0) m[2] = 29; for (j = 1; x>m[j]; j++,newmonth++) { t=j; x-= m[j]; } newday = (int)x; return new DateUtil(newyear,newmonth,newday); } public DateUtil getPreviousNDays(int n){ long x = 0; int i; int j; int t=0; int[] m = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; for (i = 1; i < this.getDay().getMonth().getYear().getValue(); i++) { if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) x += 366; else x += 365;} if ((this.getDay().getMonth().getYear().getValue() % 4 == 0 && this.getDay().getMonth().getYear().getValue() % 100 != 0) || this.getDay().getMonth().getYear().getValue() % 400 == 0) m[2] = 29; for (j = 1; j < this.getDay().getMonth().getValue(); j++) { x += m[j]; } x += this.getDay().getValue(); x=x-n; int newyear=1,newmonth=1,newday=1; for(i=1;x>0;i++,newyear++) { if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) x -= 366; else x -= 365; } newyear=i-1; if (newyear % 4 == 0 && newyear % 100 != 0 || newyear % 400 == 0) x += 366; else x += 365; if (((newyear)% 4 == 0 && newyear % 100 != 0) || newyear % 400 == 0) m[2] = 29; for (j = 1; x>m[j]; j++,newmonth++) { t=j; x-= m[j]; } newday = (int)x; return new DateUtil(newyear,newmonth,newday); } public int getDaysofDates(DateUtil date){ int x = 0; int i; int j; int[] m = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; for (i = 1; i < date.getDay().getMonth().getYear().getValue(); i++) { if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) x += 366; else x += 365;} if ((date.getDay().getMonth().getYear().getValue() % 4 == 0 && date.getDay().getMonth().getYear().getValue() % 100 != 0) || date.getDay().getMonth().getYear().getValue() % 400 == 0) m[2] = 29; for (j = 1; j < date.getDay().getMonth().getValue(); j++) { x += m[j]; } x += date.getDay().getValue(); int x1=0; for (i = 1; i < this.getDay().getMonth().getYear().getValue(); i++) { if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) x1 += 366; else x1 += 365;} if ((this.getDay().getMonth().getYear().getValue() % 4 == 0 && this.getDay().getMonth().getYear().getValue() % 100 != 0) || this.getDay().getMonth().getYear().getValue() % 400 == 0) m[2] = 29; for (j = 1; j < this.getDay().getMonth().getValue(); j++) { x1 += m[j]; } x1+=this.getDay().getValue(); int r=Math.abs(x1-x); return r; } }
代码分析
在上一题的基础上对类进行嵌套。
采坑心得
1.要熟练练习各个编译软件的调试功能。
2.调整年越界

改进建议
无
总结
1.通过3次练习学会了类的建立与运用。、
2.重新复习了课本前8章的基础知识点。
3.要开始提前学习接口,抽象类的知识点。

浙公网安备 33010602011771号