Contents

  1. ./ConsoleUsingScannerDemo.java
  2. ./Conversion2.java
  3. ./Conversion.java
  4. ./Equals.java
  5. ./EscapeCharacters.java
  6. ./Float.java
  7. ./Format.java
  8. ./MathExample.java
  9. ./TestScore.java

./ConsoleUsingScannerDemo.java 1/9

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

/**
 * A program that demonstrates reading in from the console, using calculating
 * the area of a rectangle as the example.
 * 
 * @author Sara Sprenkle
 */
public class ConsoleUsingScannerDemo {

	/**
	 * @param args
	 *            not used in this program
	 */
	public static void main(String[] args) {

		// open the Scanner on the console input, System.in
		Scanner scan = new Scanner(System.in);

		scan.useDelimiter("\n"); // breaks up by lines, useful for console I/O

		System.out.print("Please enter the width of a rectangle: ");
		int width = scan.nextInt();

		System.out.print("Please enter the height of a rectangle: ");
		int length = scan.nextInt();

		System.out.println("The area of your square is " + length * width + ".");
	}
}

./Conversion2.java 2/9

[
top][prev][next]
/**
 * This class converts 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 3/9

[
top][prev][next]
/**
 * This class converts inches to centimeters.
 * 
 * This class also demonstrates class constants.
 *
 * @author Sara Sprenkle
 *
 */
public class Conversion {  
    
    public 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 4/9

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

}

./EscapeCharacters.java 5/9

[
top][prev][next]
/**
 * This class demonstrates using some escape characters
 *
 * @author Sara Sprenkle
 */
public class EscapeCharacters {  
  
   /**
    * Called when user runs 
    *  java First
    */
   public static void main(String[] args) { 
       System.out.println("To print a \\, you must use \"\\\\\"");
   }
}

./Float.java 6/9

[
top][prev][next]
/**
 * This class demonstrates how to specify floats vs doubles in Java.
 *
 * But, it's easier to just use doubles.
 *
 * @author Sara Sprenkle
 */
public class Float { 
  
   /**
    * Called when user runs 
    *  java Float
    */
   public static void main(String[] args) {
       float f = 3.14f;
       // double f = 3.14;
       System.out.println(f);
   }
}

./Format.java 7/9

[
top][prev][next]
/**
 * This class demonstrates formatted printing in Java
 * 
 * @author Sara Sprenkle
 */
public class Format { 
  
   /**
    * Called when user runs 
    *  java Format
    */
   public static void main(String[] args) { 
       double d1=3.14159, d2=1.45, total=9.43;

       // simple formatting...
       System.out.printf("%10.5f and %5.2f ", d1, d2);

       // %n is platform-specific line separator, e.g., \n or \r\n
       System.out.printf("%-6s%5.2f%n", "Tax:", total);

   }
}

./MathExample.java 8/9

[
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);
        
        // These print statements would benefit from some formatting using printf
        
        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));
        
    }
}

./TestScore.java 9/9

[
top][prev][next]
/**
 * Demonstrate automatically changing a double to a String in a print statement
 * and casting a variable to a certain type.
 *
 * @author Sara Sprenkle
 */
public class TestScore {

    public static void main(String[] args) {
        int totalPoints = 110;
        int earnedPoints = 87;

        /* try removing the (double) below to see what happens. */
        double testScore = (double)  earnedPoints/totalPoints;

        System.out.println("Your score is " + testScore);   
    }
    
}

Generated by GNU Enscript 1.6.6.