Included below are short-answer and programming exercises. Answers are provided for those exercises whose exercise number is a hyperlink. Because college faculty use these exercises in their exams, we have provided answers to roughly half of the exercises included here.
23.5 Define each of the following terms in the context of hashing:
23.6 Explain briefly the operation of each of the following methods of class Vector:
23.7 Explain why inserting additional elements into a Vector object whose current size is less than its capacity is a relatively fast operation and why inserting additional elements into a Vector object whose current size is at capacity is a relatively slow operation.
23.8 In the text we state that the default capacity increment of doubling the size of a Vector may seem wasteful of storage, but it is actually an efficient way for Vectors to grow quickly to be "about the right size." Explain this statement. Explain the pros and cons of this doubling algorithm. What can a program do when it determines that the doubling is wasting space?
23.9 Explain the use of the Enumeration interface with objects of class Vector.
23.10 By extending class Vector, Javas designers were able to create class Stack quickly. What are the negative aspects of this use of inheritance, particularly for class Stack?
23.11 Explain briefly the operation of each of the following methods of class Hashtable:
23.12 Explain how to use the Random class to create pseudorandom numbers with the kind of repeatability we need for debugging.
23.13 Use a Hashtable to create a reusable class for choosing one of the 13 predefined colors in class Color. The color names should be used as keys and the predefined Color objects should be used as values. Place this class in a package that can be imported into any Java program. Use your new class in an application that allows the user to select a color and draw a shape in that color.
23.14 Modify your solution to Exercise 13.18the polymorphic painting programto store every shape the user draws in a Vector of MyShape objects. For the purpose of this exercise, create your own Vector subclass called ShapeVector that manipulates only MyShape objects. Provide the following capabilities in your program:
This method should be overridden for each subclass of MyShape to determine if the coordinates where the user pressed the mouse button are inside the shape.
23.15 What does it mean when we state that a Properties object is a "persistent" Hashtable object? Explain the operation of each of the following methods of the Properties class:
23.16 Why might you want to use objects of class BitSet? Explain the operation of each of the following methods of class BitSet:
23.17 Write a program that right shifts an integer variable 4 bits with sign extension and then right shifts the same integer variable 4 bits with zero extension. The program should print the integer in bits before and after each shift operation. Run your program once with a positive integer and run it again with a negative integer.
23.18 Show how shifting an integer left by 1 can be used to simulate multiplication by 2 and how shifting an integer right by 1 can be used to simulate division by 2. Be careful to consider issues related to the sign of an integer.
23.19 Write a program that reverses the order of the bits in an integer value. The program should input the value from the user and call method reverseBits to print the bits in reverse order. Print the value in bits both before and after the bits are reversed to confirm that the bits are reversed properly. You might want to implement both a recursive and an iterative solution.
23.20 Modify your solution to Exercise 22.10 to use class Stack.
23.21 Modify your solution to Exercise 22.12 to use class Stack.
23.22 Modify your solution to Exercise 22.13 to use class Stack.
Included below are answers to approximately half the of the exercises in the Cyber Classroom. We are not able to include answers to every exercise because college faculty use these exercises in their classroom exams.
23.5 Define each of the following terms in the context of hashing:
23.6 Explain briefly the operation of each of the following methods of class Vector:
23.11 Explain briefly the operation of each of the following methods of class Hashtable:
23.16 Why might you want to use objects of class BitSet?
ANS: To manipulate bits sets. Bit sets are useful for representing a set of boolean flags.
Explain the operation of each of the following methods of class BitSet:
23.17
// Exercise 23.17 Solution // BitShift.java // Using the bitwise shift operators import java.awt.*; import java.awt.event.*; import javax.swing.*; public class BitShift extends JFrame { private JTextField bits1, bits2, bits3; public BitShift() { super( "Shifting bits" ); Container c = getContentPane(); c.setLayout( new FlowLayout() ); bits1 = new JTextField( 33 ); bits2 = new JTextField( 33 ); bits3 = new JTextField( 33 ); c.add( new JLabel( "Integer to shift " ) ); final JTextField value = new JTextField( 12 ); value.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { int val = Integer.parseInt( value.getText() ); bits1.setText( getBits( val ) ); rightShift( val ); rightShiftSign( val ); } } ); c.add( value ); bits1.setEditable( false ); c.add( bits1 ); c.add( new JLabel( "Bits right shifted 4 positions with >>" ) ); c.add( bits2 ); c.add( new JLabel( "Bits right shifted 4 positions with >>>" ) ); c.add( bits3 ); setSize( 400, 200 ); show(); } private void rightShift( int number ) { number >>= 4; bits2.setText( getBits( number ) ); } public void rightShiftSign( int number ) { number >>>= 4; bits3.setText( getBits( number ) ); } private String getBits( int value ) { int displayMask = 1 << 31; StringBuffer buf = new StringBuffer( 35 ); for ( int c = 1; c <= 32; c++ ) { buf.append( ( value & displayMask ) == 0 ? '0' : '1' ); value <<= 1; if ( c % 8 == 0 ) buf.append( ' ' ); } return buf.toString(); } public static void main( String args[] ) { BitShift app = new BitShift(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); } } /************************************************************************** * (C) Copyright 1999 by Deitel & Associates, Inc. and Prentice Hall. * * All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * *************************************************************************/
23.18
// Exercise 23.18 Solution import java.awt.*; import java.awt.event.*; import javax.swing.*; public class BitShift extends JFrame { public BitShift() { super( "Shifting bits" ); Container c = getContentPane(); c.setLayout( new FlowLayout() ); final JTextField bits = new JTextField( 33 ); c.add( new JLabel( "Integer to shift " ) ); final JTextField value = new JTextField( 12 ); value.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { int val = Integer.parseInt( value.getText() ); bits.setText( getBits( val ) ); } } ); c.add( value ); bits.setEditable( false ); c.add( bits ); JButton left = new JButton( "multiply by 2" ); left.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { int val = Integer.parseInt( value.getText() ); val <<= 1; value.setText( Integer.toString( val ) ); bits.setText( getBits( val ) ); } } ); c.add( left ); JButton rightSign = new JButton( "divide by 2" ); rightSign.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { int val = Integer.parseInt( value.getText() ); val >>= 1; value.setText( Integer.toString( val ) ); bits.setText( getBits( val ) ); } } ); c.add( rightSign ); setSize( 400, 120 ); show(); } private String getBits( int value ) { int displayMask = 1 << 31; StringBuffer buf = new StringBuffer( 35 ); for ( int c = 1; c <= 32; c++ ) { buf.append( ( value & displayMask ) == 0 ? '0' : '1' ); value <<= 1; if ( c % 8 == 0 ) buf.append( ' ' ); } return buf.toString(); } public static void main( String args[] ) { BitShift app = new BitShift(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); } } /************************************************************************** * (C) Copyright 1999 by Deitel & Associates, Inc. and Prentice Hall. * * All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * *************************************************************************/
23.19
// Exercise 23.19 Solution import java.awt.*; import java.awt.event.*; import javax.swing.*; public class BitShift extends JFrame { public BitShift() { super( "Shifting bits" ); Container c = getContentPane(); c.setLayout( new FlowLayout() ); final JTextField bits = new JTextField( 33 ); final JTextField bits2 = new JTextField( 33 ); c.add( new JLabel( "Integer to shift " ) ); final JTextField value = new JTextField( 12 ); value.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { int val = Integer.parseInt( value.getText() ); bits.setText( getBits( val ) ); bits2.setText( getBits( reverseBits( val ) ) ); } } ); c.add( value ); bits.setEditable( false ); c.add( bits ); c.add( new JLabel( "Reversed bits:" ) ); c.add( bits2 ); setSize( 400, 150 ); show(); } public int reverseBits( int value ) { int mask = 1, temp = 0; for ( int x = 0; x <= 31; x++ ) { temp <<= 1; temp |= ( value & mask ); value >>= 1; } return temp; } private String getBits( int value ) { int displayMask = 1 << 31; StringBuffer buf = new StringBuffer( 35 ); for ( int c = 1; c <= 32; c++ ) { buf.append( ( value & displayMask ) == 0 ? '0' : '1' ); value <<= 1; if ( c % 8 == 0 ) buf.append( ' ' ); } return buf.toString(); } public static void main( String args[] ) { BitShift app = new BitShift(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); } } /************************************************************************** * (C) Copyright 1999 by Deitel & Associates, Inc. and Prentice Hall. * * All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * *************************************************************************/