//SquareInt.java
// A programmer-defined square method import java.awt.Container; import javax.swing.*; public class SquareInt extends JApplet {    public void init()    {       String output = "";       JTextArea outputArea = new JTextArea( 10, 20 );       // get the applet's GUI component display area       Container c = getContentPane();       // attach outputArea to Container c       c.add( outputArea );       int result;       for ( int x = 1; x <= 10; x++ ) {          result = square( x );          output += "The square of " + x +                   " is " + result + "\n";       }       outputArea.setText( output );    }    // square method definition    public int square( int y )    {       return y * y;    } } ------------------------------------------------------------ //Maximum.java // Finding the maximum of three doubles import java.awt.Container; import javax.swing.*; public class Maximum extends JApplet {    public void init()    {       JTextArea outputArea = new JTextArea();       String s1 = JOptionPane.showInputDialog(                      "Enter first floating-point value" );       String s2 = JOptionPane.showInputDialog(                     "Enter second floating-point value" );       String s3 = JOptionPane.showInputDialog(                     "Enter third floating-point value" );       double number1 = Double.parseDouble( s1 );       double number2 = Double.parseDouble( s2 );       double number3 = Double.parseDouble( s3 );       double max = maximum( number1, number2, number3 );       outputArea.setText( "number1: " + number1 +                          "\nnumber2: " + number2 +                          "\nnumber3: " + number3 +                          "\nmaximum is: " + max );       // get the applet's GUI component display area       Container c = getContentPane();       // attach outputArea to Container c       c.add( outputArea );    }    // maximum method definition    public double maximum( double x, double y, double z )    {       return Math.max( x, Math.max( y, z ) );    } } ------------------------------------------------------------ //RandomInt.java // Shifted, scaled random integers import javax.swing.JOptionPane; public class RandomInt {    public static void main( String args[] )    {       int value;       String output = "";       for ( int i = 1; i <= 20; i++ ) {          value = 1 + (int) ( Math.random() * 6 );          output += value + "  ";          if ( i % 5 == 0 )             output += "\n";       }       JOptionPane.showMessageDialog( null, output,          "20 Random Numbers from 1 to 6",          JOptionPane.INFORMATION_MESSAGE );       System.exit( 0 );    } }
//RollDie.java // Roll a six-sided die 6000 times import javax.swing.*; public class RollDie {    public static void main( String args[] )    {       int frequency1 = 0, frequency2 = 0,           frequency3 = 0, frequency4 = 0,           frequency5 = 0, frequency6 = 0, face;       // summarize results       for ( int roll = 1; roll <= 6000; roll++ ) {          face = 1 + (int) ( Math.random() * 6 );          switch ( face ) {             case 1:                ++frequency1;                break;             case 2:                ++frequency2;                break;             case 3:                ++frequency3;                break;             case 4:                ++frequency4;                break;             case 5:                ++frequency5;                break;             case 6:                ++frequency6;                break;          }       }       JTextArea outputArea = new JTextArea( 7, 10 );       outputArea.setText(         "Face\tFrequency" +          "\n1\t" + frequency1 +         "\n2\t" + frequency2 +         "\n3\t" + frequency3 +         "\n4\t" + frequency4 +         "\n5\t" + frequency5 +         "\n6\t" + frequency6 );       JOptionPane.showMessageDialog( null, outputArea,          "Rolling a Die 6000 Times",          JOptionPane.INFORMATION_MESSAGE );       System.exit( 0 );    } }
//Craps.java // Craps import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Craps extends JApplet implements ActionListener {    // constant variables for status of game    final int WON = 0, LOST = 1, CONTINUE = 2;     // other variables used in program    boolean firstRoll = true;   // true if first roll    int sumOfDice = 0;          // sum of the dice    int myPoint = 0;   // point if no win/loss on first roll    int gameStatus = CONTINUE;  // game not over yet    // graphical user interface components    JLabel die1Label, die2Label, sumLabel, pointLabel;    JTextField firstDie, secondDie, sum, point;    JButton roll;    // setup graphical user interface components    public void init()    {       Container c = getContentPane();       c.setLayout( new FlowLayout() );       die1Label = new JLabel( "Die 1" );       c.add( die1Label );       firstDie = new JTextField( 10 );       firstDie.setEditable( false );       c.add( firstDie );       die2Label = new JLabel( "Die 2" );       c.add( die2Label );       secondDie = new JTextField( 10 );       secondDie.setEditable( false );       c.add( secondDie );       sumLabel = new JLabel( "Sum is" );       c.add( sumLabel );       sum = new JTextField( 10 );       sum.setEditable( false );       c.add( sum );       pointLabel = new JLabel( "Point is" );       c.add( pointLabel );       point = new JTextField( 10 );       point.setEditable( false );       c.add( point );       roll = new JButton( "Roll Dice" );       roll.addActionListener( this );       c.add( roll );    }    // call method play when button is pressed    public void actionPerformed( ActionEvent e )    {       play();    }    // process one roll of the dice    public void play()    {       if ( firstRoll ) {             // first roll of the dice          sumOfDice = rollDice();                 switch ( sumOfDice ) {             case 7: case 11:         // win on first roll                gameStatus = WON;                point.setText( "" );  // clear point text field                break;             case 2: case 3: case 12: // lose on first roll                gameStatus = LOST;                point.setText( "" );  // clear point text field                break;             default:                 // remember point                gameStatus = CONTINUE;                myPoint = sumOfDice;                point.setText( Integer.toString( myPoint ) );                firstRoll = false;                break;          }       }       else {          sumOfDice = rollDice();          if ( sumOfDice == myPoint )    // win by making point             gameStatus = WON;          else             if ( sumOfDice == 7 )       // lose by rolling 7                gameStatus = LOST;       }       if ( gameStatus == CONTINUE )          showStatus( "Roll again." );       else {          if ( gameStatus == WON )             showStatus( "Player wins. " +               "Click Roll Dice to play again." );          else             showStatus( "Player loses. " +               "Click Roll Dice to play again." );          firstRoll = true;       }    }    // roll the dice    public int rollDice()    {       int die1, die2, workSum;         die1 = 1 + ( int ) ( Math.random() * 6 );       die2 = 1 + ( int ) ( Math.random() * 6 );       workSum = die1 + die2;       firstDie.setText( Integer.toString( die1 ) );       secondDie.setText( Integer.toString( die2 ) );       sum.setText( Integer.toString( workSum ) );       return workSum;    } }
//Scoping.java // A scoping example import java.awt.Container; import javax.swing.*; public class Scoping extends JApplet {    JTextArea outputArea;    int x = 1;      // instance variable    public void init()    {       outputArea = new JTextArea();       Container c = getContentPane();       c.add( outputArea );    }    public void start()    {       int x = 5;   // variable local to method start       outputArea.append( "local x in start is " + x );       methodA();   // methodA has automatic local x       methodB();   // methodB uses instance variable x       methodA();   // methodA reinitializes automatic local x       methodB();   // instance variable x retains its value       outputArea.append( "\n\nlocal x in start is " + x );    }    public void methodA()    {       int x = 25;  // initialized each time a is called       outputArea.append( "\n\nlocal x in methodA is " + x +                          " after entering methodA" );       ++x;       outputArea.append( "\nlocal x in methodA is " + x +                          " before exiting methodA" );    }    public void methodB()    {       outputArea.append( "\n\ninstance variable x is " + x +                          " on entering methodB" );       x *= 10;       outputArea.append( "\ninstance variable x is " + x +                          " on exiting methodB" );    } }  
//FactorialTest.java // Recursive factorial method import java.awt.*; import javax.swing.*; public class FactorialTest extends JApplet {    JTextArea outputArea;    public void init()    {       outputArea = new JTextArea();       Container c = getContentPane();       c.add( outputArea );       // calculate the factorials of 0 through 10       for ( long i = 0; i <= 10; i++ )          outputArea.append(             i + "! = " + factorial( i ) + "\n" );    }    // Recursive definition of method factorial    public long factorial( long number )    {                        if ( number <= 1 )  // base case          return 1;       else          return number * factorial( number - 1 );    }  }