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.要開始提前學習介面,抽象類的知識點。