© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

43
© 2006 Pearson Addison-Wesley. All rights reserved 8 A-1 CMPT 225 Midterm Review
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    219
  • download

    0

Transcript of © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

Page 1: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-1

CMPT 225

Midterm Review

Page 2: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-2

Topics Covered

• Java review• Software Engineering• Recursion

– (and a little algorithm efficiency)

• Abstract Data Types• Linked Lists• Stacks• Queues

Page 3: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-3

Topics Covered

• Roughly: chapters 1-8

• Minus chapter 6... though we did some of it in class

• We didn’t read the second half of ch. 7, but we covered it in class

• The algorithm efficiency was not in any of these chapters

Page 4: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-4

Java Review

• You should be able to read Java code– You don’t need to memorize a bunch of

java.util classes

• You should be able to write Java code– Defining your own classes, interfaces, methods,

etc.

Page 5: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-5

Software Engineering

• What is a good solution to a problem?

• Object oriented design– Read a UML diagram– Write a UML diagram

• Modularity, modifiability, readability, ease of use, fail-safe programming…

Page 6: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-6

Recursion

• Write recurrence relations– e.g. rabbits(n) = rabbits(n-1) + rabbits(n-2)

• Write recursive (Java) methods

• Solve problems with simple recursive solutions

• Backtracking

• Relation with induction

Page 7: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-7

Algorithm Efficiency

• Big-O notation:– What does O(n) mean?– What about O(n2)?

• Count the steps of an algorithm, translate to big-O

Page 8: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-8

Abstract Data Types

• Difference:– ADT vs. data structure

• How do you specify ADTs?

• Designing the right ADT for a problem

• ADT List and SortedList

Page 9: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-9

Linked Lists

• Object references

• Using nodes and references

• Programming with linked lists– inserting, deleting, traversing

• Variations– tail references, circular lists, doubly linked

Page 10: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-10

Stacks

• ADT Stack– LIFO

• Implementations

• Problem solving with Stacks

Page 11: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-11

Queues

• ADT Queue– FIFO

• Implementations

• Problem solving with Queues

Page 12: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-12

Example 1

• Write a pre-condition, post-condition, and loop invariant for the following:

public int Factorial(int n){

int val = 1;for(int i=1; i<=n; i++)

val = val*i;return val;

}

Page 13: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-13

Example 1 - Solution

• Pre-condition:– n is an integer greater than or equal to 1

• Post-condition:– returned value is n!

• Loop invariant:– after iteration i of the for loop, val is equal to i!

Page 14: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-14

Example 2

• What is wrong with this:

void printN(int n)

{

System.out.println(n);

printN(n-1);

}

Page 15: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-15

Example 2 - Solution

• No base case!

• It will run forever

Page 16: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-16

Example 3

• Write a recursive method that takes input n, and prints the numbers from n to 0.

Page 17: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-17

Example 3 - Solution

void printUpToN(int n){if(n<0)

System.out.print(“”);else{

System.out.print(n);printUpToN(n-1);

}}

Page 18: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-18

Example 4

• Using only the List ADT operations, write a pseudocode method swap(List list, int i, int j) that exchanges positions i and j in List

Page 19: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-19

Example 4 - Solution

swap(List list, int i, int j):

int temp = list.retrieve(i);

list.add(j+1,temp);

temp = list.retrieve(j);

list.remove(j);

list.add(i+1,temp);

list.remove(i);

Page 20: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-20

Example 5

• Specify an abstract data type for a Bag... items can be inserted, contents dumped out, removed one by one

Page 21: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-21

Example 5 - Solution

ADT Bag+createBag()//Creates an empty bag

+isEmpty():boolean{query}//Determines if the Bag is empty

+add(in newItem:BagItemType)//Adds newItem to the Bag

+removeAll()//Removes all items from the Bag

+pickRandom()//Removes a random item from the Bag

Page 22: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-22

Example 6

• Write pseudocode to add the contents of one list to the end of another list

Page 23: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-23

Example 6 - Solution

append(List list1, List list2)

newList.createList();

while(!list1.isEmpty())

newList.add(newList.size()+1,list1.get(1));

list1.remove(1);

while(!list2.isEmpty())

newList.add(newList.size()+1,list2.get(1));

list2.remove(1);

return newList

Page 24: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-24

Example 7

• Same thing... using Queue ADT

Page 25: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-25

Example 7 - Solution

append(Queue queue1, Queue queue2)

newQueue.createQueue();

while(!queue1.isEmpty())

newQueue.enQueue(queue1.deQueue());

while(!queue2.isEmpty())

newQueue.enQueue(queue2.deQueue());

return newQueue

Page 26: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-26

Example 8

• Write a pseudocode method to count the number of items in a linked list.

Page 27: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-27

Example 8 – Solution (Iterative)

numElements(LinkedList ll)

Node curr = head;int count = 0;

while(curr.getNext()!=null)count++;curr = curr.getNext();

return count;

Page 28: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-28

Example 9

• Write a pseudocode method to find the largest element in a linked list of integers

Page 29: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-29

Example 9 - Solution

largestElement(LinkedList ll)

if(head==null)throw exception

Node curr = head;int largest = head.getItem();

while(curr.getItem()!=null)if(largest<curr.getItem())

largest = curr.getItem()curr=curr.getNext();

return largest;

Page 30: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-30

Example 10

• Given one possible explanation for a StackException that is thrown from a push operation

Page 31: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-31

Example 10 - Solution

• Explanation:– The Stack ADT was implemented with a fixed

size array, and it has reached maximum capacity.

Page 32: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-32

Example 11

• What are contents of aStack following this:aStack.createStack();if(aStack.isEmpty())aStack.push(4);

push(5);pop()popall();push(6);push(7);

Page 33: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-33

Example 11 - Solution

• What are contents of aStack following this:aStack.createStack();if(aStack.isEmpty())aStack.push(4);

push(5);pop()popall();push(6);push(7);

7

6

Page 34: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-34

Example 12

• Briefly explain/define these terms:– circular linked list– data structure– encapsulation

Page 35: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-35

Example 12

• Briefly explain/define these terms:– circular linked list

• a linked list where tail node references the head

– data structure• a construct in a programming language that stores

data

– encapsulation• objects keep internal data enclosed internally, hiding

it from view and preventing it from being accessed

Page 36: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-36

Example 13

• Given the following running times for input n, what is the big-O measure of efficiency– n + 6n steps– n2 + n(n2 + 5) steps– log n + n steps

Page 37: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-37

Example 13 - Solution

• Given the following running times for input n, what is the big-O measure of efficiency– n + 6n steps ----------------------- O(n)– n2 + n(n2 + 5) steps -------------- O(n3)– log n + n steps -------------------- O(n)

Page 38: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-38

Example 14

• Which ADT is appropriate for each type of data:– An alphabetic list of names– Airplanes that stack above a busy airport

waiting to land– An employer that is laying off employees,

starting with the most recent hires

Page 39: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-39

Example 14 - Solution

• Which ADT is appropriate for each type of data:– An alphabetic list of names LIST– Airplanes that stack above a busy airport

waiting to land QUEUE– An employer that is laying off employees,

starting with the most recent hires STACK

Page 40: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-40

Example 15

• Give UML diagram from requirements:– A drawing program is to allow kids to draw all sorts of shapes:

lines, rectangles, and circles. While using this program, kids select the shape they wish to draw, the colour of the shape and they indicate the shape's position on the canvas (a canvas is a drawing area on a window created by the drawing program).

– The position of a line is indicated by the x and y coordinates of its end points.

– The position of a rectangle is indicated by the x and y coordinates of its top left corner and the x and y coordinates of its bottom right corner.

– The position of a circle is indicated by the x and y coordinates of its center and the length of its radius.

Page 41: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-41

Example 15 - Solution

colour

shape

display( ) or draw( )

one end x,one end yother end xother end y

line

topleft xtopleft y bottomright xbottomright y

rectangle

center xcenter yradius

circle

display( ) or draw( )

display( ) or draw( )

display( ) or draw( )

Page 42: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-42

More types of examples

• Predict the output of Java code....

• Recursive methods for traversal... for recognizing classes of strings (e.g. all ‘a’)

• Defining ADT’s from description... bank account, rectangle

• Merging sorted lists

Page 43: © 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

© 2006 Pearson Addison-Wesley. All rights reserved 8 A-43

More types of examples

• Displaying contents of a Queue or Stack

• Writing data structure for an ADT... say the Bag from earlier... need to decide data members

• Inserting nodes in doubly linked/circular linked lists