22
CS221 Week 5 - Wednesday

Week 5 - Wednesday. What did we talk about last time? Recursion Definitions: base case, recursive case Recursive methods in Java

Embed Size (px)

Citation preview

Page 1: Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java

CS221Week 5 - Wednesday

Page 2: Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java

Last time

What did we talk about last time? Recursion

Definitions: base case, recursive case Recursive methods in Java

Page 3: Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java

Questions?

Page 4: Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java

Infix to Postfix Converter

Project 2

Page 5: Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java

Review

Page 6: Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java

Week 1

Programming model Java

OOP Polymorphism Interfaces Threads Exceptions Generics

Java Collections Framework

Page 7: Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java

Week 2

Big Oh Notation Formal definition: f(n) is O(g(n)) if and only if▪ f(n) ≤ c∙g(n) for all n > N▪ for some positive real numbers c and N

Worst-case, asymptotic, upper bound of running time

Ignore lower-order terms and constants Big Omega and Big Theta Abstract Data Types Array-backed list

Page 8: Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java

Week 3

Stacks FILO data structure Operations: push, pop, top, empty Dynamic array implementation

Queues FIFO data structure Operations: enqueue, dequeue, front, empty Circular (dynamic) array implementation

JCF implementations: Deque<T> interface ArrayDeque<T> LinkedList<T>

Page 9: Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java

Week 4

Linked lists Performance issues Single vs. double Insert, delete, find times

Special lists Circular Skip Self-organizing

Linked list implementation of stacks Linked list implementation of queues

Page 10: Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java

Sample Problems

Page 11: Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java

Running time

Let M and N be two integers, where M is no larger than N

Use Big Θ notation to give a tight upper bound, in terms of N, on the time that it will take to Add M and N (by hand, using the normal

algorithm) Multiply M and N (by hand, using the normal

algorithm) Use Big Θ notation to give the same bounds

but this time in terms of n, where n is the number of digits in N

Page 12: Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java

What's the running time in Θ?

int end = n;int count = 0;for( int i = 1; i <= n; i++ ){end /= 2;for( int j = 1; j <= end; j++ )

count++;}

Page 13: Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java

What's the running time in Θ?

int end = n;int count = 0;for( int i = 1; i <= n*n; i+=2)

count++;

Page 14: Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java

What's the running time in Θ?

int count = 0;for( int i = 1; i <= n; i++ ){for( int j = 1; j <= n/j; j++ )

count++;}

Page 15: Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java

Lighten

If we increase the R, G, and B values of every pixel by 25%, the image will get lighter

Let pixels be a byte[][] array with height rows and width*3 bytes in each row

Write the code to lighten the image by 25% To do the math, you've got to change the

range from -128 – 127 to 0 – 255 Then cap the value of each color component

at 255 and shift back to -128 – 127

Page 16: Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java

Reverse a linked list

Assume the following:

public class List {private static class Node {public int data;

public Node next;}

private Node head = null;…

}

Write a method in List that reverses the linked list.

Page 17: Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java

Code to reverse a linked listpublic void reverse() {if( head != null ) {

Node reversed = head;Node temp = head;Node rest = head.next;temp.next = null;while( rest != null ) {temp = rest;rest = rest.next;temp.next = reversed;reversed = temp;}head = reversed;

}}

Page 18: Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java

Palindrome

Write a method that takes a CharBuffer object The CharBuffer object has two methods:

nextChar() which extracts a char from the input stream

hasNextChar() which returns true as long as there is another char to extract

The method should return true if the input stream is a palindrome (the same backwards as forwards) and false otherwise

Use no String objects or arrays (other than the one embedded in the stack)

Hint: Use at least 3 JCF Deque (stack) objects

Page 19: Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java

Quiz

Page 20: Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java

Upcoming

Page 21: Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java

Next time…

Exam 1!

Page 22: Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java

Reminders

Exam on Friday Keep working on Project 2 Computer Science Club tonight at

6pm