Information and Computer Sciences University of Hawaii, Manoa

Slides:



Advertisements
Similar presentations
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Advertisements

Written by: Dr. JJ Shepherd
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
15-Jun-15 Lists in Java Part of the Collections Framework.
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
CS 280 Data Structures Professor John Peterson. Project 9 Questions? IS280.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
List Interface CS java.util.List Interface and its Implementers CS340 2.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Arrays And ArrayLists - S. Kelly-Bootle
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Information and Computer Sciences University of Hawaii, Manoa
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
COMP 103 Bitsets. 2 Sets, and more Sets!  Unsorted Array  Sorted ArrayO(n) for at least one of  Linked Listcontains, add, remove  Binary Search TreeO(log.
12/18/2015ITK 2751 CollectionList A bstractSequentialList Vector Stack LinkedList {interface} AbstractList {abstract} ArrayList Java Provides a List interface.
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201.
Course: Object Oriented Programming - Abstract Data Types Unit2: ADT ListsSlide Number 1 Principles for implementing ADTs ADT operations as “walls” between.
Linked Lists Revision Based on: v/
Recursive Objects (Part 2) 1. Adding to the front of the list  adding to the front of the list  t.addFirst('z') or t.add(0, 'z') 2 'a' 'x' LinkedList.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Lecture 7 February 24, Javadoc version and author Tags These go in the comments before named classes. –Put your SS# on a separate line from the.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
Written by: Dr. JJ Shepherd
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture.
Object Oriented Programming in Java Habib Rostami Lecture 7.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
Recursive Objects Singly Linked List (Part 2) 1. Operations at the head of the list  operations at the head of the list require special handling because.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
Linked Data Structures
Linked Lists Chapter 5 (continued)
Implementing ArrayList Part 1
Top Ten Words that Almost Rhyme with “Peas”
null, true, and false are also reserved.
Programming in Java Lecture 11: ArrayList
Data Type (how to view it)
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Need for Iterators O(n2) this is a common application
Programming II (CS300) Chapter 07: Linked Lists and Iterators
ArrayLists 22-Feb-19.
Collections Framework
JCF Collection classes and interfaces
Linked Lists Chapter 5 (continued)
Programming II (CS300) Chapter 07: Linked Lists
TCSS 143, Autumn 2004 Lecture Notes
CSE 143 Lecture 21 Advanced List Implementation
Linked Lists Chapter 5 (continued)
Java Generics & Iterators
Presentation transcript:

Information and Computer Sciences University of Hawaii, Manoa Midterm 1 Solutions ICS 211 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

2 3 Question 1 String x = new String(“cat”); String y = new String(“cat”); String z = x; How many objects are created? How many references? Is x == y true or false Is x.equals(y) true or false 2 3

Question 2 Write the List<E> interface: /** An ordered collection. The user can access elements by their * integer index, and search for elements in the list. */ public interface List<E> extends Collection<E> { E get(int index); // returns object at given position E set(int index, E element); // sets element, returns old value E remove(int index); // removes object at the given position int size(); // returns number of elements in list boolean add(E element); // add element at the end of the list void add(int index, E element); // add element at given position int indexOf(Object o); // returns index of object in list ... }

Question 2 Write the List<E> interface: /** An ordered collection. The user can access elements by their * integer index, and search for elements in the list. */ public interface List<E> extends Collection<E> { E get(int index); // returns object at given position E set(int index, E element); // sets element, returns old value E remove(int index); // removes object at the given position int size(); // returns number of elements in list boolean add(E element); // add element at the end of the list void add(int index, E element); // add element at given position int indexOf(Object o); // returns index of object in list ... }

Question 2 Write the List<E> interface: /** An ordered collection. The user can access elements by their * integer index, and search for elements in the list. */ public interface List<E> extends Collection<E> { E get(int index); // returns object at given position E set(int index, E element); // sets element, returns old value E remove(int index); // removes object at the given position int size(); // returns number of elements in list boolean add(E element); // add element at the end of the list void add(int index, E element); // add element at given position int indexOf(Object o); // returns index of object in list ... }

Question 2 Write the List<E> interface: /** An ordered collection. The user can access elements by their * integer index, and search for elements in the list. */ public interface List<E> extends Collection<E> { E get(int index); // returns object at given position E set(int index, E element); // sets element, returns old value E remove(int index); // removes object at the given position int size(); // returns number of elements in list boolean add(E element); // add element at the end of the list void add(int index, E element); // add element at given position int indexOf(Object o); // returns index of object in list ... }

Question 2 Write the List<E> interface: /** An ordered collection. The user can access elements by their * integer index, and search for elements in the list. */ public interface List<E> extends Collection<E> { E get(int index); // returns object at given position E set(int index, E element); // sets element, returns old value E remove(int index); // removes object at the given position int size(); // returns number of elements in list boolean add(E element); // add element at the end of the list void add(int index, E element); // add element at given position int indexOf(Object o); // returns index of object in list ... }

Question 2 Write the List<E> interface: /** An ordered collection. The user can access elements by their * integer index, and search for elements in the list. */ public interface List<E> extends Collection<E> { E get(int index); // returns object at given position E set(int index, E element); // sets element, returns old value E remove(int index); // removes object at the given position int size(); // returns number of elements in list boolean add(E element); // add element at the end of the list void add(int index, E element); // add element at given position int indexOf(Object o); // returns index of object in list ... }

Question 2 Write the List<E> interface: /** An ordered collection. The user can access elements by their * integer index, and search for elements in the list. */ public interface List<E> extends Collection<E> { E get(int index); // returns object at given position E set(int index, E element); // sets element, returns old value E remove(int index); // removes object at the given position int size(); // returns number of elements in list boolean add(E element); // add element at the end of the list void add(int index, E element); // add element at given position int indexOf(Object o); // returns index of object in list ... }

Question 2 Write the List<E> interface: /** An ordered collection. The user can access elements by their * integer index, and search for elements in the list. */ public interface List<E> extends Collection<E> { E get(int index); // returns object at given position E set(int index, E element); // sets element, returns old value E remove(int index); // removes object at the given position int size(); // returns number of elements in list boolean add(E element); // add element at the end of the list void add(int index, E element); // add element at given position int indexOf(Object o); // returns index of object in list ... }

Question 3 Does Student inherit all the non-private methods of Person? Does Student inherit all the non-private methods of GradeLevel? Is Student required to provide all the methods of Person? Is Student required to provide all the methods of GradeLevel? Is Student a subclass of Person or GradeLevel (circle one) public class Student extends Person implements GradeLevel { yes no no yes*

Question 4 Complete this implementation of the remove method. public class MyArrayList<E> implements List<E> { protected E[] data; protected int size; … public E remove(int index) { if (index <0 || index >= size) { throw new IndexOutOfBoundsException(“bad index “ + index); }

Question 4 Complete this implementation of the remove method. public class MyArrayList<E> implements List<E> { protected E[] data; protected int size; … public E remove(int index) { if (index <0 || index >= size) { throw new IndexOutOfBoundsException(“bad index “ + index); } E returnValue = data[index];

Question 4 Complete this implementation of the remove method. public class MyArrayList<E> implements List<E> { protected E[] data; protected int size; … public E remove(int index) { if (index <0 || index >= size) { throw new IndexOutOfBoundsException(“bad index “ + index); } E returnValue = data[index]; for (int i = index; i < size – 1; i++) { data[i] = data[i + 1];

Question 4 Complete this implementation of the remove method. public class MyArrayList<E> implements List<E> { protected E[] data; protected int size; … public E remove(int index) { if (index <0 || index >= size) { throw new IndexOutOfBoundsException(“bad index “ + index); } E returnValue = data[index]; for (int i = index; i < size – 1; i++) { data[i] = data[i + 1]; size--; return returnValue;

Question 5 Tell me (briefly) everything wrong with this method. public static double sumProduct(Iterator<Integer> itter) { double result = 0.0; int index = 1; for (Integer x : itter) { result = x.doubleValue() * index; index = index.next(); } return index; return result;

Question 5 Tell me (briefly) everything wrong with this method. public static double sumProduct(Iterator<Integer> itter) { double result = 0.0; int index = 1; for (Integer x : itter) { result = x.doubleValue() * index; index = index.next(); } return index; return result;

Question 5 Tell me (briefly) everything wrong with this method. public static double sumProduct(Iterator<Integer> itter) { double result = 0.0; int index = 1; for (Integer x : itter) { result = x.doubleValue() * index; index = index.next(); } return index; return result;

Question 5 Tell me (briefly) everything wrong with this method. public static double sumProduct(Iterator<Integer> itter) { double result = 0.0; int index = 1; for (Integer x : itter) { result = x.doubleValue() * index; index = index.next(); } return index; return result;

Question 5 Tell me (briefly) everything wrong with this method. public static double sumProduct(Iterator<Integer> itter) { double result = 0.0; int index = 1; for (Integer x : itter) { result = x.doubleValue() * index; index = index.next(); } return index; return result;

Question 5 Tell me (briefly) everything wrong with this method. public static double sumProduct(Iterator<Integer> itter) { double result = 0.0; int index = 1; for (Integer x : itter) { result = x.doubleValue() * index; index = index.next(); } return index; return result;

Question 6 What is the Big-O of the following code? double result = 0.0; for (int i = 5; i < 2n; i++) { result = result * i; for (int j = n – 2; j > 1; j--) { int temp = j – i; result += temp; }

Question 6 What is the Big-O of the following code? double result = 0.0; for (int i = 5; i < 2n; i++) { 2n – 6 result = result * i; for (int j = n – 2; j > 1; j--) { int temp = j – i; result += temp; }

Question 6 What is the Big-O of the following code? double result = 0.0; for (int i = 5; i < 2n; i++) { 2n – 6 result = result * i; for (int j = n – 2; j > 1; j--) { n - 3 int temp = j – i; result += temp; } (2n – 6) * (n – 3)

Question 6 What is the Big-O of the following code? double result = 0.0; for (int i = 5; i < 2n; i++) { 2n – 6 result = result * i; for (int j = n – 2; j > 1; j--) { n - 3 int temp = j – i; result += temp; } 2n2 – 9n - 18

Question 6 What is the Big-O of the following code? double result = 0.0; for (int i = 5; i < 2n; i++) { 2n – 5 result = result * i; for (int j = n – 2; j > 1; j--) { n - 3 int temp = j – i; result += temp; } O(n2)

Question 7 The super class of all Java classes is __________________________. Object

Question 8 The Node class for a single-linked list has references to the data and to the next and previous Nodes? True of False. (circle one)

Question 9 Add the class variable(s) and implement the hasNext() and next() methods for this Linked List class: public class MyLinkedList<E> implements List<E> { protected LinkedNode<E> head; // singly-linked list … private class MyIterator implements Iterator<E> { public boolean hasNext() { } public E next() { // may throw java.util.NoSuchElementException

Question 9 Add the class variable(s) and implement the hasNext() and next() methods for this Linked List class: public class MyLinkedList<E> implements List<E> { protected LinkedNode<E> head; // singly-linked list … private class MyIterator implements Iterator<E> { private LinkedNode<E> next; public boolean hasNext() { } public E next() { // may throw java.util.NoSuchElementException

Question 9 Add the class variable(s) and implement the hasNext() and next() methods for this Linked List class: public class MyLinkedList<E> implements List<E> { protected LinkedNode<E> head; // singly-linked list … private class MyIterator implements Iterator<E> { private LinkedNode<E> next; public boolean hasNext() { return next != null; } public E next() { // may throw java.util.NoSuchElementException

Question 9 Add the class variable(s) and implement the hasNext() and next() methods for this Linked List class: public class MyLinkedList<E> implements List<E> { protected LinkedNode<E> head; // singly-linked list … private class MyIterator implements Iterator<E> { private LinkedNode<E> next; public boolean hasNext() { return next != null; } public E next() { // may throw java.util.NoSuchElementException if (!hasNext()) { throw new java.util.NoSuchElementException(); }

Question 9 Add the class variable(s) and implement the hasNext() and next() methods for this Linked List class: public class MyLinkedList<E> implements List<E> { protected LinkedNode<E> head; // singly-linked list … private class MyIterator implements Iterator<E> { private LinkedNode<E> next; public boolean hasNext() { return next != null; } public E next() { // may throw java.util.NoSuchElementException if (!hasNext()) { throw new java.util.NoSuchElementException(); } E returnValue = next.data; next = next.next; return returnValue;

Question 10 Implement the following method using the bubble sort algorithm: public static void bubbleSort(E[] data, Comparator<E> c) { }

Question 10 Implement the following method using the bubble sort algorithm: public static void bubbleSort(E[] data, Comparator<E> c) { int pass = 1; boolean exchanged = false; }

Question 10 Implement the following method using the bubble sort algorithm: public static void bubbleSort(E[] data, Comparator<E> c) { int pass = 1; boolean exchanged = false; do { } while (exchanged); }

Question 10 Implement the following method using the bubble sort algorithm: public static void bubbleSort(E[] data, Comparator<E> c) { int pass = 1; boolean exchanged = false; do { exchanged = false; for (int i = 0; i < data.length – pass; i++) { } } while (exchanged);

Question 10 Implement the following method using the bubble sort algorithm: public static void bubbleSort(E[] data, Comparator<E> c) { int pass = 1; boolean exchanged = false; do { exchanged = false; for (int i = 0; i < data.length – pass; i++) { if (c.compare(data[i], data[i + 1] > 0) { } } while (exchanged);

Question 10 Implement the following method using the bubble sort algorithm: public static void bubbleSort(E[] data, Comparator<E> c) { int pass = 1; boolean exchanged = false; do { exchanged = false; for (int i = 0; i < data.length – pass; i++) { if (c.compare(data[i], data[i + 1] > 0) { exchanged = true; E temp = data[i]; data[i] = data[i + 1]; data[i + 1] = temp; } } while (exchanged);

Question 11 A Java interface is a(n) ______________________ between the interface designer and the programmer who codes a class that implements the interface. (circle one) precondition postcondition message contract

Question 12 When looping over a circularly linked list, how do you know when you have reached the end? temp.next == head; or temp == tail;