Contents

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

./ArrayLength.java 1/7

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

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

./Conversion2.java 3/7

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

./Conversion.java 4/7

[
top][prev][next]
/**
 * This class converts from inches to centimeters.
 * 
 * This class demonstrates variable declarations and class constants.
 *
 * @author Sara Sprenkle
 *
 */
public class Conversion {  
    
    static final double CM_PER_INCH = 2.540;
  
   /**
    * Called when user runs 
    *  java Conversion
    */
    public static void main(String[] args) {
        int numInches = 1;
        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 5/7

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

}

./Grades.java 6/7

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

[
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.4.