University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design II Lecture 7, Thu Jan 26 2006

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Mathematical Operations, Static Methods Lecture 9, Thu Feb 2.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Interfaces, Polymorphism II Lecture 21, Thu Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Advanced Class Design Lecture 19, Thu Mar
Road Map Introduction to object oriented programming. Classes
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Class Design Lecture 9, Mon Jan
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 11, Fri Jan
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Arrays II Lecture 15, Thu Mar
Chapter Day 7. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 7 Questions from last Class?? Problem set 1 Corrected  Good results 3.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Mathematical Operators, Static Methods Lecture 14,
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design Lecture 6, Tue Jan
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 20, Fri Mar
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Interfaces, Polymorphism Lecture 29, Fri Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 10, Wed Jan
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 12, Mon Feb
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Conditionals II Lecture 11, Thu Feb
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Objects, Class Design Lecture 8, Fri Jan
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 13, Wed Feb
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Writing Classes (Chapter 4)
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
Procedural programming in Java Methods, parameters and return values.
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Programming in Java (COP 2250) Lecture 10 Chengyong Yang Fall, 2005.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Objects as a programming concept
More Sophisticated Behavior
Chapter 7 User-Defined Methods.
Chapter 4: Writing Classes
Java Programming: Guided Learning with Early Objects
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Chapter 4: Writing Classes
Defining Classes and Methods
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Chapter 4: Writing classes
Chapter 6 Methods: A Deeper Look
Group Status Project Status.
Introduction to Classes and Objects
Chapter 4 Writing Classes.
Defining Classes and Methods
Outline Anatomy of a Class Encapsulation Anatomy of a Method
CS360 Client/Server Programming Using Java
Encapsulation September 13, 2006 ComS 207: Programming I (in Java)
Defining Classes and Methods
ITE “A” GROUP 2 ENCAPSULATION.
Classes and Objects Systems Programming.
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design II Lecture 7, Thu Jan based on slides by Paul Carter

Reading This Week n Chap 3 n Reading for next week n re-read Chapter

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 pm 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: :00 n Geography 100 & 200

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

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)

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

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."

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

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

Recap: UML n UML diagram for Die class we designed Die private public - sides: int + Die() + setSides(numSides: int): void + roll(): int

Objectives n understand how to design new classes using abstraction and encapsulation n understand how to implement new classes in Java

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

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

Implementing RollDice public class RollDice { public static void main ( String [] args) { }

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

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

Designing Point : UML n class to represent points in 2D space

Implementing Point public class Point { }

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

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 { }

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