350 likes | 443 Views
Public, Static and Arrays. CS61BL Summer 2009. Access Levels (HFJ pg 667). public – any code anywhere can access the public thing protected – same as default + subclasses outside the package can inherit the protected thing.
E N D
Public, Static and Arrays CS61BL Summer 2009
Access Levels (HFJ pg 667) • public – any code anywhere can access the public thing • protected – same as default + subclasses outside the package can inherit the protected thing. • default – only code within the same package as the class with the default thing can access the default thing • private – only code within the same class can access the private thing. • By “thing” we mean class, variable, method, constructor
Public vs. Private Variables public variables are accessible outside of the class private variables are only accessible inside of the class When we get to inheritance - if you have a sub-class, it can’t access the private variables
Public vs. Private Methods Private methods are not accessible by other classes Helper methods should be private
static fields Global state shared by all instances of the class Looks like an instance variable declaration + static Access them as ClassName.fieldName
static methods • static methods do not operate on an object • Has no implicit parameter this • Can NOT access instance variables • Can access static fields • Call the method as ClassName.methodName(…)
static version Non static version
Announcements Join the google group (link from the webpage) Don’t worry about your quiz scores!!! Do the reading PRACTICE, PRACTICE, PRACTICE!
Debugging an Array After the first line:
Debugging an Array After the second line:
Debugging an Array main people Person myAge myName myParent 115 Gertrude Baines
main notes 64 62 60 62 64 64 64 62 62 62
main notes 64 62 60 68 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 (intoneNote : notes) { System.out.println(oneNote + “ “); }
main notes notes2 64 62 60 62 64 64 64 62 62 62 0 64 0 62 60 0 0 62 0 64 64 0 64 0 0 62 0 62 0 62 0 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;
Never EVER start writing a program until you can solve the problem by hand!
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 break Code after Loop Code after
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
How to do Pair Programming? • Share one computer with your partner. • The “Driver” types and uses the mouse. • The “Navigator” watches, discusses and focuses on the big picture. • Take turns (about every 30 minutes) to switch roles.
Why to do 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
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
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