Office: Albertus Hall 400-2
Office Hours: Monday 2:30-4:00pm, Tuesday 1:00-2:30pm
Email: click here to email me
Phone: 518-485-3755
(on campus x3755)

variable scope

public class Example26InClass
{
    public static int globalVar = 10;
    public static int x = 12;  // this variable x has global scope

    public static void main(String[] args)
    {
        int x = 5;  // variable x is "in scope" from this
                    // point forward until the closing }
        int y = 0;
        if ( x == 5 ) {
            int z = x * x * x;  // z is "in scope" in this block ONLY
            y = z;
            System.out.println( z );
        }

        System.out.println( "down here, z is: " + y );

        int n = 500;  // this n variable is "in scope" until }
        int q = doSomething( 8 );  // call the method
        System.out.println( "8 cubed is: " + q );
        System.out.println( "globalVar is: " + globalVar );
    }
    
    public static int doSomething( int n ) // n is "in scope" ...
    {
        globalVar = n * n;
        return n * n * n;
    }
}

variable scope

public class ScopeQuestions
{
    public static double taxRate = 0.095;
    
    public static void main(String[] args)
    {
        System.out.println( taxRate );
        double taxRate = 0.0825;
        System.out.println( taxRate );
        int n = 100;
        n = doSomethingElse( n );
        System.out.println( n );
        System.out.println( taxRate );
    }
    
    public static int doSomethingElse( int n ) {
        taxRate = 0.02;
        return 5;
    }
}

arrays and methods

public class ArraysReview
{
    public static void main(String[] args)
    {
        // Create an array to hold 50 integers.
        int[] array = new int[50];

        // Initialize each integer to the value 24.
        for ( int i = 0 ; i < array.length ; i++ ) {
            array[i] = (int)( Math.random() * 70 + 1 );
        }

        // Display the values of the array by calling a method
        displayArray( array );
        
        // Reverse the elements of the array
        int[] arrayReversed = new int[array.length];
        
        // move this to a method....
//        for ( int j = 0 ; j < arrayReversed.length ; j++ ) {
//            arrayReversed[j] = array[array.length - j - 1];
//        }
        
        // Call the reverse method:
        arrayReversed = reverseArray( array );
        
        // Display the array again:
        displayArray( arrayReversed );
    }
    
    // Reverse any integer array, returning a new array
    public static int[] reverseArray( int[] a )
    {
        int[] reverseA = new int[a.length];
        for ( int j = 0 ; j < reverseA.length ; j++ ) {
            reverseA[j] = a[a.length - j - 1];
        }
        return reverseA;
    }
    
    public static void displayArray( int[] a ) {
        // Display all values of the array.
        for ( int i = 0 ; i < a.length ; i++ ) {
            System.out.print( a[i] + " " );
        }
        System.out.println();
    }
    
}

array utility methods

import java.util.Arrays;

public class MoreArrays
{
    public static void main(String[] args)
    {
        int[] array = new int[80];
        
        // Fill entire array with 24
        Arrays.fill( array, 24 );
        
        // Fill remainder of array with 27
        array[0] = 12;
        array[1] = 300;
        array[2] = 48;
        Arrays.fill( array, 3, 79, 27 );

        // Fill in slice with value 100
        Arrays.fill( array, 5, 10, 100 );
        
        // Sort the array
        Arrays.sort( array );
        // reverse the array to sort in descending order....
        
        int x = Arrays.binarySearch( array, 20 );
        System.out.println( "Found 48 at " + x );
        
        for ( int i = 0 ; i < array.length ; i++ ) {
            System.out.print( array[i] + " " );
        }
        System.out.println();


    }
}