2013242032-弓彩霞-實驗二_第1頁
2013242032-弓彩霞-實驗二_第2頁
2013242032-弓彩霞-實驗二_第3頁
2013242032-弓彩霞-實驗二_第4頁
2013242032-弓彩霞-實驗二_第5頁
已閱讀5頁,還剩16頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

山山 西西 大大 學(xué)學(xué) 計計 算算 機機 與與 信信 息息 技技 術(shù)術(shù) 學(xué)學(xué)院院 實實驗驗報報告告 姓 名 弓 彩 霞學(xué) 號 2013242032 專業(yè)班級軟件工程 4 班 課程名稱 Java 實驗實驗日期 2015 6 11 成 績指導(dǎo)教師楊紅菊批改日期 實驗名 稱 實驗 2 面向?qū)ο蠡A(chǔ)和 JAVA 常用類 一 實驗?zāi)康?1 掌握類的定義 類的實例化方法 2 掌握類中的靜態(tài)變量和靜態(tài)方法的使用 3 掌握使用類的構(gòu)造方法對對象的數(shù)據(jù)域進行初始化 4 掌握getter和setter的使用 5 了解如何構(gòu)建一個字符串 掌握常用的 String StringBuffer StringBuilder 類的構(gòu)造方法的 使用 6 掌握字符串的比較方法 尤其 equals 方法和 比較的區(qū)別 7 掌握 String 類常用方法的使用 8 掌握字符串與字符數(shù)組和 byte 數(shù)組之間的轉(zhuǎn)換方法 9 Date Random 類的常用方法 二 實驗內(nèi)容 1 現(xiàn)在要求設(shè)計一個表示學(xué)生的類 里面有學(xué)生的姓名 學(xué)號 年齡 還要有三項成績 計算機 成績 數(shù)學(xué)成績 英語成績 要求可以求總分 平均分 最高 最低分 并且可以輸出一個學(xué)生的完 整信息 請設(shè)計此類 提示 程序開發(fā)步驟 根據(jù)需求定義出所要的類 根據(jù)題目中的要求規(guī)劃出類的屬性 所有的屬性必須封裝 private 所有的屬性必須通過getter和setter訪問 如果需要增加構(gòu)造方法 為屬性賦初值 適當采用構(gòu)造方法重載 所有的信息不要類中直接輸出 而是交給調(diào)用處輸出 即在該類中盡量不出現(xiàn)System out println 語句 例如在調(diào)用類中輸出如下結(jié)果 總分 267 70000000000005 平均分 89 23333333333335 最高分 91 4 最低分 87 3 學(xué)生信息 學(xué)號 100 姓名 John 年齡 20 英語 89 0 計算機 87 3 數(shù)學(xué) 91 4 程序代碼 package Java第二次實驗 import java util public class Student private String s name private int s number private int s age private double s computerGrade private double s mathGrade private double s englishGrade public Student String s name int s number int s age double s computerGrade double s mathGrade double s englishGrade this s name s name this s number s number this s age s age this s computerGrade s computerGrade this s mathGrade s mathGrade this s englishGrade s englishGrade public String getS name return s name public void setS name String s name this s name s name public int getS number return s number public void setS number int s number this s number s number public int getS age return s age public void setS age int s age this s age s age public double getS computerGrade return s computerGrade public void setS computerGrade double s computerGrade this s computerGrade s computerGrade public double getS mathGrade return s mathGrade public void setS mathGrade double s mathGrade this s mathGrade s mathGrade public double getS englishGrade return s englishGrade public void setS englishGrade double s englishGrade this s englishGrade s englishGrade public double totalPoints double tp s englishGrade s mathGrade s computerGrade return tp public double averagePoints double ap s englishGrade s mathGrade s computerGrade 3 0 return ap public double topScore double ts s mathGrade if ts s englishGrade ts s englishGrade else if tss englishGrade ls s englishGrade else if ls s computerGrade ls s computerGrade return ls public static void main String args System out println 請輸入學(xué)生姓名 學(xué)號 年齡 及其計算機 數(shù)學(xué) 英語成績 請您按順序輸入 Scanner input new Scanner System in Student s1 new Student input next input nextInt input next Int input nextDouble input nextDouble input n extDouble System out println total score s1 totalPoints System out println average score s1 averagePoints System out println top score s1 topScore System out println low score s1 lowScore System out println 學(xué)生信息 學(xué)生信息 學(xué)號 100 姓名 John 年齡 20 英語 89 0 計算機 87 3 數(shù)學(xué) 91 4 System out println 學(xué)號 s1 getS number System out println 姓名 s1 getS name System out println 年齡 s1 getS age System out println 英語 s1 getS englishGrade System out println 計算機 s1 getS computerGrade System out println 數(shù)學(xué) s1 getS mathGrade 運行結(jié)果貼圖 2 賬戶類Account 設(shè)計一個名為Account的類 它包括 一個名為id的int類型私有賬戶數(shù)據(jù)域 默認值為0 一個名為balance的double類型私有賬戶數(shù)據(jù)域 默認值為0 一個名為annualInterestRate的double類型私有數(shù)據(jù)域存儲當前利率 默認值為0 假設(shè)所有的 賬戶都有相同的利率 一個名為dateCreated的Date類型私有數(shù)據(jù)域存儲賬戶的開戶日期 一個能創(chuàng)建默認賬戶的無參構(gòu)造方法 一個能創(chuàng)建帶特定id和初始余額的賬戶的構(gòu)造方法 id balance和annualInterestRate的訪問器和修改器 dateCreated的訪問器 一個名為getMonthlyInterestRate 的方法返回月利率 一個名為withDraw的方法從賬戶提取特定數(shù)額 一個名為deposit的方法向賬戶存儲特定數(shù)額 實現(xiàn)這個類 編寫一個測試程序 創(chuàng)建一個賬戶ID為1122 余額為20000美元 年利率為4 5 的 Account對象 使用withdraw方法取款2500美元 使用deposit方法存款3000美元 然后打印余額 月 利息以及這個賬戶的開戶日期 程序代碼 package Java第二次實驗 import java util Date public class Account private int id private double balance private double annualInterestRate private Date dateCreated new Date public Account id 0 balance 0 annualInterestRate 0 dateCreated new Date public Account int id double balance this id id this balance balance public int getId return id public void setId int id this id id public double getBalance return balance public void setBalance double balance this balance balance public double getAnnualInterestRate return annualInterestRate public void setAnnualInterestRate double annualInterestRate this annualInterestRate annualInterestRate public Date getDateCreated return dateCreated public double getMonthlyInterestRate double MonthlyInterestRate annualInterestRate 12 return MonthlyInterestRate public double withDraw double expence balance expence return balance public double deposit double expence balance expence return balance public static void main String args Account a new Account 1122 20000 a setAnnualInterestRate 0 045 a withDraw 2500 a deposit 3000 System out println 賬戶余額為 a getBalance System out println 賬戶月利息是 a getMonthlyInterestRate System out println 創(chuàng)戶日期 a getDateCreated 運行結(jié)果貼圖 3 風(fēng)扇類Fan 設(shè)計一個名為Fan的類來表示一個風(fēng)扇 這個類包括 三個名為SLOW MEDIUM FAST而值是1 2和3的常量表示風(fēng)扇的速度 一個名為speed的int類型私有數(shù)據(jù)域表示風(fēng)扇的速度 默認值為SLOW 一個名為on的boolean類型的私有數(shù)據(jù)域表示風(fēng)扇是否打開 默認值為false 一個名為radius的double類型的私有數(shù)據(jù)域表示風(fēng)扇的半徑 默認值為5 一個名為color的String類型的數(shù)據(jù)域表示風(fēng)扇的顏色 默認值為blue 這四個數(shù)據(jù)域的訪問器和修改器 一個創(chuàng)建默認風(fēng)扇的無參構(gòu)造方法 一個名為toString 的方法返回描述風(fēng)扇的字符串 如果風(fēng)扇是打開的 那么該方法在一 個組合的字符串中返回風(fēng)扇的速度 顏色和半徑 如果風(fēng)扇沒有打開 該方法就返回一個由 fan is off 和風(fēng)扇顏色及半徑組合成的字符串 實現(xiàn)這個類 編寫一個測試程序 創(chuàng)建兩個fan對象 將第一個對象設(shè)置為最大速度 半徑為10 顏 色為yellow 狀態(tài)為打開 將第二個對象設(shè)置為中等速度 半徑為5 顏色為blue 狀態(tài)為關(guān)閉 通過 調(diào)用它們的toString方法顯示這些對象 程序代碼 package Java第二次實驗 public class Fans1 public static final int SLOW 1 public static final int MEDIUM 2 public static final int FAST 3 private int speed private boolean on private double radius public String color public Fans1 speed SLOW on false radius 5 color blue public Fans1 int speed boolean on double radius String color this speed speed this on on this radius radius this color color public int getSpeed return speed public void setSpeed int speed this speed speed public boolean isOn return on public void setOn boolean on this on on public double getRadius return radius public void setRadius double radius this radius radius public String getColor return color public void setColor String color this color color Override public String toString TODO Auto generated method stub String str if on str n該風(fēng)扇的速度為 speed n該 風(fēng)扇的顏色 color n該風(fēng)扇的半徑 radius else str fan is off n該風(fēng)扇的顏色 color n該風(fēng)扇的半徑 radius return str public static void main String args Fans1 f1 new Fans1 FAST true 10 yellow Fans1 f2 new Fans1 MEDIUM false 5 blue System out println f1 f1 toString nf2 f2 toString 運行結(jié)果貼圖 4 一些網(wǎng)站設(shè)定了一些制定密碼的規(guī)則 編寫一個方法 檢驗一個字符串是否是合法的密碼 假 設(shè)密碼規(guī)則如下 1 密碼必須至少有8個字符 2 密碼只能包括字母和數(shù)字 3 密碼必須至少有2個數(shù)字 編寫一個程序 提示用戶輸入密碼 如果該密碼符合規(guī)則就顯示 Valid Password 否則顯示 Invalid Password 程序源代碼 package Java第二次實驗 import java util public class Password public static void main String args System out println Please enter your passWord Scanner input new Scanner System in String passWord input next passWord passWord toLowerCase if passWord length 8 System out println Invalid Password else for int i 0 i a else System out println Invalid Password int count 0 for int i1 0 i1 2 System out println valid Password else System out println Invalid Password 程序運行結(jié)果貼圖 5 使用下面的方法頭編寫一個方法 找出某個指定字符在字符串中出現(xiàn)的次數(shù) public static int count String str char a 例如 count Welcome e 返回2 編寫一個測試程序 提示用戶輸入一個字符串 在該字符 串后緊跟著一個字符 然后顯示這個字符在字符串中出現(xiàn)的次數(shù) 程序源代碼 package Java第二次實驗 import java util public class CountLetters public static void main String args System out println 請輸入一個字符串 Scanner input new Scanner System in String str input next System out println 請輸入您要查找的字符 char ch input next charAt 0 count str ch static int count 0 public static int count String str char a for int i 0 i str length i if str charAt i a count System out println a 一共出現(xiàn)了 count 次 return count 程序運行結(jié)果貼圖 6 編寫一個方法 檢測兩個單詞是否互為變位詞 如果在不計順序的情況

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論