Chapter 20 (Hosting web) Java Utilities Package and Bit Manipulation

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).
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.

Leave a Reply