1CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)Lecture Set 5:Design and ClassesThis Set:Methods and Parameter PassingBasics of program design Pseudo-codeObjects and classesHeapsGarbage CollectionMore about Creating Objects and classes in JavaMethodsConstructors, Accessors, MutatorsEqualityPrinting an objectCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)1methods: defining and invoking One useful type of method for this project:defined within the FlagMaker classinvoked from within another method that is defined in the same classwe’ll do a lot more variations laterDefined based on a name and a list of parameterspublic static void name(parameterlist){body}Invoked by stating its name and giving an argument for each element of the parameter listname(argumentlist);CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)2method information:parameters and argumentsparameter listtype name for each item in the liste.g. (MyGrid grid, char where)argument listexpression for each item in the liste.g. (grid, ‘t’)Matched between the arguments and the parameters based on position in the list2CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)3The Software LifecycleRequirementsDesignCodingTestingDeploymentMaintenanceEvolutionWhat customers wantWhat you plan to doYour programDid you meet requirements?Delivery (documentation, etc.)Bug fixesNew versionsCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)4In the Real World, Requirements and Design RuleGetting requirements right is essential for successful projectsFBI 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 easierIn “the real world” coding typically < 30% of total project costsA good design improves:efficiency (speed)efficiency (memory)ease of codingease of debuggingease of expansionCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)5Program DesignThere are many aspects to good designArchitectureModelingRequirements decompositionPseudo-codeIn this class we will focus on latter3CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)6What 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 2007Jan Plane (adapted from Bonnie Dorr)7Example: Requirement: email program that allows you to send a message either to oneperson, or to your whole address bookPseudo-code:prompt "Enter message: "input messageprompt "Send to whole address book? "input answerif answer == "no“prompt "Enter recipient: "input recipientsend message to recipientotherwisefor each recipient, r, in address booksend message to rCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)8What Is Pseudo-Code? (cont.)NOT EnglishNOT a programSomething in-betweenCaptures the "logic" and "flow" of the algorithmNote that pseudo-code could be translated into ANY programming language (not just Java)Good programming practiceWrite pseudo-code first and keep it as your designInclude it as comments in your code to help you connect code to design4CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)9TestingSome testing is done by customer (acceptance testing)E.g. testing we do on your projects!You want to avoid errors surfacing during acceptance testingHow to avoid errors during acceptance testing?Test thoroughly before releaseCover all cases in code (if/else branches, etc.)Identify “corner cases” (extreme values of inputs) and test with theseWe will study testing more later in semesterCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)10QuestionsWhat is System in System.out.println()?Why use str.equals(“cat”) to compare equality of String str and “cat”?Is the similarity of the notationsSystem.out.println()str.equals()sc.nextInt()important, or coincidental?CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)11ObjectsBundles of (related)data (“state”)operations (“behavior”)Data often referred to as instance variablesOperations usually called methodsInvoking operations can change state (values stored in instance variables)5CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)12Sample Student ObjectKerry KeenanName444230695ID06-22-1987DOBCMSCMajorState MethodsgetAgedate → agegetGradessemester → gradesetc.etc.CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)13Accessing State / MethodsIf o is an object v is an instance variable of the objectm is a method of the objectTheno.v is how to access the data v in oo.m is how to invoke m in oSoSystem is an object, with out an instance variableout is also an object, with println a methodSystem.out.println is how to access this method!Suppose str is a Stringstr is an object!Methods of this object: equals, compareTo, etc.str.equals, str.compareTo, etc. invokes these methods on that objectCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)14Object-Oriented ProgrammingPrograms are collections of interacting objectsWriting programs involves identifying what the objects should be and programming themObject-oriented languages provide features to ease object-oriented programmingDefining objects involves indentifyingstatemethods6CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)15Classes“Blueprints” (“templates”) for objectsClasses include specifications ofInstance variables (including types, etc.) to include in objectsImplementations of methods to include in objectsClasses can include other information also, as will be seen laterstatic methods / instance variablespublic / private methods, instance variablesAnd so onCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)16Student Class ExampleConceptually:Instance variables:String nameint IDint dateOfBirthString majorMethodsgetAgegetGradesetc.The actual class implementation will include code for the methodsThis describes a blueprint for student objectsCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)17How Are
View Full Document