Download presentation
Presentation is loading. Please wait.
Published byHolly Dixon Modified over 8 years ago
1
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design II Lecture 7, Thu Jan 26 2006 http://www.cs.ubc.ca/~tmm/courses/cpsc111-06-spr based on slides by Paul Carter
2
Reading This Week n Chap 3 n Reading for next week n re-read Chapter 4.1-4.6
3
News n Assignment 1 due Tue Jan 31 5pm n Extra TA hours in ICICS 008 to answer questions n Thu Jan 24 (today!) 4-6pm n Olivia Siu n Fri Jan 25 5-7pm n Ciaran Llachlan Leavitt n Sat Jan 26 12:30-2:30pm n Simon Hastings n Weekly questions due today n Stay tuned for bboard postings with (some) answers n Midterm reminder: Tue Feb 7, 18:30 - 20:00 n Geography 100 & 200
4
Recap: Escape Characters n How can we make a String that has quotes? n String foo = “oh so cool”; n String bar = “oh so \”cool\”, more so”; n Escape character: backslash n general principle
5
Recap: Random Numbers n Random class in java.util package n public Random() n Constructor n public float nextFloat() n Returns random number between 0.0 (inclusive) and 1.0 (exclusive) n public int nextInt() n Returns random integer ranging over all possible int values n public int nextInt( int num ) n Returns random integer in range 0 to (num-1)
6
Recap: Abstraction n Abstraction: process whereby we n hide non-essential details n provide a view that is relevant n Often want different layers of abstraction depending on what is relevant
7
Recap: Encapsulation and Info Hiding n Encapsulation: process whereby n inner workings made inaccessible to protect them and maintain their integrity n operations can be performed by user only through well-defined interface. n aka information hiding n Hide fields from client programmer n maintain their integrity n allow us flexibility to change them without affecting code written by client programmer n Parnas' Law: n "Only what is hidden can by changed without risk."
8
Recap: Designing Classes n Blueprint for constructing objects n build one blueprint n manufacture many instances from it n Consider two viewpoints n client programmer: want to use object in program n what public methods do you need n designer: creator of class n what private fields do you need to store data n what other private methods do you need
9
Recap: UML n UML diagram representing class design Classname private public - field: type - method(): return type + Classname() + field: type + method(): return type + method(param1 type, param2 type): return type
10
Recap: UML n UML diagram for Die class we designed Die private public - sides: int + Die() + setSides(numSides: int): void + roll(): int
11
Objectives n understand how to design new classes using abstraction and encapsulation n understand how to implement new classes in Java
12
Implementing Die n Last time n designed UML diagram n first draft of implementation n it compiled, but untested! n This time n refine implementation n test and debug implementation
13
Using Die n Change hats from Die designer to Die user n Roll two dice n print each value, and sum n Design and implement RollDice driver: class with main method
14
Implementing RollDice public class RollDice { public static void main ( String [] args) { }
15
Separation and Modularity n Design possibilities n Die and RollDie as separate classes n one single class that does it all n Separation allows code re-use through modularity n another software design principle n One module for modeling a die: Die class n Other modules can use die or dice n we wrote one, the RollDice class n Modularization also occurs at file level n modules stored in different files n also makes re-use easier
16
Control Flow Between Modules n So far, easy to understand control flow: order in which statements are executed n march down line by line through file n Now consider control flow between modules int rollResult; myDie.setSides(); rollResult = myDie.roll(); public int roll() { … } public void setSides() { … } Client code Die class methods
17
Designing Point : UML n class to represent points in 2D space
18
Implementing Point public class Point { }
19
Formal vs. Actual Parameters n formal parameter: in declaration of class n actual parameter: passed in when method is called n variable names may or may not match n if parameter is primitive type n call by value: value of actual parameter copied into formal parameter when method is called n changes made to formal parameter inside method body will not be reflected in actual parameter value outside of method n if parameter is object: covered later
20
Scope n Fields of class are have class scope: accessible to any class member n in Die and Point class implementation, fields accessed by all class methods n Parameters of method and any variables declared within body of method have local scope: accessible only to that method n not to any other part of your code n In general, scope of a variable is block of code within which it is declared n block of code is defined by braces { }
21
Key Topic Summary Borrowed phrasing from Steve Wolfman n Generalizing from something concrete n fancy name: abstraction n Hiding the ugly guts from the outside n fancy name: encapsulation n Not letting one part ruin the other part n fancy name: modularity n Breaking down a problem n fancy name: functional decomposition
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.