Computer Science 15-100 (Sections T & U), Fall 2007
Class Notes, Day 7:   Thu 20-Sep-2007


Logistics

  1. Meeting 7 out of 28 (1/4th of the way!)
  2. Schedule
  3. Reading

Topic Outline:

  1. Formatting with printf /format:

    %[flags][width][.precision]conversion

    a)  Simple use

       int x = 3;
       double d = 4.5;
       System.out.printf("x = %d\n",x); // %d prints an integer
       System.out.printf("d = %f\n",d); // %f prints a floating point number


    b)  Equivalence of "printf" and "format"

       int x = 3;
       double d = 4.5;
       System.out.format("x = %d\n",x);
       System.out.format("d = %f\n",d);


    c)  Use of String.format

       int x = 3;
       double d = 4.5;
       String s;
       s = String.format("x = %d\n",x);
       System.out.format(s);
       s = String.format("d = %f\n",d);
       System.out.format(s);


    d)  The Conversion Types
     
    b boolean
    d decimal integer
    o octal integer
    h hex integer
    H Hex integer
    f Floating-point number
    e Floating-point number in scientific notation
    g Floating-point number in compact form

    e)  Examples

       // convert to hexadecimal
       int y = 165;
       System.out.printf("%d\n",y); // 165
       System.out.printf("%h\n",y); // a5
       System.out.printf("%H\n",y); // A5

       // different forms of floating-point numbers
       double d = Math.pow(Math.PI,20);
       System.out.printf("%e\n",d); // 8.769957e+09
       System.out.printf("%f\n",d); // 8769956796.082693
       System.out.printf("%g\n",d); // 8.76996e+09

    f)  Field width

       int y = 123, z = 45;
       System.out.printf("123456789\n");  // 123456789
       System.out.printf("%4d%4d\n",y,z); //  123  45
       System.out.printf("%1d%4d\n",y,z); // 123  45

    g)  Flags:  Left-Justified ('-'), Use-Sign ('+'), and Zero-Padded ('0')

       int y = 123, z = 45;
       System.out.printf("123456789\n");    // 123456789
       System.out.printf("%4d%+4d\n",y,z);  //  123 +45
       System.out.printf("%-4d%+4d\n",y,z); // 123  +45
       System.out.printf("%+05d%4d\n",y,z); // +0123  45

    h)  Precision

       double d = 45.678;
       System.out.printf("%.0f\n",d);    // 46
       System.out.printf("%.1f\n",d);    // 45.7
       System.out.printf("%.2f\n",d);    // 45.68
       System.out.printf("%+06.2f\n",d); // +45.68
       System.out.printf("%+07.2f\n",d); // +045.68

     

  2. Enumerated Types

    a)  Basic use

    class MyCode {
       static enum Season { winter, spring, summer, fall };
       public static void main(String[] args) {
          Season s1 = Season.winter;
          Season s2 = Season.spring;
          System.out.println(s1);                   // winter
          System.out.println(s2);                   // spring
          System.out.println(s1 == Season.winter);  // true
          System.out.println(s1 == Season.spring);  // false
       }
    }

    b)  Cannot be local to a method

    class MyCode {
       public static void main(String[] args) {
          enum Season { winter, spring, summer, fall };  // WILL NOT COMPILE
          Season s1 = Season.winter;
          Season s2 = Season.spring;
          System.out.println(s1);
          System.out.println(s2);
          System.out.println(s1 == Season.winter);
          System.out.println(s1 == Season.spring);
       }
    }

     
  3. Writing Static Methods

    a) void methods with no parameters

    class MyCode {
       public static void doSomething() {
          System.out.println("Carpe diem");
       }


       public static void main(String[] args) {
          doSomething();
          doSomething();
       }
    }

    b) void methods with one parameter

    class MyCode {
       public static void printSquare(int n) {
          System.out.println(n + "^2 = " + (n*n));
       }

       public static void main(String[] args) {
          printSquare(2);
          printSquare(3);
       }
    }

    c) void methods with multiple parameters

    class MyCode {
       public static void printPower(double base, double exponent) {
          System.out.println(base + "^" + exponent + " = " +
                             Math.pow(base,exponent));
       }

       public static void main(String[] args) {
          printPower(2,3);
          printPower(2,0.5); // square root!
       }
    }


    d) int methods (Methods that Return Values)

    class MyCode {
       public static int getSquare(int n) {
          return n*n;
       }

       public static void main(String[] args) {
          int n, nSquared;
          n = 3;
          nSquared = getSquare(n);
          System.out.println(n + "^2 = " + nSquared);
          n = 4;
          nSquared = getSquare(n);
          System.out.println(n + "^2 = " + nSquared);
       }
    }

    e) Methods can call other methods:

    class MyCode {
       public static int getSquare(int n) {
          return n*n;
       }

       public static void printSquare(int n) {
          System.out.println(n + "^2 = " + getSquare(n));
       }

       public static void main(String[] args) {
          printSquare(3);
          printSquare(4);
       }
    }

    f) Can also have methods return double, char, String, boolean, or any other type!

    class MyCode {
       public static double getCubeRoot(double d) {
          return Math.pow(d,1.0/3.0);
       }

       public static void printCubeRoot(double d) {
          System.out.println(d + "^(1/3) = " + getCubeRoot(d));
       }

       public static void main(String[] args) {
          printCubeRoot(8);
          printCubeRoot(9);
       }
    }

    g) Local variables
    Aside:  Methods can have local variables (just like main):

    h) Parameters are local variables!

    i) Local variables are local -- they only exist within the method where they are defined!
    Aside:  this means different methods can have local variables with the same name, but these are different variables.
     
  4. Simple "for" Loops

    a)  Print the numbers from 1 to 5:

    int x;
    for (x=0; x<=5; x++) {
       System.out.println("x = " + x);
    }

    b)  Print the SUM of the numbers from 1 to 5:

    int x;
    int sum = 0;
    for (x=0; x<=5; x++) {
       System.out.println("x = " + x);
       sum += x;
    }
    System.out.println("sum = " + sum);
     
  5. Simple "if" statements

    a)  Print the sum of the ODD numbers from 1 to 5:

    int x;
    int sum = 0;
    for (x=0; x<=5; x++) {
       if (x % 2 == 1) {
          System.out.println("x = " + x);
          sum += x;
       }
    }
    System.out.println("sum = " + sum);
     
  6. Case Study:  Drawing a star using methods and "for" loops

Carpe diem!