topic 3 object oriented programming
play

Topic 3 Object Oriented Programming Encapsulation - Implementing - PowerPoint PPT Presentation

Topic 3 Object Oriented Programming Encapsulation - Implementing Classes Creating large programs that work turns out to be very difficult DIA Automated baggage handling system as ... object-oriented analysis and design Ariane 5 Flight 501 (a


  1. Topic 3 Object Oriented Programming Encapsulation - Implementing Classes Creating large programs that work turns out to be very difficult DIA Automated baggage handling system as ... object-oriented analysis and design Ariane 5 Flight 501 (a clever way of breaking up software More programming instructions and data into Object oriented programming is one way of small, reusable objects, based on certain managing the complexity of programming abstraction principles and design and software projects -Michael A. Cusumano, Break up big problems into smaller, more The Business Of Software manageable problems CS 314 Encapsulation - Implementing Classes 2 Object Oriented Programming Object Oriented Programming "Object-oriented programming is a method of In other words break the problem up based programming based on a hierarchy of classes, and on the things / data types that are part of the well-defined and cooperating objects. " problem What is a class? Not the only way "A class is a structure that defines the data and the One of many different kinds of strategies or methods to work on that data. When you write paradigms for software development programs in the Java language, all program data is wrapped in a class, whether it is a class you write functional, procedural, event driven, data flow, or a class you use from the Java platform API formal methods, agile or extreme, ... libraries." In 314 we will do a lot of object based a new data type programming CS 314 Encapsulation - Implementing Classes 3 CS 314 Encapsulation - Implementing Classes 4

  2. Example - Monopoly Encapsulation One of the features of object oriented languages Hide the data of an object (variable) Group operations and data together into a new data type If we had to start Usually easier to use something than from scratch what understand exactly how it works classes would we need to create? microwave, car, computer, software, mp3 player CS 314 Encapsulation - Implementing Classes 5 CS 314 Encapsulation - Implementing Classes 6 The IntList Class Clicker Question 1 We will develop a class that models a list of ints Our IntList class will have an array of ints instance variable ( int[] container ). What Improvement on an array of ints should the capacity of this internal array be? resize automatically insert easily A. less than or equal to the size of the list remove easily B. greater than or equal to the size of the list A list - our first data structure C. equal to the size of the list a variable that stores other variables Lists maintain elements in a definite order and D. some fixed amount that never changes duplicates are allowed E. 0 CS 314 Encapsulation - Implementing Classes 7 CS 314 Encapsulation - Implementing Classes 8

  3. Clicker Question 2 IntList Design Create a new, empty IntList When adding a new element to a list new IntList -> [] what should be the default location for The above is not code. It is a notation that shows the new element? what the results of operations. [] is an empty list. add to a list. A. The beginning [].add(1) -> [1] [1].add(5) -> [1, 5] B. The end [1, 5].add(4) -> [1, 5, 4] C. The middle elements in a list have a definite order and a D. A random location position. zero based position or 1 based positioning? CS 314 Encapsulation - Implementing Classes 9 CS 314 Encapsulation - Implementing Classes 10 The IntList Class The IntList Class instance variables testing!! constructors toString default Joshua Bloch initial capacity insert method (int pos, int value) preconditions, exceptions, postconditions, assert meaning of static remove method (int pos) add method insertAll method get method (int pos, IntList other) size method king of the IntLists CS 314 Encapsulation - Implementing Classes 11 CS 314 Encapsulation - Implementing Classes 12

  4. Clicker 3 - Timing Experiment Instance Variables Add N elements to an initially empty IntList then call Internal data toString. Time both events. How does the time to add compare to the time to complete toString? also called instance variables because every instance (object) of this class has its own copy of IntList list = new IntList(); these for (int i = 0; i < N; i++) something to store the elements of the list list.add(i); // resize, cap * 2 size of internal storage container? String s = list.toString(); if not what else is needed A. time to add << time for toString() Must be clear on the difference between the B. time to add < time for toString() internal data of an IntList object and the C. time to add ~= time for toString() IntList that is being represented D. time to add > time for toString() Why make internal data private? E. time to add >> time for toString() CS 314 Encapsulation - Implementing Classes 14 Constructors IntList aList = new IntList(); aList.add(42); For initialization of objects aList.add(12); aList.add(37); aList IntList constructors default Abstract view of IntList initial capacity? list of integers size 3 redirecting to another constructor [42, 12, 37] this(10); container class constants what static means The wall of 42 12 37 0 0 0 0 0 0 0 abstraction. 0 1 2 3 4 5 6 7 8 9 CS 314 Encapsulation - Implementing Classes 15 CS 314 Encapsulation - Implementing Classes 16

  5. Default add method toString method where to add? return a Java String of list empty list -> [] what if not enough space? one element -> [12] [].add(3) -> [3] multiple elements -> [12, 0, 5, 4] [3].add(5) -> [3, 5] Beware the performance of String [3, 5].add(3) -> [3, 5, 3] concatenation. StringBuilder alternative Testing, testing, testing! a toString method would be useful CS 314 Encapsulation - Implementing Classes 17 CS 314 Encapsulation - Implementing Classes 18 Clicker Question 4 get and size methods get What is output by the following code? access element from list ArrayList<String> list preconditions? list = new ArrayList<>(25); [3, 5, 2].get(0) returns 3 System.out.println(list.size()); [3, 5, 2].get(1) returns 5 A. 25 size B. 0 number of elements in the list C. -1 Do not confuse with the capacity of the internal storage container D. unknown The array is not the list! E. No output due to runtime error. [4, 5, 2].size() returns 3 CS 314 Encapsulation - Implementing Classes 19 CS 314 Encapsulation - Implementing Classes 20

  6. Clicker Question 5 insert method What is output by the following code? add at someplace besides the end IntList list = new IntList(); [3, 5].insert(1, 4) -> [3, 4, 5] list.add(3); list.insert(0, 4); // position, value where what list.insert(1, 1); list.add(5); list.insert(2, 9); [3, 4, 5].insert(0, 4) -> [4, 3, 4, 5] System.out.println(list); preconditions? A. [4, 1, 3, 9, 5] B. [3, 4, 1, 5, 9] overload add? C. [4, 1, 9, 3, 5] chance for internal loose coupling D. [3, 1, 4, 9, 5] E. Something else CS 314 Encapsulation - Implementing Classes 21 CS 314 Encapsulation - Implementing Classes 22 remove method Clicker Question 6 remove an element from the list based on What is output by the following code? location IntList list = new IntList(); list.add(12); [3, 4, 5].remove(0) -> [4, 5] list.add(15); list.add(12); [3, 5, 6, 1, 2].remove(2) -> list.add(17); [3, 5, 1, 2] list.remove(1); System.out.println(list); preconditions? A. [15, 17] B. [12, 17] return value? C. [12, 0, 12, 17] accessor methods, mutator methods, and D. [12, 12, 17] mutator methods that return a value E. [15, 12, 17] CS 314 Encapsulation - Implementing Classes 23 CS 314 Encapsulation - Implementing Classes 24

  7. insertAll method Clicker 7 - InsertAll First Version add all elements of one list to another Wat is the order of the first version of starting at a specified location InsertAll? Assume both lists have N elements and that the insert position is halfway through [5, 3, 7].insertAll(2, [2, 3]) -> the calling list. [5, 3, 2, 3, 7] A. O(1) The parameter [2, 3] would be unchanged. B. O(logN) Working with other objects of the same type C. O(N 0.5 ) this ? D. O(N) where is private private? E. O(N 2 ) loose coupling vs. performance CS 314 Encapsulation - Implementing Classes 25 CS 314 Encapsulation - Implementing Classes 26 The Die Class Consider a class used to model a die What is the interface? What Class Design and Implementation actions should a die be able to perform? Another Example This example will not be covered in class. The methods or behaviors can be broken up into constructors, mutators, accessors CS 314 Encapsulation - Implementing Classes 28

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