CPSC 2150 - DAY 5 SEPTEMBER 1, 2016 ================================================================================= Black Box Testing ----------------- When test plans are based entirely on contracts Don't change if implementation code changes White box testing ----------------- when you look at the code as well as the contract. Tracing and Debugging .Objective - Locate errors using test inputs .Approach - analyze the code on sample test cases .Tracing - analyze, but do not execute code .debugging - execute code Objects and classes ------------------- one general principle: information hiding AKA encapsulation To keep related data and operations together so they stay in "sync" .Modifying some part of data, without corresponding changing others might make it nonsensical Date class example: .One possible Date class representation .int day .int month .int year .Representation invariant for valid Date .day, month and year should be in sync .If day, month, and year can be modified arbitrarily, invalid dates could be represented Through public methods provided by a class, access to the private representation can be controlled Date class methods either may not let you set the day, month and year independ. Object-Oriented Programming --------------------------- Fundamental component is an object .A running program is a collection of objects An object encapsulates: .State(ie data) .Behavior(how state changes) Each object is an instance of a class .Class declaration is a blueprint of a class Declare one class per file Give file the same name as the class declaration it contains class Pencil { boolean hasEraser; String color = "red"; int length; int sharpen (int amount) { .. } String getDescription() { .. } } note that the invariant is a logical comment. Here, haseraser is true iff length >=10. Members ------- Two kinds of members in a class declaration Explicit object creation with new(); Unlike C/C++, memory is not explicitly freed .References just go out of scope .Automatic garbage collection (eventually) deletes unreachable objects Initialization of an Object's Fields .Implicit: Default initial values based on type .Explicit: initialization with field declaration .Special method: constructior .Syntax: name is same as class, no return type. Visibility ---------- private public Group member declarations by visibility No fields should be public Don't provide public accessor methods for getting and setting private fields .there's no point in making them private in the first place! Constructior arguments that are used directly to set object fields can be given the same name as the field. Method Overloading .A class can have more than one menthod with the same name as long as they have different paramenter lists The compiler knows which method to used bsed on the type of parameter passed Differing only return type is NOT allowed!