THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

TURN_RIGHT constants. What advantage is there to using the enum? 152


Exercise 6.3: Redefine the Verbose interface from Section 4.2.1 on page 121 to use an enum for the
verbosity level instead of integer constants. 152


Exercise 6.4: Expand your traffic light color enum from Exercise 6.1 on page 152 so that each enum constant
has a suitable Color object that can be retrieved with getColor. 156


Exercise 6.5: Redo Exercise 6.4 making getColor an abstract method and defining constant-specific
methods for each enum constant to return the correct Color object. Would you recommend using
constant-specific methods to do this? 159


Exercise 7.1: Just for fun, write a "Hello, World" program entirely using Unicode escape sequences. 163


Exercise 7.2: Write a class that declares a field for each of the primitive numeric types, and try to assign
values using the different literal formsfor example, try to assign 3.5f to an int field. Which literals can be
used with which type of field? Try changing the magnitude of the values used to see if that affects things. 169


Exercise 7.3: Write a program that calculates Pascal's triangle to a depth of 12, storing each row of the triangle
in an array of the appropriate length and putting each of the row arrays into an array of 12 int arrays. Design
your solution so that the results are printed by a method that prints the array of arrays using the lengths of
each array, not a constant 12. Now change the code to use a constant other than 12 without modifying your
printing method. 178


Exercise 9.1: Write a program that uses the operators +, , *, and /, on two infinite operands and show the
result. Ensure that you try both same signed and opposite-signed infinity values. 203


Exercise 9.2: Write a method that determines the number of 1 bits in a passed-in int, by using just the bit
manipulation operators (that is, don't use Integer.bitCount). Compare your solution with published
algorithms for doing thissee the related reading for "General Programming Techniques" on page 758 for one
source. 210


Exercise 9.3: Review your solution to Exercise 7.3 to see if it can be written more clearly or succinctly with
the operators you've learned about in this chapter. 213


Exercise 9.4: Using what you've learned in this chapter but without writing code, figure out which of the
following expressions are invalid and what the type and values are of the valid expressions: 223


Exercise 10.1: Using ifelse in a loop, write a method that takes a string parameter and returns a string with
all the special characters in the original string replaced by their language equivalents. For example, a string
with a " in the middle of it should create a return value with that " replaced by \". (Section 7.2.3 on page 167
lists all special characters). 232


Exercise 10.2: Rewrite your method from Exercise 10.1 to use a switch. 235


Exercise 10.3: Using your "days of the week" enum from Exercise 6.1 write a method that takes a day of the
week and returns true if it is a working day, and false otherwise. First use nested ifelse statements and
then a switch statement. Which do you think results in clearer code? 235


Exercise 10.4: Select a few previous exercise solutions for which you have used a for loop and rewrite it
using a while loop. Can you also rewrite it using a dowhile loop? Would you do so? If not, why not? 236


Exercise 10.5: Write a method that takes two char parameters and prints the characters between those two
values, including the endpoints. 239

Free download pdf