Archive for May, 2007

Chapter 20 Java Utilities Package and Bit Manipulation (Web server logs)

Saturday, May 5th, 2007

Chapter 20 Java Utilities Package and Bit Manipulation 1195 erence to the top element of the stack without removing the element. Stack method emptydetermines whether the stack is empty. A Dictionary transforms keys to values. Hashing is a high-speed scheme for converting keys into unique array subscripts for storage and retrieval of information.The load factor is the ratio of the number of occupied cells in a hash table to the size of the hash table. The closer this ratio gets to 1.0, the greater the chance of collisions. The no-argument Hashtable constructor creates a Hashtable with a default capacity of 101 elements and a default load factor of .75. The Hashtable constructor that takes one argument specifies the initial capacity; the constructor that takes two arguments specifies the initial capacity and load factor, respectively. Hashtable method putadds a key and a value into a Hashtable. Method get locates the value associated with the specified key. Method remove deletes the value associated with the specified key. Method isEmpty determines whether the table is empty. Hashtable method containsKey determines whether the key specified as an argument is in the Hashtable (i.e., a value is associated with that key). Method contains determines whether the Object specified as its argument is in the Hashtable. Method clear empties the Hashtable. Method elements obtains an Enumeration of the values. Method keys obtains an Enumeration of the keys. A Propertiesobject is a persistent Hashtable object. Class Propertiesextends Hash- table. Keys and values in a Properties object must be Strings. The Properties no-argument constructor creates an empty Properties table with no default properties. There is also an overloaded constructor that is passed a reference to a default Properties object containing default property values. Properties method getProperty locates the value of the key specified as an argument. Method store saves the contents of the Properties object to the OutputStream object specified as the first argument. Method load restores the contents of the Properties object from the InputStream object specified as the argument. Method propertyNames obtains an Enumeration of the property names. Java provides extensive random-number generation capabilities in class Random. Class Random s no-argument constructor uses the time to seed its random-number generator differently each time it is called. To create a pseudorandom-number generator with repeatability, use the Random constructor that takes a seed argument. Randommethod setSeed sets the seed. Methods nextIntand nextLong generate uniformly distributed random integers. Methods nextFloatand nextDouble generate uniformly distributed values in the range 0.0 <= x < 1.0. The bitwise AND (&) operator sets each bit in the result to 1 if the corresponding bit in both operands is 1. The bitwise inclusive OR (|) operator sets each bit in the result to 1 if the corresponding bit in either (or both) operand(s) is 1. The bitwise exclusive OR (^) operator sets each bit in the result to 1 if the corresponding bit in exactly one operand is 1. The left shift (<<) operator shifts the bits of its left operand to the left by the number of bits specified in its right operand. The right shift operator with sign extension (>>) shifts the bits in its left operand to the right by the number of bits specified in its right operand if the left operand is negative, 1s are shifted in from the left; otherwise, 0s are shifted in from the left.
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision shared web hosting services

Web design portfolio - 1194 Java Utilities Package and Bit Manipulation Chapter

Saturday, May 5th, 2007

1194 Java Utilities Package and Bit Manipulation Chapter 20 of setting its bit to off with BitSet method clear. The number 3 is divisible by 1 and itself. Therefore, we can eliminate all multiples of 3 (keep in mind that all even numbers have already been eliminated). After the list of primes is displayed, the user can type a value from 2 to 1023 in the textfield and press enter to determine whether the number is prime. Method actionPerformed (lines 42 53) uses BitSet method get (line 46) to determine whether the bit for the number the user entered is set. If so, lines 47 48 display a message indicating that the number is prime. Otherwise, lines 51 52 display a message indicating that the number is not prime. SUMMARY Class Vector manages dynamically resizable arrays. At any time the Vector contains a certain number of elements which is less than or equal to its capacity. The capacity is the space that has been reserved for the array. If a Vector needs to grow, it grows by an increment that you specify or by a default assumed by the system. If you do not specify a capacity increment, the system automatically doubles the size of the Vector each time additional capacity is required. Vectors store references to Objects. To store values of primitive data types in Vectors, use the type-wrapper classes (Byte, Short, Integer, Long, Float, Double, Boolean and Character) to create objects containing the primitive data type values. Class Vector provides three constructors. The no-argument constructor creates an empty Vector. The constructor that takes one argument creates a Vector with an initial capacity specified by the argument. The constructor that takes two arguments creates a Vector with an initial capacity specified by the first argument and a capacity increment specified by the second argument. Vector method addElement adds its argument to the end of the Vector. Method insertElementAt inserts an element at the specified position. Method setElementAt sets the element at a specific position. Vector method removeElement removes the first occurrence of its argument. Method removeAllElements removes every element from the Vector. Method removeElementAt removes the element at the specified index. Vector method firstElement returns a reference to the first element. Method lastElement returns a reference to the last element. Vector method isEmptydetermines whether the Vector is empty. Vector method containsdetermines whether the Vector contains the searchKey specified as an argument. Vector method indexOf gets the index of the first location of its argument. The method returns 1 if the argument is not found in the Vector. Vector method trimToSize cuts the capacity of the Vector to the Vector s size. Methods sizeand capacity determine the number of elements currently in the Vector and the number of elements that can be stored in the Vector without allocating more memory, respectively. Vector method elements returns a reference to an Enumeration containing the elements of the Vector. Enumeration method hasMoreElements determines whether there are more elements. Method nextElement returns a reference to the next element. Class Stack extends class Vector. Stack method push adds its argument to the top of the stack. Method popremoves the top element of the stack. Method peek returns an Object ref
Note: If you are looking for reliable webhost to maintain and run your java application check Vision java hosting services

Chapter 20 (Christian web host) Java Utilities Package and Bit Manipulation

Saturday, May 5th, 2007

Chapter 20 Java Utilities Package and Bit Manipulation 1193 84 for ( int i = 2; i < size; i++ ) 85 86 if ( sieve.get( i ) ) { 87 primesArea.append( String.valueOf( i ) ); 88 primesArea.append( ++counter % 7 == 0 ? "n" : "t" ); 89 } 90 91 setSize( 600, 450 ); 92 setVisible( true ); 93 } 94 95 // execute application 96 public static void main( String args[] ) 97 { 98 BitSetTest application = new BitSetTest(); 99 100 application.setDefaultCloseOperation( 101 JFrame.EXIT_ON_CLOSE ); 102 } 103 104 } // end class BitSetTest Fig. 20.13 Fig. 20.13 Demonstrating the Sieve of Eratosthenes using a BitSet(part 3 of 3). Line 22 creates a BitSet of 1024 bits. We ignore the bit at index 0 in this program. Lines 68 69 set all the bits in the BitSetto on with BitSetmethod set. Lines 72 79 determine all the prime numbers from 2 to 1023. The integer finalBit specifies when the algorithm is complete. The basic algorithm is that a number is prime if it has no divisors other than 1 and itself. Starting with the number 2, once we know a number is prime, we can eliminate all multiples of that number. The number 2 is only divisible by 1 and itself, so it is prime. Therefore, we can eliminate 4, 6, 8 and so on. Elimination of a value consists
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision make web site services

1192 Java Utilities Package and Bit Manipulation Chapter (Web hosting reviews)

Friday, May 4th, 2007

1192 Java Utilities Package and Bit Manipulation Chapter 20 33 34 // textfield for user to input a value from 2 to 1023 35 inputField = new JTextField( 10 ); 36 37 inputField.addActionListener( 38 39 new ActionListener() { 40 41 // determine whether value is prime number 42 public void actionPerformed( ActionEvent event ) 43 { 44 int value = Integer.parseInt( inputField.getText() ); 45 46 if ( sieve.get( value ) ) 47 statusLabel.setText( 48 value + ” is a prime number” ); 49 50 else 51 statusLabel.setText( value + 52 ” is not a prime number” ); 53 } 54 } 55 ); 56 57 inputPanel.add( inputField ); 58 container.add( inputPanel, BorderLayout.NORTH ); 59 60 JTextArea primesArea = new JTextArea(); 61 62 container.add( new JScrollPane( primesArea ), 63 BorderLayout.CENTER ); 64 65 // set all bits from 1 to 1023 66 int size = sieve.size(); 67 68 for ( int i = 2; i < size; i++ ) 69 sieve.set( i ); 70 71 // perform Sieve of Eratosthenes 72 int finalBit = ( int ) Math.sqrt( sieve.size() ); 73 74 for ( int i = 2; i < finalBit; i++ ) 75 76 if ( sieve.get( i ) ) 77 78 for ( int j = 2 * i; j < size; j += i ) 79 sieve.clear( j ); 80 81 // display prime numbers from 1 to 1023 82 int counter = 0; 83 Fig. 20.13 Fig. 20.13 Demonstrating the Sieve of Eratosthenes using a BitSet(part 2 of 3).
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 (Linux web host) Utilities Package and Bit Manipulation

Friday, May 4th, 2007

Chapter 20 Java Utilities Package and Bit Manipulation 1191 The expression b.size() returns the size of the BitSet. The expression b.equals( b1 ) compares the two BitSets for equality. The expression b.toString() creates a Stringrepresentation of the BitSetcontents. This is helpful for debugging. Figure 20.13 revisits the Sieve of Eratosthenes for finding prime numbers, which we discussed in Exercise 7.27. This example uses a BitSetrather than an array to implement the algorithm. The program displays all the prime numbers from 2 to 1023 in a JTextAreaand provides a JTextFieldin which the user can type any number from 2 to 1023 to determine whether that number is prime (in which case a message is displayed in a JLabel). 1 // Fig. 20.13: BitSetTest.java 2 // Using a BitSet to demonstrate the Sieve of Eratosthenes. 3 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; 7 import java.util.*; 8 9 // Java extension packages 10 import javax.swing.*; 11 12 public class BitSetTest extends JFrame { 13 private BitSet sieve; 14 private JLabel statusLabel; 15 private JTextField inputField; 16 17 // set up GUI 18 public BitSetTest() 19 { 20 super( “BitSets” ); 21 22 sieve = new BitSet( 1024 ); 23 24 Container container = getContentPane(); 25 26 statusLabel = new JLabel( “” ); 27 container.add( statusLabel, BorderLayout.SOUTH ); 28 29 JPanel inputPanel = new JPanel(); 30 31 inputPanel.add( new JLabel( 32 “Enter a value from 2 to 1023″ ) ); Fig. 20.13 Fig. 20.13 Demonstrating the Sieve of Eratosthenes using a BitSet(part 1 of 3).
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision servlet hosting services

1190 Java Utilities Package and Bit Manipulation Chapter (Cedant web hosting)

Friday, May 4th, 2007

1190 Java Utilities Package and Bit Manipulation Chapter 20 Bitwise assignment operators &= Bitwise AND assignment operator. |= Bitwise inclusive OR assignment operator. ^= Bitwise exclusive OR assignment operator. <<= Left shift assignment operator. >>= Right shift with sign extension assignment operator. >>>= Right shift with zero extension assignment operator. Fig. 20.12The bitwise assignment operators. 20.12 20.9 BitSetClass Class BitSet makes it easy to create and manipulate bit sets. Bit sets are useful for representing a set of boolean flags. BitSets are dynamically resizable. More bits can be added as needed, and a BitSetobject will grow to accommodate the additional bits. The statement BitSet b = new BitSet(); creates a BitSetthat initially is empty. Also, a program can specify the size of a BitSet with the statement BitSet b = new BitSet( size ); which creates a BitSetwith sizebits. The statement b.set( bitNumber ); sets bit bitNumber on. This makes the underlying value of that bit 1. Note that bit numbers are zero based, like Vectors. The statement b.clear( bitNumber ); sets bit bitNumber off. This makes the underlying value of that bit 0. The statement b.get( bitNumber ); gets the value of bit bitNumber. The result is returned as true if the bit is on, false if the bit is off. The statement b.and( b1 ); performs a bit-by-bit logical AND between BitSets band b1. The result is stored in b. Bitwise logical OR and bitwise logical XOR are performed by the statements b.or( b1 ); b.xor( b2 );
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision j2ee hosting services

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

Friday, May 4th, 2007

Chapter 20 Java Utilities Package and Bit Manipulation 1189 Fig. 20.11 Fig. 20.11 Demonstrating the bitwise shift operators (part 5 of 5). The left shift operator (<<) shifts the bits of its left operand to the left by the number of bits specified in its right operand (performed at line 58 in the program). Bits vacated to the right are replaced with 0s; 1s shifted off the left are lost. The first four output windows of Fig. 20.11 demonstrate the left shift operator. Starting with the value 1, the left shift button was pressed twice, resulting in the values 2 and 4, respectively. The fourth output window shows the result of value1being shifted 31 times. Note that the result is a negative value. That is because a 1 in the high-order bit is used to indicate a negative value in an integer. The right shift operator with sign extension (>>) shifts the bits of its left operand to the right by the number of bits specified in its right operand (performed at line 78 in the program). Performing a right shift causes the vacated bits at the left to be replaced by 0s if the number is positive or 1s if the number is negative. Any 1s shifted off the right are lost. The fifth and sixth output windows show the results of right shifting (with sign extension) the value in the fourth output window two times. The right shift operator with zero extension (>>>) shifts the bits of its left operand to the right by the number of bits specified in its right operand (performed at line 98 in the program). Performing a right shift causes the vacated bits at the left to be replaced by 0s. Any 1s shifted off the right are lost. The eighth and ninth output windows show the results of right shifting (with zero extension) the value in the seventh output window two times. Each bitwise operator (except the bitwise complement operator) has a corresponding assignment operator. These bitwise assignment operators are shown in Fig. 20.12.
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision personal web hosting services

Http web server - 1188 Java Utilities Package and Bit Manipulation Chapter

Friday, May 4th, 2007

1188 Java Utilities Package and Bit Manipulation Chapter 20 132 // append space to buffer every 8 bits 133 if( bit % 8 == 0 ) 134 buffer.append( ‘ ‘ ); 135 } 136 137 return buffer.toString(); 138 } 139 140 // execute application 141 public static void main( String args[] ) 142 { 143 BitShift application = new BitShift(); 144 145 application.setDefaultCloseOperation( 146 JFrame.EXIT_ON_CLOSE ); 147 } 148 149 } // end class BitShift Fig. 20.11 Fig. 20.11 Demonstrating the bitwise shift operators (part 4 of 5).
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision virtual web hosting services

Chapter 20 Java Utilities Package and Bit Manipulation (Web hosting e commerce)

Thursday, May 3rd, 2007

Chapter 20 Java Utilities Package and Bit Manipulation 1187 80 bitsField.setText( getBits( value ) ); 81 } 82 } 83 ); 84 85 container.add( rightSignButton ); 86 87 // button to right shift value one position with zero extension 88 JButton rightZeroButton = new JButton( “>>>” ); 89 90 rightZeroButton.addActionListener( 91 92 new ActionListener() { 93 94 // right shift one position and display new value 95 public void actionPerformed( ActionEvent event ) 96 { 97 int value = Integer.parseInt( valueField.getText() ); 98 value >>>= 1; 99 valueField.setText( Integer.toString( value ) ); 100 101 bitsField.setText( getBits( value ) ); 102 } 103 } 104 ); 105 106 container.add( rightZeroButton ); 107 108 setSize( 400, 120 ); 109 setVisible( true ); 110 } 111 112 // display bit representation of specified int value 113 private String getBits( int value ) 114 { 115 // create int value with 1 in leftmost bit and 0s elsewhere 116 int displayMask = 1 << 31; 117 118 // buffer to build output 119 StringBuffer buffer = new StringBuffer( 35 ); 120 121 // for each bit append 0 or 1 to buffer 122 for ( int bit = 1; bit <= 32; bit++ ) { 123 124 // use displayMask to isolate bit and determine whether 125 // bit has valueof 0 or1 126 buffer.append( 127 ( value & displayMask ) == 0 ? '0' : '1' ); 128 129 // shift value one position to left 130 value <<= 1; 131 Fig. 20.11 Fig. 20.11 Demonstrating the bitwise shift operators (part 3 of 5).
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services

1186 Java Utilities Package and Bit (Web site development) Manipulation Chapter

Thursday, May 3rd, 2007

1186 Java Utilities Package and Bit Manipulation Chapter 20 27 container.add( valueField ); 28 29 valueField.addActionListener( 30 31 new ActionListener() { 32 33 // read value and display its bitwise representation 34 public void actionPerformed( ActionEvent event ) 35 { 36 int value = Integer.parseInt( valueField.getText() ); 37 bitsField.setText( getBits( value ) ); 38 } 39 } 40 ); 41 42 // textfield to display bitwise representation of an integer 43 bitsField = new JTextField( 33 ); 44 bitsField.setEditable( false ); 45 container.add( bitsField ); 46 47 // button to shift bits left by one position 48 JButton leftButton = new JButton( “<<" ); 49 50 leftButton.addActionListener( 51 52 new ActionListener() { 53 54 // left shift one position and display new value 55 public void actionPerformed( ActionEvent event ) 56 { 57 int value = Integer.parseInt( valueField.getText() ); 58 value <<= 1; 59 valueField.setText( Integer.toString( value ) ); 60 bitsField.setText( getBits( value ) ); 61 } 62 } 63 ); 64 65 container.add( leftButton ); 66 67 // button to right shift value one position with sign extension 68 JButton rightSignButton = new JButton( ">>” ); 69 70 rightSignButton.addActionListener( 71 72 new ActionListener() { 73 74 // right shift one position and display new value 75 public void actionPerformed( ActionEvent event ) 76 { 77 int value = Integer.parseInt( valueField.getText() ); 78 value >>= 1; 79 valueField.setText( Integer.toString( value ) ); Fig. 20.11 Fig. 20.11 Demonstrating the bitwise shift operators (part 2 of 5).
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision mysql5 web hosting services