Contents

  1. ./ArrayLength.java
  2. ./Calculator.java
  3. ./Counter.java
  4. ./Equals.java
  5. ./Grades.java
  6. ./Main.java

./ArrayLength.java 1/6

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

./Calculator.java 2/6

[
top][prev][next]
/**
 * To practice static methods, we are writing a method that calculates
 * the average of two integers.
 * 
 * Running the class (via main) will test the average function
 * 
 * @author CSCI209
 */
public class Calculator {

    /**
     * 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 ) {
	double avg = (double) (a + b)/2;
	return avg;
    }

    /**
     * Tests the average function
     */ 
    public static void main(String[] args) {
	// some tests
	double avg = average(1, 2);
	System.out.println(avg);
	
	avg = average(2, 2);
	System.out.println(avg);

	// TODO: More testing!!!
    }

    
}

./Counter.java 3/6

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

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

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

./Main.java 6/6

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

/**
 * Main driver of Calculator program: Prompts user for two numbers and
 * displays the average of those two numbers.
 * 
 * @author CSCI209
 */
public class Main {

    public static void main( String args[] ) {
        System.out.println("This program will calculate the average of two integers\n");
    
        // Step 1: to start, just hardcode the inputs
        int first = 1;
        int second = 2;

	// Step 2: get user input for the numbers
	/*
	Scanner scan = new Scanner(System.in);
	scan.useDelimiter("\n"); // breaks up by lines, useful for console I/O

	System.out.print("Enter the first number: ");
	int first = scan.nextInt();

	System.out.print("Enter the second numbr: ");
	int second = scan.nextInt();
	scan.close();
	*/

	// Call the method
	double average = Calculator.average(first, second);
	
	// display the output
        System.out.print("The average of " + first + " and " + second + " is ");
        System.out.println(average);	

    }

}

Generated by GNU Enscript 1.6.6.