adt:s as programming tools · handout alarmclock • test-run! lecture 13 - håkan jonsson...

5
D0010E Lecture 13 Iterators MasterMind Random numbers ADT:s as Programming Tools Lecture 13 - Håkan Jonsson Lab 5 • Reminder: Groups must have their designs approved before any actual programming is allowed to start. Some review sessions will also be available on Tuesday, but opt for those on Monday of you can (gives you more time). • Extra help with design and UML. – Copies handed out now. • Extra-extra help concerning lab 5 found! A precious napkin from a meeting in a restaurant somewhere close... Lecture 13 - Håkan Jonsson Lecture 12 - Håkan Jonsson 4 ADT:s as programming tools • An Abstract Data Type (ADT) gives an abstract view on a class of objects. • States what objects are and do, not how this is achieved in code. • Formulating parts of programs as ADT:s makes it easier to program since implementation details can be ignored until later. – To finally get a working program, code must of course be supplied.

Upload: others

Post on 27-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ADT:s as programming tools · Handout AlarmClock • Test-run! Lecture 13 - Håkan Jonsson Iterators • Riley 2.9, 4.4 • An iterator is an object the provides the possibility to

D0010E!Lecture 13!

Iterators!MasterMind

Random numbers!

ADT:s as Programming

Tools!

Lecture 13 - Håkan Jonsson

Lab 5 •  Reminder:

–  Groups must have their designs approved before any actual programming is allowed to start.

–  Some review sessions will also be available on Tuesday, but opt for those on Monday of you can (gives you more time).

•  Extra help with design and UML. – Copies handed out now.

•  Extra-extra help concerning lab 5 found! –  A precious napkin from a meeting in a

restaurant somewhere close...

Lecture 13 - Håkan Jonsson Lecture 12 - Håkan Jonsson 4

ADT:s as programming tools • An Abstract Data Type (ADT) gives

an abstract view on a class of objects.

• States what objects are and do, not how this is achieved in code.

• Formulating parts of programs as ADT:s makes it easier to program since implementation details can be ignored until later. – To finally get a working program,

code must of course be supplied.

Page 2: ADT:s as programming tools · Handout AlarmClock • Test-run! Lecture 13 - Håkan Jonsson Iterators • Riley 2.9, 4.4 • An iterator is an object the provides the possibility to

Lecture 12 - Håkan Jonsson 5

When reading Riley •  In fact, if you read Part 3 in the course

compendium carefully, programs (a class) are often presented in five (5) ways: –  Informal: An informal description of a kind of object

and maybe even a picture. –  Design: An ADT specification.

•  What is an object like this, what can it do, and what is true about it and its actions?

–  Design: A UML-diagram. •  Show details and relationships with other ADT:s (their

UML-diagrams). –  Coding: An implementation of the ADT as an

abstract class or an interface. •  Idea: Keep the abstract view separated from the

implementation view.

–  Coding: A concrete implementation of a class that is faithful to the ADT.

•  Pure working code!

Design!

Coding!

Lecture 12 - Håkan Jonsson 6

Representation • To implement an ADT involves

choosing a representation. – The representation is the concrete

data that is used in the implementation to achieve what the ADT stands for.

• Major questions: – What variables should be used? What

should their types be? – How should these variables be used?

Example: AlarmClock • Fig. 1.3, 1.6, handout about

AlarmClock. • Note that AlarmClock makes use of

TimeOfDay but is independant of its particular implementation. – AlarmClock is a client of TimeOfDay.

• The programmer that implements AlarmClock also acts as a client.

– The programmer only needs the abstract view of TimeOfDay provided by the ADT and the UML diagram.

Lecture 12 - Håkan Jonsson 7 Lecture 12 - Håkan Jonsson 8

Handout AlarmClock • Test-run!

Page 3: ADT:s as programming tools · Handout AlarmClock • Test-run! Lecture 13 - Håkan Jonsson Iterators • Riley 2.9, 4.4 • An iterator is an object the provides the possibility to

Lecture 13 - Håkan Jonsson

Iterators •  Riley 2.9, 4.4 •  An iterator is an object the provides the

possibility to access (“visit”) all items of a container one at a time.

•  A very common operation in computer programs.

•  Iterator Design Pattern –  Fig 2.18 –  interface Iterator –  Note that the container class has a method that

returns an iterator. –  Visits to items in a container are made via this

iterator object, not the container itself. •  Since the iterator class is an inner class of the

container class, it can access everything in the (outer) container class.

Lecture 13 - Håkan Jonsson

Fig 2.18 Iterator Design Pattern!

Lecture 13 - Håkan Jonsson

Fig 4.14 SimpleSequence UML

Lecture 13 - Håkan Jonsson

import java.util.*; public abstract class SimpleSequenceADT { public SimpleSequenceADT (int m ) {} public int size() { Object temp; int result = 0; Iterator it = iterator(); while ( it.hasNext() ) { temp = it.next(); result++; } return result; } public Object ith( int i ) { if (i < 1) { throw new NoSuchElementException(); } Object result = null; Iterator it = iterator(); for (int j=i; j!=0; j--) { result = it.next(); } return result; } public abstract void concatItem( Object z ); public abstract Iterator iterator(); }

Page 4: ADT:s as programming tools · Handout AlarmClock • Test-run! Lecture 13 - Håkan Jonsson Iterators • Riley 2.9, 4.4 • An iterator is an object the provides the possibility to

Lecture 13 - Håkan Jonsson

import java.util.*; public class SimpleSequenceArray extends SimpleSequenceADT { private int count; private Object[] seqItems; public SimpleSequenceArray( int m ) { super(m); seqItems = new Object[m]; count = 0; } public int size() { return count; } public Object ith( int i) { return seqItems[i-1]; } public void concatItem( Object z ) { if (count == seqItems.length) { throw new IllegalStateException(); } else { seqItems[count] = z; count++; } } public Iterator iterator() { return new SimpleSequenceIterator(); }

Lecture 13 - Håkan Jonsson

private class SimpleSequenceIterator implements Iterator { private int itemsProbed; public SimpleSequenceIterator() { itemsProbed = 0; } public Object next() { if (itemsProbed == count) { throw new NoSuchElementException(); } else { itemsProbed++; return seqItems[itemsProbed-1]; } } public boolean hasNext() { return itemsProbed < count; } public void remove() { throw new UnsupportedOperationException(); } } }

The for-each loop

Lecture 13 - Håkan Jonsson

The elements in the array/collection can only be accessed, not changed.

MasterMind • MasterMind is a board game for

two players.

• A code breaker tries to guess a secret code while the codemaker gives cryptic feedback on the guesses.

• To program MasterMind one can proceed as in the handout.

Lecture 13 - Håkan Jonsson

Page 5: ADT:s as programming tools · Handout AlarmClock • Test-run! Lecture 13 - Håkan Jonsson Iterators • Riley 2.9, 4.4 • An iterator is an object the provides the possibility to

Lecture 13 - Håkan Jonsson

Design discussion • Identify classes, and relationships

between them.

• What is the responsibility of each class? –  If you know this, it is relatively easy

to modify the design.

Lecture 13 - Håkan Jonsson

Random • Examples of how randomness can

be used in Java. – Note: This is not true randomness but

“pseudo-randomness” (very good but fake).

Lecture 13 - Håkan Jonsson