University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Class Design Lecture 9, Mon Jan 24 2010

Slides:



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

The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Interfaces, Polymorphism II Lecture 21, 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 More Class Design Lecture 11, Fri Jan
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 Class Design Lecture 6, Tue Jan
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
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.
Aalborg Media Lab 26-Jun-15 Software Design Lecture 5 “ Writing Classes”
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 30, Mon Mar
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
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.
MIT AITI 2003 Lecture 7 Class and Object - Part I.
Writing Classes (Chapter 4)
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
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.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Chapter 4 Writing Classes Part 2. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Classes A class can contain data declarations and method declarations.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 4 Introduction to Classes, Objects, Methods and strings
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
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.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
OOP Basics Classes & Methods (c) IDMS/SQL News
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design II Lecture 7, Thu Jan
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 3: Using Methods, Classes, and Objects
Road Map Introduction to object oriented programming. Classes
Chapter 4: Writing Classes
Introduction to Computer Programming
Defining Your Own Classes Part 1
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
Classes & Objects: Examples
Chapter 4 Writing Classes.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Encapsulation September 13, 2006 ComS 207: Programming I (in Java)
Object Oriented Programming in java
Creating and Using Classes
CSG2H3 Object Oriented Programming
Chapter 5 Classes.
Presentation transcript:

University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Class Design Lecture 9, Mon Jan borrowing from slides by Paul Carter and Wolfgang Heidrich

2 News n If you have a midterm conflict with first midterm, let me know by end of day today at the latest n Mon 2/8 6:30-8pm

3 Reading Assignments n Chapter 3

4 Recap: References vs Values n You copy a CD for your friend. Her dog chews it up. Does that affect your CD? n no: different values n like primitive types n You and your friend start eating a slice of cake on one shared plate. You get up to make a cup of tea. Her dog jumps on the table and eats the cake. Does that affect your half of the dessert? n yes: both forks reference the same plate n like objects

5 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

6 Recap: Encapsulation 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 Cell phone example n inner workings encapsulated in hand set n cell phone users can’t get at them n intuitive interface makes using them easy n without understanding how they actually work

7 Recap: Designing Die Class n Blueprint for constructing objects of type Die n Think of manufacturing airplanes or dresses or whatever n design one blueprint or pattern n manufacture many instances from it n Consider two viewpoints n client programmer: wants to use Die object in a program n designer: creator of Die class

8 Recap: Designer n Decide on inner workings n implementation of class n Objects need state n attributes that distinguish one instance from another n many names for these n state variables n fields n attributes n data members n what fields should we create for Die ?

9 Implementing Die /** Provides a simple model of a die (as in pair of dice). */ public class Die { }

10 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)

11 Implementing Die /** Provides a simple model of a die (as in pair of dice). */ public class Die { }

12 return Statement n Use the return statement to specify the return value when implementing a method: int addTwoInts (int a, int b) { return a+b; } n Syntax: return expression ; n The method stops executing at that point and “returns” to caller.

13 Implementing Die /** Provides a simple model of a die (as in pair of dice). */ public class Die { }

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

15 Public vs Private n public keyword indicates that something can be referenced from outside object n can be seen/used by client programmer n private keyword indicates that something cannot be referenced from outside object n cannot be seen/used by client programmer n Let’s fill in public/private for Die class

16 Public vs. Private Example public class Die {... public int roll()... private void cheat(int nextRoll)... }

17 Public vs. Private Example Die myDie = new Die(); int result = myDie.roll();// OK myDie.cheat(6); //not allowed!

18 Implementing Die /** Provides a simple model of a die (as in pair of dice). */ public class Die { }

19 Trying It Out! n Die class has no main method. n Best is to write another class that instantiates some objects of your new class and tries them out. n Sometimes called a “tester” or “testbench”

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