AppendixC/ 0040755 0000071 0000534 00000000000 06503046513 0013137 5 ustar 00weiss user 0000230 0000007 AppendixC/TestString.java 0100644 0000071 0000534 00000003114 06350140014 0016073 0 ustar 00weiss user 0000230 0000007 // Illustrates inefficient strings // badLongString is quadratic // goodLongString is linear public class TestString { static String badLongString( int n ) { String result = ""; for( int i = 0; i < n; i++ ) result += 'A'; return result; } static String goodLongString( int n ) { StringBuffer result = new StringBuffer( ); for( int i = 0; i < n; i++ ) result.append( 'A' ); return new String( result ); } public static void main( String [ ] args ) { long time = System.currentTimeMillis( ); String theString; // Test them first System.out.println( "Should twice print 20 As next" ); System.out.println( goodLongString( 20 ) ); System.out.println( badLongString( 20 ) ); // Time the efficient implementation for( int i = 1000; i < 1000000; i *= 2 ) { theString = goodLongString( i ); long newTime = System.currentTimeMillis( ); System.out.println( "Time for goodLongString( " + i + " ) is " + ( newTime - time ) ); time = newTime; } // Time the inefficient implementation for( int i = 1000; i < 100000; i *= 2 ) { theString = badLongString( i ); long newTime = System.currentTimeMillis( ); System.out.println( "Time for goodLongString( " + i + " ) is " + ( newTime - time ) ); time = newTime; } } } AppendixD/ 0040755 0000071 0000534 00000000000 06520262373 0013143 5 ustar 00weiss user 0000230 0000007 AppendixD/GUI.java 0100644 0000071 0000534 00000006203 06503046111 0014417 0 ustar 00weiss user 0000230 0000007 import java.awt.*; import java.awt.event.*; public class GUI extends Panel implements ActionListener { public GUI( ) { makeTheObjects( ); doTheLayout( ); theDrawButton.addActionListener( this ); } // Make all the objects private void makeTheObjects( ) { theCanvas = new GUICanvas( ); theCanvas.setBackground( Color.white ); theCanvas.setSize( 100, 100 ); theShape = new Choice( ); theShape.add( "Circle" ); theShape.add( "Square" ); theColor = new List( 2, false ); theColor.add( "red" ); theColor.add( "blue" ); theColor.select( 0 ); // make red default theXCoor = new TextField( 5 ); theYCoor = new TextField( 5 ); CheckboxGroup theSize = new CheckboxGroup( ); smallPic = new Checkbox( "Small", theSize, false ); mediumPic = new Checkbox( "Medium", theSize, true ); largePic = new Checkbox( "Large", theSize, false ); theFillBox = new Checkbox( "Fill" ); theFillBox.setState( false ); theDrawButton = new Button( "Draw" ); theMessage = new TextField( 25 ); theMessage.setEditable( false ); } // Layout all the objects private void doTheLayout( ) { Panel topHalf = new Panel( ); Panel bottomHalf = new Panel( ); // Layout the top half topHalf.setLayout( new FlowLayout( ) ); topHalf.add( theCanvas ); topHalf.add( new Label( "Shape" ) ); topHalf.add( theShape ); topHalf.add( theColor ); topHalf.add( new Label( "X coor" ) ); topHalf.add( theXCoor ); topHalf.add( new Label( "Y coor" ) ); topHalf.add( theYCoor ); // Layout the bottom half bottomHalf.setLayout( new FlowLayout( ) ); bottomHalf.add( smallPic ); bottomHalf.add( mediumPic ); bottomHalf.add( largePic ); bottomHalf.add( theFillBox ); bottomHalf.add( theDrawButton ); bottomHalf.add( theMessage ); // Now layout GUI setLayout( new BorderLayout( ) ); add( topHalf, "North" ); add( bottomHalf, "South" ); } public void actionPerformed( ActionEvent evt ) { try { theCanvas.setParams( theShape.getSelectedItem( ), theColor.getSelectedItem( ), Integer.parseInt( theXCoor.getText( ) ), Integer.parseInt( theYCoor.getText( ) ), smallPic.getState( ) ? 0 : mediumPic.getState( ) ? 1 : 2, theFillBox.getState( ) ); theMessage.setText( "" ); } catch( Exception e ) { theMessage.setText( "Incomplete input" ); } } private GUICanvas theCanvas; private Choice theShape; private List theColor; private TextField theXCoor; private TextField theYCoor; private Checkbox smallPic; private Checkbox mediumPic; private Checkbox largePic; private Checkbox theFillBox; private Button theDrawButton; private TextField theMessage; } AppendixD/README 0100644 0000071 0000534 00000000176 06520262373 0014024 0 ustar 00weiss user 0000230 0000007 Note: Netscape 4.0 does not understand the setSize method. You'll need to use Explorer or the deprecated resize method. AppendixD/BasicGUI.java 0100644 0000071 0000534 00000000453 06347431666 0015405 0 ustar 00weiss user 0000230 0000007 import java.awt.*; import java.awt.event.*; public class BasicGUI extends CloseableFrame { public static void main( String [ ] args ) { Frame f = new BasicGUI( ); f.setLayout( new FlowLayout( ) ); f.add( new GUI( ) ); f.pack( ); f.show( ); } } AppendixD/BasicGUIApplet.java 0100644 0000071 0000534 00000001121 06350643700 0016530 0 ustar 00weiss user 0000230 0000007 import java.awt.*; import java.applet.*; public class BasicGUIApplet extends Applet { // Applet starts here public void init( ) { setLayout( new FlowLayout( ) ); add( new GUI( ) ); } // Constructor is called by application public BasicGUIApplet( ) { init( ); } // main method for an application public static void main( String [ ] args ) { Frame f = new CloseableFrame( ); f.setLayout( new FlowLayout( ) ); f.add( new BasicGUIApplet( ) ); f.pack( ); f.show( ); } } AppendixD/BorderTest.java 0100644 0000071 0000534 00000001040 06347420434 0016054 0 ustar 00weiss user 0000230 0000007 import java.awt.*; import java.awt.event.*; public class BorderTest extends CloseableFrame implements WindowListener { public static void main( String [ ] args ) { Frame f = new BorderTest( ); f.setLayout( new BorderLayout( ) ); f.add( new Button( "North" ), "North" ); f.add( new Button( "East" ), "East" ); f.add( new Button( "South" ), "South" ); f.add( new Button( "West" ), "West" ); f.add( new Button( "Center" ), "Center" ); f.pack( ); f.show( ); } } AppendixD/GUICanvas.java 0100644 0000071 0000534 00000002372 06311337216 0015564 0 ustar 00weiss user 0000230 0000007 import java.awt.*; public class GUICanvas extends Canvas { public void setParams( String aShape, String aColor, int x, int y, int size, boolean fill ) { this.theShape = aShape; this.theColor = aColor; xcoor = x; ycoor = y; theSize = size; fillOn = fill; repaint( ); } public void paint( Graphics g ) { if( theColor.equals( "red" ) ) g.setColor( Color.red ); else if( theColor.equals( "blue" ) ) g.setColor( Color.blue ); theWidth = 25 * ( theSize + 1 ); if( theShape.equals( "Square" ) ) if( fillOn ) g.fillRect( xcoor, ycoor, theWidth, theWidth ); else g.drawRect( xcoor, ycoor, theWidth, theWidth ); else if( theShape.equals( "Circle" ) ) if( fillOn ) g.fillOval( xcoor, ycoor, theWidth, theWidth ); else g.drawOval( xcoor, ycoor, theWidth, theWidth ); } private String theShape = ""; private String theColor = ""; private int xcoor; private int ycoor; private int theSize; // 0 = small, 1 = med, 2 = large private boolean fillOn; private int theWidth; } AppendixD/BasicGUIApplet.html 0100644 0000071 0000534 00000000146 06245413110 0016552 0 ustar 00weiss user 0000230 0000007
AppendixD/CloseableFrame.java 0100644 0000071 0000534 00000001341 06347433640 0016652 0 ustar 00weiss user 0000230 0000007 import java.awt.*; import java.awt.event.*; // Class that implements a Window that closes on a window-close event public class CloseableFrame extends Frame implements WindowListener { public CloseableFrame( ) { addWindowListener( this ); } public void windowClosing( WindowEvent event ) { System.exit( 0 ); } public void windowClosed( WindowEvent event ) { } public void windowDeiconified( WindowEvent event ) { } public void windowIconified( WindowEvent event ) { } public void windowActivated( WindowEvent event ) { } public void windowDeactivated( WindowEvent event ) { } public void windowOpened( WindowEvent event ) { } } AppendixD/CircleApplet.java 0100644 0000071 0000534 00000002453 06350645060 0016355 0 ustar 00weiss user 0000230 0000007 import java.awt.*; import java.applet.*; // This applet animates a circle moving downwards along a diagonal. // When the circle reaches the bottom, it starts again at the top. public class CircleApplet extends Applet implements Runnable { int extremity = 0; private Thread animator = null; // Called when applet page becomes inactive public void stop( ) { if( animator != null ) animator.stop( ); animator = null; } // Called when applet page becomes active public void start( ) { if( animator == null ) { animator = new Thread( this ); animator.start( ); } } // Implements the Runnable interface public void run( ) { drawCircles( ); } // Infinite loop to draw circle down the main diagonal // by calling paint repeatedly public void drawCircles( ) { for( ; ; ) for( extremity = 0; extremity < 200; extremity++ ) { repaint( ); try { Thread.sleep( 25 ); } catch( InterruptedException e ) { } } } // Draw a circle public void paint( Graphics g ) { g.fillOval( extremity, extremity, 50, 50 ); } } AppendixD/CircleApplet.html 0100644 0000071 0000534 00000000144 06350306530 0016367 0 ustar 00weiss user 0000230 0000007 AppendixD/GoodCircles.java 0100644 0000071 0000534 00000001463 06347435064 0016212 0 ustar 00weiss user 0000230 0000007 import java.awt.*; public class GoodCircles extends CloseableFrame implements Runnable { int extremity = 0; private Thread animator = null; public GoodCircles( ) { animator = new Thread( this ); animator.start( ); } public void run( ) { drawCircles( ); } public void drawCircles( ) { for( extremity = 0; extremity < 200; extremity++ ) { repaint( ); try { Thread.sleep( 25 ); } catch( InterruptedException e ) { } } } public void paint( Graphics g ) { g.fillOval( extremity, extremity, 50, 50 ); } public static void main( String [ ] args ) { Frame f = new GoodCircles( ); f.setSize( 300, 300 ); f.show( ); } } AppendixD/BadCircles.java 0100644 0000071 0000534 00000001215 06347434674 0016011 0 ustar 00weiss user 0000230 0000007 import java.awt.*; public class BadCircles extends CloseableFrame { int extremity = 0; public BadCircles( ) { drawCircles( ); } public void drawCircles( ) { for( extremity = 0; extremity < 200; extremity++ ) { repaint( ); try { Thread.sleep( 25 ); } catch( InterruptedException e ) { } } } public void paint( Graphics g ) { g.fillOval( extremity, extremity, 50, 50 ); } public static void main( String [ ] args ) { Frame f = new BadCircles( ); f.setSize( 300, 300 ); f.show( ); } } AppendixD/ListFiles.java 0100644 0000071 0000534 00000001704 06311610700 0015670 0 ustar 00weiss user 0000230 0000007 import java.io.*; public class ListFiles { public static void main( String [ ] args ) { if( args.length == 0 ) System.out.println( "No files specified" ); for( int i = 0; i < args.length; i++ ) listFile( args[ i ] ); } public static void listFile( String fileName ) { FileReader theFile; BufferedReader fileIn = null; String oneLine; System.out.println( "FILE: " + fileName ); try { theFile = new FileReader( fileName ); fileIn = new BufferedReader( theFile ); while( ( oneLine = fileIn.readLine( ) ) != null ) System.out.println( oneLine ); } catch( Exception e ) { System.out.println( e ); } // Close the stream try { if(fileIn != null ) fileIn.close( ); } catch( IOException e ) { } } } AppendixD/FileDialogTest.java 0100644 0000071 0000534 00000000775 06311150346 0016645 0 ustar 00weiss user 0000230 0000007 import java.awt.*; public class FileDialogTest { // Pop up a FileDialog, select a file, // and list contents public static void main( String [ ] args ) { Frame f = new Frame( ); f.show( ); FileDialog d; d = new FileDialog( f, "List File", FileDialog.LOAD ); d.show( ); String fileName; if( ( fileName = d.getFile( ) ) != null ) ListFiles.listFile( fileName ); // Figure 1.17 System.exit( 0 ); } } Chapter01/ 0040755 0000071 0000534 00000000000 06502424161 0013011 5 ustar 00weiss user 0000230 0000007 Chapter01/FirstProgram.java 0100644 0000071 0000534 00000000266 06324300352 0016271 0 ustar 00weiss user 0000230 0000007 // First program // MW, 9/1/97 public class FirstProgram { public static void main( String [ ] args ) { System.out.println( "Is there anybody out there?" ); } } Chapter01/MinTest.java 0100644 0000071 0000534 00000000426 06324304526 0015242 0 ustar 00weiss user 0000230 0000007 public class MinTest { public static void main( String [ ] args ) { int a = 3; int b = 7; System.out.println( min( a, b ) ); } // Function definition public static int min( int x, int y ) { return x < y ? x : y; } } Chapter01/OperatorTest.java 0100644 0000071 0000534 00000001203 06324557146 0016314 0 ustar 00weiss user 0000230 0000007 public class OperatorTest { // Program to illustrate basic operators // The output is as follows: // 12 8 6 // 6 8 6 // 6 8 14 // 22 8 14 // 24 10 33 public static void main( String [ ] args ) { int a = 12, b = 8, c = 6; System.out.println( a + " " + b + " " + c ); a = c; System.out.println( a + " " + b + " " + c ); c += b; System.out.println( a + " " + b + " " + c ); a = b + c; System.out.println( a + " " + b + " " + c ); a++; ++b; c = a++ + ++b; System.out.println( a + " " + b + " " + c ); } } Chapter02/ 0040755 0000071 0000534 00000000000 06502424162 0013013 5 ustar 00weiss user 0000230 0000007 Chapter02/Echo.java 0100644 0000071 0000534 00000000526 06552772266 0014554 0 ustar 00weiss user 0000230 0000007 public class Echo { public static void main( String [ ] args ) { for( int i = 0; i < args.length - 1; i++ ) System.out.print( args[ i ] + " " ); if( args.length != 0 ) System.out.println( args[ args.length - 1 ] ); else System.out.println( "No arguments to echo" ); } } Chapter02/Lottery.java 0100644 0000071 0000534 00000001542 06322754052 0015323 0 ustar 00weiss user 0000230 0000007 import Supporting.Random; public class Lottery { // Generate lottery numbers (from 1-49) // Print number of occurrences of each number public static final int DIFF_NUMBERS = 49; public static final int NUMBERS_PER_GAME = 6; public static final int GAMES = 1000; public static void main( String [ ] args ) { // Generate the numbers int [ ] numbers = new int [ DIFF_NUMBERS + 1 ]; for( int i = 0; i < numbers.length; i++ ) numbers[ i ] = 0; Random r = new Random( ); for( int i = 0; i < GAMES; i++ ) for( int j = 0; j < NUMBERS_PER_GAME; j++ ) numbers[ r.randomInt( 1, DIFF_NUMBERS ) ]++; // Output the summary for( int k = 1; k <= DIFF_NUMBERS; k++ ) System.out.println( k + ": " + numbers[ k ] ); } } Chapter02/MaxTest.java 0100644 0000071 0000534 00000001471 06362230506 0015244 0 ustar 00weiss user 0000230 0000007 import java.io.*; import java.util.*; public class MaxTest { public static void main( String args[ ] ) { BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) ); String oneLine; StringTokenizer str; int x; int y; System.out.println( "Enter 2 ints on one line: " ); try { oneLine = in.readLine( ); str = new StringTokenizer( oneLine ); if( str.countTokens( ) != 2 ) throw new NumberFormatException( ); x = Integer.parseInt( str.nextToken( ) ); y = Integer.parseInt( str.nextToken( ) ); System.out.println( "Max: " + Math.max( x, y ) ); } catch( Exception e ) { System.err.println( "Error: need two ints" ); } } } Chapter02/ListFiles.java 0100644 0000071 0000534 00000001704 06311610700 0015544 0 ustar 00weiss user 0000230 0000007 import java.io.*; public class ListFiles { public static void main( String [ ] args ) { if( args.length == 0 ) System.out.println( "No files specified" ); for( int i = 0; i < args.length; i++ ) listFile( args[ i ] ); } public static void listFile( String fileName ) { FileReader theFile; BufferedReader fileIn = null; String oneLine; System.out.println( "FILE: " + fileName ); try { theFile = new FileReader( fileName ); fileIn = new BufferedReader( theFile ); while( ( oneLine = fileIn.readLine( ) ) != null ) System.out.println( oneLine ); } catch( Exception e ) { System.out.println( e ); } // Close the stream try { if(fileIn != null ) fileIn.close( ); } catch( IOException e ) { } } } Chapter02/ReadInts.java 0100644 0000071 0000534 00000002743 06324311116 0015366 0 ustar 00weiss user 0000230 0000007 import java.io.*; public class ReadInts { public static void main( String [ ] args ) { int [ ] array = getInts( ); for( int i = 0; i < array.length; i++ ) System.out.println( array[ i ] ); } // Read an unlimited number of ints with no attempts at // error recovery; return an int [ ] public static int [ ] getInts( ) { BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) ); int inputVal = 0; int [ ] array = new int[ 5 ]; int itemsRead = 0; String oneLine; System.out.println( "Enter any number of integers, one per line: " ); try { while( ( oneLine = in.readLine( ) ) != null ) { inputVal = Integer.parseInt( oneLine ); if( itemsRead == array.length ) array = resize( array, array.length * 2 ); array[ itemsRead++ ] = inputVal; } } catch( Exception e ) { } // No error handling System.out.println( "Done reading" ); return resize( array, itemsRead ); } // Resize an int[ ] array; return new array public static int [ ] resize( int [ ] array, int newSize ) { int [ ] original = array; int numToCopy = Math.min( original.length, newSize ); array = new int[ newSize ]; for( int i = 0; i < numToCopy; i++ ) array[ i ] = original[ i ]; return array; } } Chapter02/DivideByTwo.java 0100644 0000071 0000534 00000001060 06324603456 0016050 0 ustar 00weiss user 0000230 0000007 import java.io.*; public class DivideByTwo { public static void main( String [ ] args ) { BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) ); int x; String oneLine; System.out.println( "Enter an integer: " ); try { oneLine = in.readLine( ); x = Integer.parseInt( oneLine ); System.out.println( "Half of x is " + ( x / 2 ) ); } catch( Exception e ) { System.out.println( e ); } } } Chapter03/ 0040755 0000071 0000534 00000000000 06502424162 0013014 5 ustar 00weiss user 0000230 0000007 Chapter03/IntCell.java 0100644 0000071 0000534 00000000644 06311150520 0015202 0 ustar 00weiss user 0000230 0000007 /** * A class for simulating an integer memory cell * @author Mark A. Weiss */ public class IntCell { /** * Get the stored value. * @return the stored value. */ public int read( ) { return storedValue; } /** * Store a value * @param x the number to store. */ public void write( int x ) { storedValue = x; } private int storedValue; } Chapter03/TestIntCell.java 0100644 0000071 0000534 00000000502 06311150572 0016042 0 ustar 00weiss user 0000230 0000007 // Exercise the IntCell class public class TestIntCell { public static void main( String [ ] args ) { IntCell m = new IntCell( ); m.write( 5 ); System.out.println( "Cell contents: " + m.read( ) ); // The next line would be illegal if uncommented // m.storedValue = 0; } } Chapter03/AllNames.html 0100644 0000071 0000534 00000004166 06311150640 0015375 0 ustar 00weiss user 0000230 0000007All Packages Class Hierarchy
java.lang.Object | +----IntCell
public IntCell()
public int read()
public void write(int x)