Contents

  1. alternative_dynamic_scoping.py
  2. Conversion.java
  3. dynamic_typing.py
  4. First.java

alternative_dynamic_scoping.py 1/4

[
top][prev][next]
"""
Demonstrates one of the benefits of static typing.
Naming can improve readability, but the programmer
could name variables poorly/incorrectly.
Sara Sprenkle, 09.07.2008
"""

def main():
    
    inputFile = file("data.dat", "r")
    
    for line in inputFile:
        itemList = line.split()
        lastItem = itemList[-1]
        firstChar = lastItem[0]
        length = len(firstChar)
        print length
    
    # How many different types was data throughout this program
    
    # print "The type of data is", type(data)
    
    # Couldn't close input file in last example because lost that variable
    inputFile.close()
    
main()

Conversion.java 2/4

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

dynamic_typing.py 3/4

[
top][prev][next]
 """
Demonstrates one of the benefits of static typing.
Not an example of good programming style
Sara Sprenkle, 09.07.2008
"""

def main():
    
    data = file("data.dat", "r")
    
    for data in data:
        data = data.split()
        data = data[-1][0]
        data = len(data)
    
    # How many different types was data throughout this program
    
    print "The type of data is", type(data)
    
main()

First.java 4/4

[
top][prev][next]
/**
 * This class demonstrates some basic Java syntax
 *
 * @author Sara Sprenkle
 *
 * 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
 */
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 \"\\\\\"");
   }
}

Generated by GNU enscript 1.6.4.