Contents

  1. ./AccountCheck.java
  2. ./ArrayLength.java
  3. ./ArraysExample.java
  4. ./CommandLineArgs.java
  5. ./Conversion2.java
  6. ./Equals.java
  7. ./Grades.java
  8. ./MathExample.java

./AccountCheck.java 1/8

[
top][prev][next]
/**
 * This class 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 small 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/8

[
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/8

[
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]);
        }
    }
}

./CommandLineArgs.java 4/8

[
top][prev][next]
/**
 * Example demonstrates use of command-line arguments
 */
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] );
    }

}

./Conversion2.java 5/8

[
top][prev][next]
/**
 * This class converts from inches to centimeters.
 * 
 * This class demonstrates variable declarations and class constants, 
 * as well as command-line arguments.
 *
 * @author Sara Sprenkle
 *
 */
public class Conversion2 {  
    
    static final double CM_PER_INCH = 2.540;
  
   /**
    * Called when user runs 
    *  java Conversion2
    */
    public static void main(String[] args) {
        
        if( args.length < 1 ) {
            System.out.println("Usage: java Conversion2 <numinches>");
            System.exit(1);
        }
        
        String numInchesStr = args[0];
        double numInches = Double.parseDouble(numInchesStr);
        double numCM = numInches*CM_PER_INCH;
        
        // need to put + in between string literals and variables
        // need to put explicit spaces into string literals
        // Note that Java will automatically convert the ints and doubles
        // to strings
        System.out.println("There are " + numCM + " cm in " + numInches + " inches.");       
        
    }
}

./Equals.java 6/8

[
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
        String string4 = "same"; //for first half of lecture
        
        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)
    }

}

./Grades.java 7/8

[
top][prev][next]
/**
 * This class demonstrates using a switch statement
 *
 * @author Sara Sprenkle
 *
 */
public class Grades {  
    
   /**
    * Called when user runs 
    *  java Grades
    */
    public static void main(String[] args) {
        char grade = 'b';
        
        switch(grade) {
            case 'a':
            case 'A':
                System.out.println("Congrats!");
                break;
            case 'b':
            case 'B':
                System.out.println("Not too shabby!");
                break;
            case 'c':
            case 'C':
            case 'd':
            case 'D':
                System.out.println("You are passing but you could improve.");
            case 'f':
            case 'F':
                System.out.println("Not good.  You failed.");
            default:
                System.out.println("Error: not a grade");
        }
    }
}

./MathExample.java 8/8

[
top][prev][next]
/**
 * This class demonstrates using constants and methods from Java's Math class
 *
 * @author Sara Sprenkle
 *
 */
public class MathExample {  
    
   /**
    * Called when user runs 
    *  java MathExample
    */
    public static void main(String[] args) {
        int radius = 7;
        
        double circumference = 2 * Math.PI * radius;
        double area = Math.PI * radius * radius;
        double area2 = Math.PI * Math.pow(radius, 2);
        
        System.out.println("A circle with radius " + radius + "...");
        System.out.println("\t has a circumference of " + circumference);
        System.out.println("\t and an area of " + area);
        System.out.println("\t verifying area " + area2);

        // practicing with trigonometric functions
        
        double angle = Math.PI;
        double sin_angle = Math.sin(angle);
        
        System.out.println("\n***** Trig Practice *****");
        System.out.println("The sine of " + angle + " is " + sin_angle);
        System.out.println("The cosine of " + angle + " is " + Math.cos(angle));
        
    }
}

Generated by GNU Enscript 1.6.6.