DOC PREVIEW
UMD CMSC 131 - Lecture 7: Design and Classes

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 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 costs2CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)3Program DesignThere are many aspects to good designArchitectureModelingRequirements decompositionPseudo-codeIn 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 bookPseudo-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 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 designCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)7TestingSome testing is done by customer (acceptance testing)E.g. testing we do on your projects!You want to avoid errors surfacing during acceptance testingHow to avoid errors 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 semesterCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)8Questions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 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, EiffelWhat is an “object”?What does it mean for a language to be object-oriented?CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)10Objects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 2007Jan Plane (adapted from Bonnie Dorr)11Example of ObjectsBank accountState: account number, type, owner, balance, etc.Operations: deposit, withdrawal, transfer, balance inquiry, etc.StudentState: name, ID, date of birth, major...Operations: lookup grades, lookup class, compute tuition, compute age, …ScannerState: stream to read from (e.g System.in)Operations: get an int, double, byte, …StringState: sequence of charactersOperations: 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 / 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 in vo.m is how to invoke mSo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 objectCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)14Object-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methods6CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)15More QuestionsHow do you create objects?Do objects have “types”?Answer: classesCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)16Classes“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 onCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)17Student Class ExampleConceptually:Instance variables:String nameint IDint dateOfBirthString majorMethodsgetAgegetGradesetc.The actual class implementation will include code for the methodsThis describes a blueprint for student


View Full Document

UMD CMSC 131 - Lecture 7: Design and Classes

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture 7: Design and Classes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 7: Design and Classes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 7: Design and Classes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?