Contents

  1. ./AccountCheck.java
  2. ./ArrayLength.java
  3. ./ArraysExample.java
  4. ./ArrayVars.java
  5. ./CoinFlip.java
  6. ./CoinFlipUsingBoolean.java
  7. ./CommandLineArgs.java
  8. ./ConsoleUsingScannerDemo.java
  9. ./Countdown.java
  10. ./Equals.java

./AccountCheck.java 1/10

[
top][prev][next]
/**
 * This class demonstrates control flow in Java
 *
 * @author Sara Sprenkle
 *
 */
public class AccountCheck {  
    
    /* Alternatively, could make the variable have a "class-level" scope
    instead of just being within the method. 
    Probably not appropriate for this problem, but may be appropriate
    in other cases.
    
    static boolean approved = false;
    */
    
   /**
    * Called when user runs 
    *  java AccountCheck
    */
    public static void main(String[] args) {
        int purchaseAmount = 700;
        int availableCredit = 500;
        
        boolean approved = false;
        if (purchaseAmount < availableCredit) {
            availableCredit -= purchaseAmount;
            /* scope of variable is within this block of code
             and cannot be seen outside of this block.
             
             boolean approved = true;
             */
            approved = true;
        }

        if( ! approved ) 
            System.out.println("Denied");
    }
}


./ArrayLength.java 2/10

[
top][prev][next]
/**
 * This class demonstrates use of "length" field for arrays and the foreach
 * loop.
 *
 * @author Sara Sprenkle
 *
 */
public class ArrayLength {  
    
   /**
    * Called when user runs 
    *  java ArrayLength
    */
    public static void main(String[] args) {
        int[] array = new int[10];
        for (int i = 0; i < array.length; i++) { 
            array[i] = i * 2; 
        }

        for (int i = array.length -1; i >= 0; i--) {
            System.out.println(array[i]); 
        }
        
        // alternative for loop to iterate through the array
        for( int element : array ) {
            System.out.println(element);
        }
        
    }
    
}

./ArraysExample.java 3/10

[
top][prev][next]
// import the Arrays class so that we can use its functionality
import java.util.Arrays;

/**
 * This class demonstrates using arrays and the Arrays class
 *
 * @author Sara Sprenkle
 *
 */
public class ArraysExample {  
    
   /**
    * Called when user runs 
    *  java ArraysExample
    */
    public static void main(String[] args) {
        double[] array = new double[10];
        
        // fill the array with PI...  mmm... pie... using the Arrays class
        Arrays.fill(array, Math.PI);
        
        for( int i=0; i < array.length; i++ ) {
            System.out.println("array[" + i + "] = " + array[i]);
        }
    }
}

./ArrayVars.java 4/10

[
top][prev][next]

/**
 * Demonstrates that assigning an array variable to another variable
 * does *not* make a copy of an array.  Both variables are
 * referencing the same array.
 */
public class ArrayVars {
    
    public static void main(String[] args) {
	int [] fibNums = {1, 1, 2, 3, 5, 8, 13};
	int [] otherFibNums;
	
	otherFibNums = fibNums;
	otherFibNums[2] = 99;
	
	System.out.println(otherFibNums[2]);
	System.out.println(fibNums[2]);

	// Each statement above will output 99.  Why?
    }

}

./CoinFlip.java 5/10

[
top][prev][next]
import java.util.Random;

/**
 * This class generates heads or tails at random.
 *
 * @author CSCI209 Class
 */ 
public class CoinFlip {  
  
   /**
    * Called when user runs 
    *  java CoinFlip
    */
    public static void main(String[] args) {
        Random coin = new Random();
        
        int side = coin.nextInt(2);
        
        if( side == 0 ) {
            System.out.println("TAILS!");
        } else {
            System.out.println("HEADS!");
        }
        
    }
}

./CoinFlipUsingBoolean.java 6/10

[
top][prev][next]
import java.util.Random;

/**
 * This class generates heads or tails at random.
 *
 * @author CSCI209 Class
 */
public class CoinFlipUsingBoolean {  
  
   /**
    * Called when user runs 
    *  java CoinFlipUsingBoolean
    */
    public static void main(String[] args) {
        Random random = new Random();
        
        if ( random.nextBoolean() ) {
            System.out.println("HEADS!");   
        } else {
            System.out.println("TAILS!");   
        }
        
    }
}

./CommandLineArgs.java 7/10

[
top][prev][next]
/**
 * Example demonstrates use of command-line arguments
 * 
 * Run the program like
 *      java CommandLineArgs
 *      java CommandLineArgs arg
 * 
 * How was the output different?
 */
public class CommandLineArgs {

    public static void main(String[] args) {
        if( args.length < 1 ) {
            System.out.println("Error: invalid number of arguments");
            System.exit(1);
        }
        
        // use the argument ...
        System.out.println("First argument is: " + args[0] );
    }

}

./ConsoleUsingScannerDemo.java 8/10

[
top][prev][next]
import java.util.Scanner;

/**
 * A program that demonstrates reading in from the console, using calculating
 * the area of a rectangle as the example.
 * 
 * @author Sara Sprenkle
 */
public class ConsoleUsingScannerDemo {

	/**
	 * @param args
	 *            not used in this program
	 */
	public static void main(String[] args) {

		// open the Scanner on the console input, System.in
		Scanner scan = new Scanner(System.in);

		scan.useDelimiter("\n"); // breaks up by lines, useful for console I/O

		System.out.print("Please enter the width of a rectangle: ");
		int width = scan.nextInt();

		System.out.print("Please enter the height of a rectangle: ");
		int length = scan.nextInt();

		System.out.println("The area of your square is " + length * width + ".");
		
		scan.close();
	}
}

./Countdown.java 9/10

[
top][prev][next]
/**
 * This class demonstrates a counting for loop
 *
 * @author Sara Sprenkle
 *
 */
public class Countdown {  
    
    public static void main(String[] args) {
        System.out.println("Counting down...");

        for (int count=5; count >= 1; count--) {
            System.out.println(count);
        }
        System.out.println("Blastoff!");
    }
}


./Equals.java 10/10

[
top][prev][next]
/**
 * Demonstrates different equals with Strings
 * Run as
 *   java Equals <command-line argument>
 * @author Sara Sprenkle
 */
public class Equals {

    public static void main(String args[]) {
        String string1 = "same";
        String string2 = string1;
        // The following statement doesn't create a _new_ String object/memory 
        // allocation Java memory optimization
        String string3 = "same"; 
        String string4 = args[0]; //enter "same" as a command-line argument
        
        System.out.println("string1 == string2? " + (string1==string2));
        System.out.println("string2 == string3? " + (string2==string3));
        System.out.println("string1 == string4? " + (string1==string4));
        // output should be
        // true
        // true
        // false, regardless of what user enters
        
        System.out.println("string1 equals string2? " + (string1.equals(string2)));
        System.out.println("string2 equals string3? " + (string2.equals(string3)));
        System.out.println("string1 equals string4? " + (string1.equals(string4)));
        
        // output should be
        // true
        // true
        // true (depending on what user enters)
    }

}

Generated by GNU Enscript 1.6.6.