Contents

  1. ArraysExample.java
  2. Credit.java
  3. Grades.java
  4. MathExample.java

ArraysExample.java 1/4

[
top][prev][next]
// import the methods from the Arrays class
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...
        Arrays.fill(array, Math.PI);
        
        for( int i=0; i < array.length; i++ ) {
            System.out.println("array[" + i + "] = " + array[i]);
        }
    }
}

Credit.java 2/4

[
top][prev][next]
/**
 * This class demonstrates importance of scope
 *
 * @author Sara Sprenkle
 *
 */
public class Credit {  
    
   /**
    * Called when user runs 
    *  java Credit
    */
    public static void main(String[] args) {
        int availableCredit = 100;
        int purchaseAmount = 110;

        boolean approved = false;  // must be initialized or it's a compiler error.
        if (purchaseAmount < availableCredit) {
            availableCredit -= purchaseAmount;
            approved = true;
        }

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

Grades.java 3/4

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

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