ImageVerifierCode 换一换
格式:DOCX , 页数:133 ,大小:55.92KB ,
资源ID:23850440      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/23850440.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(thinking injava习题答案8.docx)为本站会员(b****8)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

thinking injava习题答案8.docx

1、thinking injava习题答案8这是第9章的答案:Chapter 9To compile and execute these programs, youll need to download and install the code tree from Thinking in Java, 2nd Edition from www.BruceE, and youll need to put the full path of the base directory of the code tree (named code) into your classpath.Exercise 1/: c

2、09:E01_RandDouble.java/+M java E01_RandDouble/* Exercise 1 * * Create an array of double and fill() it using * RandDoubleGenerator. Print the results. */import com.bruceeckel.util.Arrays2;public class E01_RandDouble public static void main(String args) double da = new double10; Arrays2.fill(da, new

3、Arrays2.RandDoubleGenerator(); Arrays2.print(da); /:This is simply an exercise in using the generator from the package.The output for one run is:(0.5001203986291445, 0.8539982462192328, 0.6726664848706867, 0.892650339160878, 0.6006173265811632, 0.6156735136224198, 0.6478510992181377, 0.5858583665142

4、717, 0.8760917695678225, 0.5931881088554638)Exercise 2/: c09:E02_Gerbil.java/+M java E02_Gerbil/* Exercise 2 * * Create a new class called Gerbil with an int * gerbilNumber thats initialized in the * constructor (similar to the Mouse example in * this chapter). Give it a method called hop() * that p

5、rints out which gerbil number this is, * and that its hopping. Create an ArrayList and * add a bunch of Gerbil objects to the List. Now * use the get() method to move through the List * and call hop() for each Gerbil. */import java.util.*;class Gerbil private static int gerbilCounter = 0; private in

6、t gerbilNumber = +gerbilCounter; public String toString() return gerbil + gerbilNumber; public void hop() System.out.println(toString() + is hopping); public class E02_Gerbil public static void main(String args) ArrayList gerbils = new ArrayList(); for(int i = 0; i 10; i+) gerbils.add(new Gerbil();

7、for(int i = 0; i gerbils.size(); i+) (Gerbil)gerbils.get(i).hop(); /:The output is:gerbil 1 is hoppinggerbil 2 is hoppinggerbil 3 is hoppinggerbil 4 is hoppinggerbil 5 is hoppinggerbil 6 is hoppinggerbil 7 is hoppinggerbil 8 is hoppinggerbil 9 is hoppinggerbil 10 is hoppingExercise 3/: c09:E03_Gerbi

8、lIterator.java/+M java E03_GerbilIterator/* Exercise 3 * * Modify Exercise 2 so you use an Iterator to * move through the List while calling hop(). */import java.util.*;public class E03_GerbilIterator public static void main(String args) ArrayList gerbils = new ArrayList(); for(int i = 0; i 10; i+)

9、gerbils.add(new Gerbil(); for(Iterator it = gerbils.iterator(); it.hasNext();) (Gerbil)it.next().hop(); /:This produces the same output as before.Exercise 4/: c09:E04_GerbilMap.java/+M java E04_GerbilMap/* Exercise 4 * * Take the Gerbil class in Exercise 2 and put it * into a Map instead, associatin

10、g the name of * the Gerbil as a String (the key) for each * Gerbil (the value) you put in the table. Get * an Iterator for the keySet() and use it to * move through the Map, looking up the Gerbil * for each key and printing out the key and * telling the gerbil to hop(). */import java.util.*;public c

11、lass E04_GerbilMap public static void main(String args) HashMap map = new HashMap(); map.put(Bob, new Gerbil(); map.put(Frank, new Gerbil(); map.put(Tiffany, new Gerbil(); map.put(Ted, new Gerbil(); map.put(Wallace, new Gerbil(); map.put(Heather, new Gerbil(); Iterator it = map.keySet().iterator();

12、while(it.hasNext() String key = (String)it.next(); Gerbil value = (Gerbil)map.get(key); System.out.println(name = + key + , value = + value); /:The output is:name = Frank, value = gerbil 2name = Bob, value = gerbil 1name = Wallace, value = gerbil 5name = Ted, value = gerbil 4name = Tiffany, value =

13、gerbil 3name = Heather, value = gerbil 6Note that the objects are not stored in the order that they were entered, because of the nature of the hashing function (described in the book). If you use a TreeMap instead, youll see that the order is maintained.Exercise 5/: c09:E05_CountryList.java/+M java

14、E05_CountryList/* Exercise 5 * * Create a List (try both ArrayList and * LinkedList) and fill it using * Collections2.countries. Sort the list and * print it, then apply Collections.shuffle() to * the list repeatedly, printing it each time so * that you can see how the shuffle() method * randomizes

15、the list differently each time. */import com.bruceeckel.util.*;import java.util.*;public class E05_CountryList public static void main(String args) List lst = new ArrayList(); Collections2.fill(lst, Collections2.countries, 8); Collections.sort(lst); System.out.println(lst); for (int i = 0; i 5; i+)

16、Collections.shuffle(lst); System.out.println(lst); /:I only did it for ArrayList. You can easily change it (in only one place) for LinkedList.Heres the output from one run:ALGERIA, ANGOLA, BENIN, BOTSWANA, BURKINA FASO, BURUNDI, CAMEROON, CAPE VERDEBENIN, ANGOLA, ALGERIA, CAPE VERDE, BURUNDI, CAMERO

17、ON, BOTSWANA, BURKINA FASOALGERIA, BURKINA FASO, CAPE VERDE, BURUNDI, BOTSWANA, BENIN, ANGOLA, CAMEROONBENIN, ALGERIA, CAPE VERDE, CAMEROON, ANGOLA, BOTSWANA, BURUNDI, BURKINA FASOBOTSWANA, BURKINA FASO, ALGERIA, CAMEROON, CAPE VERDE, BURUNDI, ANGOLA, BENINALGERIA, BENIN, BOTSWANA, BURKINA FASO, BUR

18、UNDI, CAPE VERDE, CAMEROON, ANGOLAExercise 6/: c09:E06_MouseListRestriction.java/+M java E06_MouseListRestriction/* Exercise 6 * * Demonstrate that you cant add anything but a * Mouse to a MouseList. */import java.util.*;class Mouse private int mouseNumber; Mouse(int i) mouseNumber = i; / Override O

19、bject.toString(): public String toString() return This is Mouse # + mouseNumber; public int getNumber() return mouseNumber; class MouseList private ArrayList list = new ArrayList(); public void add(Mouse m) list.add(m); public Mouse get(int index) return (Mouse)list.get(index); public int size() ret

20、urn list.size(); public class E06_MouseListRestriction public static void main(String args) MouseList ml = new MouseList(); / Compile-time error: not a Mouse /! ml.add(new Object(); /:Exercise 7/: c09:E07_MouseListProblem.java/+M java E07_MouseListProblem/* Exercise 7 * * Modify MouseList.java so th

21、at it inherits from * ArrayList instead of using composition. * Demonstrate the problem with this approach. */import java.util.*;class MouseList2 extends ArrayList public void add(Mouse m) System.out.println(Adding a mouse); super.add(m); / Cant override with different return type: /! public Mouse g

22、et(int i) /! return (Mouse)super.get(i); /! / Sidestep: public Mouse getMouse(int i) return (Mouse)super.get(i); public class E07_MouseListProblem public static void main(String args) MouseList2 mice = new MouseList2(); for(int i = 0; i 3; i+) mice.add(new Mouse(i); for(int i = 0; i mice.size(); i+)

23、 System.out.println(mice.getMouse(i); / Oops! Can add a non-mouse: mice.add(new Object(); /:The compiler detects a problem when trying to override get( ) because the only thing thats different is the return value. We can “cleverly” sidestep this by making a getMouse( ) method. However, note that the

24、 ArrayList.get( ) method is still alive and well, so its still possible to pull objects out through that channel.Heres the output:Adding a mouseAdding a mouseAdding a mouseThis is Mouse #0This is Mouse #1This is Mouse #2Note that the Object does not cause an “Adding.” message to be printed, because

25、the add( ) method is actually overloading rather than overriding, which means that the add(Object) method still exists, and is called in the last case. Inheriting from ArrayList thus does not limit the type of objects that can be added, and so it definitely isnt what we want. And with a non-Mouse in

26、 the list, when you call getMouse( ) with that object an exception will be thrown during the cast.Exercise 8/: c09:E08_RepairCatsAndDogs.java/+M java E08_RepairCatsAndDogs/* Exercise 8 * * Repair CatsAndDogs.java by creating a Cats * container (utilizing ArrayList) that will only * accept and retrie

27、ve Cat objects. */import java.util.*;class Cat private int catNumber; Cat(int i) catNumber = i; void print() System.out.println(Cat # + catNumber); class Dog private int dogNumber; Dog(int i) dogNumber = i; void print() System.out.println(Dog # + dogNumber); class CatContainer private ArrayList cats

28、 = new ArrayList(); public void add(Cat c) cats.add(c); public Cat get(int i) return (Cat)cats.get(i); public int size() return cats.size(); public class E08_RepairCatsAndDogs public static void main(String args) CatContainer cats = new CatContainer(); for(int i = 0; i 7; i+) cats.add(new Cat(i); / Will not accept a Dog: /! cats.add(new Dog(7); for(int i = 0; i c

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1