thinking injava习题答.docx

上传人:牧羊曲112 文档编号:3167062 上传时间:2023-03-11 格式:DOCX 页数:15 大小:40.32KB
返回 下载 相关 举报
thinking injava习题答.docx_第1页
第1页 / 共15页
thinking injava习题答.docx_第2页
第2页 / 共15页
thinking injava习题答.docx_第3页
第3页 / 共15页
thinking injava习题答.docx_第4页
第4页 / 共15页
thinking injava习题答.docx_第5页
第5页 / 共15页
亲,该文档总共15页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《thinking injava习题答.docx》由会员分享,可在线阅读,更多相关《thinking injava习题答.docx(15页珍藏版)》请在三一办公上搜索。

1、thinking injava习题答这是第5章的答案: Chapter 5 Note that many of these exercises simply take you through the steps of discovering issues with packages. This is why so many of them are left to the reader. Exercise 1 /: c05:E01_ImplicitImports.java /+M java E01_ImplicitImports /* Exercise 1 * * Write a program

2、 that creates an ArrayList * object without explicitly importing * java.util.*. */ public class E01_ImplicitImports public static void main(String args) java.util.ArrayList al = new java.util.ArrayList; /: To do this, any references to the object must be made using its full package name. Exercise 2

3、/: c05:E02_LeftToReader.java /+M java E02_LeftToReader /* Exercise 2 * * In the section labeled package: the library * unit, turn the code fragments concerning * mypackage into a compiling and running set of * Java files. */ public class E02_LeftToReader public static void main(String args) System.o

4、ut.println(Exercise left to reader); /: Exercise 3 /: c05:E03_LeftToReader.java /+M java E03_LeftToReader /* Exercise 3 * * In the section labeled Collisions, take the * code fragments and turn them into a program, * and verify that collisions do in fact occur. */ public class E03_LeftToReader publi

5、c static void main(String args) System.out.println(Exercise left to reader); /: Exercise 4 /: c05:E04_LeftToReader.java /+M java E04_LeftToReader /* Exercise 4 * * Generalize the class P defined in this chapter * by adding all the overloaded versions of * rint and rintln necessary to handle all * th

6、e different basic Java types. */ public class E04_LeftToReader public static void main(String args) System.out.println(Exercise left to reader); /: Exercise 5 /: c05:E05_LeftToReader.java /+M java E05_LeftToReader /* Exercise 5 * * Change the import statement in TestAssert.java * to enable and disab

7、le the assertion mechanism. */ public class E05_LeftToReader public static void main(String args) System.out.println(Exercise left to reader); /: Exercise 6 /: c05:E06_AccessControl.java /+M java c05.E06_AccessControl /* Exercise 6 * * Create a class with public, private, * protected, and friendly d

8、ata members and * method members. Create an object of this class * and see what kind of compiler messages you get * when you try to access all the class members. * Be aware that classes in the same directory * are part of the default package. */ package c05; public class E06_AccessControl public int

9、 a; private int b; protected int c; int d; / Friendly public void f1 private void f2 protected void f3 void f4 / Friendly public static void main(String args) E06_AccessControl test = new E06_AccessControl; / No problem accessing everything inside / of main for this class, since main / is a member a

10、nd therefore has access: test.a = 1; test.b = 2; test.c = 3; test.d = 4; test.f1; test.f2; test.f3; test.f4; /: You can see that main( ) has access to everything because its a member. If you create a separate class within the same package, that class cannot access the private members: /: c05:E06_Oth

11、er.java / A separate class in the same package cannot / access private elements: package c05; public class E06_Other public E06_Other E06_AccessControl test = new E06_AccessControl; test.a = 1; /! test.b = 2; / Cant access: private test.c = 3; test.d = 4; test.f1; /! test.f2; / Cant access: private

12、test.f3; test.f4; /: If you create a class in a separate package (which you can do either by explicitly giving a package statement, or by simply putting it in a different directory) then it is unable to access anything but public members: /: c05:other2:E06_Other2.java / A separate class in the same

13、package cannot / access private elements: package c05.other2; public class E06_Other2 public E06_Other2 c05.E06_AccessControl test = new c05.E06_AccessControl; test.a = 1; /! test.b = 2; / Cant access: private /! test.c = 3; / Cant access: protected /! test.d = 4; / Cant access: friendly test.f1; /!

14、 test.f2; / Cant access: private /! test.f3; / Cant access: protected /! test.f4; / Cant access: friendly /: Exercise 7 /: c05:E07_ProtectedManipulation.java /+M java E07_ProtectedManipulation /* Exercise 7 * * Create a class with protected data. Create a * second class in the same file with a metho

15、d * that manipulates the protected data in the * first class. */ class WithProtected protected int i; public class E07_ProtectedManipulation public static void main(String args) WithProtected wp = new WithProtected; wp.i = 47; System.out.println(wp.i = + wp.i); /: The point of this exercise is to sh

16、ow that protected also means “package access” (aka “friendly”). That is, you can access protected fields within the same package. Just to be sure, try adding a protected method to WithProtected and accessing it from within E07_ProtectedManipulation. Exercise 8 /: c05:E08_LeftToReader.java /+M java E

17、08_LeftToReader /* Exercise 8 * * Change the class Cookie as specified in the * section labeled protected: sort of * friendly. Verify that bite is not public. */ public class E08_LeftToReader public static void main(String args) System.out.println(Exercise left to reader); /: Just follow the instruc

18、tions in the book. Exercise 9 /: c05:E09_Widget.java /+M java E09_Widget /* Exercise 9 * * In the section titled Class access youll * find code fragments describing mylib and * Widget. Create this library, then create a * Widget in a class that is not part of the * mylib package. */ import mylib.Wid

19、get; public class E09_Widget public static void main(String args) new Widget; /: /: c05:mylib:Widget.java package mylib; public class Widget public Widget System.out.println(Making a Widget); /: Exercise 10 /: c05:E10_LeftToReader.java /+M java E10_LeftToReader /* Exercise 10 * * Create a new direct

20、ory and edit your CLASSPATH * to include that new directory. Copy the * P.class file (produced by compiling * com.bruceeckel.tools.P.java) to your new * directory and then change the names of the * file, the P class inside and the method names. * (You might also want to add additional output * to wa

21、tch how it works.) Create another program * in a different directory that uses your new * class. */ public class E10_LeftToReader public static void main(String args) System.out.println(Exercise left to reader); /: Exercise 11 /: c05:E11_ConnectionManager.java /+M java E11_ConnectionManager /* Exerc

22、ise 11 * * Following the form of the example Lunch.java, * create a class called ConnectionManager that * manages a fixed array of Connection objects. * The client programmer must not be able to * explicitly create Connection objects, but can * only get them via a static method in * ConnectionManage

23、r. When the ConnectionManager * runs out of objects, it returns a null * reference. Test the classes in main. */ import c05.connection.*; public class E11_ConnectionManager public static void main(String args) Connection c = ConnectionManager.getConnection; while(c != null) System.out.println(c); c.

24、doSomething; c = ConnectionManager.getConnection; /: /: c05:connection:Connection.java package c05.connection; public class Connection private static int counter = 0; private int id = counter+; Connection public String toString return Connection + id; public void doSomething /: /: c05:connection:Con

25、nectionManager.java package c05.connection; public class ConnectionManager private static Connection pool = new Connection10; private static int counter = 0; static for(int i = 0; i pool.length; i+) pooli = new Connection; / Very simple - just hands out each one once: public static Connection getCon

26、nection if(counter pool.length) return poolcounter+; return null; /: The Connection class uses a static int called counter to automatically give each Connection object an identifier, which it will produce as part of its toString( ) representation. Connection also has a doSomething( ) method, which i

27、s presumably the useful work that motivates you to use the Connection object. Note that the constructor for Connection is “friendly,” so it is unavailable outside of this package. Thus, the client programmer is unable to access that constructor and cannot make instances of Connection directly. The o

28、nly way to get Connection objects is through the ConnectionManager. In the ConnectionManager, a static array of objects is initialized inside the static clause, which is only called once when the class is loaded (you can look up further details on static clauses in the book). In this version of the

29、solution, the connection manager is trivial, since it only produces each Connection object in the array once. You might want to create a slightly more sophisticated connection manager by allowing a connection to be checked back in when the client programmer is finished with it. Heres one way to acco

30、mplish this: /: c05:E11_ConnectionManager2.java /+M java E11_ConnectionManager2 / Connections that can be checked in. import c05.connection2.*; public class E11_ConnectionManager2 public static void main(String args) Connection ca = new Connection10; / Use up all the connections for(int i = 0; i 10;

31、 i+) cai = ConnectionManager.getConnection; / Should produce null since there are no / more connections: System.out.println( ConnectionManager.getConnection); / Return connections, then get them out: for(int i = 0; i 5; i+) cai.checkIn; Connection c = ConnectionManager.getConnection; System.out.prin

32、tln(c); c.doSomething; c.checkIn; /: /: c05:connection2:Connection.java package c05.connection2; public class Connection private static int counter = 0; private int id = counter+; Connection public String toString return Connection + id; public void doSomething public void checkIn ConnectionManager.

33、checkIn(this); /: /: c05:connection2:ConnectionManager.java package c05.connection2; public class ConnectionManager private static Connection pool = new Connection10; static for(int i = 0; i pool.length; i+) pooli = new Connection; / Produce the first available connection: public static Connection g

34、etConnection for(int i = 0; i pool.length; i+) if(pooli != null) Connection c = pooli; pooli = null; / Indicates in use return c; return null; / None left public static void checkIn(Connection c) for(int i = 0; i pool.length; i+) if(pooli = null) pooli = c; / Check it back in return; /: When a Conne

35、ction is checked out, its slot in pool is set to null. When the client programmer is done with the connection, checkIn( ) will return it to the connection pool by finding a null slot in pool and assigning it there. Of course, there are all kinds of problems with this approach. What if a client check

36、s a Connection in and then continues to use it? What if they check it in more than once? The book Thinking in Patterns (available from www.BruceE) has a more thorough coverage of the problem of the connection pool. Exercise 12 Create the following file in the c05/local directory (presumably in your

37、CLASSPATH): /: c05:local:PackagedClass.java /=M echo compile by hand to see results package c05.local; class PackagedClass public PackagedClass System.out.println( Creating a packaged class); /: Then create the following file in a directory other than c05: /: c05:foreign:Foreign.java /=M echo compil

38、e by hand to see results package c05.foreign; import c05.local.*; public class Foreign public static void main (String args) PackagedClass pc = new PackagedClass; /: Explain why the compiler generates an error. Would making the Foreign class part of the c05.local package change anything? Solution: PackagedClass is in its own package, and it is not a public class so it is unavailable outside of package c05.local. If Foreign was also part of c05.local, then it would have access to PackagedClass, since they would then both be in the same package.

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号