Data Structures and Java Collection Framework - William Collins - Chapter 2 Solutions

CONCEPT EXERCISES
2.1 The System class in java.lang has a class constant identifier that has been extensively used in Chapters 0, 1 and 2. What is that constant identifier? Why should a class’s constant identifiers be static ? Should a method’s constant identifiers be static ? Explain. Get the solution of 2.1


2.2 Create a catch block that will handle any exception. Create a catch block that will handle any input/output exception. Create a catch block that will handle any run-time exception. Get the solution of 2.2


2.3 What is wrong with the following skeleton?
try
{
...
} // try
catch (IOException e)
{
...
} // catch IOException
catch (FileNotFoundException e)
{
...
} // catch FileNotFoundException  Get the solution of 2.3


2.4 Suppose fileScanner is a Scanner object for reading from an input file, and printWriter is a Print
Writer object for writing to an output file. What will happen if, at the end of a program, you forget to close fileScanner ? What will happen if, at the end of a program, you do not close printWriter ? Get the solution of 2.4


2.5 What does “bottom-up” testing mean with respect to the classes in a project? Get the solution of 2.5


2.6 Suppose we create a two-dimensional array (literally, an array in which each element is an array). The following creates an int array with 50000 rows and 100000 columns:
int [ ][ ] a = new int [50000][100000];
If this code is executed, the program terminates abnormally, and the message is java.lang.OutOfMemoryError
Exception in thread "main"
Why wasn’t memory re-allocated by the garbage collector? Hypothesize whether this abnormal termination be handled with a try-block and catch-block. Test your hypothesis and explain. Get the solution of 2.6


2.7 Can a protected field be accessed outside of the class in which it is declared and subclasses of that class?
What does the following statement mean? “Subclassing represents more of a commitment than mere use.” Get the solution of 2.7


2.8 Arrays are strange objects because there is no array class. But an array object can call methods from the Object class. Determine and explain the output from the following code:
int [ ] a = new int [10];
int [ ] b = new int [10];
a [3] = 7;
b [3] = 7;
System.out.println (a.equals(b)); Get the solution of 2.8



PROGRAMMING EXERCISES
2.1 Develop the specification for a method that scans one line that is supposed to contain three double values and returns the largest. Throw all possible exceptions. Start with a stub for your method and create a test class to test your method. Re-test your method as you define it. Finally, include a main method and a run( ) method that calls the method you developed. Get the solution of 2.1


2.2 Develop the specification for a method that scans (what are supposed to be) double values from a file and returns the largest. Throw all possible exceptions. Start with a stub for your method and create a test class to test your method. Re-test your method as you define it. Finally, include a main method and a run( ) method that calls the method you developed. Get the solution of 2.2


2.3 Modify the run method for the Company class to scan from an input file and write to an output file. Include a re-prompt if either the input or output path is incorrect. Get the solution of 2.3


2.4 Hypothesize what is wrong with the following method:
public static boolean isEven (int i)
{
if (i % 2 == 0)
return true;
if (i % 2 != 0)
return false;
} // method isEven
Test your hypothesis by calling this method from a run( ) method. Can a try-block and catch-block
handle the problem? Explain. Get the solution of 2.4


2.5 Hypothesize the output from the following:
System.out.println (null + "null");
Test your hypothesis. Provide the code in the String class that explains why the output is what it is. Get the solution of 2.5


2.6 Give an example to show that private visibility is more restrictive than default visibility. Give an example to show that default visibility is more restrictive than protected visibility. Give an example to show that protected visibility is more restrictive than public visibility. In each case, test your code to make sure that the more restrictive choice generates a compile-time error message. No error message should be generated for the less restrictive choice. Get the solution of 2.6


2.7 Protectedness transfers across packages, but only within a subclass, and only for objects whose type is that subclass. For a bare-bones illustration, suppose we have class A declared in package APackage:
package APackage;
public class A
{
protected int t;
} // class A
Also, suppose that classes C and D are subclasses of A and that C and D are in a different package from A. Then within class D, the t field is treated as if it were declared in D instead of in A. Here are possible declarations for classes C and D:
import APackage.*;
public class C extends A { }
Programming Exercises 101
Class D is declared in another file. For each of the four accesses of t in the following declaration of class D,
hypothesize whether the access is legal or illegal:
import APackage.*;
public class D extends A
{
public void meth()
{
D d = new D();
d.t = 1; // access 1
t = 2; // access 2
A a = new A();
a.t = 3; // access 3
C c = new C();
c.t = 4; // access 4
} method meth
} // class D
Test your hypotheses by creating and running a project that includes the above files. Get the solution of 2.7


2.8 Re-do Programming Exercise 1.2 to print out the number of above-average salaries. Use an array field to hold the salaries, and assume there will be at most 10 salaries in the input. Get the solution of 2.8


2.9 Study the specification of the arraycopy method in the System class, and then write a short program that uses the arraycopy method to copy all the elements of an array to another array. Output the elements in the destination array to make sure the copying actually occurred. Get the solution of 2.9


2.10 Re-do Programming Exercise 2.8 if the input can contain an arbitrary number of salaries.
Hint: Start with an array of length 10. Whenever the number of salaries in the input exceeds the current length of the array field, create a new array of twice that length, copy the old array to the new array—see Programming Exercise 2.9—and then assign the new array (reference) to the old array (reference). Get the solution of 2.10


2.11 According to the full method specification in the Object class, any override of the Object class’s equals method should satisfy the following five properties:
1. reflexivity, that is, for any non-null reference x,
x.equals (x)
should return true.
2. symmetry, that is, for any non-null references x and y,
x.equals (y)
should return the same result as
y.equals (x)
3. transitivity, that is, for any references x, y and z if
x.equals (y)
returns true, and
y.equals (z)
returns true, then
x.equals (z)
should return true.
4. consistency, that is, for any non-null references x and y, multiple invocations of
x.equals (y)
should consistently return true or consistently return false, provided no information used in equals
comparisons on the objects is modified.
5. actuality, that is, for any non-null reference x,
x.equals (null)
should return false.
For the FullTimeEmployee class’s equals method (see Section 2.7), provide examples to support the
claim that the equals method satisfies those five properties. You are not being asked to prove that the
FullTimeEmployee class’s equals method satisfies those properties. Get the solution of 2.11


2.12 Create and run a test class for the equals method defined in Section 2.7 for the FullTimeEmployee class. Get the solution of 2.12