Java编程思想 第4版 实验报告及答案 ([美]Bruce)
实验报告配套教材:
书名:Java编程思想 第4版
作者:[美]Bruce Eckel 著 陈昊鹏 译
出版社:机械工业出版社
实验报告概述:
1. 实现一个表示电话号码(要求带有区号和号码,如010-68911205)的Phone 类,其中某个构造函数的参数应为表示电话号码的字符串。要求实现相应的set 和get 函数来分别设置及获取电话号码中的区号(area,如010)和号码(number, 如68911205),重写Object.toString( )函数打印出整个电话号码。(提示:可使用 String.indexOf( )和String.substring( )等函数) 2. a) Write the method signature for an instance method named minValue, which takes as a formal parameter an array of int and returns the minimum value found in that array. b) Implement the minValue method. Some or all of the values in the array may be less than zero, so your implementation will need to cope with this. c) Implement an instance method named cumulativeSums, which takes an array of int as formal parameter and returns an array of int. The returned array should have the same number of elements as array passed as the formal parameter. Each element in the returned array should contain the sum of the elements in the formal parameter, up to and including the corresponding element. For example, given an array {1,2,3}, the resulting array would be {1,3,6}, since 3 = 1 + 2 and 6 = 1 + 2 +3. d) Declare and initialise an array of seven String values to contain the days of the week, starting from Sunday. 3. This question involves the design and implementation of a LibraryBook class of objects representing library books. a) Decide what information needs to be maintained about each library book. At this stage, do not worry about information needed to support operations on library books, but only about information that to represent the books in the library. Indicate the type of each piece of information (you should not need to use types other than the basic types and String). You should assume there is only one copy of each book. b) The only complex operations of the LibraryBook class are those involved in the borrowing process. These are as follows: reserve: Mark the books as being reserved. Only one person can reserve a book at a time and a book cannot be borrowed or reserved by anyone else whilst it is reserved. Do nothing if reservation is not possible. ableToBorrow: Returns a boolean value indicating whether the book can currently be borrowed, which is only possible if the book is not already on loan or reserved by someone else. borrow: Record that the book is on loan, if it is currently able to be borrowed. cancelReservation: Cancels a previous reservation. This can only be done by the person who placed the reservation. return: Record that the book has been returned. This can only be done by the person who borrowed it. You should assume that the person attempting to do the operation is passed in as a String parameter for each of these operations. Your first task is to write the method signatures of these five methods. c) Now, design algorithms for each of the five operations in the previous question. d) Finally, implement the LibraryBook class. Before you write the methods, declare any instance variables, including both the variable from the first part of the question and any instance variables needed in the methods you have just designed. After that, implement the methods by translating your algorithms into Java. Finally, implement a constructor method, this should take parameters corresponding to each piece of data you identified in the first part of the question and should initialize that data using those parameters. You should also ensure that any other instance variables you use in your methods are initialized properly (e.g. a book should not be on loan or reserved when the object representing it is created). 4. 实现一个名为Value 的类,其中含有一个字符类型的成员x 和双精度类型的 成员y。请重写(override)其equals( )方法,使得用equals( )比较Value 类的两 个对象的内容时,相等条件是对象的所有成员分别对应相等。 5. 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 ConnectionManager. When the ConnectionManager runs out of objects, it returns a null reference. Test the classes in main( ). 6. Create the following file in the c05/local directory (presumably in your CLASSPATH): ///: c05:local:PackagedClass.java 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 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?