imperative vs object oriented paradigms
play

Imperative vs. object- oriented paradigms 1 11/14/17 Imperative - PDF document

11/14/17 CSCI-2320 Object-Oriented Paradigm: Ruby Mohammad T . Irfan Imperative vs. object- oriented paradigms 1 11/14/17 Imperative vs. object-oriented u Imperative u Procedural decomposition u Procedures are all powerful u Data is


  1. 11/14/17 CSCI-2320 Object-Oriented Paradigm: Ruby Mohammad T . Irfan Imperative vs. object- oriented paradigms 1

  2. 11/14/17 Imperative vs. object-oriented u Imperative u Procedural decomposition u Procedures are all powerful u Data is helpless, at the mercy of procedures u Object-oriented (OO) u Data-centric: data governs the decomposition u Classes – templates/patterns, abstracts away data u Objects – actual things/instantiations of classes u Advantages of OO paradigm u Collaboration u Debugging u Reuse Roadmap u OOP principles u Learn the basics of Ruby u Investigate Ruby’s object-oriented design principles u Ruby on Rails for web programming 2

  3. 11/14/17 OOP Principles Examples: Java OOP principles 1. Encapsulation 2. Inheritance 3. Polymorphism 4. Abstraction These principles typically interact with one another. 3

  4. 11/14/17 1. Encapsulation u Hide internal representation of data by providing methods (e.g., getter and setter methods; other methods that work on data) u Benefits: decoupling data and functionality, protection of data, hiding unnecessary details on representation Encapsulation example: Book class u Want a class for representing certain information about a book u Note: each object is one single book u Multiple objects à many books 1. What are the attributes or properties of a book? 2. What are the actions or behaviors that you can apply on book data? u Encapsulation: bind the above two together u Sample code: u http://www.javaworld.com/article/2979739/ learn-java/java-101-classes-and-objects-in- java.html 4

  5. 11/14/17 5

  6. 11/14/17 book1 book2 book3 A tale of two cities Moby Dick Unknown 1859 1851 -1 Shared class variable: count = 3 Methods are also shared 6

  7. 11/14/17 Encapsulation question u Build on the Book class to include author names. How would you represent multiple authors? 2. Inheritance u Allows one class to "inherit" the methods (functionalities) and attributes (variables) of another class u Subclass (AKA derived class) extends (or inherits) Superclass (AKA base class) u Java's keyword: extends 7

  8. 11/14/17 Inheritance in picture u Hierarchical organization Subclass of subclass variables and methods Subclass variables and methods Superclass variables and methods Inheritance in Java Subclass u ✗ Multiple superclasses Multiple subclasses u ✓ Chain of inheritance Superclass 8

  9. 11/14/17 9

  10. 11/14/17 Chain of inheritance (Multilevel inheritance) u New class for shipping a box, inherits WeightedBox Demo 10

  11. 11/14/17 3. Polymorphism u The ability of an object to take many forms u Compare with method overloading u This is achieved via inheritance u Note the inter-relation u Superclass can refer to subclass object u Concept: method overriding Method overriding u Subclass re-defines a superclass method with the same method signature u Next few slides u PlainBox class models a simple 3D box u WeightedBox class extends PlainBox class u Adds weight variable u Overrides the multiply method u BoxDemo class gives a demo 11

  12. 11/14/17 Superclass' multiply method is hidden from the subclass unless the subclass explicitly calls it using super 12

  13. 11/14/17 Output 4. Abstraction u Generalization u Examples u C (not OOP): qsort works with different data types u C++: STL u Java: Abstract class allows generalization by hiding implementation details 13

  14. 11/14/17 Encapsulation vs abstraction u Debates on orthogonality of concepts u http://www.tonymarston.co.uk/php-mysql/ abstraction.txt u Roughly – information/representation hiding (capsule) vs implementation hiding (generalization) Ruby 14

  15. 11/14/17 Ruby Installation u Several ways to install, as described here: https://www.ruby-lang.org/en/downloads/ u Mac/Linux: Use RVM (https://rvm.io/rvm/install) u Command line$ \curl -sSL https://get.rvm.io | bash -s stable –ruby u Then, follow the instruction given in terminal u To test, commands are: u ruby –v u rvm –v u If you get errors, run the following commands (assuming 2.1.4 is the latest version—look up rvm website for it) u brew update && brew upgrade u rvm reinstall 2.1.4 --disable-binary u Windows: Install Ruby 2.0.0 from RubyInstaller.org http://rubyinstaller.org/downloads/ u Recommended IDE u Aptana Studio http://www.aptana.com/ Ruby resources u Learning u English translation of the creator’s user guide (by Mark Slagell) u http://www.rubyist.net/~slagell/ruby/index.html u Go to reference u Documentation: http://ruby-doc.org/core-2.0.0/ u http://www.tutorialspoint.com/ruby/ u Interactive tutorial using only your web-browser u http://tryruby.org 15

  16. 11/14/17 Origin u Designed by Yukihiro Matsumoto (Matz) in early 1990s u Inspired by Perl and Python u Less scripting than Perl u More object-oriented than Python u Happy experience! Quotes u Bruce Stewart (2001): Did you have a guiding philosophy when designing Ruby? u Matz: Yes, it's called the "principle of least surprise." I believe people want to express themselves when they program. They don't want to fight with the language. Programming languages must feel natural to programmers. I tried to make people enjoy programming and concentrate on the fun and creative part of programming when they use Ruby. (http://www.linuxdevcenter.com/pub/a/linux/2001/11/29/ruby.html) u Bill Venners (2003): In an introductory article on Ruby, you wrote, "For me the purpose of life is partly to have joy. Programmers often feel joy when they can concentrate on the creative side of programming, So Ruby is designed to make programmers happy." How can Ruby make programmers happy? u Matz: You want to enjoy life, don't you? If you get your job done quickly and your job is fun, that's good isn't it? That's the purpose of life, partly. Your life is better. I want to solve problems I meet in the daily life by using computers, so I need to write programs. By using Ruby, I want to concentrate the things I do, not the magical rules of the language, like starting with public void something something something to say, "print hello world." I just want to say, "print this!" I don't want all the surrounding magic keywords. I just want to concentrate on the task. That's the basic idea. So I have tried to make Ruby code concise and succinct. (http://www.artima.com/intv/ruby.html) 16

  17. 11/14/17 Interview of Matz u http://vimeo.com/52954702 Features u Purely object oriented u Every data value is an object – no primitive type u Every subroutine is a method u Inheritance can be applied to any class u Both classes and objects are dynamic ! u Can add methods to classes and objects dynamically u Different objects of the same class can behave differently You should be able to u Dynamically typed explain these! u Static scoping u 37 reasons to love Ruby! u http://rubyhacker.com/ruby37.html 17

  18. 11/14/17 Let’s code in Ruby Before we start u If you want to quickly check something without writing a program u Use the irb command in Terminal u Examples u x = 10 if x % 2 == 0 puts “Even” else puts “Odd” end u What does nil mean in the output? In Ruby, there is no statement. Everything is an expression returning a value, whether you explicitly say return or not. u x = [“NFL”, “NBA”, 100] x.class x.class.methods x.include? “NBA” x.include? 200 18

  19. 11/14/17 Variables u Type is implicit u Type can be changed dynamically u Naming: $ Global variable @ Instance variable [a-z] or _ Local variable [A-Z] Constant u Examples (in irb) u x = 10.99 x.class #prints Float x = “Hello Ruby!” x.class #prints String u Very rich String class u Examples: http://ruby-doc.org/core-2.0.0/String.html Arrays (mutable) u Creation, insertion, deletion u myArray = [“NFL”, “NBA”, 100] u myString = myArray.join(“ ”) #outputs “NFL NBA 100” u left = myArray.shift #left has value “NFL” u myArray #myArray is now [“NBA”, 100] u myArray.push(“MLS”) #myArray is now [“NBA”, 100, “MLS”] u myArray.unshift(“NFL”) #myArray is now [“NFL”, “NBA”, 100, “MLS”] u delete(obj), delete_at(index), delete_if { |item| block } u Accessing elements u myArray[0] #“NFL” u myArray[0..-1] #everything in the array u myArray.each {|item| puts item} #iterate through items u myArray.each_index {|i| print i, “->”, myArray[i], “\n”} 19

  20. 11/14/17 Sample program: factorial u Save it as source.rb def fact(n) if n == 0 1 else n * fact(n-1) end end u Ways to run u 1. Add this line at the end of source.rb and click on run u puts fact(10) u 2. ruby -I ./ -r source.rb -e "puts fact(10)” u Command line arguments are also supported Problem: Collatz Conjecture u From Wikipedia http://en.wikipedia.org/wiki/Collatz_conjecture u Take any integer n > 0 as input . The conjecture is that no matter what n is, you will always eventually reach 1 if you follow this procedure: u If n is even, assign n = n /2. If n is odd, assign n = 3 n + 1. Repeat the process until you reach n = 1 (conditional statements and loops) u (Extra job) Print all these numbers to a file u The # of steps is called the cycle length of n u Output the cycle length (to standard output) u (Extra job) Also write it to the file 20

  21. 11/14/17 # of steps (y) vs. input number (x) Solution 21

  22. 11/14/17 Review: What’s new in Ruby? (vs. Java/C++) u Purely object oriented u Classes and objects are dynamic u Class can be defined later, dynamically 22

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend