DOC PREVIEW
UMD CMSC 131 - Lecture 25: Design

This preview shows page 1-2-21-22 out of 22 pages.

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

Unformatted text preview:

10/27/2006 CMSC 131 Fall 2006Rance Cleaveland©2006 Univeristy of MarylandLecture 25:DesignLast time:1. Interfaces (cont.)2. WrappersToday:1. Project #5 is due 10/31 at 11 pm2. Program designs: algorithms, interfaces, use casesCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland1Project #5 Assigned! Project due Tuesday, 10/31 at 11 pm Project is closed You must complete the project by yourself Assistance can only be provided by teaching assistants (TAs) and instructors You must not look at other students' code Start now! Read entire assignment from beginning to end before starting to code Check out assignment now from CVS Follow the instructions exactly, as much of grading is automatedCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland2Conditional Expressions if-else can be used to create conditional statements Java also has a conditional expression operation Form: <bexp> ? <exp1> : <exp2> Meaning If <bexp> evaluates to true … … then <exp1> is evaluated … otherwise <exp2> is evaluated Exampleint x = 0;int y = (x < 0) ? x+1 : x+2;System.out.println(y);2 is printed Difference between if-else, <bexp> ? <exp1> : <exp2> if-else is for statements <bexp> ? <exp1> : <exp2> is for expressions Can’t e.g. say int y = if (x<0) x+1 else x+2; (x < 0) ? y = x+1; : y = x+2;CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland3Coding vs. Software Design Coding: writing of (Java) code to implement classes, methods, etc. Projects so far have been primarily coding We have told you what to code Design: determination of what to code What classes are needed?  How should classes interact? What methods belong in each class? How should method functionality be implemented?High-levelLow-levelCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland4Low-Level Design:Pseudo-Code and AlgorithmsWe have already talked about pseudo-code as a design technique NOT English NOT a program Something in-between Captures the logic, flow of desired code Note 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 problems Algorithms are often coded as single methodsCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland5Example: Linear Search Recall findMin from last time Given a non-empty array … … find the smallest element Algorithm 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 min The (polymorphic) findMin method is the Java code implementing linear searchCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland6findMinstatic Comparable findMin (Comparable[] a){Comparable min = a[0];for (int i=1; i < a.length; i++)if (a[i].compareTo(min) < 0)min = a[i];return min;}CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland7Concerns at the Algorithmic Level of DesignCorrectnessDoes my algorithm correctly solve the problem? EfficiencyIs my algorithm fast enough for the job? ClarityIs my algorithm understandable?CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland8Interfaces and Design Next level up the design hierarchy: what methods should go in classes? This information can be captured using interfaces These interfaces can also be used to identify opportunities for polymorphism (reusable code)CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland9Example: Casino Suppose we are writing a simple casino program Two games: Roulette and Dice Roulette bets: 1-36, Even, Odd Dice bets: 1-6 Rough structure of classes given to rightCasinoDice RouletteCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland10Designing a Casino Game InterfaceWhat methods should be in interface between Casino and Dice / Roulette? Methods should support a generic “game driver” in Casino class Methods should be common to both Dice, Roulette What notions are common to Dice, Roulette? Players make types of bets Players bet a certain amount Players play against the house (not each other) There is either a payoff or a lossCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland11Interface Game.javapublic interface Game {String getName (); // Name of gameString[] getValidBets (); // Types of betsboolean isValidBet (String b); // Is bet valid?void setBetAmount (int amt); // Set bet amountvoid setBet (String bet); // Set bet typeint play (); // Play the game}CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland12Rules of Thumb Keep interfaces small Think carefully about operations needed “between classes” Use interfaces to support polymorphism (and keep code size down)CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland13A Generic Game Method in CasinoSee play method in Casino.javaCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland14Upper Levels of Software DesignWhere do ideas for classes, interactions between classes come from? Software development part of larger system design process System design requires identifying what system users expect system to do These user requirements often suggest system components and how they fit together First part of software design: understand system designCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland15System Design: What Is It? System design is concerned with: coordinating a collection of entities… … to achieve a complex process Each entity has its own responsibilities to the others to achieve an overall objective E.g. Restaurant Entities Chef, owners, waiters, etc. System RestaurantCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland16System Examples Classroom 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, …CMSC 131 Fall


View Full Document

UMD CMSC 131 - Lecture 25: Design

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 25: Design
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 25: Design 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 25: Design 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?