26-Jun-15 Abstract Data Types. 2 Data types I We type data--classify it into various categories-- such as int, boolean, String, Applet A data type represents.

Slides:



Advertisements
Similar presentations
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Advertisements

23-Apr-15 Abstract Data Types. 2 Data types I We type data--classify it into various categories-- such as int, boolean, String, Applet A data type represents.
Written by: Dr. JJ Shepherd
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Road Map Introduction to object oriented programming. Classes
11-Jun-15 Using the Java API
Using the Java API
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
29-Jun-15 Using the Java API
13-Jul-15 Abstract Data Types. 2 Data types I We type data--classify it into various categories-- such as int, boolean, String, Applet A data type represents.
Stacks, Queues, and Deques
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
CS2110 Recitation Week 8. Hashing Hashing: An implementation of a set. It provides O(1) expected time for set operations Set operations Make the set empty.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
Object Oriented Data Structures
Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Course: Object Oriented Programming - Abstract Data Types Unit1: IntroductionSlide Number 1 Introduction Course: Object Oriented Programming Abstract Data.
Haskell. 2 GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
Data Structures and Algorithms -- Chapter 3 Abstract Data Types Mohamed Mustaq.
CIT 590 Intro to Programming First lecture on Java.
23-Oct-15 Abstract Data Types. 2 Data types A data type is characterized by: a set of values a data representation, which is common to all these values,
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Linked List. Background Arrays has certain disadvantages as data storage structures. ▫In an unordered array, searching is slow ▫In an ordered array, insertion.
CSE 114 Computer Science I Objects Lake Superior, Michigan.
Copyright 2008 by Pearson Education Building Java Programs ArrayList Reading: 10.1.
10-Nov-15 Java Object Oriented Programming What is it?
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to.
Data Abstaraction Chapter 10.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
The ArrayList Data Structure Standard Arrays at High Speed! More Safety, More Efficient, and Less Overhead!
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Written by: Dr. JJ Shepherd
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
Objects as a programming concept
More Sophisticated Behavior
Working with Java.
Introduction to Computer Science / Procedural – 67130
Data Structures Mohammed Thajeel To the second year students
Pointers and References
Pointers and References
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.
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Chapter 1: Computer Systems
Stacks.
Abstract Data Types 24-Feb-19.
Focus of the Course Object-Oriented Software Development
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Introduction to Data Structure
Java Programming Language
CS2013 Lecture 7 John Hurley Cal State LA.
Review: libraries and packages
Presentation transcript:

26-Jun-15 Abstract Data Types

2 Data types I We type data--classify it into various categories-- such as int, boolean, String, Applet A data type represents a set of possible values, such as {..., -2, -1, 0, 1, 2,... }, or { true, false } By typing our variables, we allow the computer to find some of our errors Some operations only make sense when applied to certain kinds of data (you can’t divide by a boolean) Some methods only make sense for certain objects Typing simplifies internal representation A String requires more and different storage than a boolean

3 Data types II A data type is characterized by: a set of values a data representation, which is common to all these values, and a set of operations, which can be applied uniformly to all these values

4 Primitive types in Java Java provides eight primitive types: boolean char, byte, short, int, long float, double Each primitive type has a set of values a data representation a set of operations These are “set in stone”—there is nothing the programmer can do to change anything about them

5 Primitive types as data types

6 Classes in Java A class defines a data type The possible values of a class are called objects The data representation is a reference (pointer) to a block of storage The structure of this block is defined by the fields (both inherited and immediate) of the class The operations on the objects are called methods Many classes are defined in Java’s packages To program in Java, you can (and must) define your own classes, as well as use the ones provided

7 Methods and operators An operator typically Is written with non-alphabetic characters: +, *, ++, +=, &&, etc. Is written as prefix, infix, or postfix: -x, x+y, x++ Has only one or two arguments, or operands A method (or function) typically Is written with letters, and its arguments are enclosed in parentheses: toString(), Math.abs(n) Has a fixed, predetermined number of arguments

8 Methods are operators The differences between methods and operations are only syntactic differences, not fundamental ones Many languages (not including Java) let you define new operators, that is, new syntax When you define a new class and its methods, you are, fundamentally, defining a new data type and its operators Suppose a language defines the to mean “times 3 plus 1”; for is 22 Would you consider this a good operation to have in the language? What does this suggest about defining classes and their methods?

9 Insertion into a list A list is just a sequence of values There are many ways you could insert a new item into a list: Is it a good idea to supply all of these? If not, why not? As the new first item As the new last item Before a given item After a given item In place of a given item Before the n th element After the n th element Before the n th from the end After the n th from the end In the correct location to keep the list in sorted order

10 Cognitive load Human minds are limited—you can’t remember everything You probably don’t even remember all the Java operators for integers What’s the difference between >> and >>> ? What about between << and <<< ? We want our operators (and methods) to be useful and worth remembering The methods we define should be both necessary and sufficient to use the class for its intended purpose Of course, we can’t always foresee how the class will be used In addition, we typically want some convenience methods

11 Efficiency A list is just a sequence of values—it could be implemented by a linked list or by an array Inserting as a new first element is efficient for a linked list representation, inefficient for an array Accessing the n th element is efficient for an array representation, inefficient for a linked list Inserting in the n th position is efficient for neither Do we want to make it easy for the user to be inefficient? Do we want the user to have to know the implementation?

12 Abstract Data Types An Abstract Data Type (ADT) is: a set of values a set of operations, which can be applied uniformly to all these values To abstract is to leave out information, keeping (hopefully) the more important parts What part of a Data Type does an ADT leave out?

13 Data representation in an ADT An ADT must obviously have some kind of representation for its data The user need not know the representation The user should not be allowed to tamper with the representation Solution: Make all data private But what if it’s really more convenient for the user to have direct access to the data? Solution: Use setters and getters

14 Example of setters and getters class Pair { private int first, last; public getFirst() { return first; } public setFirst(int first) { this.first = first; } public getLast() { return last; } public setLast(int last) { this.last = last; } }

15 Aside: naming setters and getters Setters and getters should be named by: Capitalizing the first letter of the variable ( first becomes First ), and Prefixing the name with get or set ( setFirst ) For boolean variables, you can replace get with is (for example, isRunning ) This is more than just a convention—if and when you start using JavaBeans, it becomes a requirement

16 What’s the point? Setters and getters allow you to keep control of your implementation For example, you decide to define a Point in a plane by its x-y coordinates: class Point { public int x; public int y; } Later on, as you gradually add methods to this class, you decide that it’s more efficient to represent a point by its angle and distance from the origin, θ and ρ Sorry, you can’t do that—you’ll break too much code that accesses x and y directly If you had used setters and getters, you could redefine them to compute x and y from θ and ρ

17 Contracts Every ADT should have a contract (or specification) that: Specifies the set of valid values of the ADT Specifies, for each operation of the ADT: Its name Its parameter types Its result type, if any Its observable behavior Does not specify: The data representation The algorithms used to implement the operations

18 Importance of the contract A contract is an agreement between two parties; in this case The implementer of the ADT, who is concerned with making the operations correct and efficient, and also with preserving the flexibility to make changes later The applications programmer, who just wants to use the ADT to get a job done It doesn’t matter if you are both of these parties; the contract is still essential for good code This separation of concerns is essential in any large project

19 Promise no more than necessary For a general API, the implementer should provide as much generality as feasible But for a specific program, the class author should provide only what is essential at the moment In Extreme Programming terms, “You ain’t gonna need it!” In fact, XP practice is to remove functionality that isn’t currently needed! Your documentation should not expose anything that the application programmer does not need to know If you design for generality, it’s easy to add functionality later—but removing it may have serious consequences

20 Implementing an ADT To implement an ADT, you need to choose: a data representation that must be able to represent all possible values of the ADT should be private an algorithm for each of the possible operations that must be consistent with the chosen representation all auxiliary (helper) operations that are not in the contract should be private

21 Writing the contract In most cases, the Javadoc for a class is the contract This means: The Javadoc documentation should describe what the class is for and how it should be used The Javadoc documentation should not describe implementation details Also, now is a good time to read Documentation Comments (rules 38 to 58) in The Elements of Java Style Sometimes, however... The particular implementation makes certain operations efficient at the cost of making others inefficient For example, Java provides both ArrayList (fast random access) and LinkedList (fast insertions and deletions) The user needs to know this information, but doesn’t need detailed implementation information

22 Contract and implementation in Java, method I Express the contract as an outline class declaration, showing only: public fields headings of constructors and methods (this is what you would describe in javadoc comments) Express the implementation as a completed class declaration You can’t write the outline class declaration directly, but Javadoc can create it from your code

23 Contract and implementation in Java, method II Express the contract as an interface Express the implementation as a class that implements the interface Disadvantage: you can’t describe constructors in an interface Nevertheless, this is a good technique, and is used heavily in Java

24 Example contract (method I) General description of class public class Die { // Each value is a die (singular of dice) with n sides, // numbered 1 to n, with one face showing Constructor public Die(int numberOfSides) // constructs a die with faces 1 thru numberOfSides Accessor int lastRoll() // returns the result of the previous roll Transformer (mutative) int roll() // returns the result of a new roll of the die }

25 Implementation, method I, page 1 import java.util.*; public class Die { private int numberOfSides; private static Random random = new Random(); private int face; public Die(int numberOfSides) { this.numberOfSides = numberOfSides; face = roll(); // construct in a valid state! }

26 Implementation, method I, page 2 int lastRoll() { return face; } int roll() { face = random.nextInt(numberOfSides) + 1; return face; } } // class Die

27 Contract, method II interface DieRoller { int lastRoll(); int roll(); } Notice: There is no way to define a constructor However, this should be part of the contract! We need two names: one for the interface, and one for the class that implements it Usually more than one class will implement an interface

28 Implementation, method II import java.util.*; public class Die implements DieRoller { // Everything else is the same } Frequently more than one class will implement an interface The interface describes the general (more abstract) case Implementations are designed for specific classes

29 Responsibilities A class is responsible for its own values It should protect them from careless or malicious users Ideally, a class should be written to be generally useful The goal is to make the class reusable The class should not be responsible for anything specific to the application in which it is used In practice, most classes are application-specific Java’s classes are, on the whole, extremely well designed They weren’t written specifically for your program Strive to make your classes more like Java’s!

30 Aside: an interesting bug Originally, I left the word static out of private static Random random = new Random(); Then I created a Vector of ten dice ( Die s) When I printed the Vector, I got: Why? These really were ten different Die objects Hint: How does Java initialize its random number generator?

31 Summary A Data Type describes values, representations, and operations An Abstract Data Type describes values and operations, but not representations An ADT should protect its data and keep it valid All, or nearly all, data should be private Access to data should be via getters and setters An ADT should provide: A contract A necessary and sufficient set of operations

32 The End