Contents

  1. ./ArrayLength.java
  2. ./Average.java
  3. ./Counter.java
  4. ./Equals.java
  5. ./Grades.java

./ArrayLength.java 1/5

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

./Average.java 2/5

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

/**
 * To practice static methods, we are writing a method that calculates
 * the average of two integers.
 * 
 * @author CSCI209
 */
public class Average {

    public static void main( String args[] ) {
        Scanner scanner = new Scanner(System.in);
        scanner.useDelimiter("\n");
        System.out.println("This program will calculate the average of two integers\n");
    
        // To start, just hardcode the inputs
        //int first = 1;
        //int second = 2;
        
        System.out.print("Enter the first operand: ");
        int first = scanner.nextInt();
        System.out.print("Enter the second operand: ");
        int second = scanner.nextInt();
    
        // Note that Python would not allow you to have a variable and
        // function with the same name--in this case, average.
        double average = average(first, second); 
        // Alternatively, could call Average.average(first, second) but
        // compiler assumes that if there is no ClassName., then you're
        // looking for a static method in this class.
    
        System.out.print("The average of " + first + " and " + second + " is ");
        System.out.println(average);
    }

    /**
     * This method returns the average of the two int parameters
     * @param a the first operand
     * @param b the second operand
     * @return the average of the two int parameters as a double
     */
    public static double average( int a, int b ) {
        return ((double) a + b)/2;
    }

}

./Counter.java 3/5

[
top][prev][next]
/**
 * This class demonstrates using a while loop
 *
 * @author Sara Sprenkle
 *
 */
public class Counter {  
    
    public static void main(String[] args) {
        int counter = 0;
        while (counter < 5) {
            System.out.println(counter);
            counter++;
        }
        System.out.println("Done: " + counter);
    }
}


./Equals.java 4/5

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

}

./Grades.java 5/5

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

Generated by GNU Enscript 1.6.6.