Solutions to Self-Study Exercises


[Page 398]

Solution 8.1

Running the TestPrint program will produce the output shown here. It is clear that the inherited toString() method is used by println() when printing a TestPrint object.

56.0 TestPrint@be2d65 


Solution 8.2

If you override the toString() method in TestPrint, you should get something like the output shown here, depending on how you code toString(). It is clear that the toString() method is used polymorphously by println().

56.0 Hello from TestPrint 


Solution 8.3

The output produced when constructing objects of type A and B in the order shown in the exercise would be as follows, with each letter occurring on a separate line:

A B B 


Solution 8.4

The new implementation of B's method() will invoke A's version of the method before printing B. This will print A A B A B.

void method () {     super.method();     System.out.println("B"); } 


Solution 8.5

Given the definitions of classes A and B in the exercise, the marked statements would be invalid:

A a = new B();   // Valid a B is an A a = new A();     // Ok B b = new A();   // Invalid. An A is not necessarily a B b = new B();     // OK 


Solution 8.6

Given the class definitions and code segment in this exercise, the output would be A A B A B C, with each letter printing on a separate line.


[Page 399]
Solution 8.7

Definition of an Pig subclass of Animal:

public class Pig extends Animal {   public Pig() {       kind = "pig";   }   public String speak() {       return "oink";   } } 


Solution 8.8

If polymorphism was not used in our design, the talk() method would have to be modified to the following in order to accommodate a Pig subclass:

public String talk(Animal a) {   if (a instanceof Cow)      return "I am a " + kind + " and I go " + a.moo();   else if (a instanceof Cat)      return "I am a " + kind + " and I go " + a.meow();   else if (a instanceof Pig)      return "I am a " + kind + " and I go " + a.oink();   else      return "I don't know what I am"; } 


Solution 8.9

Code to swap two boolean variables:

boolean temp = b1;   // Save b1's value b1 = b2;             // Change b1 to b2 b2 = temp;           // Change b2 to b1's original value 


Solution 8.10

Creating a ToggleButton that can be used to deal or collect cards:

private ToggleButton dealer = new ToggleButton("deal","collect"); add(dealer); dealer.addActionListener(this); 


Solution 8.11

Modify the Caesar class so that it will allow various-sized shifts to be used.

private int shift; public void setShift(int n) { shift = n; } public int getShift()       { return shift; }                                                   // Modification to encode(): ch = (char)('a' + (ch -'a'+ shift) % 26);         // Shift                                                   // Modification to decode(): ch = (char)('a' + (ch -'a'+ (26-shift)) % 26);    // Shift 



[Page 400]
Solution 8.12

Modify transpose.encode() so that it uses a rotation instead of a reversal. The operation here is very similar to the shift operation in the Caesar cipher. It uses modular arithmetic to rearrange the letters in the word. For example, suppose the word is "hello". Its letters are indexed from 0 to 4. The following table shows how the expression ((k+2) % 5) will rearrange the letters as k varies from 0 to 4:

    k  charAt(k)  (k+2) % 5   charAt((k+2) % 5)     -------------------------------------------     0  'h'          2          'l'     1  'e'          3          'l'     2  'l'          4          'o'     3  'l'          0          'h'     4  'o'          1          'e' // Modification to encode(): public String encode(String word) {     StringBuffer result = new StringBuffer();     for (int k=0; k < word.length(); k++)         result.append(word.charAt((k+2) % word.length()));     return result.toString(); } 


Solution 8.13

A NimPlayer class that plays the optimal OneRowNim game would be identical to the NimPlayerBad class except that the move():int method would be replaced with the following implementation:

public int move() {    int sticksLeft = game.getSticks();    if (sticksLeft % (game.MAX_PICKUP + 1) != 1)        return (sticksLeft - 1) % (game.MAX_PICKUP +1);    else {        int maxPickup = Math.min(game.MAX_PICKUP, sticksLeft);         return 1 + (int)(Math.random() * maxPickup);    } } 





Java, Java, Java(c) Object-Orienting Problem Solving
Java, Java, Java, Object-Oriented Problem Solving (3rd Edition)
ISBN: 0131474340
EAN: 2147483647
Year: 2005
Pages: 275

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net