Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the.

Slides:



Advertisements
Similar presentations
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Advertisements

Overview of Data Structures and Algorithms
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.
1 Objects, Classes, and Packages “Static” Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in.
CS 211 Inheritance AAA.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Objects and Classes Objects and Classes objects defining classes method declaration object references and aliases instance variables encapsulation and.
ACM/JETT Workshop - August 4-5, Object-Oriented Basics & Design.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
OOP Languages: Java vs C++
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
Writing Classes (Chapter 4)
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Module 7: Object-Oriented Programming in Visual Basic .NET
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
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.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Chapter 4 Introduction to Classes, Objects, Methods and strings
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.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 Strings, Classes, and Working With Class Interfaces CMPSC 122 Penn State University Prepared by Doug Hogan.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Chapter 11: Abstract Data Types Lecture # 17. Chapter 11 Topics The Concept of Abstraction Advantages of Abstract Data Types Design Issues for Abstract.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Polymorphism and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.
Data Structures and Algorithms revision
Programming Logic and Design Seventh Edition
3 Introduction to Classes and Objects.
Chapter 3: Using Methods, Classes, and Objects
Computer Science II Exam 1 Review.
Object Based Programming
Polymorphism and access control
Defining Classes and Methods
Chapter 8 Classes User-Defined Classes and ADTs
Chapter 11: Inheritance and Composition
Introduction to Object-Oriented Programming
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Chapter 1: Object Oriented Paradigm

1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the user myCoin.flip(); myCoin.getValue(); – from the details that make the abstraction work if (Math.random() > 0.5) side = "heads"; else side="tails"; Accomplished by organizing code into – Interface: details important to user – Implementation: “unimportant” details hidden from user Who is "user?" – person who writes the application…and/or…YOU !

Strings represented on Macs vs Linux

1.2 The Object Model A convenient design method – program data managed by objects – objects manage internal data, determines state point object manages its x,y coordinates coin object manages its side indicator student record object manages it's name, ID, GPA – this helps manage chaos as complexity grows reduces unexpected linkages between parts of program

Primary focus of class how we implement and evaluate objects with methods that are logically complex how we can use the objects we create objects mainly will be data structures, – our primary interest! occasionally we will develop control structures that manipulate other objects

1.3 Object-Oriented Terminology Applied to a Ratio class – see Demo1 in mod1 download – encapsulation – object – instance – class – fields – methods – constructor – utility methods (reduce, gcd) – static methods (gcd)

1.4 A Special-Purpose Class: A Bank Account Manages data: – balance and account number (or account name) Performs: – getAccount – getBalance – deposit – withdraw

BankAccount equals() method public boolean equals(Object other) // pre: other is a valid bank account // post: returns true if this bank account is the same as other { BankAccount that = (BankAccount)other; // two accounts are the same if account numbers are the same return this.account.equals(that.account); }

Class ObjectObject Every class is a descendant of Object Defines fundamental methods – equals() same data as another object – toString() string representation for display – clone() makes an identical copy If you don't define your own versions of these, automatic (usually defective) ones will be provided

10 Memory Model of BankAccount object Memory allocated by the declaration BankAccount jane; jane = new BankAccount(“J. Doe”, ); BankAccount jane; jane = new BankAccount(“J. Doe”, ); null 100 J. Doe account balance jane The number 100 indicates the memory location where the object resides

11 Shallow vs Deep Copy There are two ways to “copy” an object – Deep copy creates a clone of the object The objects data values are copied into the clone – Shallow copy creates reference to the object The location of the object is copied into a reference variable After a – Deep copy, two distinct objects exist Can change one object’s data without changing the other’s – Shallow copy two references to one object exists

12 Code of Shallow and Deep Copies Shallow copy is similar to copying primitives jon = jane; Deep copy – requires a class method (e.g., in the BankAccount class) public BankAccount clone() { BankAccount copy = new BankAccount(); copy.account = account; copy.balance = balance; return copy; } – Invoked as jon = jane.clone(); //copy jane into jon

13 Memory Model of Two Types of Copies jane jon location 200 location 100 jon j. doe j. doe deep copy A Deep Copy of the Object jane into the Object jon account balance account balance jane jon location shallow copy A Shallow Copy of the Object jane into the Object jon j. doe

Using Scanner and File Scanner – a class representing objects that read input streams or files and extracts primitive data types from them Scanner File – a class representing a path to a location on disk or other media File We will now apply these to our BankAccount app

1.5 A General-Purpose Class: An Association very general class represents a connection between two kinds of objects protected Object theKey; // the key of the key-value pair protected Object theValue; // the value of the key-value pair Association can be used to link – ID (key) with an employee record – Two words from different languages (pig latin) – Methods: setValue, setKey, getValue, getKey

Protected vs Private Usually, it is best to restrict methods as much as possible. – private: member is only used within class itself – protected: member is available to other classes in a package or to subclasses through inheritance – public: member is available to any user

An app that uses Association atinLay – a Pig Latin translatorPig Latintranslator This program uses the argument list set up in main, as would be obtained from a “command line” execution We (or you!) could convert it to read from the System.in console with a Scanner object

1.6 Sketching an Example: A Word List Suppose we want to develop a Hangman app – The program should : 1.Select a random word from a list of words (TODO!) 2.Receive user guesses and draw stick figure (demo) We propose to develop a WordList class to help us with item number 1) – BTW this is a “Data Structure” How do we design such a thing????

5 Step Data Structure Design Process

Sketch an example test application helps familiarize operations needed

Sketch the “interface” for the class names of methods, parameters, return types

Develop the implementation Choose an internal representation – An array of words? Write the methods Test and debug using tester class Then write the complete implementation