Archive for April, 2007

1166 Java Utilities Package and Bit Manipulation Chapter (Frontpage web hosting)

Saturday, April 28th, 2007

1166 Java Utilities Package and Bit Manipulation Chapter 20 186 187 // button to display hash table elements 188 JButton listElementsButton = new JButton( “List objects” ); 189 190 listElementsButton.addActionListener( 191 192 new ActionListener() { 193 194 // display hash table elements 195 public void actionPerformed( ActionEvent event ) 196 { 197 StringBuffer buffer = new StringBuffer(); 198 199 for ( Enumeration enumeration = table.elements(); 200 enumeration.hasMoreElements(); ) 201 buffer.append( 202 enumeration.nextElement() ).append( ‘n’ ); 203 204 displayArea.setText( buffer.toString() ); 205 } 206 } 207 ); 208 209 southPanel.add( listElementsButton ); 210 211 // button to display hash table keys 212 JButton listKeysButton = new JButton( “List keys” ); 213 214 listKeysButton.addActionListener( 215 216 new ActionListener() { 217 218 // display hash table KEYS 219 public void actionPerformed( ActionEvent event ) 220 { 221 StringBuffer buffer = new StringBuffer(); 222 223 for ( Enumeration enumeration = table.keys(); 224 enumeration.hasMoreElements(); ) 225 buffer.append( 226 enumeration.nextElement() ).append( ‘n’ ); 227 228 JOptionPane.showMessageDialog( null, 229 buffer.toString(), “Display”, 230 JOptionPane.PLAIN_MESSAGE ); 231 } 232 } 233 ); 234 235 southPanel.add( listKeysButton ); 236 237 Container container = getContentPane(); 238 container.add( northPanel, BorderLayout.NORTH ); Fig. 20.3 Demonstrating class Hashtable(part 5 of 6).
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision mysql hosting services

Cheapest web hosting - Chapter 20 Java Utilities Package and Bit Manipulation

Friday, April 27th, 2007

Chapter 20 Java Utilities Package and Bit Manipulation 1165 133 134 // button to detetmine whether hash table is empty 135 JButton emptyButton = new JButton( “Empty” ); 136 137 emptyButton.addActionListener( 138 139 new ActionListener() { 140 141 // determine whether hash table is empty 142 public void actionPerformed( ActionEvent event ) 143 { 144 statusLabel.setText( “Empty: ” + table.isEmpty() ); 145 } 146 } 147 ); 148 149 southPanel.add( emptyButton ); 150 151 // button to determine whether hash table contains key 152 JButton containsKeyButton = new JButton( “Contains key” ); 153 154 containsKeyButton.addActionListener( 155 156 new ActionListener() { 157 158 // determine whether hash table contains key 159 public void actionPerformed( ActionEvent event ) 160 { 161 statusLabel.setText( “Contains key: ” + 162 table.containsKey( lastNameField.getText() ) ); 163 } 164 } 165 ); 166 167 southPanel.add( containsKeyButton ); 168 169 // button to clear all hash table contents 170 JButton clearButton = new JButton( “Clear table” ); 171 172 clearButton.addActionListener( 173 174 new ActionListener() { 175 176 // clear hash table contents 177 public void actionPerformed( ActionEvent event ) 178 { 179 table.clear(); 180 statusLabel.setText( “Clear: Table is now empty” ); 181 } 182 } 183 ); 184 185 southPanel.add( clearButton ); Fig. 20.3 Demonstrating class Hashtable(part 4 of 6).
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision professional web hosting services

1164 Java Utilities Package and Bit (Web server type) Manipulation Chapter

Friday, April 27th, 2007

1164 Java Utilities Package and Bit Manipulation Chapter 20 80 81 getButton.addActionListener( 82 83 new ActionListener() { 84 85 // get value for specific key 86 public void actionPerformed( ActionEvent event ) 87 { 88 Object value = table.get( lastNameField.getText() ); 89 90 // value found for key 91 if ( value != null ) 92 statusLabel.setText( 93 “Get: ” + value.toString() ); 94 95 // value not found for key 96 else 97 statusLabel.setText( 98 “Get: ” + lastNameField.getText() + 99 ” not in table” ); 100 } 101 } 102 ); 103 104 southPanel.add( getButton ); 105 106 // button to remove key/value pair from table 107 JButton removeButton = new JButton( “Remove” ); 108 109 removeButton.addActionListener( 110 111 new ActionListener() { 112 113 // remove key/value pair 114 public void actionPerformed( ActionEvent event ) 115 { 116 Object value = 117 table.remove( lastNameField.getText() ); 118 119 // key found 120 if ( value != null ) 121 statusLabel.setText( “Remove: ” + 122 value.toString() ); 123 124 // key not found 125 else 126 statusLabel.setText( “Remove: ” + 127 lastNameField.getText() + ” not in table” ); 128 } 129 } 130 ); 131 132 southPanel.add( removeButton ); Fig. 20.3 Demonstrating class Hashtable(part 3 of 6).
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision professional web hosting services

Chapter 20 Java Utilities Package and Bit Manipulation (Web design tools)

Friday, April 27th, 2007

Chapter 20 Java Utilities Package and Bit Manipulation 1163 27 displayArea.setEditable( false ); 28 29 JPanel northSubPanel = new JPanel(); 30 31 northSubPanel.add( new JLabel( “First name” ) ); 32 firstNameField = new JTextField( 8 ); 33 northSubPanel.add( firstNameField ); 34 35 northSubPanel.add( new JLabel( “Last name (key)” ) ); 36 lastNameField = new JTextField( 8 ); 37 northSubPanel.add( lastNameField ); 38 39 JPanel northPanel = new JPanel(); 40 northPanel.setLayout( new BorderLayout() ); 41 northPanel.add( northSubPanel, BorderLayout.NORTH ); 42 northPanel.add( statusLabel, BorderLayout.SOUTH ); 43 44 JPanel southPanel = new JPanel(); 45 southPanel.setLayout( new GridLayout( 2, 5 ) ); 46 JButton putButton = new JButton( “Put” ); 47 48 putButton.addActionListener( 49 50 new ActionListener() { 51 52 // add new key/value pair to hash table 53 public void actionPerformed( ActionEvent event ) 54 { 55 Employee employee = new Employee( 56 firstNameField.getText(), 57 lastNameField.getText() ); 58 59 Object value = 60 table.put( lastNameField.getText(), employee ); 61 62 // first time this key was added 63 if ( value == null ) 64 statusLabel.setText( 65 “Put: ” + employee.toString() ); 66 67 // replaced previous value for this key 68 else 69 statusLabel.setText( 70 “Put: ” + employee.toString() + 71 “; Replaced: ” + value.toString() ); 72 } 73 } 74 ); 75 76 southPanel.add( putButton ); 77 78 // button to get value for specific key 79 JButton getButton = new JButton( “Get” ); Fig. 20.3 Demonstrating class Hashtable(part 2 of 6).
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision personal web hosting services

1162 Java Utilities Package and Bit Manipulation Chapter (Web hosting servers)

Friday, April 27th, 2007

1162 Java Utilities Package and Bit Manipulation Chapter 20 Performance Tip 20.6 The load factor in a hash table is a classic example of a space time trade-off: By increasing the load factor, we get better memory utilization, but the program runs slower, due to increased hashing collisions. By decreasing the load factor, we get better program speed, because of reduced hashing collisions, but we get poorer memory utilization, because a larger portion of the hash table remains empty. The complexity of programming hash tables properly is too much for most casual programmers. Computer science students study hashing schemes thoroughly in courses called Data Structures or Algorithms. Recognizing the value of hashing to most programmers, Java provides class Hashtable and some related features to enable programmers to use hashing without having to implement the messy details. Actually, the preceding sentence is profoundly important in our study of object-oriented programming. As discussed in earlier chapters, classes encapsulate and hide complexity (i.e., implementation details) and offer user-friendly interfaces. Crafting classes to do this properly is one of the most valued skills in the field of object-oriented programming. Figure 20.3 provides a GUI that enables you to test several Hashtable methods. Line 25 creates an empty Hashtable with a default capacity of 101 elements and a default load factor of .75. When the number of occupied slots in the Hashtablebecomes more than the capacity times the load factor, the table grows larger. Class Hashtablealso provides a constructor that takes one argument specifying the capacity and a constructor that takes two arguments, specifying the capacity and load factor, respectively. 1 // Fig. 20.3: HashtableTest.java 2 // Demonstrates class Hashtable of the java.util package. 3 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; 7 import java.util.*; 8 9 // Java extensions packages 10 import javax.swing.*; 11 12 public class HashtableTest extends JFrame { 13 private JLabel statusLabel; 14 private Hashtable table; 15 private JTextArea displayArea; 16 private JTextField lastNameField; 17 private JTextField firstNameField; 18 19 // set up GUI to demonstrate Hashtable features 20 public HashtableTest() 21 { 22 super( “Hashtable Example” ); 23 24 statusLabel = new JLabel(); 25 table = new Hashtable(); 26 displayArea = new JTextArea( 4, 20 ); Fig. 20.3 Demonstrating class Hashtable(part 1 of 6).
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision shared web hosting services

Chapter 20 Java Utilities Package and (Free web servers) Bit Manipulation

Thursday, April 26th, 2007

Chapter 20 Java Utilities Package and Bit Manipulation 1161 20.5 Hashtable Class Object-oriented programming languages facilitate creating new data types. When a program creates objects of new or existing types, the program then needs to manage those objects efficiently. This includes storing and retrieving objects. Storing and retrieving information with arrays is efficient if some aspect of your data directly matches the key value and if those keys are unique and tightly packed. If you have 100 employees with 9digit Social Security numbers and you want to store and retrieve employee data by using the Social Security number as a key, it would nominally require an array with 999,999,999 elements, because there are 999,999,999 unique 9-digit numbers. This is impractical for virtually all applications that use Social Security numbers as keys. If the program could have an array that large, the program could get high performance for both storing and retrieving employee records by simply using the Social Security number as the array index. There are numerous applications that have this problem, namely, that either the keys are of the wrong type (i.e., not nonnegative integers), or they may be of the right type, but sparsely spread over a huge range. What is needed is a high-speed scheme for converting keys such as Social Security numbers, inventory part numbers and the like into unique array subscripts. Then, when an application needs to store something, the scheme could convert the application key rapidly into a subscript and the record of information could be stored at that slot in the array. Retrieval is accomplished the same way: Once the application has a key for which it wants to retrieve the data record, the application simply applies the conversion to the key this produces the array subscript where the data is stored and the data is retrieved. The scheme we describe here is the basis of a technique called hashing. Why the name? When we convert a key into an array subscript, we literally scramble the bits, forming a kind of mishmashed number. The number actually has no real significance beyond its usefulness in storing and retrieving this particular number data record. A glitch in the scheme occurs when collisions occur (i.e., when two different keys hash into the same cell (or element) in the array). We cannot store two different data records in the same space, so we need to find an alternative home for all records beyond the first that hash to a particular array subscript. There are many schemes for doing this. One is to hash again (i.e., to reapply the hashing transformation to the key to provide a next candidate cell in the array). The hashing process is designed to distribute the values throughout the table, so the assumption is that with just a few hashes an available cell will be found. Another scheme uses one hash to locate the first candidate cell. If that cell is occupied, successive cells are searched linearly until an available cell is found. Retrieval works the same way: The key is hashed once to determine the initial location to check to see whether it contains the desired data. If it does, the search is finished. If it does not, successive cells are searched linearly until the desired data is found. The most popular solution to hash table collisions is to have each cell of the table be a hash bucket, typically a linked list of all the key value pairs that hash to that cell. This is the solution that Java s Hashtable class (from package java.util) implements. One factor that affects the performance of hashing schemes is called the load factor. This is the ratio of the number of occupied cells in the hash table to the size of the hash table. The closer this ratio gets to 1.0, the greater the chance of collisions.
Note: In case you are looking for affordable webhost to host and run your web application check Vision http web server services

1160 Java Utilities Package and Bit Manipulation Chapter (Web host server)

Thursday, April 26th, 2007

1160 Java Utilities Package and Bit Manipulation Chapter 20 180 } // end class StackTest Fig. 20.2 Demonstrating class Stackof package java.util(part 5 of 5). Line 25 creates an empty Stack. Line 43 calls Stackmethod push to add its argument to the top of the stack. The method returns an Objectreference to its argument. Line 61 calls Stackmethod pop to remove the top element of the stack. The method returns an Object reference to the removed element. If there are no elements in the Stack, method pop throws an EmptyStackException. Line 85 calls Stack method peek to view the top element of the stack without removing the element. Method peekreturns an Objectreference to the element. Line 108 calls Stackmethod empty to determine whether the stack is empty. If it is empty, the method returns true; otherwise, the method returns false. Line 127 calls Stack method search to determine whether its argument is in the stack. If so, the method returns the position of the element in the stack. Note that the top element is position 1. If the element is not in the stack, 1is returned. The entire publicinterface of class Vectoris actually part of class Stack, because Stackinherits from Vector. To prove this, our example provides a button to display the contents of the stack. This button invokes method elementsto get an Enumerationof the stack; it then uses the Enumerationto walk through the stack elements. Testing and Debugging Tip 20.1 Stack extends Vector, so the user may perform operations on Stack objects that are ordinarily not allowed on conventional stack data structures. This could “corrupt” the elements of the Stack and destroy the integrity of the Stack. 20.4 DictionaryClass A Dictionary maps keys to values. When searching a Dictionaryfor a value, the program provides a key and the Dictionaryreturns the corresponding value. Dictionary is an abstractclass. In particular, it is the superclass of class Hashtable, which we discuss in Section 20.5. Class Dictionaryprovides the publicinterface methods required to maintain a table of key value pairs where the keys help store and retrieve the values in the table. Each key in the table is unique. The data structure is similar to a dictionary of words and definitions the word is the key that is used to look up the definition (i.e., the value). Dictionary method size returns the number of key value pairs in a Dictionary object. Method isEmpty returns true if a Dictionary is empty, and false otherwise. Method keys returns an Enumeration that a program can use to iterate through a Dictionary s keys. Method elements returns an Enumeration that a program can use to iterate through a Dictionary s values. Method get returns the object that corresponds to a given key value. Method put puts an object associated with a given key into the table. Method remove removes an element corresponding to a given key and returns a reference to it.
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision php5 hosting services

Chapter 20 Java Utilities Package and Bit Manipulation (Database web hosting)

Thursday, April 26th, 2007

Chapter 20 Java Utilities Package and Bit Manipulation 1159 127 int result = stack.search( searchKey ); 128 129 if ( result == -1 ) 130 statusLabel.setText( searchKey + ” not found” ); 131 else 132 statusLabel.setText( searchKey + 133 ” found at element ” + result ); 134 } 135 } 136 ); 137 138 container.add( searchButton ); 139 140 // button to display stack contents 141 JButton displayButton = new JButton( “Display” ); 142 143 displayButton.addActionListener( 144 145 new ActionListener() { 146 147 public void actionPerformed( ActionEvent event ) 148 { 149 // output Stack contents 150 Enumeration enumeration = stack.elements(); 151 StringBuffer buffer = new StringBuffer(); 152 153 while ( enumeration.hasMoreElements() ) 154 buffer.append( 155 enumeration.nextElement() ).append( ” ” ); 156 157 JOptionPane.showMessageDialog( null, 158 buffer.toString(), “Display”, 159 JOptionPane.PLAIN_MESSAGE ); 160 } 161 } 162 ); 163 164 container.add( displayButton ); 165 container.add( statusLabel ); 166 167 setSize( 675, 100 ); 168 setVisible( true ); 169 } 170 171 // execute application 172 public static void main( String args[] ) 173 { 174 StackTest application = new StackTest(); 175 176 application.setDefaultCloseOperation( 177 JFrame.EXIT_ON_CLOSE ); 178 } 179 Fig. 20.2 Demonstrating class Stackof package java.util(part 4 of 5).
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp web hosting services

1158 Java Utilities Package and Bit Manipulation Chapter (Web site translator)

Thursday, April 26th, 2007

1158 Java Utilities Package and Bit Manipulation Chapter 20 74 // button to look at top element of stack 75 JButton peekButton = new JButton( “Peek” ); 76 77 peekButton.addActionListener( 78 79 new ActionListener() { 80 81 public void actionPerformed( ActionEvent event ) 82 { 83 // look at top object on Stack 84 try { 85 statusLabel.setText( “Top: ” + stack.peek() ); 86 } 87 88 // process exception if Stack empty 89 catch ( EmptyStackException exception ) { 90 statusLabel.setText( exception.toString() ); 91 } 92 } 93 } 94 ); 95 96 container.add( peekButton ); 97 98 // button to determine whether stack is empty 99 JButton emptyButton = new JButton( “Is Empty?” ); 100 101 emptyButton.addActionListener( 102 103 new ActionListener() { 104 105 public void actionPerformed( ActionEvent event ) 106 { 107 // determine if Stack is empty 108 statusLabel.setText( stack.empty() ? 109 “Stack is empty” : “Stack is not empty” ); 110 } 111 } 112 ); 113 114 container.add( emptyButton ); 115 116 // button to determine whether search key is in stack 117 JButton searchButton = new JButton( “Search” ); 118 119 searchButton.addActionListener( 120 121 new ActionListener() { 122 123 public void actionPerformed( ActionEvent event ) 124 { 125 // search Stack for specified object 126 String searchKey = inputField.getText(); Fig. 20.2 Demonstrating class Stackof package java.util(part 3 of 5).
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision ftp web hosting services

Freelance web design - Chapter 20 Java Utilities Package and Bit Manipulation

Wednesday, April 25th, 2007

Chapter 20 Java Utilities Package and Bit Manipulation 1157 22 Container container = getContentPane(); 23 24 statusLabel = new JLabel(); 25 stack = new Stack(); 26 27 container.setLayout( new FlowLayout() ); 28 container.add( new JLabel( “Enter a string” ) ); 29 inputField = new JTextField( 10 ); 30 container.add( inputField ); 31 32 // button to place object on stack 33 JButton pushButton = new JButton( “Push” ); 34 35 pushButton.addActionListener( 36 37 new ActionListener() { 38 39 public void actionPerformed( ActionEvent event ) 40 { 41 // put object on Stack 42 statusLabel.setText( “Pushed: ” + 43 stack.push( inputField.getText() ) ); 44 } 45 } 46 ); 47 48 container.add( pushButton ); 49 50 // button to remove top object on stack 51 JButton popButton = new JButton( “Pop” ); 52 53 popButton.addActionListener( 54 55 new ActionListener() { 56 57 public void actionPerformed( ActionEvent event ) 58 { 59 // remove element from Stack 60 try { 61 statusLabel.setText( “Popped: ” + stack.pop() ); 62 } 63 64 // process exception if Stack empty 65 catch ( EmptyStackException exception ) { 66 statusLabel.setText( exception.toString() ); 67 } 68 } 69 } 70 ); 71 72 container.add( popButton ); 73 Fig. 20.2 Demonstrating class Stackof package java.util(part 2 of 5).
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision make web site services