Archive for April, 2007

1176 Java Utilities (Web hosting companies) Package and Bit Manipulation Chapter

Monday, April 30th, 2007

1176 Java Utilities Package and Bit Manipulation Chapter 20 Operator Name Description & bitwise AND The bits in the result are set to 1if the corresponding bits in the two operands are both 1. | bitwise inclusive OR The bits in the result are set to 1if at least one of the corresponding bits in the two operands is 1. ^ bitwise exclusive OR The bits in the result are set to 1if exactly one of the corresponding bits in the two operands is 1. << left shift Shifts the bits of the first operand left by the number of bits specified by the second operand; fill from the right with 0 bits. >> right shift with sign extension Shifts the bits of the first operand right by the number of bits specified by the second operand. If the first operand is negative, 1s are shifted in from the left; otherwise, 0s are shifted in from the left. >>> right shift with zero extension Shifts the bits of the first operand right by the number of bits specified by the second operand; 0s are shifted in from the left. ~ one s complement All 0bits are set to 1and all 1bits are set to 0. Fig. 20.5The bitwise operators . g. 20.5 When using the bitwise operators, it is useful to display values in their binary representation to illustrate the effects of these operators. The application of Fig. 20.6 allows the user to enter an integer into a JTextField and press Enter. Method actionPerformed(lines 32 36) reads the Stringfrom the JTextField, converts it to an integer and invokes method getBits (lines 55 80) to obtain a String representation of the integer in bits. The result is displayed in the output JTextField. The integer is displayed in its binary representation in groups of eight bits each. Method getBitsuses the bitwise AND operator to combine variable value with variable displayMask. Often, the bit- wise AND operator is used with an operand called a mask an integer value with specific bits set to 1. Masks are used to hide some bits in a value while selecting other bits. In get- Bits, mask variable displayMask is assigned the value 1<<31or 10000000 00000000 00000000 00000000 The left shift operator shifts the value 1from the low-order (rightmost) bit to the high-order (leftmost) bit in displayMaskand fills in 0bits from the right. 1 // Fig. 20.6: PrintBits.java 2 // Printing an unsigned integer in bits 3 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; Fig. 20.6Printing the bits in an integer (part 1 of 3). Fig. 20.6
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision make web site services

Chapter 20 Java Utilities Package and Bit Manipulation (Web domain)

Monday, April 30th, 2007

Chapter 20 Java Utilities Package and Bit Manipulation 1175 0 through 5. Then simply shift this value by adding 1 to produce a number in the range from 1 through 6. The expression is as follows: Math.abs( r.nextInt() ) % 6 + 1 The calls r.nextFloat() r.nextDouble() generate uniformly distributed values in the range 0.0 <= x < 1.0. The call r.nextGaussian() generates a double value with a probability density of a Gaussian (i.e., normal ) distribution (mean of 0.0 and standard deviation of 1.0). 20.8 Bit Manipulation and the Bitwise Operators Java provides extensive bit-manipulation capabilities for programmers who need to get down to the so-called bits-and-bytes level. Operating systems, test equipment software, networking software and many other kinds of software require that the programmer communicate directly with the hardware. In this section and the next, we discuss Java s bit- manipulation capabilities. We introduce Java s bitwise operators, and we demonstrate their use in live-code examples. Computers represent all data internally as sequences of bits. Each bit can assume the value 0 or the value 1. On most systems, a sequence of 8 bits forms a byte the standard storage unit for a variable of type byte. Other data types are stored in larger numbers of bytes. The bitwise operators can manipulate the bits of integral operands (i.e., those having type byte, char, short, int and long). Note that the bitwise operator discussions in this section show the binary representations of the integer operands. For a detailed explanation of the binary (also called base 2) number system, see Appendix E, Number Systems. The bitwise operators are bitwise AND (&), bitwise inclusive OR (|), bitwise exclusive OR (^), left shift (<<), right shift with sign extension (>>), right shift with zero extension (>>>) and complement (~). The bitwise AND, bitwise inclusive OR and bitwise exclusive OR operators compare their two operands bit by bit. 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. The right shift operator with zero extension shifts the bits in its left operand to the right by the number of bits specified in its right operand 0s are shifted in from the left. The bitwise complement operator sets all 0 bits in its operand to 1 in the result and sets all 1 bits to 0 in the result. Detailed discussions of each bitwise operator appear in the following examples. The bitwise operators are summarized in Fig. 20.5.
Note: If you are looking for reliable webhost to maintain and run your java application check Vision java hosting services

How to cite a web site - 1174 Java Utilities Package and Bit Manipulation Chapter

Monday, April 30th, 2007

1174 Java Utilities Package and Bit Manipulation Chapter 20 Testing and Debugging Tip 20.2 Use Properties method list to display the contents of a Properties object for debugging purposes. Line 181 calls Properties method load to restore the contents of the Properties object from the InputStream specified as the first argument (in this case, a FileInputStream). Line 208 calls Properties method propertyNames to obtain an Enumeration of the property names. The value of each property can be determined by using method getProperty. 20.7 Random Class We discussed random-number generation in Chapter 6, Methods, where we used Math class method random. Java provides extensive additional random number generation capabilities in class Random. We briefly walk through the API calls here. A new random-number generator can be created by using Random r = new Random(); This form uses the computer s current time to seed its random-number generator differently during each constructor call and thus generates different sequences of random numbers for each Random object. To create a pseudorandom-number generator with repeatability, use Random r = new Random( seedValue ); The seedValue argument (type long) is used in the random number calculation to seed the random number generator. If the same seedValue is used every time, the Random object produces the same sequence of random numbers. Testing and Debugging Tip 20.3 While a program is under development, use the form Random(seedValue) that produces a repeatable sequence of random numbers. If a bug occurs, fix the bug and test with the same seedValue; this allows you to reconstruct the exact same sequence of random numbers that caused the bug. Once the bugs have been removed, use the form Random(), which generates a new sequence of random numbers each time the program is run. The call r.setSeed( seedValue ); resets r s seed value at any time. The calls r.nextInt() r.nextLong() generate uniformly distributed random integers. You can use Math.abs to take the absolute value of the number produced by nextInt, thus giving a number in the range from zero through approximately 2 billion. Then use the % operator to scale the number. For example, to roll a six-sided die, if you scale with a 6, you will get a number in the range from
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision web and email hosting services

Chapter 20 (Apache web server for windows) Java Utilities Package and Bit Manipulation

Sunday, April 29th, 2007

Chapter 20 Java Utilities Package and Bit Manipulation 1173 219 } // end method ListProperties 220 221 // display String in statusLabel label 222 public void showstatus( String s ) 223 { 224 statusLabel.setText( s ); 225 } 226 227 // execute application 228 public static void main( String args[] ) 229 { 230 PropertiesTest application = new PropertiesTest(); 231 232 application.setDefaultCloseOperation( 233 JFrame.EXIT_ON_CLOSE ); 234 } 235 236 } // end class PropertiesTest Fig. 20.4 Demonstrating class Properties(part 6 of 6). Line 25 uses the no-argument constructor to create an empty Propertiestable with no default properties. Class Properties also provides an overloaded constructor that receives a reference to a Propertiesobject containing default property values. Lines 68 69 call Propertiesmethod setPropertyto store a value for the specified key. If the key does not exist in the table, setProperty returns null; otherwise, it returns the previous value for that key. Lines 116 117 call Properties method getProperty to locate the value associated with the specified key. If the key is not found in this Properties object, get- Propertyuses the one in the default Propertiesobject (if there is one). The process continues recursively until there are no more default Propertiesobjects (remember that every Properties object can be initialized with a default Properties object), at which point getPropertyreturns null. An overloaded version of this method receives two arguments, the second of which is the default value to return if getPropertycannot locate the key. Line 150 calls Properties method store to save the contents of the Propertiesobject to the OutputStreamobject specified as the first argument (in this case, a FileOutputStream). The String argument is a description of the Properties object. Class Properties also provides method list, which takes a PrintStream argument. This method is useful for displaying the set of properties.
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision shared web hosting services

1172 Java Utilities Package and (Adelphia web hosting) Bit Manipulation Chapter

Sunday, April 29th, 2007

1172 Java Utilities Package and Bit Manipulation Chapter 20 166 // button to load contents of Properties table from file 167 JButton loadButton = new JButton( “Load” ); 168 169 loadButton.addActionListener( 170 171 new ActionListener() { 172 173 // use method load to read contents from file 174 public void actionPerformed( ActionEvent event ) 175 { 176 // load contents of table 177 try { 178 FileInputStream input = 179 new FileInputStream( “props.dat” ); 180 181 table.load( input ); 182 input.close(); 183 listProperties(); 184 } 185 186 // process problems with file input 187 catch( IOException ioException ) { 188 ioException.printStackTrace(); 189 } 190 } 191 } 192 ); // end call to addActionListener 193 194 southPanel.add( loadButton ); 195 196 container.add( southPanel, BorderLayout.SOUTH ); 197 198 setSize( 550, 225 ); 199 setVisible( true ); 200 } 201 202 // output property values 203 public void listProperties() 204 { 205 StringBuffer buffer = new StringBuffer(); 206 String name, value; 207 208 Enumeration enumeration = table.propertyNames(); 209 210 while ( enumeration.hasMoreElements() ) { 211 name = enumeration.nextElement().toString(); 212 value = table.getProperty( name ); 213 214 buffer.append( name ).append( ‘t’ ); 215 buffer.append( value ).append( ‘n’ ); 216 } 217 218 displayArea.setText( buffer.toString() ); Fig. 20.4 Demonstrating class Properties(part 5 of 6).
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision servlet hosting services

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

Sunday, April 29th, 2007

Chapter 20 Java Utilities Package and Bit Manipulation 1171 113 // use method getProperty to obtain a property value 114 public void actionPerformed( ActionEvent event ) 115 { 116 Object value = table.getProperty( 117 nameField.getText() ); 118 119 if ( value != null ) 120 showstatus( “Get property: ” + 121 nameField.getText() + ” ” + 122 value.toString() ); 123 124 else 125 showstatus( “Get: ” + nameField.getText() + 126 ” not in table” ); 127 128 listProperties(); 129 } 130 } 131 ); // end call to addActionListener 132 133 southPanel.add( getPropertyButton ); 134 135 // button to contents of Properties table to file 136 JButton saveButton = new JButton( “Save” ); 137 138 saveButton.addActionListener( 139 140 new ActionListener() { 141 142 // use method save to place contents in file 143 public void actionPerformed( ActionEvent event ) 144 { 145 // save contents of table 146 try { 147 FileOutputStream output = 148 new FileOutputStream( “props.dat” ); 149 150 table.store( output, “Sample Properties” ); 151 output.close(); 152 153 listProperties(); 154 } 155 156 // process problems with file output 157 catch( IOException ioException ) { 158 ioException.printStackTrace(); 159 } 160 } 161 } 162 ); // end call to addActionListener 163 164 southPanel.add( saveButton ); 165 Fig. 20.4 Demonstrating class Properties(part 4 of 6).
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision best web hosting services

1170 Java Utilities Package and (Abyss web server) Bit Manipulation Chapter

Sunday, April 29th, 2007

1170 Java Utilities Package and Bit Manipulation Chapter 20 61 putButton.addActionListener( 62 63 new ActionListener() { 64 65 // put name/value pair in Properties table 66 public void actionPerformed( ActionEvent event ) 67 { 68 Object value = table.setProperty( 69 nameField.getText(), valueField.getText() ); 70 71 if ( value == null ) 72 showstatus( “Put: ” + nameField.getText() + 73 ” ” + valueField.getText() ); 74 75 else 76 showstatus( “Put: ” + nameField.getText() + 77 ” ” + valueField.getText() + 78 “; Replaced: ” + value.toString() ); 79 80 listProperties(); 81 } 82 } 83 ); // end call to addActionListener 84 85 southPanel.add( putButton ); 86 87 // button to empty contents of Properties table 88 JButton clearButton = new JButton( “Clear” ); 89 90 clearButton.addActionListener( 91 92 new ActionListener() { 93 94 // use method clear to empty table 95 public void actionPerformed( ActionEvent event ) 96 { 97 table.clear(); 98 showstatus( “Table in memory cleared” ); 99 listProperties(); 100 } 101 } 102 ); // end call to addActionListener 103 104 southPanel.add( clearButton ); 105 106 // button to get value of a property 107 JButton getPropertyButton = new JButton( “Get property” ); 108 109 getPropertyButton.addActionListener( 110 111 new ActionListener() { 112 Fig. 20.4 Demonstrating class Properties(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 (Business web hosting) Package and Bit Manipulation

Saturday, April 28th, 2007

Chapter 20 Java Utilities Package and Bit Manipulation 1169 9 // Java extension packages 11 import javax.swing.*; 12 13 public class PropertiesTest extends JFrame { 14 private JLabel statusLabel; 15 private Properties table; 16 private JTextArea displayArea; 17 private JTextField valueField, nameField; 18 19 // set up GUI to test Properties table public PropertiesTest() 21 { 22 super( “Properties Test” ); 23 24 // create Properties table 25 table = new Properties(); 26 27 Container container = getContentPane(); 28 29 // set up NORTH of window’s BorderLayout JPanel northSubPanel = new JPanel(); 31 32 northSubPanel.add( new JLabel( “Property value” ) ); 33 valueField = new JTextField( 10 ); 34 northSubPanel.add( valueField ); 35 36 northSubPanel.add( new JLabel( “Property name (key)” ) ); 37 nameField = new JTextField( 10 ); 38 northSubPanel.add( nameField ); 39 JPanel northPanel = new JPanel(); 41 northPanel.setLayout( new BorderLayout() ); 42 northPanel.add( northSubPanel, BorderLayout.NORTH ); 43 44 statusLabel = new JLabel(); 45 northPanel.add( statusLabel, BorderLayout.SOUTH ); 46 47 container.add( northPanel, BorderLayout.NORTH ); 48 49 // set up CENTER of window’s BorderLayout displayArea = new JTextArea( 4, 35 ); 51 container.add( new JScrollPane( displayArea ), 52 BorderLayout.CENTER ); 53 54 // set up SOUTH of window’s BorderLayout 55 JPanel southPanel = new JPanel(); 56 southPanel.setLayout( new GridLayout( 1, 5 ) ); 57 58 // button to put a name/value pair in Properties table 59 JButton putButton = new JButton( “Put” ); Fig. 20.4 Demonstrating class Properties(part 2 of 6).
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision php5 hosting services

1168 Java Utilities (Submit web site) Package and Bit Manipulation Chapter

Saturday, April 28th, 2007

1168 Java Utilities Package and Bit Manipulation Chapter 20 Lines 59 60 call Hashtable method put to add a key (the first argument) and a value (the second argument) into the Hashtable. Method putreturns null if key has not been inserted in the Hashtablepreviously. Otherwise, method putreturns the original value for that key in the Hashtable; this helps the program manage cases in which it intends to replace the value stored for a given key. If either the key or the value is null, a NullPointerExceptionoccurs. Line 88 calls Hashtable method get to locate the value associated with the key specified as an argument. If the key is present in the table, getreturns an Objectreference to the corresponding value; otherwise, the method returns null. Lines 116 117 call Hashtable method remove to remove a key value pair from the table. The method returns a reference to the removed Object. If there is no value mapped to the specified key, the method returns null. Line 144 calls Hashtable method isEmpty, which returns true if the Hash- tableis empty; otherwise it returns false. Line 162 calls Hashtable method containsKey to determine whether the key specified as an argument is in the Hashtable(i.e., a value is associated with that key). If so, the method returns true; otherwise, the method returns false. Class Hashtable also provides method contains to determine whether the Objectspecified as its argument is in the Hashtable. Line 179 calls Hashtablemethod clear to empty the Hashtablecontents. Line 199 calls Hashtable method elements to obtain an Enumerationof the values in the Hashtable. Line 223 calls Hashtablemethod keys to obtain an Enumeration of the keys in the Hashtable. For more information on class Hashtableand its methods, see the online Java API documentation. 20.6 PropertiesClass A Properties object is a persistent Hashtableobject that normally stores key value pairs of Strings assuming that you use methods setProperty and getProperty to manipulate the table rather than Hashtablemethods putand get. By persistent, we mean that the Hashtableobject can be written to an output stream and directed to a file, and read back in through an input stream. In fact, most objects in Java can now be output and input with Java s object serialization (see Chapter 16). The Properties class extends class Hashtable, so Properties objects have the methods we discussed in Fig. 20.3. The keys and values in a Propertiesobject must be of type String. Class Propertiesprovides some additional methods that are demonstrated in Fig. 20.4. 1 // Fig. 20.4: PropertiesTest.java 2 // Demonstrates class Properties of the java.util package. 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; 7 import java.io.*; 8 import java.util.*; Fig. 20.4 Demonstrating class Properties(part 1 of 6).
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision personal web hosting services

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

Saturday, April 28th, 2007

Chapter 20 Java Utilities Package and Bit Manipulation 1167 239 container.add( new JScrollPane( displayArea ), 240 BorderLayout.CENTER ); 241 container.add( southPanel, BorderLayout.SOUTH ); 242 243 setSize( 540, 300 ); 244 setVisible( true ); 245 } 246 247 // execute application 248 public static void main( String args[] ) 249 { 250 HashtableTest application = new HashtableTest(); 251 252 application.setDefaultCloseOperation( 253 JFrame.EXIT_ON_CLOSE ); 254 } 255 256 } // end class HashtableTest 257 258 // Employee class to represent first and last name 259 class Employee { 260 private String first, last; 261 262 // initialize an Employee 263 public Employee( String firstName, String lastName ) 264 { 265 first = firstName; 266 last = lastName; 267 } 268 269 // convert Employee to String representation 270 public String toString() 27 { 27 return first + ” ” + last; 27 } 27 27 } // end class Employee Fig. 20.3 Demonstrating class Hashtable(part 6 of 6).
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision best web hosting services