Midterm 1 - University of...

20

Transcript of Midterm 1 - University of...

Page 1: Midterm 1 - University of Iowahomepage.cs.uiowa.edu/~bdmyers/cs2230_sp17/public/lectures/lecture-011-polymorphism.pdfMidterm 1 •Next Friday 2/17 in class, 50 minutes •What is on
Page 2: Midterm 1 - University of Iowahomepage.cs.uiowa.edu/~bdmyers/cs2230_sp17/public/lectures/lecture-011-polymorphism.pdfMidterm 1 •Next Friday 2/17 in class, 50 minutes •What is on

Midterm1

• NextFriday2/17inclass,50minutes• Whatisonit?• Anythingyou’vepracticedincludingHW3(incl.quizzes,peerinstructions,HWs,discussionwork)

• Isthereanypractice/review?• We’llputupatleast1priorexam/solutions• Discussionon2/13-2/15willbeusedasareview(comereadytoworkonpracticeexamquestions)

• Notesallowed?• 1double-sidedsheetofnotes

Page 3: Midterm 1 - University of Iowahomepage.cs.uiowa.edu/~bdmyers/cs2230_sp17/public/lectures/lecture-011-polymorphism.pdfMidterm 1 •Next Friday 2/17 in class, 50 minutes •What is on

CS2230CSII:Datastructures

Meeting9:ListADT,BrandonMyers

UniversityofIowa

Page 4: Midterm 1 - University of Iowahomepage.cs.uiowa.edu/~bdmyers/cs2230_sp17/public/lectures/lecture-011-polymorphism.pdfMidterm 1 •Next Friday 2/17 in class, 50 minutes •What is on

Today’sbigideas

• InterfacescanbeusedtoexpressAbstractDataTypes,likeList

• Generics allowustobuilddatastructuresthatcanholdelementsofanytype

• LearnabouttwomoreAbstractDataTypes,StackandQueue

Page 5: Midterm 1 - University of Iowahomepage.cs.uiowa.edu/~bdmyers/cs2230_sp17/public/lectures/lecture-011-polymorphism.pdfMidterm 1 •Next Friday 2/17 in class, 50 minutes •What is on

public interface List {// add the element to the end of the listpublic void append(Object element);

// check if the element exists in the listpublic boolean contains(Object element);

// remove first element from the list and return itpublic Object removeFirst();

// return true if the list is emptypublic boolean isEmpty();

}

class ArrayList implements List {private Object[] elements;private int numElements;@Overridepublic void append(Object element) {}

@Overridepublic boolean contains(Object element) {

}

@Overridepublic Object removeFirst() {

}

@Overridepublic boolean isEmpty() {

}}

class LinkedList implements List {private ListNode header;

@Overridepublic void append(Object element) {}

@Overridepublic boolean contains(Object element) {

}

@Overridepublic Object removeFirst() {

}

@Overridepublic boolean isEmpty() {

}}

Page 6: Midterm 1 - University of Iowahomepage.cs.uiowa.edu/~bdmyers/cs2230_sp17/public/lectures/lecture-011-polymorphism.pdfMidterm 1 •Next Friday 2/17 in class, 50 minutes •What is on

InArrayList’s implementationofList.append,whatshouldwedointhiscase?

a) allocateabiggerarrayandcopyalltheelementstoitb) returnanerrorc) increasethelengthofthearraytomakeroomd) replacethelastelementwith900e) don’tchangeanything

myList.append(900)

400 500 600 700 800

5numElements

elements

myList

https://b.socrative.com/login/student/CS2230Aids1000-4999CS2230Bids5000+

Page 7: Midterm 1 - University of Iowahomepage.cs.uiowa.edu/~bdmyers/cs2230_sp17/public/lectures/lecture-011-polymorphism.pdfMidterm 1 •Next Friday 2/17 in class, 50 minutes •What is on

class ArrayList implements List {private Object[] elements;private int numElements;

@Overridepublic void append(Object ele) {

// finish this method}

}

public interface List {// add the element to the end of the listpublic void append(Object ele);

}

foryourinformation,hereistherelevantpartofList

makeagroupof3students:• manager(ensureeveryonecontributes)• scribe(sitsinthemiddle,doesn’tdecidewhattowrite)• skeptic(questioneverything)

400 500 600 700 800

5numElements

elements

myList

Page 8: Midterm 1 - University of Iowahomepage.cs.uiowa.edu/~bdmyers/cs2230_sp17/public/lectures/lecture-011-polymorphism.pdfMidterm 1 •Next Friday 2/17 in class, 50 minutes •What is on

Generics allowustobuilddatastructuresthatcanholdelementsofanytype

Page 9: Midterm 1 - University of Iowahomepage.cs.uiowa.edu/~bdmyers/cs2230_sp17/public/lectures/lecture-011-polymorphism.pdfMidterm 1 •Next Friday 2/17 in class, 50 minutes •What is on

/*Interface for a generic List ADT, an ordered collection of elements

GList<T> can store objects of some type T;for example, a GList<String> can store Stringsand a GList<Cat> can store Cats*/public interface GList<T> {

// add the element to the end of the listpublic void append(T element);

// check if the element exists in the listpublic boolean contains(T element);

// remove first element from the list and return itpublic T removeFirst();

// return true if the list is emptypublic boolean isEmpty();

}

Page 10: Midterm 1 - University of Iowahomepage.cs.uiowa.edu/~bdmyers/cs2230_sp17/public/lectures/lecture-011-polymorphism.pdfMidterm 1 •Next Friday 2/17 in class, 50 minutes •What is on

public interface Glist<T> {// add the element to the end of the listpublic void append(T element);

// check if the element exists in the listpublic boolean contains(T element);

// remove first element from the list and return itpublic T removeFirst();

// return true if the list is emptypublic boolean isEmpty();

}

class LinkedList<T> implements Glist<T> {private ListNode header;

@Overridepublic void append(T element) {}

@Overridepublic boolean contains(T element) {

}

@Overridepublic T removeFirst() {

}

@Overridepublic boolean isEmpty() {

}}

unlimitednumberofLinkedList classesforthepriceof1!!

\

head

tail

data next data next data next

200 300 \100

head

tail

data next data next data next

"Siamese" 5.8

"Tabby" 6"GrumpyC

at" 1

LinkedList<Cat>

LinkedList<Integer>

"B" "C" \"A"

head

tail

data next data next data next

LinkedList<String>

Page 11: Midterm 1 - University of Iowahomepage.cs.uiowa.edu/~bdmyers/cs2230_sp17/public/lectures/lecture-011-polymorphism.pdfMidterm 1 •Next Friday 2/17 in class, 50 minutes •What is on

public interface Glist<T> {// add the element to the end of the listpublic void append(T element);

// check if the element exists in the listpublic boolean contains(T element);

// remove first element from the list and return itpublic T removeFirst();

// return true if the list is emptypublic boolean isEmpty();

}

class ArrayList<T> implements GList<T> {private T[] elements;private int numElements;@Overridepublic void append(T element) {}

@Overridepublic boolean contains(T element) {

}

@Overridepublic T removeFirst() {

}

@Overridepublic boolean isEmpty() {

}}

class LinkedList<T> implements Glist<T> {private ListNode header;

@Overridepublic void append(T element) {}

@Overridepublic boolean contains(T element) {

}

@Overridepublic T removeFirst() {

}

@Overridepublic boolean isEmpty() {

}}

Page 12: Midterm 1 - University of Iowahomepage.cs.uiowa.edu/~bdmyers/cs2230_sp17/public/lectures/lecture-011-polymorphism.pdfMidterm 1 •Next Friday 2/17 in class, 50 minutes •What is on

LearnabouttwomoreAbstractDataTypes,StackandQueue

Page 13: Midterm 1 - University of Iowahomepage.cs.uiowa.edu/~bdmyers/cs2230_sp17/public/lectures/lecture-011-polymorphism.pdfMidterm 1 •Next Friday 2/17 in class, 50 minutes •What is on

QueueADT

https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Data_Queue.svg/2000px-Data_Queue.svg.png

Page 14: Midterm 1 - University of Iowahomepage.cs.uiowa.edu/~bdmyers/cs2230_sp17/public/lectures/lecture-011-polymorphism.pdfMidterm 1 •Next Friday 2/17 in class, 50 minutes •What is on

public interface Queue<T> {/* Insert element at back of queue */public void enqueue(T ele);

/* Remove element from front of queueand return it*/public T dequeue();

/* Return the element at the front of queue */public T peek();

}

Page 15: Midterm 1 - University of Iowahomepage.cs.uiowa.edu/~bdmyers/cs2230_sp17/public/lectures/lecture-011-polymorphism.pdfMidterm 1 •Next Friday 2/17 in class, 50 minutes •What is on

/*Interface for a generic List ADT, an ordered collection of elements

GList<T> can store objects of some type T;for example, a GList<String> can store Stringsand a GList<Cat> can store Cats*/public interface GList<T> {

// add the element to the end of the listpublic void append(T element);

// check if the element exists in the listpublic boolean contains(T element);

// remove first element from the list and return itpublic T removeFirst();

// return true if the list is emptypublic boolean isEmpty();

}

public interface Queue<T> {/* Insert element at back of queue */public void enqueue(T ele);

/* Remove element from front of queueand return it*/public T dequeue();

/* Return the element at the front of queue */public T peek();

}

https://b.socrative.com/login/student/CS2230Aids1000-4999CS2230Bids5000+

True/False

Queue<T>couldbeimplementedusingaGList<T>

Page 16: Midterm 1 - University of Iowahomepage.cs.uiowa.edu/~bdmyers/cs2230_sp17/public/lectures/lecture-011-polymorphism.pdfMidterm 1 •Next Friday 2/17 in class, 50 minutes •What is on

StackADT

Page 17: Midterm 1 - University of Iowahomepage.cs.uiowa.edu/~bdmyers/cs2230_sp17/public/lectures/lecture-011-polymorphism.pdfMidterm 1 •Next Friday 2/17 in class, 50 minutes •What is on

Real-lifeexamplewhere...

• aprocess/situation/etc behaveslikeastack• or,aspecificapplicationwhereastackdatastructurewouldbeuseful

https://b.socrative.com/login/student/CS2230Aids1000-4999CS2230Bids5000+

Page 18: Midterm 1 - University of Iowahomepage.cs.uiowa.edu/~bdmyers/cs2230_sp17/public/lectures/lecture-011-polymorphism.pdfMidterm 1 •Next Friday 2/17 in class, 50 minutes •What is on

public interface Stack<T> {/* put the element on the top of the stack */public void push(T ele);

/* remove the element on top of the stackand return it*/public T pop();

/* return the element on top of the stack */public T peek();

}

Page 19: Midterm 1 - University of Iowahomepage.cs.uiowa.edu/~bdmyers/cs2230_sp17/public/lectures/lecture-011-polymorphism.pdfMidterm 1 •Next Friday 2/17 in class, 50 minutes •What is on

/*Interface for a generic List ADT, an ordered collection of elements

GList<T> can store objects of some type T;for example, a GList<String> can store Stringsand a GList<Cat> can store Cats*/public interface GList<T> {

// add the element to the end of the listpublic void append(T element);

// check if the element exists in the listpublic boolean contains(T element);

// remove first element from the list and return itpublic T removeFirst();

// return true if the list is emptypublic boolean isEmpty();

}

https://b.socrative.com/login/student/CS2230Aids1000-4999CS2230Bids5000+

True/False

Stack<T>couldbeimplementedusingaGList<T>

public interface Stack<T> {/* put the element on the top of the stack */public void push(T ele);

/* remove the element on top of the stackand return it*/public T pop();

/* return the element on top of the stack */public T peek();

}

Page 20: Midterm 1 - University of Iowahomepage.cs.uiowa.edu/~bdmyers/cs2230_sp17/public/lectures/lecture-011-polymorphism.pdfMidterm 1 •Next Friday 2/17 in class, 50 minutes •What is on

Today’sbigideas

• InterfacescanbeusedtoexpressAbstractDataTypes,likeList

• Generics allowustobuilddatastructuresthatcanholdelementsofanytype

• LearnabouttwomoreAbstractDataTypes,StackandQueue