Docsity

Abstraction in Computer Science: Understanding ADTs and Objects - Prof. Alan Mattox Beckma, Papers of Data Structures and Algorithms

Toggle sidebar
bg1
CS 331 Abstraction January 28, 2009
1 Objectives
Your objectives for today are:
•to know the most fundamental concept of computer science.
•to be able to describe the components of an ADT.
•to explain the steps of object oriented design.
•know the common models that data structures implement.
•read the Java syntax for creating, initializing, copying, and updating primitives, references, arrays, and
classes.
2 Abstraction
Abstraction is the most powerful concept in computer science. Almost everything we do as programmers or
as researchers involves abstraction.
Abstraction is another term for “model”. How do you make a model?
•First, you need a phenomenon to model.
•Second, you ask some questions about the phenomenon.
–Question 1: What are the elements of the phenomenon?
–Question 2: What are the properties of the elements?
–Question 3: Which of these elements and actions do we care about?
–This is the interface or view.
•Third, you simulate the phenomenon.
–Create an implementation for your model.
–Use the interface to restrict access to the model.
•Important question: what are the limitations of the model?
Example: Integers
•Phenomenon: the integers.
•Questions:
–Elements: zero, and positive and negative whole numbers.
–Properties: infinite set, can add, increment, subtract, etc.
–Don’t care: maybe it’s not that important to count to ∞.
•Simulation: use 16 binary digits. First one is a sign bit.
–Use built-in assembly operations to manipulate the bits.
•Limitations: can’t count past 32767.
Mattox Beckman Page 1 Illinois Institute of Technology
pf3
pf4
pf5

Partial preview of the text

Download Abstraction in Computer Science: Understanding ADTs and Objects - Prof. Alan Mattox Beckma and more Papers Data Structures and Algorithms in PDF only on Docsity!

1 Objectives

Your objectives for today are:

  • to know the most fundamental concept of computer science.
  • to be able to describe the components of an ADT.
  • to explain the steps of object oriented design.
  • know the common models that data structures implement.
  • read the Java syntax for creating, initializing, copying, and updating primitives, references, arrays, and classes.

2 Abstraction

Abstraction is the most powerful concept in computer science. Almost everything we do as programmers or as researchers involves abstraction. Abstraction is another term for “model”. How do you make a model?

  • First, you need a phenomenon to model.
  • Second, you ask some questions about the phenomenon.
    • Question 1: What are the elements of the phenomenon?
    • Question 2: What are the properties of the elements?
    • Question 3: Which of these elements and actions do we care about?
    • This is the interface or view.
  • Third, you simulate the phenomenon.
    • Create an implementation for your model.
    • Use the interface to restrict access to the model.
  • Important question: what are the limitations of the model?

Example: Integers

  • Phenomenon: the integers.
  • Questions:
    • Elements: zero, and positive and negative whole numbers.
    • Properties: infinite set, can add, increment, subtract, etc.
    • Don’t care: maybe it’s not that important to count to ∞.
  • Simulation: use 16 binary digits. First one is a sign bit.
    • Use built-in assembly operations to manipulate the bits.
  • Limitations: can’t count past 32767.

Example: Calculator

Example: Pipe

Example: Strings Here is some code from the Linux kernel.

1 char * strcat(char * dest, const char * src) 2 { 3 char tmp = dest; 4 while (dest) 5 dest++; 6 while ((*dest++ = *src++) != ’\0’); 7 return tmp; 8 }

  • What is a string? (What are the elements? ... actions?)
  • How do dest and src act like strings?
  • How do they not act like strings?

3.3 Polymorphism and Inheritance

  • This is the greatest strength of OOP.
    • Separates and hierarchicalizes properties.
    • Classic examples: graphics libraries, animals.
    • You may promise more, but you may not deliver less.
  • This is the greatest weakness of OOP.
    • Not all “subclasses” follow this relation.
    • Semantics of inheritance is not well understood.

Note Well: OOP is a powerful, popular development model. But it’s not the only one!

3.4 Good Models

  • How do you know a good model when you see it?
    • Simplicity
    • Reusability Also ask — do you need to make a new model?
    • Expandability — what things might be added in the future?

3.5 Simplicity

  • A simple tool does one thing well.
  • Complicated things are easy to break.
  • The interface is what we evaluate here.
  • Ways to break simplicity:
    • Have one variable represent more than one piece of data.
    • Have operations that need multiple steps, and if one is left out, the object’s state will be incon- sistent.
    • Make all the data public.
    • Don’t follow established protocols. (E.g., a temperature class which needs strings like “F 94” instead of “94 F”.)

3.6 Reusability

  • Ways to break reusability:
    • Make the class way too specific. ∗ A DogColor class to keep track of different colors of dogs.
    • Don’t follow established protocols. ∗ Just about anything from a certain well-known operating system vendor.
    • Don’t document the class.
    • Don’t add all the functionality one would expect. ∗ A Shape class that has a rotate method, but no translate method. “But I didn’t need one of those when I wrote it.”

3.7 Extendibility

  • Extendibility is really a kind of reusability.
  • Think: how might we want to use this class in the future?
    • Complex numbers: Maybe we’ll want both Cartesian and polar coordinates later.
  • Ways to break extendibility:
    • Make the class way too specific. ∗ A DogColor class to keep track of different colors of dogs.
    • Don’t follow established protocols.
    • Don’t document the class.
    • Make everything private instead of protected.

4 Common Models

4.1 Set

  • Sets contain objects.
  • Order is not considered.
  • Sets can contain at most one “copy” of an element.
  • Operations: insertion, deletion, membership testing not “how many”
  • Implementations: arrays, lists, trees ...
  • Modification:
    • Allow order to matter (ordered lists, stacks queues).
    • Allow multiple copies of an object (bags).

4.2 Array

  • Arrays contain objects.
  • Position is very important.
  • Arrays can contain multiple “copies” of an element.
  • Operations: insertion, lookup
  • Implementations: arrays, hash-tables (sort of)

4.3 Dictionary

  • Dictionaries contain objects pairs.
    • Key and Value
  • Order is not considered.
  • Every key in a dictionary is distinct from the others.
  • Operations: insertion, deletion, lookup
  • Implementations: association lists, trees