1 / 35

Public, Static and Arrays

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.

Download Presentation

Public, Static and Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Public, Static and Arrays CS61BL Summer 2009

  2. 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

  3. 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

  4. Public vs. Private Methods Private methods are not accessible by other classes Helper methods should be private

  5. static fields Global state shared by all instances of the class Looks like an instance variable declaration + static Access them as ClassName.fieldName

  6. static methods

  7. 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(…)

  8. static version Non static version

  9. Announcements Join the google group (link from the webpage) Don’t worry about your quiz scores!!! Do the reading PRACTICE, PRACTICE, PRACTICE!

  10. Arrays

  11. Debugging an Array After the first line:

  12. Debugging an Array After the second line:

  13. Debugging an Array main people Person myAge myName myParent 115 Gertrude Baines

  14. Arrays

  15. main notes 64 62 60 62 64 64 64 62 62 62

  16. main notes 64 62 60 68 62 64 64 64 62 62 62

  17. For Loops for (initialize; test; increment) { // loop-body }

  18. For Loops for (int i = 0; i<10; i++) { System.out.println(i + “ “); }

  19. Awesome For Loops (FOR EACH) int [] notes = {60, 61, 62}; for (intoneNote : notes) { System.out.println(oneNote + “ “); }

  20. 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

  21. 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;

  22. Never EVER start writing a program until you can solve the problem by hand!

  23. 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.

  24. return continue break Code after Loop Code after

  25. Test Driven Development static void beAGoodProgrammer() { while (true) // for each method { writeTest(); writeCode(); writeAFewMoreTests(); writeCode(); } }

  26. 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

  27. 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.

  28. 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

  29. 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

  30. 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

More Related