Contents

  1. ./ConsoleUsingScannerDemo.java
  2. ./Equals.java
  3. ./EscapeCharacters.java
  4. ./Float.java
  5. ./TestScore.java

./ConsoleUsingScannerDemo.java 1/5

[
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: ");
		double length = scan.nextDouble();

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

./Equals.java 2/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
        //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 3/5

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

[
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.14;
       // double f = 3.14;
       System.out.println(f);
   }
}

./TestScore.java 5/5

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