1 Lecture Set 5: Design and Classes This Set: Basics of program design Pseudo-code Objects and classes Heaps Garbage Collection More about Creating Objects and classes in Java Methods Constructors, Accessors, Mutators Equality Printing an object Unit testing CMSC 131 Spring 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr) 0 The Software Lifecycle (“waterfall”) 1 CMSC 131 Spring 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr) Requirements Design Coding Testing Deployment Maintenance Evolution What customers want What you plan to do Your program Did you meet requirements? Delivery (documentation, etc.) Bug fixes New versions The Software Lifecycle (actual) 2 CMSC 131 Spring 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr) Requirements Design Coding Testing Deployment Maintenance Evolution2 In the Real World, Requirements and Design Rule Getting requirements right is essential for successful projects FBI electronic case file (junked after $180m) IRS system upgrade in late 90s (junked after >$2bn) FAA air-traffic control (false starts, >$10bn spent) Good design makes other parts of lifecycle easier In “the real world” coding typically < 30% of total project costs A good design improves: efficiency (speed) efficiency (memory) ease of coding ease of debugging ease of expansion 3 CMSC 131 Spring 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr) Usability Matters 4 CMSC 131 Spring 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr) Program Design There are many aspects to good design Architecture Modeling Requirements decomposition Pseudo-code In this class we will focus on latter 5 CMSC 131 Spring 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr)3 What Is “Pseudo-code”? When developing a complex part of a program (an algorithm), one of the tools often useful is pseudo-code. It's not English, not programming language -- somewhere between. Captures the flow of the program without worrying about language-specific details. 6 CMSC 131 Spring 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr) Objects Bundles of (related) data (“state”) operations (“behavior”) Data often referred to as instance variables Operations usually called methods Invoking operations can change state (values stored in instance variables) 7 CMSC 131 Spring 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr) Sample Student Class 8 CMSC 131 Spring 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr) Name ID DOB Major State Methods getAge date → age getGrades sem., class → grades etc. etc.4 Sample Student Object 9 CMSC 131 Spring 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr) Kerry Keenan Name 444230695 ID 06-22-1987 DOB CMSC Major State Methods getAge date → age getGrades sem., class → grades etc. etc. Accessing State / Methods If o is an object v is an instance variable of the object m is a method of the object Then o.v is how to access the data v in o o.m() is how to invoke m in o So If you have already done String str = “Jan” Then str is a String str is an instance of an object! Methods of this object: equals, compareTo, etc. str.equals(), str.compareTo(), etc. invokes these methods on that object 10 CMSC 131 Spring 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr) Object-Oriented Programming Programs are collections of interacting objects Writing programs involves identifying what the objects should be and programming them Object-oriented languages provide features to ease object-oriented programming Defining objects involves indentifying state methods 11 CMSC 131 Spring 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr)5 Classes “Blueprints” (“templates”) for objects Classes include specifications of Instance variables (including types, etc.) to include in objects Implementations of methods to include in objects Classes can include other information also, as will be seen later static methods / instance variables public / private methods, instance variables And so on 12 CMSC 131 Spring 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr) Student Class Example Conceptually: Instance variables: String name int ID int dateOfBirth String major Methods getAge() getGrades() etc. The actual class implementation will include code for the methods This describes a blueprint for student objects 13 CMSC 131 Spring 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr) StudentClassExample1/Student.java How Are Objects Created? In Java: using new Recall: Scanner sc = new Scanner(System.in); Invoking new: creates fresh copies of instance variables in the “heap” returns the “address” where the fresh variables are stored Heap? Address? 14 CMSC 131 Spring 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr) Driver.java6 Heap = “Fresh Memory” While a program is running, some memory is used to store variables Terminology: stack We have been representing the stack as a table, e.g. Rest of memory is called heap and can be used for other purposes, including storing new objects 15 CMSC 131 Spring 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr) Variable Value x 3 y 4.5 Main Memory Stack grows, shrinks during program execution (why?) So does “allocated heap” (part of heap in use) Unallocated part of heap is called “free” 16 CMSC 131 Spring 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr) Allocated Heap Stack Free Heap Object Creation New space allocated in heap to store instance variables Reference (= address) to this space is returned Scanner sc = new (…); 17 CMSC 131 Spring 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr) Allocated Heap Stack Free Heap sc7 Strings Are Objects Where is new in String name = “Narita”; ? Java provides it! String is special because it is used so often Java automatically “fills in” new You can too: String name = new String(“Narita”); 18 CMSC 131 Spring 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr) In Java, 9 Sorts of Variables 8 primitive types Types are the 8 built-ins (int, byte,
View Full Document