Princeton COS 126 - Encapsulation and ADTs (7 pages)

Previewing pages 1, 2 of 7 page document View the full content.
View Full Document

Encapsulation and ADTs



Previewing pages 1, 2 of actual document.

View the full content.
View Full Document
View Full Document

Encapsulation and ADTs

70 views

Lecture Notes


Pages:
7
School:
Princeton University
Course:
Cos 126 - General Computer Science

Unformatted text preview:

Abstract Data Types 3 4 Encapsulation and ADTs Data type Set of values and operations on those values Ex int String Complex Vector Document Wave Tour Abstract data type Data type whose internal representation is hidden Separate implementation from design specification Class provides data representation and code for operations Client uses data type as black box API specifies contract between client and class Bond What s your escape route Saunders Sorry old man Section 26 paragraph 5 that information is on a need to know basis only I m sure you ll understand Introduction to Computer Science Robert Sedgewick and Kevin Wayne Copyright 2005 http www cs Princeton EDU IntroCS 2 Counter Data Type Counter Abstract Data Type Counter Data type to count electronic votes Counter Abstract data type to count electronic votes public class Counter int count public class Counter private int count public Counter count 0 public void hit count public int get return count public Counter count 0 public void hit count public int get return count Legal Java client Counter c new Counter c count 16022 Does not compile Pitfall Al Gore receives 16 022 votes in Volusia County Florida Counter c new Counter c count 16022 Benefit Can guarantee invariant that each data type value remains in a consistent state 5 6 Changing Internal Representation Time Bombs Java ADTs Keep data representation hidden with private access modifier Expose API to clients using public access modifier Y2K Two digit years January 1 2000 Y2038 32 bit seconds since 1970 January 19 2038 ZIP codes USPS changed from ZIP to ZIP 4 code in 1983 public class Complex private double re private double im public public public public public Complex double re double im double abs Complex plus Complex b Complex times Complex b String toString VIN numbers Will run out by 2010 representation change ahead Lesson By exposing data representation to client need to sift through millions of lines of code in client to update Advantage Can switch to



View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

Loading Unlocking...
Login

Join to view Encapsulation and ADTs and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Encapsulation and ADTs and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?