Vector Java - Uom.gr

Transcription

CHAPTER 2312345678910111213JAVA UTILITIES PACKAGE AND BIT MANIPULATION1103// Fig. 23.1: VectorTest.java// Testing the Vector class of the java.util packageimport java.util.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class VectorTest extends JFrame {Fig. 23.1public VectorTest(){super( "Vector Example" );Demonstrating class Vector of package java.util (part 1 of 5).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company

62Fig. 23.1JAVA UTILITIES PACKAGE AND BIT MANIPULATIONCHAPTER 23final JLabel status new JLabel();Container c getContentPane();final Vector v new Vector( 1 );c.setLayout( new FlowLayout() );c.add( new JLabel( "Enter a string" ) );final JTextField input new JTextField( 10 );c.add( input );JButton addBtn new JButton( "Add" );addBtn.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){v.addElement( input.getText() );status.setText( "Added to end: " input.getText() );input.setText( "" );}});c.add( addBtn );// add the input valueJButton removeBtn new JButton( "Remove" );removeBtn.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){if ( v.removeElement( input.getText() ) )status.setText( "Removed: " input.getText() );elsestatus.setText( input.getText() " not in vector" );}});c.add( removeBtn );JButton firstBtn new JButton( "First" );firstBtn.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){try {status.setText( "First element: " v.firstElement() );}Demonstrating class Vector of package java.util (part 2 of 5).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company

CHAPTER 108109110111112113114115Fig. 23.1JAVA UTILITIES PACKAGE AND BIT MANIPULATION1105catch ( NoSuchElementException exception ) {status.setText( exception.toString() );}}});c.add( firstBtn );JButton lastBtn new JButton( "Last" );lastBtn.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){try {status.setText( "Last element: " v.lastElement() );}catch ( NoSuchElementException exception ) {status.setText( exception.toString() );}}});c.add( lastBtn );JButton emptyBtn new JButton( "Is Empty?" );emptyBtn.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){status.setText( v.isEmpty() ?"Vector is empty" : "Vector is not empty" );}});c.add( emptyBtn );JButton containsBtn new JButton( "Contains" );containsBtn.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){String searchKey input.getText();if ( v.contains( searchKey ) )status.setText( "Vector contains " searchKey );elsestatus.setText( "Vector does not contain " searchKey );}});Demonstrating class Vector of package java.util (part 3 of 5).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company

4165Fig. 23.1JAVA UTILITIES PACKAGE AND BIT MANIPULATIONCHAPTER 23c.add( containsBtn );JButton locationBtn new JButton( "Location" );locationBtn.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){status.setText( "Element is at location " v.indexOf( input.getText() ) );}});c.add( locationBtn );JButton trimBtn new JButton( "Trim" );trimBtn.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){v.trimToSize();status.setText( "Vector trimmed to size" );}});c.add( trimBtn );JButton statsBtn new JButton( "Statistics" );statsBtn.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){status.setText( "Size " v.size() "; capacity " v.capacity() );}});c.add( statsBtn );JButton displayBtn new JButton( "Display" );displayBtn.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){Enumeration enum v.elements();StringBuffer buf new StringBuffer();while ( enum.hasMoreElements() )buf.append(enum.nextElement() ).append( "" );Demonstrating class Vector of package java.util (part 4 of 5).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company

CHAPTER 182183184185186187188189190191192 }Fig. 23.1JAVA UTILITIES PACKAGE AND BIT MANIPULATION1107JOptionPane.showMessageDialog( null,buf.toString(), "Display",JOptionPane.PLAIN MESSAGE );}});c.add( displayBtn );c.add( status );setSize( 300, 200 );show();}public static void main( String args[] ){VectorTest app new VectorTest();app.addWindowListener(new WindowAdapter() {public void windowClosing( WindowEvent e ){System.exit( 0 );}});}Demonstrating class Vector of package java.util (part 5 of 5).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company

8293031323334353637383940414243JAVA UTILITIES PACKAGE AND BIT MANIPULATIONCHAPTER 23// Fig. 23.2: StackTest.java// Testing the Stack class of the java.util packageimport java.util.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class StackTest extends JFrame {Fig. 23.2public StackTest(){super( "Stacks" );Container c getContentPane();final JLabel status new JLabel();final Stack s new Stack();c.setLayout( new FlowLayout() );c.add( new JLabel( "Enter a string" ) );final JTextField input new JTextField( 10 );c.add( input );JButton pushBtn new JButton( "Push" );pushBtn.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){status.setText( "Pushed: " s.push( input.getText() ) );}});c.add( pushBtn );JButton popBtn new JButton( "Pop" );popBtn.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){try {status.setText( "Popped: " s.pop() );}Demonstrating class Stack of package java.util (part 1 of 3).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company

CHAPTER 93949596Fig. 23.2JAVA UTILITIES PACKAGE AND BIT MANIPULATION1111catch ( EmptyStackException exception ) {status.setText( exception.toString() );}}});c.add( popBtn );JButton peekBtn new JButton( "Peek" );peekBtn.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){try {status.setText( "Top: " s.peek() );}catch ( EmptyStackException exception ) {status.setText( exception.toString() );}}});c.add( peekBtn );JButton emptyBtn new JButton( "Is Empty?" );emptyBtn.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){status.setText( s.empty() ?"Stack is empty" : "Stack is not empty" );}});c.add( emptyBtn );JButton searchBtn new JButton( "Search" );searchBtn.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){String searchKey input.getText();int result s.search( searchKey );if ( result -1 )status.setText( searchKey " not found" );elsestatus.setText( searchKey " found at element " result );}});c.add( searchBtn );Demonstrating class Stack of package java.util (part 2 of 3).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company

130131132133134135136 }Fig. 23.2JAVA UTILITIES PACKAGE AND BIT MANIPULATIONCHAPTER 23JButton displayBtn new JButton( "Display" );displayBtn.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){Enumeration enum s.elements();StringBuffer buf new StringBuffer();while ( enum.hasMoreElements() )buf.append(enum.nextElement() ).append( " " );JOptionPane.showMessageDialog( null,buf.toString(), "Display",JOptionPane.PLAIN MESSAGE );}});c.add( displayBtn );c.add( status );setSize( 675, 100 );show();}public static void main( String args[] ){StackTest app new StackTest();app.addWindowListener(new WindowAdapter() {public void windowClosing( WindowEvent e ){System.exit( 0 );}});}Demonstrating class Stack of package java.util (part 3 of 3).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company

CHAPTER 23123456789101112131415161718192021JAVA UTILITIES PACKAGE AND BIT MANIPULATION// Fig. 23.3: HashtableTest.java// Demonstrates class Hashtable of the java.util package.import java.util.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class HashtableTest extends JFrame {Fig. 23.3public HashtableTest(){super( "Hashtable Example" );final JLabel status new JLabel();final Hashtable table new Hashtable();final JTextArea display new JTextArea( 4, 20 );display.setEditable( false );JPanel northPanel new JPanel();northPanel.setLayout( new BorderLayout() );JPanel northSubPanel new JPanel();Demonstrating class Hashtable (part 1 of 5).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company1115

70Fig. 23.3JAVA UTILITIES PACKAGE AND BIT MANIPULATIONCHAPTER 23northSubPanel.add( new JLabel( "First name" ) );final JTextField fName new JTextField( 8 );northSubPanel.add( fName );northSubPanel.add( new JLabel( "Last name (key)" ) );final JTextField lName new JTextField( 8 );northSubPanel.add( lName );northPanel.add( northSubPanel, BorderLayout.NORTH );northPanel.add( status, BorderLayout.SOUTH );JPanel southPanel new JPanel();southPanel.setLayout( new GridLayout( 2, 5 ) );JButton put new JButton( "Put" );put.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){Employee emp new Employee(fName.getText(), lName.getText() );Object val table.put( lName.getText(), emp );if ( val null )status.setText( "Put: " emp.toString() );elsestatus.setText( "Put: " emp.toString() "; Replaced: " val.toString() );}});southPanel.add( put );JButton get new JButton( "Get" );get.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){Object val table.get( lName.getText() );if ( val ! null )status.setText( "Get: " val.toString() );elsestatus.setText( "Get: " lName.getText() " not in table" );}});southPanel.add( get );JButton remove new JButton( "Remove" );Demonstrating class Hashtable (part 2 of 5).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company

CHAPTER 13114115116117118119120121122Fig. 23.3JAVA UTILITIES PACKAGE AND BIT MANIPULATION1117remove.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){Object val table.remove( lName.getText() );if ( val ! null )status.setText( "Remove: " val.toString() );elsestatus.setText( "Remove: " lName.getText() " not in table" );}});southPanel.add( remove );JButton empty new JButton( "Empty" );empty.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){status.setText( "Empty: " table.isEmpty() );}});southPanel.add( empty );JButton containsKey new JButton( "Contains key" );containsKey.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){status.setText( "Contains key: " table.containsKey( lName.getText() ) );}});southPanel.add( containsKey );JButton clear new JButton( "Clear table" );clear.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){table.clear();status.setText( "Clear: Table is now empty" );}});southPanel.add( clear );Demonstrating class Hashtable (part 3 of 5).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company

1172Fig. 23.3JAVA UTILITIES PACKAGE AND BIT MANIPULATIONCHAPTER 23JButton listElems new JButton( "List objects" );listElems.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){StringBuffer buf new StringBuffer();for ( Enumeration enum table.elements();enum.hasMoreElements(); )buf.append(enum.nextElement() ).append( '\n' );display.setText( buf.toString() );}});southPanel.add( listElems );JButton listKeys new JButton( "List keys" );listKeys.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){StringBuffer buf new StringBuffer();for ( Enumeration enum table.keys();enum.hasMoreElements(); )buf.append(enum.nextElement() ).append( '\n' );JOptionPane.showMessageDialog( null,buf.toString(), "Display",JOptionPane.PLAIN MESSAGE );}});southPanel.add( listKeys );Container c getContentPane();c.add( northPanel, BorderLayout.NORTH );c.add( new JScrollPane( display ), BorderLayout.CENTER );c.add( southPanel, BorderLayout.SOUTH );setSize( 540, 300 );show();}public static void main( String args[] ){HashtableTest app new HashtableTest();Demonstrating class Hashtable (part 4 of 5).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company

CHAPTER 23JAVA UTILITIES PACKAGE AND BIT MANIPULATION173app.addWindowListener(174new WindowAdapter() {175public void windowClosing( WindowEvent e )176{177System.exit( 0 );178}179}180);181}182 }183184 class Employee {185private String first, last;186187public Employee( String fName, String lName )188{189first fName;190last lName;191}192193public String toString() { return first " " last; }194 }Fig. 23.3Demonstrating class Hashtable (part 5 of 5).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company1119

CHAPTER 930313233343536373839404142434445464748495051JAVA UTILITIES PACKAGE AND BIT MANIPULATION1121// Fig. 23.4: PropertiesTest.java// Demonstrates class Properties of the java.util package.import java.io.*;import java.util.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class PropertiesTest extends JFrame {private JLabel status;private Properties table;private JTextArea display;Fig. 23.4public PropertiesTest(){super( "Properties Test" );table new Properties();Container c getContentPane();JPanel northPanel new JPanel();northPanel.setLayout( new BorderLayout() );JPanel northSubPanel new JPanel();JPanel southPanel new JPanel();northSubPanel.add( new JLabel( "Property value" ) );final JTextField propVal new JTextField( 10 );northSubPanel.add( propVal );northPanel.add( northSubPanel, BorderLayout.NORTH );northSubPanel.add( new JLabel( "Property name (key)" ) );final JTextField propName new JTextField( 10 );northSubPanel.add( propName );display new JTextArea( 4, 35 );JButton put new JButton( "Put" );put.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){Object val table.put( propName.getText(),propVal.getText() );if ( val null )showStatus( "Put: " propName.getText() " " propVal.getText() );elseshowStatus( "Put: " propName.getText() " " propVal.getText() "; Replaced: " val.toString() );Demonstrating class Properties (part 1 of 4).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company

100101102103104Fig. 23.4JAVA UTILITIES PACKAGE AND BIT MANIPULATIONCHAPTER 23listProperties();}});southPanel.setLayout( new GridLayout( 1, 5 ) );southPanel.add( put );JButton clear new JButton( "Clear" );clear.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){table.clear();showStatus( "Table in memory cleared" );listProperties();}});southPanel.add( clear );JButton getProperty new JButton( "Get property" );getProperty.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){Object val table.getProperty(propName.getText() );if ( val ! null )showStatus( "Get property: " propName.getText() " " val.toString() );elseshowStatus( "Get: " propName.getText() " not in table" );listProperties();}});southPanel.add( getProperty );JButton save new JButton( "Save" );save.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){try {FileOutputStream output;output new FileOutputStream( "props.dat" );table.store( output, "Sample Properties" );output.close();Demonstrating class Properties (part 2 of 4).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company

CHAPTER 54155156157Fig. 23.4JAVA UTILITIES PACKAGE AND BIT MANIPULATION1123listProperties();}catch( IOException ex ) {showStatus( ex.toString() );}}});southPanel.add( save );JButton load new JButton( "Load" );load.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){try {FileInputStream input;input new FileInputStream( "props.dat" );table.load( input );input.close();listProperties();}catch( IOException ex ) {showStatus( ex.toString() );}}});southPanel.add( load );status new JLabel();northPanel.add( status, BorderLayout.SOUTH );c.add( northPanel, BorderLayout.NORTH );c.add( new JScrollPane( display ), BorderLayout.CENTER );c.add( southPanel, BorderLayout.SOUTH );setSize( 550, 225 );show();}public void listProperties(){StringBuffer buf new StringBuffer();String pName, pVal;Enumeration enum table.propertyNames();while( enum.hasMoreElements() ) {pName enum.nextElement().toString();pVal table.getProperty( pName );buf.append( pName ).append( '\t' );Demonstrating class Properties (part 3 of 4).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company

73174175176177178179180181 }Fig. 23.4JAVA UTILITIES PACKAGE AND BIT MANIPULATIONCHAPTER 23buf.append( pVal ).append( '\n' );}display.setText( buf.toString() );}public void showStatus( String s ){status.setText( s );}public static void main( String args[] ){PropertiesTest app new PropertiesTest();app.addWindowListener(new WindowAdapter() {public void windowClosing( WindowEvent e ){System.exit( 0 );}});}Demonstrating class Properties (part 4 of 4).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company

CHAPTER 23JAVA UTILITIES PACKAGE AND BIT MANIPULATION1127OperatorNameDescription&bitwise ANDThe bits in the result are set to 1 if the corresponding bitsin the two operands are both 1. bitwise inclusiveORThe bits in the result are set to 1 if at least one of the corresponding bits in the two operands is 1. bitwise exclusiveORThe bits in the result are set to 1 if exactly one of the corresponding bits in the two operands is 1. left shiftShifts the bits of the first operand left by the number of bitsspecified by the second operand; fill from the right with 0bits. right shift with signextensionShifts the bits of the first operand right by the number ofbits specified by the second operand. If the first operand isnegative, 1s are shifted in from the left; otherwise, 0s areshifted in from the left. right shift with zeroextensionShifts the bits of the first operand right by the number ofbits specified by the second operand; 0s are shifted in fromthe left. one’s complementAll 0 bits are set to 1 and all 1 bits are set to 0.Fig. 23.5The bitwise operators .from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company

8293031323334JAVA UTILITIES PACKAGE AND BIT MANIPULATIONCHAPTER 23// Fig. 23.6: PrintBits.java// Printing an unsigned integer in bitsimport java.awt.*;import java.awt.event.*;import javax.swing.*;public class PrintBits extends JFrame {Fig. 23.6public PrintBits(){super( "Printing bit representations for numbers" );Container c getContentPane();c.setLayout( new FlowLayout() );c.add( new JLabel( "Enter an integer " ) );final JTextField output new JTextField( 33 );JTextField input new JTextField( 10 );input.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){int val Integer.parseInt(e.getActionCommand() );output.setText( getBits( val ) );}});c.add( input );c.add( new JLabel( "The integer in bits is" ) );output.setEditable( false );c.add( output );setSize( 720, 70 );Displaying the bit representation of an integer (part 1 of 2).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company

CHAPTER 596061626364656667JAVA UTILITIES PACKAGE AND BIT MANIPULATIONshow();}private String getBits( int value ){int displayMask 1 31;StringBuffer buf new StringBuffer( 35 );for ( int c 1; c 32; c ) {buf.append(( value & displayMask ) 0 ? '0' : '1' );value 1;if ( c % 8 0 )buf.append( ' ' );}return buf.toString();}public static void main( String args[] ){PrintBits app new PrintBits();app.addWindowListener(new WindowAdapter() {public void windowClosing( WindowEvent e ){System.exit( 0 );}});}}Fig. 23.6Displaying the bit representation of an integer (part 2 of 2).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company1129

1130JAVA UTILITIES PACKAGE AND BIT MANIPULATIONCHAPTER 23Bit 1Bit 2Bit 1 & Bit 2000100010111Fig. 23.7Results of combining two bits with the bitwise AND operator (&).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company

CHAPTER A UTILITIES PACKAGE AND BIT MANIPULATION1131// Fig. 23.8: MiscBitOps.java// Using the bitwise AND, bitwise inclusive OR, bitwise// exclusive OR, and bitwise complement operators.import java.awt.*;import java.awt.event.*;import javax.swing.*;public class MiscBitOps extends JFrame {private JTextField input1, input2, bits1, bits2;private int val1, val2;Fig. 23.8public MiscBitOps(){super( "Bitwise operators" );JPanel inputPanel new JPanel();inputPanel.setLayout( new GridLayout( 4, 2 ) );inputPanel.add( new JLabel( "Enter 2 ints" ) );inputPanel.add( new JLabel( "" ) );inputPanel.add( new JLabel( "Value 1" ) );input1 new JTextField( 8 );inputPanel.add( input1 );inputPanel.add( new JLabel( "Value 2" ) );input2 new JTextField( 8 );inputPanel.add( input2 );inputPanel.add( new JLabel( "Result" ) );final JTextField result new JTextField( 8 );result.setEditable( false );inputPanel.add( result );JPanel bitsPanel new JPanel();bitsPanel.setLayout( new GridLayout( 4, 1 ) );bitsPanel.add( new JLabel( "Bit representations" ) );bits1 new JTextField( 33 );bits1.setEditable( false );bitsPanel.add( bits1 );bits2 new JTextField( 33 );bits2.setEditable( false );bitsPanel.add( bits2 );final JTextField bits3 new JTextField( 33 );bits3.setEditable( false );bitsPanel.add( bits3 );JPanel buttonPanel new JPanel();JButton and new JButton( "AND" );Demonstrating the bitwise AND, bitwise inclusive OR, bitwise exclusiveOR and bitwise complement operators (part 1 of 4).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company

0101102103Fig. 23.8JAVA UTILITIES PACKAGE AND BIT MANIPULATIONCHAPTER 23and.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){setFields();result.setText( Integer.toString( val1 &val2 ) );bits3.setText( getBits( val1 & val2 ) );}});buttonPanel.add( and );JButton inclusiveOr new JButton( "Inclusive OR" );inclusiveOr.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){setFields();result.setText( Integer.toString( val1 val2 ) );bits3.setText( getBits( val1 val2 ) );}});buttonPanel.add( inclusiveOr );JButton exclusiveOr new JButton( "Exclusive OR" );exclusiveOr.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){setFields();result.setText( Integer.toString( val1 val2 ) );bits3.setText( getBits( val1 val2 ) );}});buttonPanel.add( exclusiveOr );JButton complement new JButton( "Complement" );complement.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){input2.setText( "" );bits2.setText( "" );int val Integer.parseInt( input1.getText() );result.setText( Integer.toString( val ) );bits1.setText( getBits( val ) );Demonstrating the bitwise AND, bitwise inclusive OR, bitwise exclusiveOR and bitwise complement operators (part 2 of 4).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company

CHAPTER 6137138139140141142143144Fig. 23.8JAVA UTILITIES PACKAGE AND BIT MANIPULATION1133bits3.setText( getBits( val ) );}});buttonPanel.add( complement );Container c getContentPane();c.setLayout( new BorderLayout() );c.add( inputPanel, BorderLayout.WEST );c.add( bitsPanel, BorderLayout.EAST );c.add( buttonPanel, BorderLayout.SOUTH );setSize( 600, 150 );show();}private void setFields(){val1 Integer.parseInt( input1.getText() );val2 Integer.parseInt( input2.getText() );bits1.setText( getBits( val1 ) );bits2.setText( getBits( val2 ) );}private String getBits( int value ){int displayMask 1 31;StringBuffer buf new StringBuffer( 35 );for ( int c 1; c 32; c ) {buf.append(( value & displayMask ) 0 ? '0' : '1' );value 1;if ( c % 8 0 )buf.append( ' ' );}return buf.toString();}Demonstrating the bitwise AND, bitwise inclusive OR, bitwise exclusiveOR and bitwise complement operators (part 3 of 4).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company

1134145146147148149150151152153154155156157158 }Fig. 23.8JAVA UTILITIES PACKAGE AND BIT MANIPULATIONCHAPTER 23public static void main( String args[] ){MiscBitOps app new MiscBitOps();app.addWindowListener(new WindowAdapter() {public void windowClosing( WindowEvent e ){System.exit( 0 );}});}Demonstrating the bitwise AND, bitwise inclusive OR, bitwise exclusiveOR and bitwise complement operators (part 4 of 4).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company

CHAPTER 23JAVA UTILITIES PACKAGE AND BIT MANIPULATIONBit 1Bit 2Bit 1 Bit 2000101011110Fig. 23.9Results of combining two bits with the bitwise exclusive OR operator ( ).Bit 1Bit 2Bit 1 Bit 2000101011111Fig. 23.101234567891011121314151617181135Results of combining two bits with the bitwise inclusive OR operator ( ).// Fig. 23.11: BitShift.java// Using the bitwise shift operators.import java.awt.*;import java.awt.event.*;import javax.swing.*;public class BitShift extends JFrame {public BitShift(){super( "Shifting bits" );Fig. 23.11Container c getContentPane();c.setLayout( new FlowLayout() );final JTextField bits new JTextField( 33 );c.add( new JLabel( "Integer to shift " ) );final JTextField value new JTextField( 12 );Demonstrating the bitwise shift operators (part 1 of 4).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company

1136JAVA UTILITIES PACKAGE AND BIT 636465666768Fig. 23.11CHAPTER 23value.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){int val Integer.parseInt( value.getText() );bits.setText( getBits( val ) );}});c.add( value );bits.setEditable( false );c.add( bits );JButton left new JButton( " " );left.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){int val Integer.parseInt( value.getText() );val 1;value.setText( Integer.toString( val ) );bits.setText( getBits( val ) );}});c.add( left );JButton rightSign new JButton( " " );rightSign.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){int val Integer.parseInt( value.getText() );val 1;value.setText( Integer.toString( val ) );bits.setText( getBits( val ) );}});c.add( rightSign );JButton rightZero new JButton( " " );rightZero.addActionListener(new ActionListener() {public void actionPerformed( ActionEvent e ){int val Integer.parseInt( value.getText() );val 1;value.setText( Integer.toString( val ) );Demonstrating the bitwise shift operators (part 2 of 4).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company

CHAPTER 93949596979899100101102103104105106107108 }JAVA UTILITIES PACKAGE AND BIT MANIPULATIONbits.setText( getBits( val ) );}});c.add( rightZero );setSize( 400, 120 );show();}private String getBits( int value ){int displayMask 1 31;StringBuffer buf new StringBuffer( 35 );for ( int c 1; c 32; c ) {buf.append(( value & displayMask ) 0 ? '0' : '1' );value 1;if ( c % 8 0 )buf.append( ' ' );}return buf.toString();}public static void main( String args[] ){BitShift app new BitShift();app.addWindowListener(new WindowAdapter() {public void windowClosing( WindowEvent e ){System.exit( 0 );}});}12Fig. 23.11Demonstrating the bitwise shift operators (part 3 of 4).from Java: How To Program, 3eby Deitel & Deitel 2000 Prentice Hall, Inc.A Pearson Company1137

1138JAVA UTILITIES PACKAGE AND BIT MANIPULATION3456789Fig. 23.11Demonstrating the bit

by deitel & deitel from java: how to program, 3e. 1118 j ava u tilities p ackage and b it m anipulation c hapter 23. c hapter 23 j ava u tilities p ackage and b it m anipulation. c hapter 23 j ava u tilities p ackage and b it m anipulation. 23. 23. 23 & &). ). .