16 - memory representations and abstract data...

52
Memory Representations and the List Abstract Data Type COMP110 - Lecture 16

Upload: others

Post on 07-Apr-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

Memory Representations and the List Abstract Data Type

COMP110 - Lecture 16

Page 2: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

Announcements

• WS02 is This Week’s Assignment

• Second half deadline extended to Sunday at 11:59pm

• Office Hours are Whiteboard Only for Part 2

• Why?

• It is valuable practice for understanding loops and arrays without relying on the computer.

Page 3: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

MT0 Score

# of Tickets

Sweet Spot

0 1-3 4-6 7-9 10-15 16+

Page 4: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

Today

• We’ll build a halloween costume idea generator and learn about memory representations of our data.

Page 5: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

Data Representations• Binary

• Numbers

• Characters

• Strings

• Arrays

• Objects (Groups of primitives)

Page 6: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

How is data physically represented in memory?

• Memory is a vast bank of nano-scale transistors and capacitors holding billions of “bits” of information.

• A bit can have two values:

• 0 or 1

• A byte is 8 bits.

• We tend to imagine memory looks like this. Each address holds one byte.

01234567

Addresses 8 through 1015

10231022102110201019101810171016

Page 7: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

The Beauty of Abstraction

• We’re about to look under the hood at how values are represented in memory …

• … yet we didn’t need to know this to write Java programs.

• Good abstractions allow us to code at larger scales (integers) without sweating the details (integers are just bytes). 0

1234567

Addresses 8 through 1015

10231022102110201019101810171016

Page 8: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

If all memory can hold are 0s and 1s, how do programs work

with numbers and Strings?

• What do these variables look like in memory?

• int[]a=newint[]{85,78,67};

• Strings=“UNC”;

• We interpret groups of bits using a base 2 numeral system.

• “binary”

• This allows us to represent more useful values.0

010000110000000001001110000000000101010100000000

1234567

Addresses 8 through 1015

10231022102110201019101810171016

Page 9: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

0 0 0 0 0 0 0 0

Binary works like Base Ten

1248163264128

with only 0s and 1s

0 0 0

110100

Base Ten:

Binary:

Page 10: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

0 0 0 0 0 0 0 1

Binary works like Base Ten

1248163264128

with only 0s and 1s

0 0 1

110100

Base Ten:

Binary:

Page 11: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

0 0 0 0 0 0 1 0

Binary works like Base Ten

1248163264128

with only 0s and 1s

0 0 2

110100

Base Ten:

Binary:

Page 12: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

0 0 0 0 0 0 1 1

Binary works like Base Ten

1248163264128

with only 0s and 1s

0 0 3

110100

Base Ten:

Binary:

Page 13: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

0 0 0 0 0 1 0 0

Binary works like Base Ten

1248163264128

with only 0s and 1s

0 0 4

110100

Base Ten:

Binary:

Page 14: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

0 0 0 0 0 1 0 1

Binary works like Base Ten

1248163264128

with only 0s and 1s

0 0 5

110100

Base Ten:

Binary:

Page 15: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

0 0 0 0 0 1 1 0

Binary works like Base Ten

1248163264128

with only 0s and 1s

0 0 6

110100

Base Ten:

Binary:

Page 16: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

0 0 0 0 0 1 1 1

Binary works like Base Ten

1248163264128

with only 0s and 1s

0 0 7

110100

Base Ten:

Binary:

Page 17: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

0 0 0 0 1 0 0 0

Binary works like Base Ten

1248163264128

with only 0s and 1s

0 0 8

110100

Base Ten:

Binary:

Page 18: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

0 0 0 0 1 0 0 1

Binary works like Base Ten

1248163264128

with only 0s and 1s

0 0 9

110100

Base Ten:

Binary:

Page 19: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

0 0 0 0 1 0 1 0

Binary works like Base Ten

1248163264128

with only 0s and 1s

0 1 0

110100

Base Ten:

Binary:

Page 20: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

0 0 0 0 1 0 1 1

Binary works like Base Ten

1248163264128

with only 0s and 1s

0 1 1

110100

Base Ten:

Binary:

Page 21: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

0 0 0 0 1 1 0 0

Binary works like Base Ten

1248163264128

with only 0s and 1s

0 1 2

110100

Base Ten:

Binary:

Page 22: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

Convert this value from binary

to an integer.

0

010000110100111001010101

1234567

Addresses 8 through 1015

10231022102110201019101810171016

0 1 0 1 0 1 0 1

1248163264128

Every “place” with a 1, add up the number below it.

Page 23: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

Convert this value from binary

to an integer.

0

0100001101001110

851234567

Addresses 8 through 1015

10231022102110201019101810171016

0 1 0 1 0 1 0 1

1248163264128

Every “place” with a 1, add up the number below it.

This is 85.

Page 24: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

Java has 4 types of integers

Type # of bits # of bytes Min Max

int 32 4 -2,147,483,648 2,147,483,647

byte 8 1 -128 127

short 16 2 -32768 32767

long 64 8 -9,223,372,036,854,775,808 9,223,372,036,854,775,807

Page 25: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

Here’s roughly what memory looks like after we declare

and assign some variables…

0

010000110100111001010101

1234567

Addresses 8 through 1015

10231022102110201019101810171016

bytea,b,c;a=85;b=78;c=67;

cba

Page 26: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

What if we declare int instead of byte?

0

0100111000000000000000000000000001010101000000000000000000000000

1234567

Addresses 8 through 1015

10231022102110201019101810171016

inta,b,c;a=85;b=78;c=67;

b

a

Page 27: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

Should I worry about an int using 4 bytes?

0

0100111000000000000000000000000001010101000000000000000000000000

1234567

Addresses 8 through 7,999,999,992

8,000,000,000

b

a

W.W.B.G.D.?

Hey Bill, you have $82,000,000,000 dollars. Do you worry yourself when

something costs $10 vs. $40?

7,999,999,9997,999,999,9987,999,999,9977,999,999,9967,999,999,9957,999,999,9947,999,999,993

As a programmer living in 2016, you’re a byte billionaire. Live it up.

Page 28: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

What about float and double?

0

0000000000000000

YOUWILL

LEARNIN

COMP411

1234567

Addresses 8 through 1015

10231022102110201019101810171016

floata,b,c;a=85.0;b=78.0;c=67.0;

Page 29: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

What about character values?

0

010000110000000001000011000000000100111000000000

1234567

Addresses 8 through 1015

10231022102110201019101810171016

chara,b,c;a=85;b=78;c=67; a

b

c

Page 30: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

Hands-on1. In PrimitiveDataTypes.java’s main method, uncomment:

PrimitiveDataTypesRunner.charExample();

2. In the charExample method, after the 3 char variables are initialized, print out the following using System.out.println:

3. “The chars are: “ + a + b + c

4. Check-in on pollev.com/comp110 when you see what printed.

5. Already done? Try changing the numbers assigned to each char to spell ‘ABC’.

Page 31: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

An ASCII Table is a char encoding table

American Standard Code for Information Interchange

85

78

67

U

N

C

Page 32: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

What about character values?

0

010000110000000001000011000000000100111000000000

1234567

Addresses 8 through 1015

10231022102110201019101810171016

chara,b,c;a='U';b='N';c='C'; a

b

c

It’s not often you work with char values directly, but you can surround single letters in single quotes when you do.

Page 33: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

U N C

A Stringis an array of chars

85 78 67

“ ”

Page 34: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

What is a String?

0

010000110000000001000011000000000100111000000000

1234567

Addresses 8 through 1015

10231022102110201019101810171016

char[]_s=newchar[]{'U','N','C'};

s

• String is a class. Each String instance has an instance variable storing array of characters like the one above.

Page 35: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

Why do we have to write new char[3]?

0

010000110000000001000011000000000100111000000000

1234567

Addresses 8 through 1015

10231022102110201019101810171016

s

• When an array is initialized, we must specify its type and length.

• This is how the computer knows how much memory to allocate for the array.

• char (2 bytes per) array with a length of 3? Allocate 6 bytes of memory.

Page 36: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

How does array indexing work?

0

010000110000000001000011000000000100111000000000

1234567

Addresses 8 through 1015

10231022102110201019101810171016

s• Java keeps track of the first address of

byte 0 in each array.

• When an index is requested, Java computes how many bytes away from the beginning address it is.

• sbegin is 1017

• s[n] address is:sbegin + n * bytesper element

• s[0] is 1017 + 0 * 2 => 1017 • s[1] is 1017 + 1 * 2 => 1019 • s[2] is 1017 + 2 * 2 => 1021

Page 37: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

And that’s the magic behind…

0

010000110000000001000011000000000100111000000000

1234567

Addresses 8 through 1015

10231022102110201019101810171016

char[]s=newnewchar[]{‘U’,'N','C'};

short[]a=newshort[]{85,78,67};

• In physical memory, these two arrays would look exactly the same in terms of 1s and 0s.

• The meaning we assign to binary values is what makes all the difference.

Page 38: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

How are objects stored?• Imagine a class with two instance variables:

classPoint{privateint_x,_y;

publicPoint(intx,inty){//elided}}

• When we construct a new instance, we allocate memory for each instance variable:

Pointp=newPoint(1,2);0

0000001000000000000000000000000000000001000000000000000000000000

1234567

Addresses 8 through 1015

10231022102110201019101810171016

p

p._x

p._y

Page 39: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

Abstract Data Types• The language’s primitive types (int, double, boolean, char) are a

language’s “atoms”

• We as programmers combine primitives into objects to model specific ideas of our apps and problems

• i.e. a Profile object has a String name, an int age, and a boolean isPublic

• Over time, programmers have realized some concepts are generally useful for organizing the data and objects in our programs.

• We generally call these concepts Abstract Data Types

• An ADT is a model for organizing data with specific characteristics and capabilities.

Page 40: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

Abstract Data Types• Some of the most common and useful ADTs

• List - a sequence of ordered values where values can occur more than once.

• Set - an unordered set of values with no repeated values.

• Associative Array - “Dictionary” like an array, but the indexing is up to you. Index could be a String.

• Others: Graph, Tree

• We’ll explore and implement ADTs over the coming weeks.

Page 41: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

Back to Halloween Costumes

• We want to pick randomly from a List of adjectives and a List of nouns.

• Users can add more ideas to both lists. We don’t know how many will be in each list at the same time.

• Why can’t we just use an array for each list?

My favorite UNC costume:Cherie Berry

Page 42: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

Wouldn’t it be nice…• If we had a List of String values we could just add to without

worrying about how many we have?

• At this point in the semester, we can build a StringList class to do just that!

• We’ll have a class with the following methods:

• void add(String value) - Add a value to our List.

• String get(int index) - Get the String held at an index in the List.

• int size() - Returns the # of Strings in the List.

Page 43: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

Follow-Along: StringList

• Open StringList.java and StringListRunner.java

• Try running the runner.

Page 44: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

// Implement your _values resize logic here. if (_count == _values.length) {

// Step 1. int newLength = _values.length * 2; String[] _newValues = new String[newLength];

// Step 2. int i = 0; while (i < _values.length) { _newValues[i] = _values[i]; i++; }

// Step 3. _values = _newValues; }

Page 45: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

Changing Gears: Looping

Page 46: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

• General form of a while loop statement: while(<booleantestcondition>){<whilebody:codetorepeat>}

• A while loop statement can be used in any method’s body.

• The test condition must be a booleanexpression

• A while loop will repeat the code in its body again and again while the test condition is true.

while Loop Statement

49

Page 47: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

How do we write a while loop that will loop N times?

• When we’ve needed to loop some specific number of times, we’ve followed this pattern:

inti=0;

while(i<N){<codetorepeat>i++;}

50

Initialize “counter variable”

Bound the loop on that variable

Modify the variable’s value

Page 48: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

• Looping with a counter variable is so common, many programming languages have shorthand syntax for this.

• Java (and many others) have a for loop:

for(<initcounter>;<bound>;<modifycounter>){<forbody:codetorepeat>}

• We can rewrite the previous while loop as a for loop:

for(inti=0;i<N;i++){<forbody:codetorepeat>}

for Loop Statement

51

Page 49: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

• The following while loop:

inti=0;while(i<N){<whilebody:codetorepeat>i++;}

• Can be written as a for loop:

for(inti=0;i<N;i++){<forbody:codetorepeat>}

whileandfor Loops Can Be Equivalent

52

Page 50: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

for(inti=0;i<N;i++){<forbody:codetorepeat>}

for Loop Syntax

53

1. The counter variable initialization happens first and only once.

2. The test condition is checked. If true, go on. If false, jump past loop.

3. The code within the for body is run.

4. The counter variable is modified.

5. Jump to step 2.

Page 51: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

Hands-on• Rewrite the while loop in the

add method of StringList to be a for loop, instead.

• Check-in on PollEv when you’re done!

• The following while loop:

inti=0;while(i<N){<body>i++;}

• Can be written as a for loop:

for(inti=0;i<N;i++){<body>}

Page 52: 16 - Memory Representations and Abstract Data Typess3.amazonaws.com/110-2016-fall/Slides/16-Memory-Representation… · Announcements • WS02 is This Week’s Assignment • Second

Let’s rewrite that while loop as a for loop

// Implement your _values resize logic here. if (_count == _values.length) {

// Step 1. int newLength = _values.length * 2; String[] _newValues = new String[newLength];

// Step 2. for (int i = 0; i < _values.length; i++) { _newValues[i] = _values[i]; }

// Step 3. _values = _newValues; }