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.
24.3 Define each of the following terms:
24.4 Briefly answer the following questions.
24.5 Explain briefly the operation of each of the following Iterator-related methods:
24.6 State whether each of the following is true or false. If false, explain why.
24.7 Rewrite method printList of Fig. 24.4 to use a ListIterator. 24.8 Rewrite lines 13 through 19 in Fig. 24.4 to be more concise by using the asList method and the LinkedList constructor that takes a Collection argument. 24.9 Write a program that reads in a series of first names and stores them in a LinkedList. Do not store duplicate names. Allow the user to search for a first name. 24.10 Modify the program of Fig. 24.13 to count the number of occurrences for all letters (e.g., 5 occurrences of "o" in the example). Display the results. 24.11 Write a program that determines and prints the number of duplicate words in a sentence. Treat uppercase and lowercase letters the same. Ignore punctuation. 24.12 Rewrite your solution to Exercise 22.8 to use a LinkedList collection. 24.13 Rewrite your solution to Exercise 22.9 to use a LinkedList collection. 24.14 Write a program that takes a whole number input from a user and determines if it is prime. If the number is prime, add it to a JTextArea. If the number is not prime, display the prime factors for the number in a JLabel. Remember that a prime numbers factors are only 1 and the prime number itself. Every number that is not prime has a unique prime factorization. For example, consider the number 54. The factors of 54 are 2, 3, 3 and 3. When the values are multiplied together, the result is 54. For the number 54, the prime factors output should be 2 and 3. Use Sets as part of your solution. 24.15 Rewrite your solution to Exercise 22.21 to use a LinkedList. 24.16 Write a program that tokenizes (using class StreamTokenizer) a line of text input by the user and places each token in a tree. Print the sorted tree elements. |
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.
24.3 Define each of the following terms:
24.5 Explain briefly the operation of each of the following Iterator-related methods:
24.6 State whether each of the following is true or false. If false, explain why.
// Exercise 24.7 Solutionimport java.util.*;public class ListTest { private String colors[] = { "black", "yellow", "green", "blue", "violet", "silver" }; private String colors2[] = { "gold", "white", "brown", "blue", "gray", "silver" }; public ListTest() { LinkedList link = new LinkedList(); LinkedList link2 = new LinkedList(); for ( int k = 0; k < colors.length; k++ ) { link.add( colors[ k ] ); link2.add( colors2[ k ] ); // same length as colors } link.addAll( link2 ); // concatenate lists link2 = null; // release resources printList( link ); uppercaseStrings( link ); printList( link ); System.out.print( "\nDeleting elements 4 to 6..." ); removeItems( link, 4, 7 ); printList( link ); } public void printList( List listRef ) { ListIterator it = listRef.listIterator(); System.out.println( "\nlist: " ); while ( it.hasNext() ) System.out.print( it.next() + " " ); System.out.println(); } public void uppercaseStrings( List listRef2 ) { ListIterator listIt = listRef2.listIterator(); while ( listIt.hasNext() ) { Object o = listIt.next(); // get item if ( o instanceof String ) // check for String listIt.set( ( ( String ) o ).toUpperCase() ); } } public void removeItems( List listRef3, int start, int end ) { listRef3.subList( start, end ).clear(); // remove items } public static void main( String args[] ) { new ListTest(); } }/************************************************************************** * (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. * *************************************************************************/
// Exercise 24.8 Solution// Using LinkListsimport java.util.*;public class ListTest { private String colors[] = { "black", "yellow", "green", "blue", "violet", "silver" }; private String colors2[] = { "gold", "white", "brown", "blue", "gray", "silver" }; public ListTest() { LinkedList link = new LinkedList( Arrays.asList( colors ) ); LinkedList link2 = new LinkedList( Arrays.asList( colors2 ) ); link.addAll( link2 ); // concatenate lists link2 = null; // release resources printList( link ); uppercaseStrings( link ); printList( link ); System.out.print( "\nDeleting elements 4 to 6..." ); removeItems( link, 4, 7 ); printList( link ); } public void printList( List listRef ) { System.out.println( "\nlist: " ); for ( int k = 0; k < listRef.size(); k++ ) System.out.print( listRef.get( k ) + " " ); System.out.println(); } public void uppercaseStrings( List listRef2 ) { ListIterator listIt = listRef2.listIterator(); while ( listIt.hasNext() ) { Object o = listIt.next(); // get item if ( o instanceof String ) // check for String listIt.set( ( ( String ) o ).toUpperCase() ); } } public void removeItems( List listRef3, int start, int end ) { listRef3.subList( start, end ).clear(); // remove items } public static void main( String args[] ) { new ListTest(); } }/************************************************************************** * (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. * *************************************************************************/
// Exercise 24.12 Solutionimport java.util.*;public class ListTest { public static void main( String args[] ) { LinkedList x = new LinkedList(); Integer y = null; // Create objects to store in the List for ( int k = 1; k <= 25; k++ ) { y = new Integer( ( int ) ( Math.random() * 101 ) ); x.add( y ); } Collections.sort( x ); System.out.println( x.toString() ); int count = 0; ListIterator it = x.listIterator(); while ( it.hasNext() ) count += ( ( Integer ) it.next() ).intValue(); System.out.println( "Sum is: " + count + "\nAverage is: " + ( ( double ) count / x.size() ) ); }}
// Exercise 24.16 Solutionimport javax.swing.*;import java.awt.*;import java.util.*;import java.awt.event.*;public class TreeTest extends JFrame { public TreeTest() { super( "Tokenizer" ); final JLabel prompt = new JLabel( "Enter String:" ); final JTextField input = new JTextField( 25 ); final JTextArea display = new JTextArea(); input.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { TreeSet tree = new TreeSet(); StringTokenizer s = new StringTokenizer( input.getText() ); while ( s.hasMoreTokens() ) tree.add( s.nextToken() ); display.setText( tree.toString() ); } } ); Container c = getContentPane(); JPanel p = new JPanel(); p.add( prompt, BorderLayout.NORTH ); p.add( input, BorderLayout.SOUTH ); c.add( p, BorderLayout.NORTH ); c.add( new JScrollPane( display ), BorderLayout.CENTER ); setSize( 400, 100 ); show(); } public static void main( String args[] ) { TreeTest app = new TreeTest(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); }}