1 CMSC 131 Fall 2009 Jan Plane (adapted from Bonnie Dorr) Lecture Set #6: Encapsulaton, “this”, junit testing and Libraries 1. Review of Parameter passing 2. this 3. public vs. private Choices 4. junit testing 5. Libraries CMSC 131 Fall 2009 Jan Plane (adapted from Bonnie Dorr) 1 Tracing Methods and their Parameters Issues to discuss Primitive type parameters Parameters passed to the constructor Parameters where a value (not variable) is the argument Non-primitive type parameters2 CMSC 131 Fall 2009 Jan Plane (adapted from Bonnie Dorr) 2 Parameters and Constructors Recall that methods / constructors can have parameters public Student (String newName, int IDDesired) { name = newName; id = IDDesired; tokenLevel = 3; } What is printed by the following? String newName = “Joe”; Student s = new Student(newName + “ Schmoe”, 123456789); System.out.println (s.name); System.out.println (newName); Joe Schmoe Joe CMSC 131 Fall 2009 Jan Plane (adapted from Bonnie Dorr) 3 Public Declarations public variables/methods and classes Keyword public used in declaration Every user of an object can access any public element Sometimes access should be restricted! To avoid giving object users unnecessary info (keep API small) To enforce consistency on instance variables3 Reference type Parameters Recall that methods / constructors can have parameters public int Student giveMore(Student s ) { if (numOfTokens > s.numOfTokens){ s.numOfTokens += 3; } else{ numOfTokens += 3; } } Trace Calling assume there are Student objects stu1 and stu2 Where stu1 has 5 tokens and stu2 has 12 tokens Called with stu1.giveMore(stu2); stu2.giveMore(stu1); CMSC 131 Fall 2009 Jan Plane (adapted from Bonnie Dorr) 4 CMSC 131 Fall 2009 Jan Plane (adapted from Bonnie Dorr) 5 How Does Java Evaluate Method / Constructor Calls? int newName = “Joe”; Student s = new Student (newName + “ Schmoe”, 123456789); 1. Arguments are evaluated using stack in effect at call site (place where method called) newName + “ Schmoe”, evaluates to Joe Schmoe 123456789 evaluates to 123456789 2. Stack frame (temporary addition to stack) created to associate method parameters with values 3. Stack frame put into stack 4. Body of method executed in modified stack 5. Stack frame removed from stack4 CMSC 131 Fall 2009 Jan Plane (adapted from Bonnie Dorr) 6 this a reference to the current object. (Only makes sense in a non-static method.) In an instance method, this is the object that is assumed easy to refer to members (data or methods) using the assumed object difficult to refer to the whole object without having a name to call it Only use when needed – using it all the time makes the code more difficult to read CMSC 131 Fall 2009 Jan Plane (adapted from Bonnie Dorr) 7 Private Declarations private variables, methods and classes private int tokenLevel = 3; Private variables / members cannot be accessed outside the class definition Declaring instance variables private means they can only be modified using public methods Now getters (accessors) and setters (mutators) are required5 CMSC 131 Fall 2009 Jan Plane (adapted from Bonnie Dorr) 8 What Should Be Public / Private? Class interface = API = public variables / methods Only make something public if there is a reason to Why? Encapsulation As long as interface is preserved, class can change without breaking other code The more limited the interface, the less there is to maintain Rule of thumb Make instance variables private Implement set / get methods Make auxiliary methods private CMSC 131 Fall 2009 Jan Plane (adapted from Bonnie Dorr) 9 Separate: API and the workings of the class Design so that you can change how the class works without having to change the API the only things in the API are things the user will absolutely need (make the interface as simple as possible) Demonstrations in Class Significantly Modifying the Student class – without changing the API (or the driver) The Cat class and its drivers with adding a copy constructor Project 3 API described – you are using those classes documentation / comments needed6 CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr) 10 Floating Point Calculations What will this print? public class SimpleMath { public static void main(String[] args) { if (3.9 - 3.8 == 0.1) { System.out.println("I am a very smart computer."); } else { System.out.println("I can't do simple arithmetic."); } } } I can’t do simple arithmetic. Why? Conversion of floating point to binary leads to precision errors! What can we do? CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr) 11 Floating Point Calculations (cont.) Two important rules: You can never use == to compare floating point values. Instead, check if two numbers are within a certain tolerance of each other. Never use floating point values to represent money, e.g., 3.52 to represent $3.52. Instead, use integer 352 to represent 352 pennies.7 CMSC 131Fall 2009 Jan Plane (adapted from Bonnie Dorr) 12 Documentation Types Three Styles // ... /* ... */ /** ... */ Two Purposes Internal – those reading code External – those using the class CMSC 131Fall 2009 Jan Plane (adapted from Bonnie Dorr) 13 Javadoc Documentation Standard When documenting a method, list exceptions that method can throw Use @exception tag Be sure to include unhandled exceptions that operations in method may throw Example: /** * Returns the year part of a date string * @param d date string in mm/dd/yyyy format * @return an integer representing the date * @exception IndexOutOfBoundsException * @exception NumberFormatException */ public static int getYear(String d) { … }8 CMSC 131 Fall 2009 Jan Plane (adapted from Bonnie Dorr) 14 Libraries in Java Library: implementation of useful routines that are shared by different programs Java mechanism for creating libraries: packages Package: group of related clases Example: java.util (contains Scanner class) To use a class from a package, you can use a fully qualified name (package name + class name): java.util.Scanner s = new
View Full Document