CHAPTER 8: USING OBJECTS

Slides:



Advertisements
Similar presentations
When is Orientated Programming NOT? Mike Fitzpatrick.
Advertisements

CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Chapter 13 – Introduction to Classes
Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
CHAPTER 9 DEFINING CLASSES & CREATING OBJECTS Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Road Map Introduction to object oriented programming. Classes
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
ASP.NET Programming with C# and SQL Server First Edition
C++ fundamentals.
CHAPTER 3: CORE PROGRAMMING ELEMENTS Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
CHAPTER 6: ARRAYS Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
CHAPTER 1: INTRODUCTION TO COMPUTER SCIENCE Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
CHAPTER 4: CONDITIONAL STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Lecture 2 Intro. To Software Engineering and Object-Oriented Programming (1/2)
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Introduction to OOP CPS235: Introduction.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP Basics Classes & Methods (c) IDMS/SQL News
What is an object?. What Makes an Object? An object has identity (it acts as a single whole). Every object has a name that identifies what it is. Ex.
Introduction to Java Chapter 1 - Introduction to Java1 Chapter 1 Introduction to Java.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
REEM ALMOTIRI Information Technology Department Majmaah University.
Programming Logic and Design Seventh Edition
Visit for more Learning Resources
Object-Oriented Programming Concepts
More Sophisticated Behavior
Programming Logic and Design Seventh Edition
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Understand the Fundamentals of Classes
The Object-Oriented Thought Process Chapter 1
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Introduction to Classes
JavaScript: Functions
Road Map Introduction to object oriented programming. Classes
Function There are two types of Function User Defined Function
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Programming Language Concepts (CIS 635)
JavaScript: Functions.
Anatomy of a Class & Method
– Introduction to Object Technology
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Object Based Programming
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Introduction to Classes
Inheritance Basics Programming with Inheritance
Chapter 3 Introduction to Classes, Objects Methods and Strings
Classes and Data Abstraction
Introduction to Classes and Objects
Computer Programming with JAVA
Tonga Institute of Higher Education
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Topics Introduction to Functions Defining and Calling a Function
CIS16 Application Development and Programming using Visual Basic.net
CHAPTER 4: Conditional Structures
Object Oriented Programming in java
Introduction to Object-Oriented Programming
What Is Good Software(Program)?
CS2013 Lecture 7 John Hurley Cal State LA.
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

CHAPTER 8: USING OBJECTS (c) 2012 Ophir Frieder et al CHAPTER 8: USING OBJECTS Introduction to Computer Science Using Ruby

Ruby: Philosophy & Implementation Ruby is the latest in the family of Object Oriented Programming Languages As such, its designer studied the problems and promises of past languages Ruby is an extreme implementation of such a language, containing large complexity on one hand and the ability to ignore any such complexity on the other hand (c) 2012 Ophir Frieder et al

Ruby: Philosophy & Implementation Eliminate ANY unnecessary statements, declarations and complexity Startup has to be very simple Complexity increase has to be unlimited Development is supported by interactive capability Portability is assured via an interpretive implementation (c) 2012 Ophir Frieder et al

Classes Programs can be millions of lines of code Eventually, they become very difficult to debug and maintain Classes are created to organize programs and data based on functionality (c) 2012 Ophir Frieder et al

Objects Classes define the characteristics and behaviors of objects belonging to them Classes provide an abstraction of possible objects Objects are the instantiation of classes They have a name and possess all the properties of the class Example: Simple variables and their methods (c) 2012 Ophir Frieder et al

Classes & Objects Classes are designed to separate key activities in a program Objects instantiated from classes provide the implementation of the program Activities are isolated Communicate information without knowing how it is produced Classes enable programs to be compartmentalized Programmers can work at the same time on different classes without running into each other (c) 2012 Ophir Frieder et al

Methods Classes have their own private chunks of data and actions Actions that an object instantiated from a class may perform are referred to as Methods that belong to that Class (c) 2012 Ophir Frieder et al

Built-in Classes & their Objects Everything in Ruby is an Object, even a simple variable As such, it has to be instantiated from a Class In Ruby, instantiation is many times done automatically, using “hidden” Class definitions This is one of the ways to eliminate declarations and various auxiliary and obscure statements (c) 2012 Ophir Frieder et al

Built-in Classes A Class defines the characteristics and behaviors of an object Contains the variables and the code necessary to implement the operations (Methods) of the object Examples of Built-in Classes: Array Fixnum Float String (c) 2012 Ophir Frieder et al

Built-in Objects – Classes There are many more classes than these It is not required that you know HOW they do what they do, but it is required that you know WHAT they do, how to find them, and how to deploy them (c) 2012 Ophir Frieder et al

Built-in Classes & their Objects In Ruby, instantiation can be done automatically using “hidden” Class definitions, or can be done explicitly, using the proper method This is an automatic creation of an Object (in this case: arr) No class name is used  Class is hidden  no method Example: # Automatic creation – no Class name.... arr = [1,2,3,”Wow”] (c) 2012 Ophir Frieder et al

Built-in Classes & their Objects In Ruby, instantiation is done automatically many times, using “hidden” Class definitions, or can be done explicitly. This is an explicit creation of an Object In this case arr: instantiating it from the class Array using the built-in method “new” Example: # instantiate an object from the class Array arr = Array.new (c) 2012 Ophir Frieder et al

Built-in Classes & their Methods You can understand Class functionality by looking at the Ruby API, or Application Program(ming) Interface API allows the use of built-in functionality (that is, the built-in classes and their methods) without knowing the specifics of the implementation (c) 2012 Ophir Frieder et al

Figure 8.1: String API Documentation The next slides show the API documentation for the String class It is taken straight out of the book – which is taken straight from the Ruby public information (with proper attribution) (c) 2012 Ophir Frieder et al

Figure 8.1: String API Documentation http://ruby-doc.org/core-1.9.3/String.html

Description of the String class (c) 2012 Ophir Frieder et al

Click each link to get description of methods We’ll take a look at using the length method (c) 2012 Ophir Frieder et al (on left hand side)

Built-in Methods The length method descriptor is “str.length => integer” Means that the method will return an integer irb(main):001:0> "hello".length => 5 (c) 2012 Ophir Frieder et al

Built-in Methods Not all methods are this simple: (c) 2012 Ophir Frieder et al

There are multiple ways to call this method Built-in Methods There are multiple ways to call this method (c) 2012 Ophir Frieder et al

This method requires outside data to execute Built-in Methods This method requires outside data to execute (c) 2012 Ophir Frieder et al

Parameter Passing Parameters are data supplied to a method (or to a Class – see later) See the API for the description of the built-in methods that require parameter(s) (variable(s) in parenthesis Methods with parameters send the value of the variable to the implementing code (c) 2012 Ophir Frieder et al

Example 8.1: Parameter Passing Look at the made up multiplier method: 1 x = 3 2 y = x.multiplier(4) 3 puts "The number is: " + y.to_s It multiplies the value of the parameter by x Output: The number is 12. (c) 2012 Ophir Frieder et al

Parameter Passing The method works like a black box The program inside is not known and doesn’t matter Only the function, thus the output, is important Figure 8.4: Black Box for Multiplier Method (c) 2012 Ophir Frieder et al

Example 8.2: Parameter Passing Example of Split (an actual Ruby built-in method): This method splits strings into array elements based on the parameter passed 1 my_string = "Good;day;sir!" 2 arr = my_string.split(";") 3 puts arr 4 5 # The following array is created: 6 # arr[0]: "Good" 7 # arr[1]: "day" 8 # arr[2]: "sir!” (c) 2012 Ophir Frieder et al

Example 8.2: Split Example #1 1 my_string = "Good;day;sir!" 2 arr = my_string.split(";") 3 puts arr 4 5 # The following array is created: 6 # arr[0]: "Good" 7 # arr[1]: "day" 8 # arr[2]: "sir!” Calls method to use “;” as the parameter for splitting (c) 2012 Ophir Frieder et al

Example 8.2: Split Example #1 1 my_string = "Good;day;sir!" 2 arr = my_string.split(";") 3 puts arr 4 5 # The following array is created: 6 # arr[0]: "Good" 7 # arr[1]: "day" 8 # arr[2]: "sir!” Outputs the array Output: Good Day Sir! (c) 2012 Ophir Frieder et al

Example 8.3: Split Example #2 Change the parameter to “a”: 1 my_string = "Good;day;sir!" 2 arr = my_string.split("a") 3 puts arr 4 5 # The following array is created: 6 # arr[0]: "Good;d" 7 # arr[1]: "y;sir!” Output: Good;d y;sir! (c) 2012 Ophir Frieder et al

Example 8.4: Split Example #3 A parameter not found in the string will result in an array containing a string that isn’t split 1 my_string = "Good;day;sir!" 2 arr = my_string.split("z") 3 puts arr 4 5 # The following array is created: 6 # arr[0]: "Good;day;sir!” Output: Good;day;sir! (c) 2012 Ophir Frieder et al

Summary Classes define the characteristics and behaviors of objects belonging to the class Objects are instantiations of a class: they have a name and possess all the properties of the class, namely the variables and the methods The application user interface, or API, is an interface used to communicate with some underlying functionality Parameter passing is used to transfer information to an object (c) 2012 Ophir Frieder et al