Sei sulla pagina 1di 4

Solutions Manual: Chapter 9 Review Exercises R9.

1 The following requires a cast:


c = i; // 1 i = j; // 3

Big Java, by Cay Horstmann

R9.2 None of them will throw an exception. R9.3 These assignments are legal:
Edible e = sub; // 1 sub = (Sandwich)e; // 5

R9.4 When casting a primitive type, the user acknowledges to the compiler that information may be lost. When casting objects, the user acknowledges to the compiler that the cast may cause an exception. R9.5

Solutions Manual: Chapter 9 R9.6


r r r r r r instanceof instanceof instanceof instanceof instanceof instanceof

Big Java, by Cay Horstmann

Rectangle: true Shape: true Point: false Object: true ActionListener: false Serializable: true

R9.7 The call Rectangle r = s.getBounds(); is an example of polymorphism because the Shape interface declares the method, the Rectangle class supports an implementation, and that implementation is picked at runtime. R9.8 Calls to constructors and calls to final methods are two kinds of method calls that use early binding in Java. R9.9 In the first implementation, you first need to create a DataSet object. To find the average salary, you would first need to add the Measureable objects and then construct another Measurable object to get the highest salary. You also need to modify the Employee class so that it implements the Measurable interface. In the second implementation, you first construct a Measurer object to carry out the measurement, and then a DataSet object that takes in this measurement. An object value, instead of a Measureable object, is used to add all the object values. This is used to obtain the average and highest salary. The second implementation is easier because the responsibility of measuring does not lie on the added objects. This way, you can define measurers to take on any kind of measurement. R9.10 In the first implementation, the compiler would complain that the add method cannot take String object when it is expecting an object of type Measurable. In the second implementation, the add method does not complain since it will take any object. But it will throw an exception when the measurer tries to convert the String reference to a Rectangle reference. R9.11
import java.awt.Rectangle; /** This program demonstrates the use of a Measurer.

Solutions Manual: Chapter 9

Big Java, by Cay Horstmann

*/ public class DataSetTest { public static void main(String[] args) { Measurer m = new RectangleMeasurer(); DataSet data = new DataSet(m); data.add(new Rectangle(5, 10, 20, 30)); data.add(new Rectangle(10, 20, 30, 40)); data.add(new Rectangle(20, 30, 5, 10)); System.out.println( "Average area = " + data.getAverage()); Rectangle max = (Rectangle)data.getMaximum(); System.out.println("Maximum area = " + max); } } import java.awt.Rectangle; public class RectangleMeasurer implements Measurer { public double measure(Object anObject) { Rectangle aRectangle = (Rectangle)anObject; double area = aRectangle.getWidth() * aRectangle.getHeight(); return area; } }

R9.12
import javax.swing.JOptionPane; import javax.swing.Timer; /** This program uses a timer to add interest to a bank account once per second. */ public class TimerTest { public static void main(String[] args) { BankAccount account = new BankAccount(1000); InterestAdder listener = new InterestAdder(account); final int DELAY = 1000; // milliseconds between timer ticks Timer t = new Timer(DELAY, listener); t.start(); JOptionPane.showMessageDialog(null, "Quit?"); System.exit(0); } } import java.awt.event.ActionEvent;

Solutions Manual: Chapter 9

Big Java, by Cay Horstmann

import java.awt.event.ActionListener; public class InterestAdder implements ActionListener { public InterestAdder(BankAccount acct) { account = acct; } public void actionPerformed(ActionEvent event) { double interest = account.getBalance() * RATE / 100; account.deposit(interest); System.out.println("Balance = " + account.getBalance()); } private static final double RATE = 5; private BankAccount account; }

R9.13 A strategy object is an object that carries out a particular strategy for a computation. An useful strategy for the DataSet class can be a Filter object that filters about certain information before being passed to the DataSet object. R9.14 The difference between an event and event listener is that an event listener waits for an event to happen before it is notified. R9.15 No, but the addActionListener can be called multiple times. R9.16 The f method can access the variables b, t, and x. R9.17 The compiler gives an error saying that the local variable is accessed from within inner class and needs to be declared final.

Potrebbero piacerti anche