CS61B Spring 2016 Guerrilla Section 2 Worksheet

Similar documents
CS 61B Spring 2017 Guerrilla Section 3 Worksheet. 25 February 2017

CS 61B Spring 2017 Guerrilla Section 3 Solutions

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

Basic Data Structures

CS 61B Spring 2017 Guerrilla Section 4 Worksheet. 11 March 2017

Basic Data Structures 1 / 24

csci 210: Data Structures Stacks and Queues

CMSC 132: Object-Oriented Programming II. Stack and Queue

CS61BL: Data Structures & Programming Methodology Summer Midterm 1 Solutions. public static boolean probablyequals( Object obj1, Object obj2 ) {

CE204 Data Structures and Algorithms Part 2

Examination Questions Midterm 1

Interfaces & Generics

CS 61B Midterm 2 Guerrilla Section Spring 2018 March 17, 2018

CS 61B Guerrilla Section 1 Worksheet

Introduction to Computing II (ITI 1121) Final Examination

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Queues. Stacks and Queues

INFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues

CS171 Midterm Exam. October 29, Name:

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

DNHI Homework 3 Solutions List, Stacs and Queues

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

CS61BL Summer 2013 Midterm 2

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

CS 61B Guerrilla Section 1 Solution

CSE 143 SAMPLE MIDTERM

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Name CPTR246 Spring '17 (100 total points) Exam 3

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

CS61B Spring 2016 Guerrilla Section 3 Worksheet. 12 March 2016

Implementing Dynamic Data Structures

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

CMSC 132, Object-Oriented Programming II Summer Lecture 9:

Advanced Java Concepts Unit 3: Stacks and Queues

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

Stacks and Queues. David Greenstein Monta Vista

" Which data Structure to extend to create a heap? " Two binary tree extensions are needed in a heap. n it is a binary tree (a complete one)

Queues Fall 2018 Margaret Reid-Miller

CT 229 Object-Oriented Programming Continued

Name Section Number. CS210 Exam #2 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

CS24 Week 4 Lecture 2

Abstract Data Types Spring 2018 Exam Prep 5: February 12, 2018

More Data Structures (Part 1) Stacks

Day 6. COMP1006/1406 Summer M. Jason Hinek Carleton University

Project 2 (Deques and Randomized Queues)

The class Object. Lecture CS1122 Summer 2008

Queues. CITS2200 Data Structures and Algorithms. Topic 5

CS 61B Spring 2017 Guerrilla Section 2 Worksheet. 12 February 2017

Spring 2018 Discussion 6: February 19, 2018

Summer Final Exam Review Session August 5, 2009

Data Abstraction and Specification of ADTs

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1

Announcements Queues. Queues

CS350: Data Structures Stacks

CSE 332: Data Structures. Spring 2016 Richard Anderson Lecture 1

Stacks and Queues

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)

cs Java: lecture #6

Introduction to Computer Science II (ITI 1121) Final Examination

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

ECE Fall st Midterm Examination (120 Minutes, closed book)

CS 61B Discussion 5: Inheritance II Fall 2014

Priority Queue. How to implement a Priority Queue? queue. priority queue

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Midterm Exam 2 CS 455, Spring 2011

Points off Total off Net Score. CS 314 Final Exam Spring 2016

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

CS61B Fall 2015 Guerrilla Section 3 Worksheet. 8 November 2015

Implementing Lists, Stacks, Queues, and Priority Queues

Model Solutions. COMP 103: Test April, 2013

Standard ADTs. Lecture 19 CS2110 Summer 2009

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

A queue is a linear collection whose elements are added on one end and removed from the other

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request.

EXAMINATIONS 2017 TRIMESTER 2

Model Solutions. COMP 103: Mid-term Test. 21st of August, 2014

CS 61B Spring 2017 Guerrilla Section 2 Solutions. 12 February 2017

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Apply to be a Meiklejohn! tinyurl.com/meikapply

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

== isn t always equal?

(f) Given what we know about linked lists and arrays, when would we choose to use one data structure over the other?

CSE 326 Team. Today s Outline. Course Information. Instructor: Steve Seitz. Winter Lecture 1. Introductions. Web page:

In this lecture Simple List Algorithms

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES

CS 206 Introduction to Computer Science II

DM537 Object-Oriented Programming. Peter Schneider-Kamp.

Week 6. Data structures

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

EXAMINATIONS 2016 TRIMESTER 2

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES

Introduction to Computing II (ITI 1121) Final Examination

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

Transcription:

Spring 2016 27 February 2016 Directions: In groups of 4-5, work on the following exercises. Do not proceed to the next exercise until everyone in your group has the answer and understands why the answer is what it is. Of course, a topic appearing on this worksheet does not imply that the topic will appear on the midterm, nor does a topic not appearing on this worksheet imply that the topic will not appear on the midterm. 1 Probably Equal? (a) Warm-up! What is the difference between == and.equals? Would it make sense for the scenarios below to occur? Explain why or why not. i A.equals(B) but A!= B ii A == B but!a.equals(b) (b) Write a probablyequals method that takes in two objects and returns true if one or more of the following are true: The two objects are equal (.equals) to each other. The two objects are equal (==) to each other. The two objects have the same.tostring() representation. Otherwise, probablyequals returns false. Your method should never crash given any input. You may assume that for any object instances x and y, x.equals(y) will return the same value as y.equals(x). You may also assume that every object has a unique non-random tostring representation. Note:.equals(Object o) and.tostring() are methods that every object subclass inherits from the Object class. 1

Spring 2016 2

2 Expandable Set Using inheritance, define a class TrackedQueue that behaves like Queue except for an extra method, maxsizesofar() which returns an integer corresponding to the maximum number of elements in this queue since it was constructed. Assume that the Queue class is a non-abstract class with the following methods defined: void enqueue(object obj) int size() Object dequeue() Spring 2016 3

3 Xzibit s ADTs Consider the following abstract data type definitions: 1 List <E > { 2 void insert ( E item, int position ); 3 E remove ( int position ); 4 E get ( int position ); 5 int size (); 6 7 8 Set <E > { 9 void add (E item ); 10 void remove (E item ); 11 boolean contains ( E item ); 12 Iterator <E> list (); 13 14 15 Stack <E > { 16 void push (E item ); 17 E pop (); 18 boolean isempty (); 19 20 21 Queue <E > { 22 void enqueue ( E item ); 23 E dequeue (); 24 boolean isempty (); 25 26 27 Map <K, V> { 28 put (K key, V value ); 29 remove (K key ); 30 V get (K key ); 31 Iterator <K> keys (); 32 For the following questions, you may assume the above data types have been implemented as classes. (a) Write an extension of the Set class, called IntegerSet, with the following methods: void add(integer item): adds an integer to the set boolean contains(integer item):checks item for membership in the set Iterator<Integer> list():returns an iterator over the elements of the IntegerSet Integer max():returns the maximum value in the IntegerSet, or null if the set is empty Spring 2016 4

(b) Consider the following PriorityQueue interface: public interface PriorityQueue<E> { public void enqueue(e item, int priority); public E dequeue(); public E peek(); Describe how you would implement this abstract data type using any combination of the above ADT definitions, including IntegerSet. Spring 2016 5

4 Delegation vs. Extension Consider the following class. interface MyStack<T> { void push(t item); T pop(); int size(); (a) Implement ExtensionStack which implements MyStack using extension, without using the new keyword. (Hint: ExtensionStack can extend LinkedList). public class ExtensionStack<T> { //your code here (b) Implement DelegationStack which implements MyStack using delegation. public class DelegationStack<T> { LinkedList<T> data =... int size() =... Spring 2016 6

5 FunkySets & Promotion Cross out the lines that would cause compilation errors for each of the classes. Write out the values that will be printed where indicated in the code?s comments. 1 import java. util. HashSet ; 2 public c l a s s FunkySet { 3 public s t a t i c void main ( String [] args ){ 4 HashSet < int > set = new HashSet < int >() ; 5 HashSet < Integer > set = new HashSet < int >() ; 6 HashSet < int > set = new HashSet < Integer >() ; 7 HashSet < Integer > set = new HashSet < Integer >() ; 8 int x = 3; 9 set. add (x); 10 set. add (4) ; 11 Integer y = 5; 12 set. add (y); 13 System. out. println ( set. tostring ()); 14 // what does this print? 15 i f ( set. contains (x)){ 16 set. remove (x); 17 18 i f ( set. contains (4) ){ 19 int z = 4; 20 set. remove (z); 21 22 i f ( set. contains (y)){ 23 set. remove (y); 24 25 System. out. println ( set. tostring ()); 26 //What does this print out? 27 28 29 public c l a s s FunkySetTwo { 30 public s t a t i c void main ( String [] args ){ 31 int [][] x = new int [2][3]; 32 Integer [][] y = new Integer [2][3]; 33 Integer [][] z = new int [2][3]; 34 Integer [] arrayone = {1,2,3; 35 int [] arraytwo = {4,5,6; 36 37 x [0] = arrayone ; 38 x [1] = arraytwo ; 39 40 y [0] = arrayone ; 41 y [1] = arraytwo ; 42 43 z [0] = arrayone ; 44 z [1] = arraytwo ; 45 46 Spring 2016 7

1 public c l a s s Promotion { 2 public s t a t i c void doubleprinter (double num ){ 3 System. out. println ( num ); 4 5 public s t a t i c void longprinter (long num ){ 6 System. out. println ( num ); 7 8 public s t a t i c void intprinter ( int num ){ 9 System. out. println ( num ); 10 11 public s t a t i c void intprintertwo ( Integer num ){ 12 System. out. println ( num ); 13 14 public s t a t i c void shortprinter ( short num ){ 15 System. out. println ( num ); 16 17 public s t a t i c void main ( String [] args ){ 18 int x = 45; 19 longprinter ( x); 20 //What does this print? 21 doubleprinter ( x); 22 //What does this print? 23 intprinter (( int )x); 24 //What does this print? 25 intprintertwo ( x); 26 //What does this print? 27 shortprinter ( x); 28 //What does this print? 29 shortprinter (( short ) x); 30 //What does this print? 31 32 Spring 2016 8

6 Vote Iterator Write an iterator that takes in an Integer array of vote counts and iterates over the votes. The input array contains the number of votes each selection received. For example, if the input array contained the following array: 0 2 1 0 1 0 then calls to next() would eventually return 1 twice(because at index 1, the input array has value 2), 2 once, and 4 once. After that, hasnext() would return false. Provide code for the VoteIterator class below. Make sure your iterator adheres to standard iterator rules. public class VoteIterator implements Iterator { public VoteIterator(Integer[] votes) { public boolean hasnext() { public Integer next() { Spring 2016 9

7 Iterator or Iterable? Implement the Filter class such that its main method correctly prints out the even numbers in the given collection. (Should print out 0 20 14 50 66 all on newlines) 1 public interface FilterCondition <T> { 2 3 / Evaluates if the given item passes a certain condition (ie, is even, a prime number, is icky, etc.) / 4 public boolean eval ( T item ); 5 6 public c l a s s EvenCondition implements FilterCondition < Integer >{ 7 8 public boolean eval ( Integer i) { 9 return i % 2 == 0; 10 11 12 13 14 15 import java. util. Arrays ; 16 import java. util. Iterator ; 17 import java. util. List ; 18 19 public c l a s s Filter implements Iterable < Integer > { 20 21 22 //add class attributes if necessary 23 24 public Filter ( Iterable < Integer > thingamajig, FilterCondition < Integer > cond ) { 25... 26 27 28 29 public Iterator < Integer > iterator () { 30... 31 32 33 private c l a s s FilterIterator implements Iterator < Integer > { 34... 35 36 37 public s t a t i c void main ( String [] args ) { 38 List < Integer > arr = Arrays. aslist (new Integer []{0, 11, 20, 13, 14, 50, 66) ; 39 f o r ( int i : new Filter ( arr, new EvenCondition ())) { 40 System. out. println (i); 41 42 43 Spring 2016 10

Spring 2016 11

8 Except Me for Who I Am Consider the following: 1 public c l a s s IntList { 2 private int head ; 3 private IntList tail ; 4 5 / Returns the index of an element in the list / 6 public int getindex ( int item ){ 7 int index = 0; 8 IntList temp = this ; 9 while( temp. head!= item ){ 10 temp = temp. tail ; 11 index ++; 12 13 return index ; 14 15 16 public int getindexthrowexception ( int item ) throws IllegalArgumentException { 17... 18 19 20 public int getindexdefaultnegative ( int item ){ 21... 22 23 (a) What happens when you call getindex(int item) on an element that is not in the list? Spring 2016 12

(b) Write getindexthrowexception, which attempts to get the index of an item, but throws an IllegalArgumentException with a useful message if no such item exists in the list. Do not use if statements, while loops, for loops, or recursion. (Hint: you can use get(int item)) (c) Write getindexdefaultnegative, which attempts to get the index of an item, but returns -1 if no such item exists in the list. Again, do not use if statements, while loops, for loops, or recursion. Spring 2016 13

9 ITERATORS Print ALL bark combinations of dogs from two dog lists in form of Woof DOG1! Woof DOG2!. The dogs in dogs1 should bark first. For 3 dogs in dogs1 and 5 dogs in dogs2, there must be 15 bark combinations printed. 1 public c l a s s Dog { 2 String name =...; 3 public void bark () { 4 System. out. print ( W o o f + name +! ); 5 6 7 8 public s t a t i c void barkcombinations ( Iterable <Dog > dogs1, Iterable <Dog > dogs2 ) { 9 // YOUR PRECIOUS CODE HERE 10... 11 12 Spring 2016 14