announcements i will discuss the labtest and the written test #2 common mistakes, solution, etc. in...

26
Announcements I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class not today as I am still waiting on the written test grades in the last class I will go through a few topics based on your suggestions, hence, if you want a specific topic to be discussed/reviewed, email me before Oct 22, 2011 will also discuss the final 1

Upload: jesse-jordan

Post on 30-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting

Announcements I will discuss the labtest and the written test #2

common mistakes, solution, etc. in the next class not today as I am still waiting on the written test grades

in the last class I will go through a few topics based on your suggestions, hence, if you want a specific topic to be discussed/reviewed, email me before Oct 22, 2011

will also discuss the final

1

Page 2: Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting

Arrays

[notes Chapter 8a], [AJ 6]

2

Page 3: Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting

What is an Array?an array is a container object that holds a fixed number of values of a single type.The length of an array is established when the array is created. After creation, it is fixed.

from, http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

3

Page 4: Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting

How to Use Arrays? declaring an arrayint[] anArray; // an array of integers ORint anArray[];

you can use other types: byte, short, long, float, double, boolean, char, String

anArray = new int[10]; // create an array of integers

anArray[0] = 100; // initialize first elementanArray[1] = 200; // initialize second element

System.out.println("Element 1 at index 0: "+anArray[0]);System.out.println("Element 2 at index 1: "+anArray[1]);

4

Page 5: Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting

Why use Arrays?

5

Page 6: Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting

Implementing List using Array the List interface is generic hence, the type parameter T in List<T>, which captures the

type of the elements of the list to use this interface, the client has to provide a non

primitive type as argument i.e. List<Double> represents a list containing Double

objects

note that, our List interface is different from the interface java.util.List

for simplicity, it contains fewer methods and some of the methods are specified slightly differently

6

Page 7: Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting

BoundedArrayList Class in this implementation, the maximum size of the list is

bound, later we will see how to make the list grow unboundedly

7

Page 8: Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting

note that, BoundedArrayList implements the List interface

means, we have to provide an implementation for each method specified in the List interface

the List interface extends the Iterable interface Means, we have to provide an implementation for the

single method iterator specified in the Iterable

8

Page 9: Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting

Constructors

public BoundedArrayList(int capacity) {

this.size = 0;

this.elements = new Object[capacity];

}

//default constructor, delegating constructor above

public BoundedArrayList() {

this(BoundedArrayList.CAPACITY);

}

9

Page 10: Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting

The Copy Constructorwe have to create a new array in the copy constructor to copy the content of the original array to the new one

public BoundedArrayList(BoundedArrayList list) {

this.size = list.size;

this.elements = new Object[list.elements.length];

for (int i = 0; i < list.size; i++) {

this.elements[i] = list.elements[i];

}

}

Don’t need to throw an ArrayIndexOutOfBoundException (see the class invariant)

10

Page 11: Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting

instead of manually creating a new array and copying the content from the old one to the new one, we can delegate to the Arrays class and use the static method copyOf(original, length), which is part of the java.util package

public BoundedArrayList(BoundedArrayList list)

{

this.size = list.size;

this.elements =

Arrays.copyOf(list.elements, list.elements.length);

}

11

Page 12: Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting

getSize Methodpublic int getSize()

{

return this.size;

}

12

Page 13: Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting

get Methodget(index) returns the element of the list with the given indexthe return type of the method is T whereas the base type of the array elements is Object means, we have to cast

public T get(int index) throws IndexOutOfBoundsException {

if (index >= 0 && index < this.size) {

return (T) this.elements[i];

}

else {

throw new IndexOutOfBoundsException("Invalid index");

}

}

13

Page 14: Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting

add Methodpublic boolean add(T element) {

boolean added;

if (this.size == this.elements.length) {

added = false;

}

else {

this.elements[this.size] = element;

this.size++;

added = true;

}

return added;

}

14

Page 15: Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting

contains Methodpublic boolean contains(T element) {

boolean found = false;

for (int i = 0; i < this.size; i++) {

found = found || this.elements[i].equals(element);

}

return found;

}

loop invariant:found == 0 ≤ ∃ j < i : this.elements[j].equals(element)

15

Page 16: Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting

if we want to exit once we have found the element

public boolean contains(T element) {

boolean found = false;

for (int i = 0; i < this.size && !found; i++) {

found = found || this.elements[i].equals(element);

}

return found;

}

found = this.elements[i].equals(element);

16

Page 17: Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting

remove Method

loop invariant for the second loop∀if < j < i : this.elements[j] has been copied

to this.elements[j − 1]

17

Page 18: Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting

equals Method

loop invariant for the second loopequal == this.size() == other.size() &&

0 ∀ j < i : this.elements[j].equals(other.elements[j])

18

Page 19: Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting

hashCode Methodpublic int hashCode() {

final int BASE = 31;

int hashCode = 0;

for (int i = 0; i < this.size; i++) {

hashCode = BASE * hashCode +

this.elements[i].hashCode();

}

return hashCode;

}

loop invariant:hashCode == ∑_i<j<this.size 31j ×

this.elements[j].hashCode()

19

Page 20: Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting

hashCode Revised all class must provide a hashCode() method

it digests the data stored in an instance of the class into a single hash value (identifier) and used by other code when storing or manipulating the instance

we must override hashCode() in a way so that it behaves in a way consistent with the object's equals() method

that is, an object must report the same hash value when equals() returns true.

the implementation of hashCode() may differ, which means, two unequal objects having different hashes is desirable but not mandatory

20

Page 21: Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting

in a nutshell: if x.equals(y) returns true then x.hashCode() and

y.hashCode() return the same integer for all x and y different from null

the opposite is not required, but may improve the performance of methods in some classes

21

Page 22: Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting

we must override hashCode() in a way so that it behaves in a way consistent with the object's equals() method

that is, an object must report the same hash value when equals() returns true.

the implementation of hashCode() may differ, which means, two unequal objects having different hashes is desirable but not mandatory

22

Page 23: Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting

toString Methodpublic String toString() {

StringBuffer representation = new StringBuffer("[");

if (this.size != 0) {

for (int i = 0; i < this.size - 1; i++) {

representation.append(this.elements[i]);

representation.append(", ");

}

representation.append(this.elements[this.size - 1]);

}

representation.append("]");

return representation.toString();

}

23

Page 24: Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting

iterator Methodpublic Iterator<T> iterator()

{

return new BoundedArrayListIterator<T>(this.elements);

}

remember, the iterator method in the Iterable interface returns an object of type IteratorIterator is an interface (we cannot create instances), hence, we will need to return an instance of a class that implements the Iterator interface

we need to develop a class BoundedArrayListIterator that implements the Iterator interface that will return an instance of the class

24

Page 25: Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting

/*

* class invariant: this.next >= 0 &&

* for all 0 <= i < this.elements.length : this.elements[i] instanceof T

*/

25

Page 26: Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting

Unbounded Sizepublic boolean add(T element)

{

if (this.size == this.elements.length)

{

this.elements = Array.copyOf(this.elements,

this.elements.length * 2);

}

this.elements[this.size] = element;

this.size++;

return true;

}

26