Topic 5 Implementing Classes Definitions
Total Page:16
File Type:pdf, Size:1020Kb
Topic 5 Implementing Classes “And so,,p,gg from Europe, we get things such ... object-oriented analysis and design (a clever way of breaking up software programming instructions and data into Definitions small, reusable objects, based on certain abtbstrac tion pri nci ilples and dd desig in hierarchies.)” -Michael A . Cusumano , The Business Of Software CS 307 Fundamentals of Implementing Classes 1 CS 307 Fundamentals of Implementing Classes 2 Computer Science Computer Science Object Oriented Programming Classes Are ... What is o bject or iente d programm ing ? Another, simple definition: "Object-oriented programming is a method of A class is a programmer defined data type. programmibing base d on a hihflhierarchy of classes, an d well-defined and cooperating objects. " A data type is a set of possible values and What is a class? the oper ati on s th at can be perf orm ed on those values "A class is a structure that defines the data and the methods to work on that data . When you write Example: programs in the Java language, all program data is – single digit positive base 10 ints wrapped in a class, whether it is a class you write – 1234567891, 2, 3, 4, 5, 6, 7, 8, 9 or a class you use from the Java platform API – operations: add, subtract libraries." – Sun code camp – problems ? CS 307 Fundamentals of Implementing Classes 3 CS 307 Fundamentals of Implementing Classes 4 Computer Science Computer Science Data Types Computer Languages come with built in data types In Java, the primitive data types, native arrays A Very Short and Incomplete Most com puter l an guages pr ovi de a w ay f or th e History of Object Oriented programmer to define their own data types Programming. (OOP) – Java comes with a large library of classes So object oriented programming is a way of programming that is dominated by creating new data types to solve a problem. We will look at how to create a new data type CS 307 Fundamentals of Implementing Classes 5 CS 307 Fundamentals of Implementing Classes 6 Computer Science Computer Science OOP is not new. OOP Languages Simu la 1 (1962 - 1965) an d Simu la 67 Smalltalk (1970s), Alan Kay's group at Xerox (1967) Norwegian Computing Center, Oslo, PARC NbOlNorway by Ole-JhJohan DhlDahl an dKitd Kristen Nygaard. C++ (early 1980s), Bjarne Stroustrup, Bell Labs TiTuring Awar dWid Winners - 2001 CS 307 Fundamentals of Implementing Classes 7 CS 307 Fundamentals of Implementing Classes 8 Computer Science Computer Science OOP Languages Program Design in OOP MdlModula – 3Ob3, Oberon, EifflJEiffel, Java, C#, OOP breaks up problems based on the data Python types found in the problem – many languages have some Object – as opposed to breaking up the problem based on Oriented version or capability the algorithms involved One of the dominant styles for Given a problem statement, what things imppgppglementing complex programs with appear in the problem? large numbers of interacting The nouns of the problem are candidate components classes. – … but not the only programming paradigm and The actions and verbs of the problems are there are variations on object oriented programming candidate methods of the classes CS 307 Fundamentals of Implementing Classes 9 CS 307 Fundamentals of Implementing Classes 10 Computer Science Computer Science Attendance Question 1 The process of taking a large problem and breaking it up into smaller parts is known as: Short Object Oriented A. Functional programming Programming Design Example B. Object oriented programming C. Top down design D. Bottom up design EWE. Waterf fllall met hdhod CS 307 Fundamentals of Implementing Classes 11 CS 307 Fundamentals of Implementing Classes 12 Computer Science Computer Science Monopoly Individual Class Design If we had to start fhhfrom scratch what classes would we need to create? CS 307 Fundamentals of Implementing Classes 13 CS 307 Fundamentals of Implementing Classes 14 Computer Science Computer Science The Steps of Class Design Class Design Requirements Classes s hou ld be cohihesive. – what is the problem to be solved – They should be designed to do one thing well. – detailed requirements lead to specifications Nouns may be classes Verbs signal behavior and thus methods (also defines a classes responsibilities) walkthrough scenarios to find nouns and verbs implementing and testing of classes Classes should be loosely coupled. design rather than implementation is normally the – Changing the internal implementation details of a class hardest part should not affect other classes. – loose coupling can also be achieved within a class itself – ppglanning for reuse CS 307 Fundamentals of Implementing Classes 15 CS 307 Fundamentals of Implementing Classes 16 Computer Science Computer Science Encapsulation Design to Implementation Also know as separat ion o f concerns an d Design must me implemented using the information hiding syntax of the programming language When crea ting new da ta types ( c lasses ) the de ta ils In class example with a list of integers of the actual data and the way operations work is hidden from the other programmers who will use Slides in clude an oth er ex am pl e of cr eatin g a those new data types class to represent a playing die – So theyyy don't have to worry about them – So they can be changed without any ill effects (loose coupling) Encapsulation makes it easier to be able to use something – midiidthJStilicrowave, radio, ipod, the Java String class CS 307 Fundamentals of Implementing Classes 17 CS 307 Fundamentals of Implementing Classes 18 Computer Science Computer Science The Problem with Arrays SIdttbhffiltitlSuppose I need to store a bunch of film titles from a file The Godfather AListofintsA List of ints The Princess Bride The Incredible String[] titles = new String[100]; // I never know how much // space I need! I want the arrayyg to grow and shrink CS 307 Fundamentals of Implementing Classes 19 CS 307 Fundamentals of Implementing Classes 20 Computer Science Computer Science Lists Attendance Question 2 I need a list. When adding a new element to a list A list is a collection of items with a definite what should be the default location to order. add? Our ex am pl e will be a li st of in teger s. Design and then implement to demonstrate the Java syntax for creating a class. A. The beginning B. The end C. The middle D. A random location CS 307 Fundamentals of Implementing Classes 21 CS 307 Fundamentals of Implementing Classes 22 Computer Science Computer Science IntList Design Instance Variables CILiCreate a new, empty IntList ItInterna ldtl data new IntList -> [] – also called instance variables because every The above is not code. It is a notation that shows instance (object) of this class has its own copy of what the results of operations. [] is an empty list. these – something to store the elements of the list add to a list. – size of internal storage container? [].add(1) -> [1] – if not what else is needed [1].add(5) -> [1, 5] Must be clear on the difference between the [1, 5].add(4) -> [1, 5, 4] internal data of an IntList object and the elements in a list have a definite order and a IntList that is being represented position. Whyyp make internal data private? – zero based position or 1 based positioning? CS 307 Fundamentals of Implementing Classes 23 CS 307 Fundamentals of Implementing Classes 24 Computer Science Computer Science Attendance Question 3 IntList aList = new IntList(); aList.add(42); aList.add(12); Our IntList class will have an instance variable aList.add(37); aList of ints (int[] container). What should the capacity of this internal array be? Abstract view of IntList A. less than or equal to the size of the list list of integers size 3 B. grea t er tha n or equal t o th e siz e of th e li st [42, 12, 37] container C. equal to the size of the list D. some fixed amount that never changes The wall of 42 12 37 0 0 0 0 0 0 0 E. 0 abstraction. 0 1 2 3 4 5 6 7 8 9 CS 307 Fundamentals of Implementing Classes 25 CS 307 Fundamentals of Implementing Classes 26 Computer Science Computer Science Constructors Default add method For initialization of objects where to add? IntList constructors what if not enough space? – default [].add(3) -> [3] – initial cappyacity? [3]. add(5) ->[35]> [3, 5] redirecting to another constructor [3, 5].add(3) -> [3, 5, 3] this(10); class constants Testing, testing, testing! – what sttitatic means –a toString method would be useful CS 307 Fundamentals of Implementing Classes 27 CS 307 Fundamentals of Implementing Classes 28 Computer Science Computer Science toString method Attendance Question 4 return a Java String of list What is output by the following code? empty list -> [] IntList list = new IntList(); one element -> [12] System.out.println( list.size() ); multiple elements -> [12, 0, 5, 4] A. 10 Beware the performance of String B. 0 concatenation. C. -1 StringBuffer alternative D. unknown E. No output due to runtime error. CS 307 Fundamentals of Implementing Classes 29 CS 307 Fundamentals of Implementing Classes 30 Computer Science Computer Science get and size methods insert method get add at someplace besides the end – access element from list [3, 5].insert(1, 4) -> [3, 4, 5] – preconditions ? [3, 5, 2].get(0) returns 3 where what [3, 5, 2].get(1) returns 5 [3, 4, 5].insert(0, 4) -> [4, 3, 4, 5] size preconditions? – number of elements in the list – Do not confuse with the capacity of the internal overload add? sttitorage container chance for internal loose coupling – The array is not the list! [4, 5, 2].s ize () retu rns 3 CS 307 Fundamentals of Implementing Classes 31 CS 307 Fundamentals of Implementing Classes 32 Computer Science Computer Science Attendance Question 5 remove method What is output by the following code? remove an element from the list based on IntList list = new IntList(); list.add(3); location list.insert(0, 4); [3, 4, 5].remove(0) -> [4, 5] list.insert(1, 1); list.add(();5); [[,,,,]3, 5, 6, 1, 2].remove ()(2) -> list.insert(2, 9); System.out.println( list.toString() ); [3, 5, 1, 2] A.