Docstoc

EECS Instructional Support Group Home Page

Document Sample
EECS Instructional Support Group Home Page Powered By Docstoc
					Static and Arrays

CS61BL Summer 2010
Review Method Types
Person
myAge      0

myName
myParent
  Constructor
Static Method




                All objects must be arguments   Have access
                   or created in the method      to this!
Non-Static
  Method
       static variables (shared)
• Global state shared by all instances of the class
• Looks like an instance variable declaration + static
• Access them as ClassName.fieldName
Arrays
         Debugging an Array




After
 the
 first
line:
         Debugging an Array




 After
  the
second
 line:
         Debugging an Array




main
people

              Person
              myAge      115


              myName           Gertrude Baines
              myParent
          Person
          myAge      8

          myName
          myParent       Oscar
          Person
main      myAge
          myName
                     7



people1   myParent       Bert
people2   Person
          myAge      6
people3   myName
          myParent       Ernie
Arrays
main
notes




        64 62 60 62 64 64 64 62 62 62
main
notes




              68
        64 62 60 62 64 64 64 62 62 62
For Loops
for (initialize; test; increment)
{
    // loop-body
}
For Loops
for (int i = 0; i<10; i++)
{
    System.out.println(i + “ “);
}
    Awesome For Loops (FOR EACH)
int [] notes = {60, 61, 62};
for (int oneNote : notes)
{
    System.out.println(oneNote + “ “);
}
main
notes
notes2




         64 62 60 62 64 64 64 62 62 62

      0 0 0 0 0 0 0 0 0 0 0
     64 62 60 62 64 64 64 62 62 62 70
                    Arrays
• Set to the default value for primitive types
• Set to null for objects
 Type [] arrayName = new Type[size];
Type [] arrayName = {value1, value2};
• Arrays don’t change size
• Arrays hold one type of thing
• Access items in the array using the [] notation
     arrayName[pos] = newValue;
             Flow of control
• break – breaks from the inner most loop
  enclosing the break and continues executing
  at the code after the loop.
• return – causes the method to end
  immediately and return to the calling method
• continue – transfers control to the header
  of the inner most enclosing loop.
        return
            continue

Code
Code
after
after
Loop
         Why Pair Programming?
• Writing code is easy compared to making code
  work!
    – The bugs are the part that takes a long time
•   Clearer programs, better designs, fewer bugs.
•   Great to talk about at an interview!
•   “Overcoming difficult problems”
•   Research Study – Error free code went from 70%
    to 85% with pairs. So 30% to 15% or a 50%
    reduction in bugs.
             Source: Wikipedia: Pair Programming
        Working with Partners
• Use pair programming!
• Talk about other commitments
  – Travel plans
  – Other classes
  – Jobs
• Assume that if you haven’t seen their code – it
  doesn’t exist!
• Talk to your partner the moment you get stuck
          Estimating Time
• Most people grossly underestimate how
  long it takes to program (not thinking about
  time to debug and test)
• You appear incompetent at programming
  (not true) if you can’t estimate how long
  something will take.
• This is one of the hardest things to learn!!!
  Start to practice now! For each task
  estimate how long it will take and keep
  track of how wrong you were
       Test Driven Development
static void beAGoodProgrammer()
{
    while (true) // for each method
    {
      writeTest();
      writeCode();
      writeAFewMoreTests();
      writeCode();
    }
}
Why do Test Driven Development?
• It is a way to brainstorm what your method is
  going to do.
• It is hard to write test cases
• Write your test cases before you’re biased by
  how your code is written

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:3
posted:3/23/2011
language:English
pages:32