Lecture Set 5 Design and Classes This Set Methods and Parameter Passing 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 CMSC 131 Spring 2007 Printing an object Jan Plane adapted from Bonnie Dorr methods defining and invoking One useful type of method for this project defined within the FlagMaker class invoked from within another method that is defined in the same class we ll do a lot more variations later Defined based on a name and a list of parameters public static void name parameterlist body Invoked by stating its name and giving an argument for each element of the parameter list name argumentlist CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 1 method information parameters and arguments parameter list argument list type name for each item in the list e g MyGrid grid char where expression e g grid t for each item in the list Matched between the arguments and the parameters based on position in the list CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 2 1 The Software Lifecycle Requirements What customers want Bug fixes Design What you plan to do New versions Coding Maintenance Deployment Testing Your program Evolution Delivery documentation etc Did you meet requirements CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 3 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 CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 4 Program Design There are many aspects to good design Architecture Modeling Requirements decomposition Pseudo code In this class we will focus on latter CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 5 2 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 CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 6 Example Requirement email program that allows you to send a message either to one person or to your whole address book Pseudo code prompt Enter message input message prompt Send to whole address book input answer if answer no prompt Enter recipient input recipient send message to recipient otherwise for each recipient r in address book send message to r CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 7 What Is Pseudo Code cont NOT English NOT a program Something in between Captures the logic and flow of the algorithm Note that pseudo code could be translated into ANY programming language not just Java Good programming practice Write pseudo code first and keep it as your design Include it as comments in your code to help you connect code to design CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 8 3 Testing Some testing is done by customer acceptance testing How to avoid errors during acceptance testing E g testing we do on your projects You want to avoid errors surfacing during acceptance testing Test thoroughly before release Cover all cases in code if else branches etc Identify corner cases extreme values of inputs and test with these We will study testing more later in semester CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 9 Questions What is System in System out println Why use str equals cat to compare equality of String str and cat Is the similarity of the notations System out println str equals sc nextInt important or coincidental CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 10 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 CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 11 4 Sample Student Object State Name ID Methods Kerry Keenan 444230695 DOB 06 22 1987 Major CMSC getAge getGrades date age semester grades etc etc CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 12 Accessing State Methods If o is an object v is an instance variable of the object m is a method of the object Then So o v is how to access the data v in o o m is how to invoke m in o System is an object with out an instance variable out is also an object with println a method System out println is how to access this method Suppose str is a String str is an object Methods of this object equals compareTo etc str equals str compareTo etc invokes these methods on that object CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 13 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 CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 14 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 CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 15 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 CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 16 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 CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 17 6 Heap Fresh Memory While a program is running some memory is used to store variables Terminology stack We have been representing stack as table e g
View Full Document