DOC PREVIEW
UMD CMSC 131 - Lecture Set 5: Design and Classes

This preview shows page 1-2-3-4-5 out of 15 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 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 15 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 15 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 15 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 15 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)Lecture Set 5:Design and ClassesThis 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Printing 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 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 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 argumentsparameter listtype name for each item in the liste.g. (MyGrid grid, char where)argument listexpression for each item in the liste.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 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 expansionCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)5Program DesignThere are many aspects to good designArchitectureModelingRequirements decompositionPseudo-codeIn 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 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 rCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)8What 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 design4CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)9Testing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)10Questions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?CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)11Objects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)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 v in oo.m is how to invoke m in o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)15Classes“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)16Student 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 objectsCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)17How Are


View Full Document

UMD CMSC 131 - Lecture Set 5: 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 Set 5: 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 Set 5: 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 Set 5: 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?