[Page 238] Solution 5.1 | true false true false false false | Solution 5.2 | 0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111 | Solution 5.3 | In 6 bits, you can represent 26 = 64 different values. | Solution 5.4 | If you have to represent up to 12 significant digits, you should use double, which goes up to 17 digits. | Solution 5.5 | 4 4 0 1 2 6 0 4 | Solution 5.6 | 4.0 4.5 0 0.0 1.33 | Solution 5.7 | 7 int 30 long 14.0 double 90 long 4.0 double | Solution 5.8 | 34.0 54 4 1 6 0 2 7.5 | Solution 5.9 | k==5, j==5 k==5, j==6 k==6, j==6 k==5, j==4 k==4, j==4 | Solution 5.10 | k==15, j==5 k==5, j==6 k==120, j==6 k==0, j==4 k==0, j==5 | Solution 5.11 | k = k + 1; k += 1; k++; ++k; | Solution 5.12 | m = 5 m = 6 m = 15 m = 50 m = 70 | Solution 5.13 | false false true illegal true illegal false | Solution 5.14 | public class TemperatureUI { private KeyboardReader reader; // Handles command line I/O public TemperatureUI() { reader = new KeyboardReader(); // Create reader object } // Input-process-output algorithm to convert temperatures. public void run() { reader.prompt("Converts Fahrenheit and Celsius.\n"); reader.prompt("Input a temperature in Fahrenheit > "); double tempIn = reader.getKeyboardDouble(); double tempResult = Temperature.fahrToCels(tempIn); reader.display(tempIn + " F = " + tempResult + " C\n"); reader.prompt("Input a temperature in Celsius > "); tempIn = reader.getKeyboardDouble(); tempResult = Temperature.celsToFahr(tempIn); reader.display(tempIn + " C = " + tempResult + " F\n "); } // run() public static void main(String args[]) { TemperatureUI ui = new TemperatureUI(); // Create and ui.run(); // run the user interface. } // main() } // TemperatureUI class
| | [Page 239] | Solution 5.15 | import javax.swing.*; import java.awt.*; import java.awt.event.*; // Use this panel with a JApplet top-level window (as per Chapter 4) public class TemperatureJPanel extends JPanel implements ActionListener { private JTextField inField = new JTextField(15); // GUI components private JTextField resultField = new JTextField(15); private JLabel prompt1 = new JLabel("Input Temperature >>"); private JLabel prompt2 = new JLabel("Conversion Result:"); private JButton celsToFahr = new JButton("C to F"); private JButton fahrToCels = new JButton("F to C"); private JPanel panelN = new JPanel(); // Panels private JPanel panelC = new JPanel(); private JPanel panelS = new JPanel(); private Temperature temperature = new Temperature();// Temperature object public TemperatureJPanel() // Set up user interface { setLayout(new BorderLayout()); // Use BorderLayout panelN.setLayout(new BorderLayout()); panelC.setLayout(new BorderLayout()); panelS.setLayout(new BorderLayout()); panelN.add("North", prompt1); // Input elements panelN.add("South", inField); panelC.add("West", celsToFahr); // Control buttons panelC.add("East", fahrToCels); panelS.add("North", prompt2); // Output elements panelS.add("South", resultField); add("North", panelN); // Input at the top add("Center", panelC); // Buttons in the center add("South", panelS); // Result at the bottom celsToFahr.addActionListener(this); // Register with listeners fahrToCels.addActionListener(this); setSize(175,200); } // TemperatureJPanel() public void actionPerformed(ActionEvent e) { String inputStr = inField.getText(); // User's input double userInput = double.parseDouble(inputStr); // Convert to double double result = 0; if (e.getSource() == celsToFahr) { // Process and report result = temperature.celsToFahr(userInput); resultField.setText(inputStr + " C = " + result + " F"); } else { result = temperature.fahrToCels(userInput); resultField.setText(inputStr + " F = " + result + " C"); } } // actionPerformed() } // TemperatureJPanel class
| | [Page 240] | Solution 5.16 | public class KBTestOneRowNim { public static void main(String argv[]) { KeyboardReader kb = new KeyboardReader(); OneRowNim game = new OneRowNim(11); while(game.gameOver() == false) { game.report(); // Prompt the user System.out.print("Input 1, 2, or 3: "); int sticks = kb.getKeyboardInteger(); // Get move game.takeSticks(sticks); // Do move System.out.println(); } // while game.report(); // The game is now over System.out.print("Game won by player "); System.out.println(game.getWinner()); } // main() } // KBTestOneRowNim class
| Solution 5.17 | The new version of OneRowNim should run properly with the user interface from Chapter 4. This shows that our new definition for OneRowNim is backward compatible with the old user interface. This is a good thing. | Solution 5.18 | public class NimPlayer { private OneRowNim nim; public NimPlayer (OneRowNim game) { nim = game; } // NimPlayer() public int move() { int sticksLeft = nim.getSticks(); if (sticksLeft % (nim.MAX_PICKUP + 1) != 1) return (sticksLeft - 1) % (nim.MAX_PICKUP +1); else { int maxPickup = Math.min(nim.MAX_PICKUP, sticksLeft); return 1 + (int)(Math.random() * maxPickup); } // else } // move() } // NimPlayer class
| | [Page 241] | Solution 5.19 | public class KBComputerNim { public static void main(String argv[]) { KeyboardReader kb = new KeyboardReader(); OneRowNim game = new OneRowNim(OneRowNim.MAX_STICKS); NimPlayer computer = new NimPlayer(game); System.out.println("Let's play One Row Nim"); while(game.gameOver() == false) { if (game.getPlayer() == game.PLAYER_ONE) { kb.prompt("Sticks left = " + game.getSticks() + "Your move.");// Prompt kb.prompt("You can pick up between 1 and " + Math.min(game.MAX_PICKUP,game.getSticks()) +" :"); int sticks = kb.getKeyboardInteger(); // Get move game.takeSticks(sticks); // Do move } else { kb.prompt("Sticks left = " + game.getSticks() + " My move. "); int sticks = computer.move(); game.takeSticks(sticks); System.out.println("I take " + sticks); } // else } // while // The game is now over kb.display("Sticks left = " + game.getSticks()); if (game.getWinner() == game.PLAYER_ONE) System.out.println(" You win. Nice game!"); else System.out.println(" I win. Nice game!"); } // main() } // KBComputerNim class | Solution 5.20 | public class BankCD { private double principal; // The CD's initial principal private double rate; // CD's interest rate private double years; // Number of years to maturity public BankCD(double p, double r, double y) { principal = p; rate = r; years = y; } // BandCD() public double calcYearly() { return principal * Math.pow(1 + rate, years); } // calcYearly() public double calcDaily() { return principal * Math.pow(1 + rate/365, years*365); } // calcDaily() } // BankCD class
| | [Page 242] | Solution 5.21 | import java.text.NumberFormat; // For formatting $nn.dd or n% public class TestBankCD { private KeyboardReader reader = new KeyboardReader(); private NumberFormat dollars = NumberFormat.getCurrencyInstance(); private NumberFormat percent = NumberFormat.getPercentInstance(); private BankCD cd; public void run() { reader.display("Compares daily and annual compounding for a CD.\n"); reader.prompt("Input the CD's initial principal, e.g. 1000.55 > "); double principal = reader.getKeyboardDouble(); reader.prompt("Input the CD's interest rate, e.g. 6.5 > "); double rate = reader.getKeyboardDouble() / 100.0; reader.prompt("Input the number of years to maturity, e.g., 10.5 > "); double years = reader.getKeyboardDouble(); cd = new BankCD(principal, rate, years); percent.setMaximumFractionDigits(2); System.out.println(" For Principal = " + dollars.format(principal) + " Rate = " + percent.format(rate) + " Years = " + years); double cdAnnual = cd.calcYearly(); // Compounded yearly double cdDaily = cd.calcDaily(); // Compounded annually System.out.println(" The maturity value compounded yearly is " + dollars.format(cdAnnual)); System.out.println(" The maturity value compounded daily is: " + dollars.format(cdDaily)); } // run() public static void main( String args[] ) { TestBankCD cd = new TestBankCD(); cd.run(); } // main() } // TestBankCD class
| Solution 5.22 | valid valid ch2 = (char)n; valid ch1 = (char)(m-n); | |