University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design Lecture 6, Tue Jan 24 2006

Slides:



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

Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
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 1 Interfaces, Polymorphism II Lecture 21, Thu Mar
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Objects, Methods, Parameters, Input Lecture 5, Thu Jan
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
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Objects, Input Lecture 7, Wed Jan
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Classes, Encapsulation, Methods and Constructors
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
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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.
1 Creating Classes. 2 Writing Classes Thus far, we have mainly used existing classes in the Java library  (also main classes for executing) True object-oriented.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Introduction to Java Java SE 8 for Programmers Paul Deitel Harvey Deitel Deitel Developer Series 2014.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Writing Classes (Chapter 4)
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Java Classes Using Java Classes Introduction to UML.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Working With Objects Tonga Institute of Higher Education.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and 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.
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
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.
© 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.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
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.
More Sophisticated Behavior
3 Introduction to Classes and Objects.
Yanal Alahmad Java Workshop Yanal Alahmad
Maha AlSaif Maryam AlQattan
About the Presentations
Object Based Programming
Corresponds with Chapter 7
CSE 1020:Programming by Delegation
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Object Oriented Programming in java
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 Lecture 6, Tue Jan based on slides by Paul Carter

Reading This Week n Chap 3

String firstname = "Alphonse"; char thirdchar = firstname.charAt(2); object method parameter Recap: Methods and Parameters n Methods are how objects are manipulated n pass information to methods with parameters n inputs to method call n tell charAt method which character in the String object we're interested in n methods can have multiple parameters n API specifies how many, and what type n two types of parameters n explicit parameters given between parens n implicit parameter is object itself

Recap: Return Values n Methods can have return values n Example: charAt method result n return value, the character 'n', is stored in thirdchar String firstname = "kangaroo"; char thirdchar = firstname.charAt(2); n Not all methods have return values n No return value indicated as void return value object method parameter

Recap: Constructors and Parameters n Many classes have more than one constructor, taking different parameters n use API docs to pick which one to use based on what initial data you have animal = new String(); animal = new String("kangaroo");

Recap: Keyboard Input n Want to type on keyboard and have Java program read in what we type n store it in variable to use later n Scanner class does the trick n java.util.Scanner n nicer than System.in, the analog of System.out

Recap: Importing Packages n Collections of related classes grouped into packages n tell Java which packages to keep track of with import statement n again, check API to find which package contains desired class n No need to import String, System.out because core java.lang packages automatically imported

import java.util.Scanner; public class Echo { public static void main (String[] args) { String message; Scanner scan = new Scanner (System.in); System.out.println ("Enter a line of text: "); message = scan.nextLine(); System.out.println ("You entered: \"" + message + "\""); } Recap: Scanner Class Example n Print out the message on the display

Escape Characters n How can you 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

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

Creating Classes and Objects n So far you’ve seen how to use classes created by others n Now let’s think about how to create our own n Example: rolling dice n doesn’t exist already in Java API n we need to design n we need to implement n Start with two design principles

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

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

Approach n Apply principles of abstraction and encapsulation to classes we design and implement n same idea as examples from daily life n only in software

Designing Die Class n Blueprint for constructing objects of type Die n Think of manufacturing airplanes n build one blueprint n manufacture many instances from it n Consider two viewpoints n client programmer: want to use Die object in a program n designer: creator of Die class

Client Programmer n What operations does client programmer need? n what methods should we create for Die ?

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 ?

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

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

Public vs. Private Example Die myDie = new Die(); myDie. //not allowed!

Unified Modeling Language n Unified Modeling Language (UML) provides us with mechanism for modeling design of software n critical to separate design from implementation (code) n benefits of good software design n easy to understand, easy to maintain, easy to implement n What if skip design phase and start implementing (coding)? n code difficult to understand, thus difficult to debug n We’ll use UML class diagrams represent design of our classes n Once the design is completed, could be implemented in many different programming languages n Java, C++, Python,...

UML for Die n UML diagram representing Die class design

Encapsulation Diagram n Illustrate principle of encapsulation for Die A Die object client programmer

Implementing Die public class Die { }

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