AP Computer Science Principles · Study guide

Data Abstraction

AP® Computer Science Principles · Big Idea 3 · Algorithms and Programming

Data Abstraction

How lists and strings store values in order, how a list is created and copied, and why giving a whole collection one name makes programs easier to write and change.

1Lists, elements, and indices

A list is an ordered sequence of values, written [value1, value2, value3, …]: value1 is the first element, value2 the second, and so on. Each value is an element, and each element has its own index, a whole number that says where it sits. A string works the same way with characters.

WordMeaningExample from Figure 1
listan ordered sequence of elements[12, 7, 30, 7, 25]
elementone value in a list; each element has its own indexthe 30
indexa whole number used to point to one element of a list or one character of a string3
stringan ordered sequence of characters"AP CS"
Figure 1. Every element has a unique index, counting from 1. The same 7 appears at indices 2 and 4, but each position is its own element.

Order is part of the meaning. [3, 1, 2] and [1, 2, 3] hold the same numbers but are different lists, and "CAT" and "ACT" are different strings. An index is a position, not a value: in houses, the element at index 3 is 30.

2Making a list

Reference languageWhat it does
aList ← [value1, value2, value3, …]creates a new list with those values at indices 1, 2, 3, … and assigns it to aList
aList ← []creates a new empty list (length 0) and assigns it to aList
aList ← bListassigns a copy of the list bList to aList

The last row is the one that surprises people. Assigning a list makes a copy, so the two lists are separate afterward:

bList ← [20, 40, 60]
aList ← bList
bList[1] ← 5
DISPLAY(aList)
DISPLAY(bList)

This displays [20, 40, 60] and then [5, 40, 60]. Changing bList after the copy did nothing to aList.

3Staying inside the list

Indices run from 1 to the length of the list. Many real programming languages start counting at 0; the reference language does not. Asking for an index below 1 or above the length is an error: the program shows an error message and stops.

scores ← [88, 92, 75]
DISPLAY(scores[0])

This list has 3 elements, so the only valid indices are 1, 2, and 3. scores[0] stops the program, and so would scores[4].

4Data abstraction

Suppose a program averages five daily temperatures. Without a list, each one needs its own variable:

Five separate variables
day1 ← 61
day2 ← 64
day3 ← 58
day4 ← 70
day5 ← 67
average ← (day1 + day2 + day3 + day4 + day5) / 5
DISPLAY(average)
One list
temps ← [61, 64, 58, 70, 67]
total ← 0
FOR EACH t IN temps
{
  total ← total + t
}
DISPLAY(total / LENGTH(temps))

Both display 64. Now add a sixth day. The first version needs a new variable and a new formula. In the list version you only add the number to the list; the loop and the average are unchanged, and the program displays 66.

That is data abstraction: giving a collection of data one name without referring to the details of how it is stored. It separates the abstract properties of the data (an ordered collection you can index and count) from the concrete details of its representation (where and how the computer keeps it).

Figure 2. The program refers to the collection by its name. How the values are stored can change without changing a line of the code above.

This is how data abstraction manages complexity:

  • Many related items are treated as a single value, so code can pass, copy, or loop over the whole collection by name.
  • The code does not depend on how many items there are, so it works for 5 temperatures or 5,000.
  • The program is easier to develop and maintain: a change in the data, like a new day, does not ripple through the code.

Lists are called different names in different languages; array is a common one. The idea is the same.

The key idea

A list is an ordered sequence of elements, each with an index from 1 to the length. Using a list as a data abstraction gives a whole collection one name, hides how it is stored, and lets the same code work no matter how many items there are.

5Mixed types in one list

A data abstraction can hold different types of elements. One list can describe a student with a string, a number, and a Boolean:

student ← ["Dev", 17, true]
DISPLAY(student[1])
DISPLAY(student[2] + 1)

It displays Dev and then 18. The program treats the student as one value named student, and reaches each detail by its index.

6Worked examples

Worked Example 1 Read a list

Duplicates and the last element

For v ← [9, 4, 4, 1], find (a) v[2], (b) every index where 4 appears, (c) v[LENGTH(v)].

  1. 1
    (a) Count from 1: 9 is at index 1, so v[2] is 4.
  2. 2
    (b) The value 4 is at indices 2 and 3. Two elements can hold the same value; each still has its own index.
  3. 3
    (c) LENGTH(v) is 4, and v[4] is 1. This expression always gives the last element.
Answer: (a) 4, (b) 2 and 3, (c) 1.
Worked Example 2 Trace a copy

The backup

What is displayed?

original ← ["red", "green", "blue"]
backup ← original
original[2] ← "gold"
DISPLAY(backup[2])
  1. 1
    Line 2 gives backup a copy of the list as it is now.
  2. 2
    Line 3 changes element 2 of original only.
Answer: green. The backup kept the old value.
Worked Example 3 Explain

Why use a list?

A game keeps the high scores of its top 10 players. Explain how storing them in one list named highScores manages complexity, compared with ten variables.

  1. 1
    The list gives the ten scores one name, so the code can treat them as a single value.
  2. 2
    A loop over highScores can display, sort, or search all of them without naming each one.
  3. 3
    If the game later keeps the top 20, the code that uses highScores does not need to change.
Answer: One name for the collection hides the details and keeps the code the same size however many scores there are.
Worked Example 4 Find the error

One past the end

What happens when this segment runs?

quiz ← [7, 9, 10]
i ← LENGTH(quiz) + 1
DISPLAY(quiz[i])
  1. 1
    LENGTH(quiz) is 3, so i is 4.
  2. 2
    Valid indices are 1 to 3. Index 4 is past the end.
Answer: An error message, and the program stops.

7Common mistakes

Common Mistake 1 · Counting from 0

The first element is at index 1, and the last is at index LENGTH(aList). aList[0] is an error.

Common Mistake 2 · Mixing up index and value

“The element at index 3” is a value in the list. “The index of 30” is a position. Read which one the question wants.

Common Mistake 3 · Thinking a list assignment links the lists

aList ← bList stores a copy. Later changes to either list do not affect the other.

Common Mistake 4 · Forgetting that a space is a character

In "AP CS" the space is character 3, and the string has length 5.

Explain data abstraction like this

“The program stores the temperatures in one list named temps. This data abstraction manages complexity because the code refers to the whole collection by one name instead of a separate variable for each day, so the same loop works for any number of days and the program is easier to change.”

Name the list, say what it holds, and say how one name for the collection makes the code simpler or easier to change.