1CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)Lecture 7:Design and ClassesLast time:1.More assignment operators2.Precedence and short-circuiting3.Type castingToday:1.Basics of program design 2.Pseudo-code3.Objects and classesCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)1The 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)2In 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 costs2CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)3Program DesignThere are many aspects to good designArchitectureModelingRequirements decompositionPseudo-codeIn this class we will focus on latterCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)4What 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)5Example: 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 r3CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)6What 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 designCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)7TestingSome 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)8QuestionsWhat 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?4CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)9Answers… will come, but are based on fact that Java is an object-oriented (OO) languageOther OO languages: C++, Smalltalk, EiffelWhat is an “object”?What does it mean for a language to be object-oriented?CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)10ObjectsBundles 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)CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)11Example of ObjectsBank accountState: account number, type, owner, balance, etc.Operations: deposit, withdrawal, transfer, balance inquiry, etc.StudentState: name, ID, date of birth, major...Operations: lookup grades, lookup class, compute tuition, compute age, …ScannerState: stream to read from (e.g System.in)Operations: get an int, double, byte, …StringState: sequence of charactersOperations: comparison test, equality test, (much more), …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 in vo.m is how to invoke mSoSystem 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)15More QuestionsHow do you create objects?Do objects have “types”?Answer: classesCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)16Classes“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)17Student Class ExampleConceptually:Instance variables:String nameint IDint dateOfBirthString majorMethodsgetAgegetGradesetc.The actual class implementation will include code for the methodsThis describes a blueprint for student
View Full Document