




已閱讀5頁,還剩16頁未讀, 繼續免費閱讀
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
山山 西西 大大 學學 計計 算算 機機 與與 信信 息息 技技 術術 學學院院 實實驗驗報報告告 姓 名 弓 彩 霞學 號 2013242032 專業班級軟件工程 4 班 課程名稱 Java 實驗實驗日期 2015 6 11 成 績指導教師楊紅菊批改日期 實驗名 稱 實驗 2 面向對象基礎和 JAVA 常用類 一 實驗目的 1 掌握類的定義 類的實例化方法 2 掌握類中的靜態變量和靜態方法的使用 3 掌握使用類的構造方法對對象的數據域進行初始化 4 掌握getter和setter的使用 5 了解如何構建一個字符串 掌握常用的 String StringBuffer StringBuilder 類的構造方法的 使用 6 掌握字符串的比較方法 尤其 equals 方法和 比較的區別 7 掌握 String 類常用方法的使用 8 掌握字符串與字符數組和 byte 數組之間的轉換方法 9 Date Random 類的常用方法 二 實驗內容 1 現在要求設計一個表示學生的類 里面有學生的姓名 學號 年齡 還要有三項成績 計算機 成績 數學成績 英語成績 要求可以求總分 平均分 最高 最低分 并且可以輸出一個學生的完 整信息 請設計此類 提示 程序開發步驟 根據需求定義出所要的類 根據題目中的要求規劃出類的屬性 所有的屬性必須封裝 private 所有的屬性必須通過getter和setter訪問 如果需要增加構造方法 為屬性賦初值 適當采用構造方法重載 所有的信息不要類中直接輸出 而是交給調用處輸出 即在該類中盡量不出現System out println 語句 例如在調用類中輸出如下結果 總分 267 70000000000005 平均分 89 23333333333335 最高分 91 4 最低分 87 3 學生信息 學號 100 姓名 John 年齡 20 英語 89 0 計算機 87 3 數學 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 請輸入學生姓名 學號 年齡 及其計算機 數學 英語成績 請您按順序輸入 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 學生信息 學生信息 學號 100 姓名 John 年齡 20 英語 89 0 計算機 87 3 數學 91 4 System out println 學號 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 數學 s1 getS mathGrade 運行結果貼圖 2 賬戶類Account 設計一個名為Account的類 它包括 一個名為id的int類型私有賬戶數據域 默認值為0 一個名為balance的double類型私有賬戶數據域 默認值為0 一個名為annualInterestRate的double類型私有數據域存儲當前利率 默認值為0 假設所有的 賬戶都有相同的利率 一個名為dateCreated的Date類型私有數據域存儲賬戶的開戶日期 一個能創建默認賬戶的無參構造方法 一個能創建帶特定id和初始余額的賬戶的構造方法 id balance和annualInterestRate的訪問器和修改器 dateCreated的訪問器 一個名為getMonthlyInterestRate 的方法返回月利率 一個名為withDraw的方法從賬戶提取特定數額 一個名為deposit的方法向賬戶存儲特定數額 實現這個類 編寫一個測試程序 創建一個賬戶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 創戶日期 a getDateCreated 運行結果貼圖 3 風扇類Fan 設計一個名為Fan的類來表示一個風扇 這個類包括 三個名為SLOW MEDIUM FAST而值是1 2和3的常量表示風扇的速度 一個名為speed的int類型私有數據域表示風扇的速度 默認值為SLOW 一個名為on的boolean類型的私有數據域表示風扇是否打開 默認值為false 一個名為radius的double類型的私有數據域表示風扇的半徑 默認值為5 一個名為color的String類型的數據域表示風扇的顏色 默認值為blue 這四個數據域的訪問器和修改器 一個創建默認風扇的無參構造方法 一個名為toString 的方法返回描述風扇的字符串 如果風扇是打開的 那么該方法在一 個組合的字符串中返回風扇的速度 顏色和半徑 如果風扇沒有打開 該方法就返回一個由 fan is off 和風扇顏色及半徑組合成的字符串 實現這個類 編寫一個測試程序 創建兩個fan對象 將第一個對象設置為最大速度 半徑為10 顏 色為yellow 狀態為打開 將第二個對象設置為中等速度 半徑為5 顏色為blue 狀態為關閉 通過 調用它們的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該風扇的速度為 speed n該 風扇的顏色 color n該風扇的半徑 radius else str fan is off n該風扇的顏色 color n該風扇的半徑 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 運行結果貼圖 4 一些網站設定了一些制定密碼的規則 編寫一個方法 檢驗一個字符串是否是合法的密碼 假 設密碼規則如下 1 密碼必須至少有8個字符 2 密碼只能包括字母和數字 3 密碼必須至少有2個數字 編寫一個程序 提示用戶輸入密碼 如果該密碼符合規則就顯示 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 程序運行結果貼圖 5 使用下面的方法頭編寫一個方法 找出某個指定字符在字符串中出現的次數 public static int count String str char a 例如 count Welcome e 返回2 編寫一個測試程序 提示用戶輸入一個字符串 在該字符 串后緊跟著一個字符 然后顯示這個字符在字符串中出現的次數 程序源代碼 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 一共出現了 count 次 return count 程序運行結果貼圖 6 編寫一個方法 檢測兩個單詞是否互為變位詞 如果在不計順序的情況
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 嵌入式技術在醫療行業的應用試題及答案
- 數據庫管理中的挑戰與機遇試題及答案
- 稀有稀土金屬加工前沿技術探討考核試卷
- 風險控制在嵌入式項目中的應用試題及答案
- 嵌入式產品交付策略試題及答案
- 公路管理體系與規范試題及答案
- 全面修訂安全管理制度
- 工地排水配件管理制度
- 實戰模擬計算機三級數據庫試題及答案
- 公司保溫車間管理制度
- 理論聯系實際談一談你對高質量發展的理解參考答案二
- T/CEMIA 026-2021濕電子化學品技術成熟度等級劃分及定義
- 浙江省金華市東陽市2025年七年級下學期期末數學試題及答案
- 江西省煙草專賣局(公司)筆試試題2024
- 期末復習題(試題)2024-2025學年六年級下冊數學人教版
- 多彩的非洲文化 - 人教版課件
- 2023年廣州中考政治試題及答案
- 2025年年中考物理綜合復習(壓軸特訓100題55大考點)(原卷版+解析)
- -《經濟法學》1234形考任務答案-國開2024年秋
- T-SCSTA001-2025《四川省好住房評價標準》
- 2025-2030全球及中國貨運保險行業市場現狀供需分析及市場深度研究發展前景及規劃可行性分析研究報告
評論
0/150
提交評論