CMSC 131 Fall 2007Jan Plane (Adapted from Bonnie Dorr)Lecture 21:Algorithms and DesignLast time:1. Interfaces2. WrappersToday:1. DesignCMSC 131 Fall 2007Jan Plane (Adapted from Bonnie Dorr)1Putting all your eggs in one basketProblem: I have 16 baskets full of 12 eggs each; I want to combine them or “_____”. ☺Algorithm #1 ??Combine #1 and #2Combine result with #3Combine result with #4; etc.Algorithm #2 ??Combine #1, #2; combine #3, #4; combine #5, #6…Combine <1,2> with <3,4>; Combine <5,6> with <7,8>…Combine <1,2,3,4> with <5,6,7,8>Combine last two …CMSC 131 Fall 2007Jan Plane (Adapted from Bonnie Dorr)2Algorithmic Efficiency AnalysisMeasuring which is better for time.What if the time required for the merging machine is constant?both have 15 calls to the merging machine when there are 16 baskets to mergewhat if the number of baskets is another value?What if the time required for the merging machine is dependent on the number of eggs being merged?for example 1 second per egg (i.e. merging two baskets of 12 each takes 24 seconds)this takes more math when you want to generalized on the number of basketsCMSC 131 Fall 2007Jan Plane (Adapted from Bonnie Dorr)3Big-O NotationCategories of formulasWhat takes over as n approaches infinityO(log n)O(n)O(n * log n)O(n2)O(n2* log n)CMSC 131 Fall 2007Jan Plane (Adapted from Bonnie Dorr)4Coding vs. Software DesignCoding: writing of (Java) code to implement classes, methods, etc.Projects so far have been primarily codingWe have told you what to codeDesign: determination of what to codeWhat classes are needed? How should classes interact?What methods belong in each class?How should method functionality be implemented?High-levelLow-levelCMSC 131 Fall 2007Jan Plane (Adapted from Bonnie Dorr)5Low-Level Design:Pseudo-Code and AlgorithmsWe have already talked about pseudo-code as a design techniqueNOT EnglishNOT a programSomething in-betweenCaptures the logic, flow of desired codeNote that pseudo-code could be translated into any programming language (not just Java)Pseudo-code is used to represent algorithms = step-by-step solutions to problemsAlgorithms are often coded as single methodsCMSC 131 Fall 2007Jan Plane (Adapted from Bonnie Dorr)6Example: Linear SearchRecall findMin from last timeGiven a non-empty array …… find the smallest elementAlgorithm used is called linear searchIn pseudo-code:set variable min to initial element of arrayfor each subsequent element in arraycompare element to minif element is less than min, assign its value to minThe (polymorphic) findMin method is the Java code implementing linear searchCMSC 131 Fall 2007Jan Plane (Adapted from Bonnie Dorr)7Concerns at the Algorithmic Level of DesignCorrectnessDoes my algorithm correctly solve the problem?EfficiencyIs my algorithm fast enough for the job?ClarityIs my algorithm understandable? and is it implementable?CMSC 131 Fall 2007Jan Plane (Adapted from Bonnie Dorr)8Interfaces and DesignNext level up the design hierarchy: what methods should go in classes?This information can be captured using interfacesThese interfaces can also be used to identify opportunities for polymorphism (reusable code)CMSC 131 Fall 2007Jan Plane (Adapted from Bonnie Dorr)9Designing a Twenty Questions Game InterfaceWhat methods should be in interface between Twenty Questions and Oracle / Politician / Dog?Methods should support a generic “game driver” in Twenty Questions classMethods should be common to both Oracle, Politician, DogWhat notions are common to Oracle, Politician, Dog?Say nameAnswer Y/N questionsTaunting ☺CMSC 131 Fall 2007Jan Plane (Adapted from Bonnie Dorr)10Rules of ThumbKeep interfaces smallThink carefully about operations needed “between classes”Use interfaces to support polymorphism (and keep code size down)CMSC 131 Fall 2007Jan Plane (Adapted from Bonnie Dorr)11Upper Levels of Software DesignWhere do ideas for classes, interactions between classes come from?Software development part of larger system design processSystem design requires identifying what system users expect system to doThese user requirements often suggest system components and how they fit togetherFirst part of software design: understand system designCMSC 131 Fall 2007Jan Plane (Adapted from Bonnie Dorr)12What is an algorithm?The method used to solve a particular problem is called an algorithm.Example: Make a peanut butter and jelly sandwich:Get a loaf of breadRemove two slicesGet a jar of peanut butterGet a knifeOpen the jarUsing the knife, get some peanut butter and spread it on one slice…blah, blah, blahThere is essentially one sequential process being described.CMSC 131 Fall 2007Jan Plane (Adapted from Bonnie Dorr)13System Design: What Is It?System design is concerned with:coordinating a collection of entities…… to achieve a complex processEach entity has its own responsibilities to the others to achieve an overall objectiveE.g. Running a restaurant involves a coordinated interaction of many entities within one system:Entities Chef, owners, waiters, etc.System RestaurantCMSC 131 Fall 2007Jan Plane (Adapted from Bonnie Dorr)14Other Examples of SystemsClassroom environment: Lecturers, TAs, students, …Library: Circulation (checkout and return), indexing services (online catalogue), library users, book buyers, shelvers, …Pharmacy: Patients (and medical records), pharmacists, doctors, drug retailers, the pharmacy (products in stock), …Video game: Race cars, motorcycles, warriors, space ships, death squads, monsters, aliens, mutants, guns, swords, weapons of mass destruction, cute Japanese cartoon animals with huge eyes, …Pikachu visits Doom3CMSC 131 Fall 2007Jan Plane (Adapted from Bonnie Dorr)15Essential QuestionsChallenges: System design is very hard. Once the number of entities and interactions becomes large, it is very hard to foresee all the possible consequences of these interactions.Essential Questions:What is the desired behavior of the program (as a whole)?system design - overviewWhat are the entities that produce this behavior?classes or objectsHow do these entities interact?API for each classHow does each one work?algorithm for each taskCMSC 131 Fall 2007Jan Plane (Adapted from Bonnie
View Full Document