Contents

  1. ./Conversion.java
  2. ./First.java
  3. ./Float.java
  4. ./Format.java
  5. ./Hello.java
  6. ./TestScore.java

./Conversion.java 1/6

[
top][prev][next]
/**
 * This class converts from inches to centimeters.
 * 
 * This class 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.");       
        
    }
}

./First.java 2/6

[
top][prev][next]
/**
 * This class demonstrates some basic Java syntax
 *
 * All code in a Java program must belong to a class.
 * There is typically one class per file, and the file name is ClassName.java
 * Compile this code using
 *     javac First.java
 * Run the compiled bytecode First.class using
 *     java First
 * 
 * @author Sara Sprenkle
 */
public class First {  //File must be saved as First.java
  
   /**
    * Called when user runs 
    *  java First
    */
   public static void main(String[] args) { //Blocks start/end with {}
       System.out.println("This is my first Java program!"); //Lines end with ;
       System.out.println("To print a \\, you must use \"\\\\\"");
   }
}

./Float.java 3/6

[
top][prev][next]
/**
 * This class demonstrates how to specify floats vs doubles in Java.
 *
 * But, it's easier to just deal with 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 4/6

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

   }
}

./Hello.java 5/6

[
top][prev][next]
/**
 * Our first Java class
 *
 * All code in a Java program must belong to a class.
 * There is typically one class per file, and the file name is ClassName.java
 * Compile this code using
 *     javac Hello.java
 * Run the compiled bytecode Hello.class using
 *     java Hello
 * 
 * @author Sara Sprenkle
 */
public class Hello {  //File must be saved as Hello.java
  
   /**
    * Called when user runs 
    *  java Hello
    */
   public static void main(String[] args) { // Blocks start/end with {}
       System.out.println("Hello"); // Lines end with ;
   }
}

./TestScore.java 6/6

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