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