Computer Science 15-100 (Sections T & U), Spring 2008
Class Notes:  Ch 3:  Using Classes and Objects (2 of 2)


Logistics

  1. Schedule
    1. Quiz 3 today
    2. Hw4 due Friday
  2. Reading:
    1. L&L Chapter 3:  Using Classes and Objects
      Sections 3.1 - 3.5 (last lecture)
      Sections 3.6 - 3.8 (today)

Topic Outline:

Review:  Academic Honesty + Collaboration and Cheating Policy

  1. Syllabus  (See appropriate section)
     
  2. Undergraduate Academic Disciplinary Actions Overview
     
  3. Enforcement:  Moss, JPlag, other tools, direct observation, etc.
     
  4. Bottom line:  we take the collaboration policy seriously, we routinely check for violations, and we will pursue violations subject to the university's regulations.
     
  • Ch 3:  Using Classes and Objects
     

    1. Math class redux
       

    2. Style

      1. Starting with Hw4:  good comments, variable names, whitespace, indenting, formatting
      2. More style rules coming soon!
         
    3. Converting Strings and Other Types

      1. Other types to Strings:  Use String.format (see below!)
      2. String to Integer
           String x = "123";
           int i = Integer.parseInt(x);
           System.out.println(i);
      3. Also:  Double.parseDouble, Boolean.parseBoolean, etc...
         
    4. 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

       

    5. Wrapper classes + Autoboxing
       


    carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem