OOP第一次博客作業
關于Java與面向對象:
目錄:
一、前言
前言:
這個學期才開始接觸Java,對于第一次作業總結感觸比較生疏,在很多語法中有些不適應,經常犯一些問題,對于面向對象上學期學習C語言的過程中是從來沒有接觸過的。
在這開學到現在的過程中,到現在為止一共進行了3周,雖然說時間非常短,但是在這每天的語法練習中,對于自己的水平都是有感覺到明顯的進步的。從C語言到本學期Java的學習當中,老師不會再像之前一樣的去一節課一節課教學我們的語法知識,而是在我們自己學習Java的基本使用語法的基礎上教學如何面向對象去進行刨析項目的成分再到設計出邏輯關系(power designer),然而對于這一部分的自學還是要通過自己的實踐能力去熟悉代碼的寫入,其次以便提升我的敲碼速度。
知識點:
關于類:
1、類似C++中的struct,構造函數、內置方法(函數 )都比較相似
2、但是無法重載運算符,這是比較難受的一點。
3、盡量避免代碼的重復,private和public的方法需要熟悉封裝性。
4、為對象定義類,使用構造方法構造對象。
5、使用Java庫中的類。
關于Java語法:
1、注意可變類的賦值是引用。
2、String類字符串的使用。
3、數組的基本用法。
關于作業題題量:
三次作業的題量相對還是比較多的。
關于作業難度:
三次作業的基本難度較簡單易懂。
設計與分析:
第一次作業:總共有12道題目,分別為:
| 7-1 計算年利率 | 5 |
| 7-2 身體質量指數(BMI)測算 | 10 |
| 7-3 九九乘法表(雙重循環) | 15 |
| 7-4 快遞運費 | 10 |
| 7-5 去掉重復的字符 | 5 |
| 7-6 統計一個子串在整串中出現的次數
|
5 |
| 7-7 有重復的數據 | 5 |
| 7-8 從一個字符串中移除包含在另一個字符串中的字符 | 5 |
| 7-9 Prime Numbers | 5 |
| 7-10 GPS數據處理 | 10 |
| 7-11 求定積分 | 10 |
| 7-12 列出最簡真分數序列* | 15 |
1 import java.util.Scanner; 2 3 public class Main{ 4 public static void main(String [] args){ 5 Scanner input = new Scanner(System.in); 6 int b=input.nextInt(); 7 double c=0; 8 if(b>0){ 9 if(b<=1){ 10 c=0.077*0.5; 11 } 12 else if(b>1&&b<=3){ 13 c=0.077*0.7; 14 } 15 else if(b>3&&b<=5){ 16 c=0.077*1.0; 17 } 18 else{ 19 c=0.077*1.1; 20 } 21 System.out.print(String.format("實際利率=%.2f",c*100));//使用String.format() 22 System.out.print("%"); 23 } 24 else{ 25 System.out.println("error"); 26 } 27 } 28 }
1 import java.util.Scanner; 2 3 public class Main{ 4 public static void main(String[] args) { 5 Scanner scan=new Scanner(System.in); 6 String s=scan.nextLine(); 7 String b=scan.nextLine(); 8 int index=0; 9 int count=0; 10 while(true) 11 { 12 index=s.indexOf(b); 13 if(index!=-1) 14 { 15 count++; 16 s=s.substring(index+b.length());/連接字符串 17 } 18 else 19 { 20 break; 21 } 22 } 23 System.out.print(count); 24 } 25 }
1 import java.util.Scanner; 2 public class Main{ 3 public static void main(String[] args){ 4 Scanner in = new Scanner(System.in); 5 String s1=null,time = null; 6 while (in.hasNext()) 7 { 8 s1=in.nextLine(); 9 if(s1.equals("END")) 10 { 11 break; 12 } 13 int sum=0; 14 String []split=s1.split(","); 15 if(split[0].equals("$GPRMC")) 16 { 17 int end=s1.indexOf("*"); 18 for(int i=1;i<end;i++) 19 { 20 sum^=(int)s1.charAt(i); 21 } 22 sum%=65536; 23 Boolean flag=split[2].equals("A"); 24 Boolean S=sum==Integer.parseInt(s1.substring(end+1),16); 25 if(S==true&&flag==true) 26 { 27 time=split[1]; 28 } 29 } 30 } 31 if(time!=null) 32 { 33 int h=(Integer.parseInt(time.substring(0,2))+8)%24;//substring()返回字符串的子字符串 34 String m=time.substring(2,4); 35 String s=time.substring(4,6); 36 if(h<10) 37 System.out.print("0"); 38 System.out.print(h+":"+m+":"+s); 39 } 40 } 41 }
第二次作業:總共有9道題目,分別為:
| 7-1 長度質量計量單位換算 | 5 |
| 7-2 奇數求和 | 10 |
| 7-3 房產稅費計算2022 | 12 |
| 7-4 游戲角色選擇 | 14 |
| 7-5 學號識別 | 10 |
| 7-6 巴比倫法求平方根近似值 | 10 |
| 7-7 二進制數值提取 | 10 |
| 7-8 判斷三角形類型 | 15 |
| 7-9 求下一天 | 14 |
1 import java.util.Scanner; 2 3 public class Main{ 4 public static void main(String[] args) { 5 Scanner scan=new Scanner(System.in); 6 double a=scan.nextDouble(); 7 double b=scan.nextDouble(); 8 double c,d; 9 c=a/0.45359237; 10 d=b/0.0254; 11 System.out.print((float)c+" "+(float)d);//強制轉化 12 } 13 }
其中7-8:這一道題在運用if與else的條件判斷中要求比較高,其中印象最深的也就是本題的最難一個點,在多次測例中解決不了的是等腰直角三角形的判斷。在最后交流與討論中得知,兩個整數的比值不可能必出無理數,于是將代碼進行優化,類似于 Math.abs(ch1*ch1+ch2*ch2-ch3*ch3)<0.0000001 的辦法進行判斷。了解了浮點數類型的判斷相等不能直接在if等語句的判定條件中去使用“==”進行判斷,而是將其相減得出的值小于某個極小的數從而達到判斷是否相等的目的。
1 import java.util.Scanner; 2 3 public class Main{ 4 public static void main(String[] args) { 5 Scanner scan=new Scanner(System.in); 6 double ch1=scan.nextDouble(); 7 double ch2=scan.nextDouble(); 8 double ch3=scan.nextDouble(); 9 if(ch1<1||ch1>200||ch2<1||ch2>200||ch3<1||ch3>200){ 10 System.out.println("Wrong Format"); 11 }else if(ch1+ch2<=ch3||ch1+ch3<=ch2||ch2+ch3<=ch1){ 12 System.out.println("Not a triangle"); 13 }else if(ch1==ch2&&ch2==ch3&&ch1==ch3){ 14 System.out.println("Equilateral triangle"); 15 }else if((ch1==ch2&&(Math.abs(ch1*ch1+ch2*ch2-ch3*ch3)<0.0000001) 16 ||(ch1==ch3&&(Math.abs(ch1*ch1+ch3*ch3-ch2*ch2)<0.0000001)) 17 ||(ch2==ch3&&(Math.abs(ch2*ch2+ch3*ch3-ch1*ch1)<0.0000001)))){//浮點數類型的判斷 18 System.out.println("Isosceles right-angled triangle"); 19 }else if(ch1==ch2||ch1==ch3||ch2==ch3){ 20 System.out.println("Isosceles triangle"); 21 }else if((ch1*ch1+ch2*ch2==ch3*ch3)||(ch1*ch1+ch3*ch3==ch2*ch2)||(ch3*ch3+ch2*ch2==ch1*ch1)){ 22 System.out.println("Right-angled triangle"); 23 }else{ 24 System.out.println("General triangle"); 25 } 26 } 27 }
第三次作業:總共有4道題目,分別為:
| 7-1 創建圓形類 | 6 |
| 7-2 創建賬戶類Account | 16 |
| 7-3 定義日期類 | 34 |
| 7-4 日期類設計 | 44 |
對于第三次作業的訓練,我深刻的感覺到題目的大大提升難度,從第一題開始學習如何為對象定義一個類型,構造里面的屬性以及其種類,創建對象和定義類,使用無參或者有參的構造方法構造對象(使用new操作符調用構造方法創建對象,構造方法必須與所在類同名,構造方法沒有返回類型,甚至連void也沒有,構造方法是在創建一個對象時由new操作符調用的。構造方法的作用是初始化對象),然后是針對訪問對象的數據和方法的學習,再包括對于date類的理解和其中如何進行的有進一步的理解。
其中7-3:這一題是繼承第二次作業的7-9迭代過來的,要求輸出下一天的日期,有第二題的引子下再重新優化這個源碼,可以采用學習到的創建類的使用。有了類的幫助就非常輕松的完成了7-3的功能實現
這邊是類圖:
然后是源碼展示:
1 import java.util.Scanner; 2 3 public class Main{ 4 public static void main(String[] args){ 5 Scanner scan=new Scanner(System.in); 6 Date date=new Date(); 7 int year=scan.nextInt(); 8 int month=scan.nextInt(); 9 int day=scan.nextInt(); 10 date.setYear(year); 11 date.setMonth(month); 12 date.setDay(day); 13 date.getNextDate(); 14 } 15 } 16 class Date{ 17 private int year; 18 private int month; 19 private int day; 20 int[] mon_maxnum=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31}; 21 Date(){ 22 year=0; 23 month=0; 24 day=0; 25 } 26 Date(int year,int month,int day){ 27 this.year=year; 28 this.month=month; 29 this.day=day; 30 } 31 public int getYear(){ 32 return year; 33 } 34 public void setYear(int year){ 35 this.year=year; 36 } 37 public int getMonth(){ 38 return month; 39 } 40 public void setMonth(int month){ 41 this.month=month; 42 } 43 public int getDay(){ 44 return day; 45 } 46 public void setDay(int day){ 47 this.day=day; 48 } 49 public boolean isLeapYear(int year){//判斷是否為閏年 50 if((year%4==0&&year%100!=0)||year%400==0){ 51 return true; 52 }else{ 53 return false; 54 } 55 } 56 public boolean checkInputValidity(){//檢查是否輸入有誤 57 if((year>=1900&&year<=2000)&& 58 (month>=1&&month<=12)&& 59 (day>=1&&day<=31)){ 60 if(isLeapYear(year)&&month==2&&(day>=1&&day<=29)){ 61 return true; 62 }else if(day>=1&&day<=mon_maxnum[month]){ 63 return true; 64 }else{ 65 return false; 66 } 67 }else{ 68 return false; 69 } 70 } 71 public void getNextDate(){//求下一天 72 if(checkInputValidity()==false){ 73 System.out.print("Date Format is Wrong"); 74 }else{ 75 if(isLeapYear(year)){ 76 mon_maxnum[2]=29; 77 } 78 if(day>=1&&day<mon_maxnum[month]){ 79 day++; 80 } 81 if(day==mon_maxnum[month]){ 82 day=1; 83 if(month!=12){ 84 month++; 85 }else{ 86 month=1; 87 year++; 88 } 89 } 90 System.out.println("Next day is:"+year+"-"+month+"-"+day); 91 } 92 } 93 }
不難發現,Java中的類的創建給代碼的結構化提供了無可替代的作用,至于下一天的算法只需要注意閏年的二月份帶來的變化考慮到特殊即可。
其中7-4:最為日期Date類的最后一個迭代題目,增加了前n天、后n天、兩個日期之間相差多少天的功能。優化之后的類:

代碼展示如下:
運用類的調用,使代碼更加具有條理,看起來不會太燒腦
1 public class Main { 2 public static void main(String[] args) { 3 Scanner input = new Scanner(System.in); 4 int year = 0; 5 int month = 0; 6 int day = 0; 7 int choice = input.nextInt(); 8 if (choice == 1) { // test getNextNDays method 9 int m = 0; 10 year = Integer.parseInt(input.next()); 11 month = Integer.parseInt(input.next()); 12 day = Integer.parseInt(input.next()); 13 DateUtil date = new DateUtil(year, month, day); 14 if (!date.checkInputValidity()) { 15 System.out.println("Wrong Format"); 16 System.exit(0); 17 } 18 m = input.nextInt(); 19 if (m < 0) { 20 System.out.println("Wrong Format"); 21 System.exit(0); 22 } 23 System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:"); 24 System.out.println(date.getNextNDays(m).showDate()); 25 } else if (choice == 2) { // test getPreviousNDays method 26 int n = 0; 27 year = Integer.parseInt(input.next()); 28 month = Integer.parseInt(input.next()); 29 day = Integer.parseInt(input.next()); 30 31 DateUtil date = new DateUtil(year, month, day); 32 33 if (!date.checkInputValidity()) { 34 System.out.println("Wrong Format"); 35 System.exit(0); 36 } 37 38 n = input.nextInt(); 39 40 if (n < 0) { 41 System.out.println("Wrong Format"); 42 System.exit(0); 43 } 44 45 System.out.print( 46 date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:"); 47 System.out.println(date.getPreviousNDays(n).showDate()); 48 } else if (choice == 3) { //test getDaysofDates method 49 year = Integer.parseInt(input.next()); 50 month = Integer.parseInt(input.next()); 51 day = Integer.parseInt(input.next()); 52 53 int anotherYear = Integer.parseInt(input.next()); 54 int anotherMonth = Integer.parseInt(input.next()); 55 int anotherDay = Integer.parseInt(input.next()); 56 57 DateUtil fromDate = new DateUtil(year, month, day); 58 DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay); 59 60 if (fromDate.checkInputValidity() && toDate.checkInputValidity()) { 61 System.out.println("The days between " + fromDate.showDate() + 62 " and " + toDate.showDate() + " are:" 63 + fromDate.getDaysofDates(toDate)); 64 } else { 65 System.out.println("Wrong Format"); 66 System.exit(0); 67 } 68 } 69 else{ 70 System.out.println("Wrong Format"); 71 System.exit(0); 72 } 73 } 74 }
1 class DateUtil{ 2 private int year; 3 private int month; 4 private int day; 5 int[] mon_maxnum=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31}; 6 public DateUtil(int year,int month,int day){ 7 this.year=year; 8 this.month=month; 9 this.day=day; 10 } 11 public int getYear(){ 12 return year; 13 } 14 public void setYear(int year){ 15 this.year=year; 16 } 17 public int getMonth(){ 18 return month; 19 } 20 public void setMonth(int month){ 21 this.month=month; 22 } 23 public int getDay(){ 24 return day; 25 } 26 public void setDay(int day){ 27 this.day=day; 28 } 29 public boolean isLeapYear(int year){ 30 if((year%4==0&&year%100!=0)||year%400==0){ 31 return true; 32 }else{ 33 return false; 34 } 35 } 36 public boolean checkInputValidity(){ 37 if(year < 1820 || year > 2020) 38 return false; 39 if(month < 1 || month > 12) 40 return false; 41 if(isLeapYear(year)){ 42 if(month == 2){ 43 if(day < 1 || day > 29) 44 return false; 45 } 46 else 47 if(day < 1 || day > mon_maxnum[month]) 48 return false; 49 } 50 if(!isLeapYear(year)){ 51 if(day<1 || day > mon_maxnum[month]) 52 return false; 53 } 54 return true; 55 } 56 public DateUtil getNextNDays(int n){ 57 while(n>365){ 58 if(isLeapYear(year)&&month<=2){ 59 if(day==29&&month==2){ 60 day=1; 61 month=3; 62 } 63 year++; 64 n-=366; 65 }else if(isLeapYear(year+1)&&month>2){ 66 year++; 67 n-=366; 68 }else{ 69 year++; 70 n-=365; 71 } 72 } 73 for(int i=0;i<n;i++){ 74 day++; 75 if(isLeapYear(year) && month == 2){ 76 if(day > 29){ 77 month++; 78 day = 1; 79 } 80 } 81 else if(day > mon_maxnum[month]){ 82 month++; 83 day = 1; 84 if(month > 12){ 85 month = 1; 86 year++; 87 } 88 } 89 } 90 DateUtil date = new DateUtil(year, month, day); 91 return date; 92 } 93 public DateUtil getPreviousNDays(int n){ 94 while(n>365){ 95 if(isLeapYear(year)&&month>2){ 96 year--; 97 n-=366; 98 }else if(isLeapYear(year-1)&&month<=2){ 99 year--; 100 n-=366; 101 }else{ 102 year--; 103 n-=365; 104 } 105 } 106 for(int i=0;i<n;i++){ 107 day--; 108 if(day < 1){ 109 month--; 110 if(month < 1){ 111 month = 12; 112 year--; 113 } 114 if(isLeapYear(year)&&month==2){ 115 day=29; 116 }else{ 117 day=mon_maxnum[month]; 118 } 119 } 120 } 121 DateUtil date = new DateUtil(year, month, day); 122 return date; 123 } 124 public String showDate(){ 125 return year+"-"+month+"-"+day; 126 } 127 }
對于求n天之后的代碼,算法基本與7-3相似,在之前的基礎上增加排除夠量的日期,在做出這些算法的實現后,對照之后很容易知道前n天的算法如何推出來,兩個十分相近。
踩坑心得:
1.作為一個剛入門的小白,對于最開始處理一些數據還是有很多地方做的非常差,例如在變量可取值范圍很大的時候不能直接套上循環進行程序運行,有方法去調節循環次數,將其降低。從一開始的date中的天數加減一天天實現到降低循環次數分別從年月日去實現,部分修正后改成的算法,先判斷年再到月,最后到日,可以是程序運行的更迅速。
加n天循環
1 while(n>365){//增加 2 if(isLeapYear(year)&&month<=2){ 3 if(day==29&&month==2){ 4 day=1; 5 month=3; 6 } 7 year++; 8 n-=366; 9 }else if(isLeapYear(year+1)&&month>2){ 10 year++; 11 n-=366; 12 }else{ 13 year++; 14 n-=365; 15 } 16 } 17 for(int i=0;i<n;i++){ 18 day++; 19 if(isLeapYear(year) && month == 2){ 20 if(day > 29){ 21 month++; 22 day = 1; 23 } 24 } 25 else if(day > mon_maxnum[month]){ 26 month++; 27 day = 1; 28 if(month > 12){ 29 month = 1; 30 year++; 31 } 32 } 33 }
減n天循環
1 while(n>365){ 2 if(isLeapYear(year)&&month>2){ 3 year--; 4 n-=366; 5 }else if(isLeapYear(year-1)&&month<=2){ 6 year--; 7 n-=366; 8 }else{ 9 year--; 10 n-=365; 11 } 12 } 13 for(int i=0;i<n;i++){ 14 day--; 15 if(day < 1){ 16 month--; 17 if(month < 1){ 18 month = 12; 19 year--; 20 } 21 if(isLeapYear(year)&&month==2){ 22 day=29; 23 }else{ 24 day=mon_maxnum[month]; 25 } 26 } 27 }
由此改變即可使代碼更加有層次不會太繁瑣去運行。
改進建議:
在以后的學習中,不斷地練習寫代碼的能力,無非就是對之前的問題在不斷地接受新的知識之后能夠以更快的速度和更巧妙的辦法去實現其功能,包括最基本的數組的建造可以去用ArrayList類去替換,其中包含的方法是非常好用的。不過對于這些的學習都是需要我們自己投入更多的時間去練習以及熟練掌握以后去實踐。畢竟不管在學什么專業都應該去主動。隨著入門的時間越來越長,接觸的題目也會越來越難,一種類型的題目,如同此次的題目一樣,都是不斷完善不斷迭代的。
總結:
本人第一次寫博客,這在很多地方的運用還是非常懵懂,還需要多多學習,不管是對于eclipse得分使用還是對power designer的使用等等,都是需要不斷去熟悉。對于這三次作業集,在結束了這剛開始Java代碼書寫,了解了Java的基本語法的實現。但是在類、方法什么的地方還是會有很多疑問,在日后的學習過程中,要不斷的去學習更多種類的類的使用,并且讓自己對基本的結構有更深的認識,積極參與討論交流,代碼的學習與未來的就業都不是一個人去實現,都需要團隊合作共同進步。改進算法也是其中我想要說的一個點,在和他人交流后,可以得知別人的解題分析思維,讓自己的思維更加活躍,當然在網絡平臺上也應該去多搜集他人推薦的學習方法、劃重點的好用知識以及實用的學習辦公的工具。
浙公網安備 33010602011771號