




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、Java 語言程序設計 第七章課后習題答案 1.數(shù)組的聲明與數(shù)組元素的創(chuàng)建有什么關系? 答:聲明數(shù)組僅僅是代表試圖創(chuàng)建數(shù)組,不分配任何存儲空間,聲明是為創(chuàng)建做“鋪墊” 。 2.Vector 類的對象與數(shù)組有什么關系?什么時候適合使用數(shù)組,什么時候適合使用 Vector? 答:vector 是一個能夠存放任意對象類型的動態(tài)數(shù)組,容量能自動擴充,而數(shù)組存儲固定且 類型相同的對象; 對于存儲固定類型相同的對象使用數(shù)組, 對于存儲不同類型或者動態(tài)調(diào)整 數(shù)組大小的情況使用 Vector。 3.與順序查找相比,二分查找有什么優(yōu)勢?使用二分查找的條件? 答:對于大數(shù)據(jù)量中進行查找時二分查找比順序查找效率高得
2、多;條件是已排序的數(shù)組。 4.試舉出三種常見的排序算法,并簡單說明其排序思路。 答:選擇排序:基本思想是站在未排序列中選一個最小元素,作為已排序子序列,然后再 重復地從未排序子序列中選取一個最小元素, 把它加到已經(jīng)排序的序列中, 作為已排序子序 列的最后一個元素,直到把未排序列中的元素處理完為止。 插入排序: 是將待排序的數(shù)據(jù)按一定的規(guī)則逐一插入到已排序序列中的合適位置處, 直到 將全部數(shù)據(jù)都插入為止。 二分查找:將表中間位置記錄的關鍵字與查找關鍵字比較,如果兩者相等,則查找成功; 否則利用中間位置記錄將表分成前、 后兩個子表, 如果中間位置記錄的關鍵字大于查找關鍵 字,則進一步查找前一子表,
3、否則進一步查找后一子表。重復以上過程,直到找到滿足條件 的記錄,使查找成功,或直到子表不存在為止,此時查找不成功。 5.聲明一個類 People,成員變量有姓名、出生日期、性別、身高、體重等;生成 10 個 People 類對象,并放在一個以為數(shù)組中,編寫方法按身高進行排序。 /People 類 public class People private String name; private String birthdaydate; private String sex; private double height; private double weight; public People()
4、/默認構造函數(shù) public People(People p) =; this.birthdaydate=p.birthdaydate; this.sex=p.sex; this.height=p.height; this.weight=p.weight; public People(String name,String birthdaydate,String sex,double height,double weight) =name; this.birthdaydate=birthdaydate; this.sex=sex; this.hei
5、ght=height; this.weight=weight; public String getName() return name; public void setName(String name) = name; public String getBirthdaydate() return birthdaydate; public void setBirthdaydate(String birthdaydate) this.birthdaydate = birthdaydate; public String getSex() return sex; public vo
6、id setSex(String sex) this.sex = sex; public double getHeight() return height; public void setHeight(double height) this.height = height; public double getWeight() return weight; public void setWeight(double weight) this.weight = weight; public String toString() return 姓名:+name+n出生年月:+birthdaydate+n
7、性別:+sex+n 身高:+height+n體重:+weight; /test7_5 類 public class test7_5 /* * param args */ public static void main(String args) / TODO Auto-generated method stub People people= new People(林楚金,1989年8月13日,男,182,63.5), new People(諸葛亮,181年7月23日,男,184,76.6), new People(邁克杰克遜,1958年8月29日,男,180,60), new People(喬丹
8、,1963年2月17日,男,198,98.1), new People(拿破侖,1769年8月15日,男,159.5,63), new People(蒼井空,1983年11月11日,女,155,45),; People temp=new People(); for(int i=0;ipeople.length-1;i+) for(int j=i+1;jpeople.length;j+) if(peoplei.getHeight()peoplej.getHeight() temp=peoplej; peoplej=peoplei; peoplei=temp; System.out.println
9、(按身高從小到大排序后的結果如下:); for(int i=0;ipeople.length;i+) System.out.println(peoplei+n); 運行結果: 6.聲明一個類,此類使用私有的 ArrayList 來存儲對象。使用一個 Class 類的引用得到第一個 對象的類型之后,只允許用戶插入這種類型的對象。 /Fuck 類 import java.util.ArrayList; public class Fuck private ArrayList man=new ArrayList(); private Class classType=null; public void
10、add(Object f) if(man.size()=0) classType=f.getClass(); if(classType.equals(f.getClass() man.add(f); System.out.println(插入成功.); else System.out.println(只允許插入+getClassType()+類的對象.); public ArrayList getMan() return man; public Class getClassType() return classType; public Fuck() /test7_6 public class
11、test7_6 public static void main(String args) Fuck fuckman=new Fuck(); String s=new String(林楚金); fuckman.add(s); fuckman.add(10);/測試插入插入整數(shù) fuckman.add(f);/測試插入插入字符 fuckman.add(希特勒); System.out.println(fuckman.getMan(); 運行結果: 7.找出一個二維數(shù)組的鞍點,即該位置上的元素在所在行上最大,在所在列上最小。 (也可 能沒有鞍點) /test7_7 import java.util.
12、Scanner; public class test7_7 public static void main(String args) int row, series, max; boolean T=false; Scanner cin = new Scanner(System.in); System.out.println(請輸入數(shù)組的行數(shù)); row = cin.nextInt(); System.out.println(請輸入數(shù)組的列數(shù)); series = cin.nextInt(); int Array = new introwseries; int R = new introw;/
13、記錄每行最大的數(shù)的列標 int S = new intseries;/ 記錄每列最小的數(shù)的行標 System.out.println(請輸入數(shù)組內(nèi)容); for (int i = 0; i Array.length; i+) for (int j = 0; j Arrayi.length; j+) Arrayij = cin.nextInt(); if(j=series-1) max = Arrayi0; for (int z = 1; z max) max = Arrayiz; Ri = z; for (int j = 0; j Array0.length; j+) max = Array0
14、j; for (int z = 1; z row ; z+) if (Arrayzj max) max = Arrayzj; Sj = z; for(int i=0;iArray.length;i+) if(SRi=i) System.out.println(鞍點:+Array+i+Ri+ :+ArrayiRi+n); T=true; if(T=false) System.out.println(沒有鞍點); 運行結果: 8. 聲明一個矩陣類Matrix,其成員變量是一個二維數(shù)組,數(shù)組元素類型為int,設計下面的 方法, 并聲明測試類對這些方法進行測試。 (1)構造方法。 Matrix()/
15、構造一個1010個元素的矩陣,沒有數(shù)據(jù)Matrix(int n)/構造一個nn 個元素的矩陣, 數(shù)據(jù)隨機產(chǎn)生 Matrix(int n,int m)/構造一個nm個元素的矩陣, 數(shù)據(jù)隨機產(chǎn)生 Matrix(int table)/以一個整型的二維數(shù)組構造一個矩陣 (2) 實例方法。public void output()/輸出Matrix類中數(shù)組的元素值public Matrix transpose/求一個矩陣的轉(zhuǎn)置矩陣 Public Boolean isTriangular /判斷一個矩陣是否為上三角矩陣 Public Boolean isSymmetry() /判斷一個矩陣是否為對稱矩陣 P
16、ublic void add(Matrix b)/將矩陣b與接受著對象相加,結果放在接受著對象中 用key-value對來填充一個HashMap,并按hash code排列輸出。 編寫一個方法,在方法中使用Iterator類遍歷Collection,并輸出此集合類中每個對象的 hashCode()值。用對象填充不同類型的Collection類對象,并將此方法應用于每一種 Collection類對象。 /Matrix類 public class Matrix int array; Matrix() / 構造一個10X10個元素的矩陣,沒有數(shù)據(jù) array = new int1010; Matri
17、x(int n) / /構造一個nn個元素的矩陣,數(shù)據(jù)隨機產(chǎn)生 array = new intnn; for (int i = 0; i n; i+) for (int j = 0; j n; j+) arrayij = (int) (Math.random() * 10); Matrix(int n, int m) / 構造一個n*m個元素的矩陣,數(shù)據(jù)隨機產(chǎn)生 array = new intnm; for (int i = 0; i n; i+) for (int j = 0; j m; j+) arrayij = (int) (Math.random() * 10); Matrix(int
18、 table) / 以一個整型的二維數(shù)組構造一個矩陣 array = table; public void output() / 輸出Matrix類中數(shù)組的元素值 for (int i = 0; i array.length; i+) for (int j = 0; j array0.length; j+) System.out.print(arrayij + ); System.out.println(); public void transpose() / 輸出一個矩陣的轉(zhuǎn)置矩陣 for (int i = 0; i array0.length; i+) for (int j = 0; j
19、array.length; j+) System.out.print(arrayji + ); System.out.println(); public boolean isTriangular() / 判斷一個矩陣是否為上三角矩陣 boolean flag = true; for (int i = 0; i array.length; i+) for (int j = 0; j j) return flag; public boolean isSymmetry() / 判斷一個矩陣是否為對稱矩陣 boolean Symmetry = true; for (int i = 0; i array.length; i+) for (int j = i; j array0.length; j+) if (arrayij = arrayji) Symmetry = false; return Symmetry; public void add(Matrix b) / 將矩陣b與接受著對象相加,結果放在接受著對 象中 for (int i = 0; i array.length; i+) for (int j = 0; j array0.length; j+) this.arrayij = arrayij + b.arrayi
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年冷庫緊急情況處理試題
- 山西小學生課件網(wǎng)
- 展風采班會課件
- 小學生鐘表制作課件圖片
- 35 必修2 第六單元 第29講 生物的進化
- 24 必修2 第四單元 第21講 基因的自由組合定律拓展題型
- 2025年廣東省中考道德與法治真題含答案
- 酒后代駕安全責任合同
- 智能制造展覽會參展商權益保障協(xié)議
- 舊車翻新專業(yè)保養(yǎng)與維修協(xié)議書
- 2024年安徽大學專職輔導員招聘筆試真題
- GB 9743-2024轎車輪胎
- 茶文化講座優(yōu)選ppt資料
- 綠化工程施工技術方案及措施(可編輯)
- 會計知識競賽題庫附答案2021
- 廠房鋼筋混凝土地坪板工程施工方案
- 項目延期申請表(樣本)
- 固井工藝技術培訓教學課件(77p)
- 入團志愿書(2016版本)(可編輯打印標準A4) (1)
- 盤扣式腳手架模板與支撐架專項施工方案
- 消防器材購銷合同2
評論
0/150
提交評論