CS1020 Week 8: 12 th March 2015. Contents Sit-in lab #2 Common Mistakes Take-home Lab #3 Week 8.

46
CS1020 Week 8: 12 th March 2015

Transcript of CS1020 Week 8: 12 th March 2015. Contents Sit-in lab #2 Common Mistakes Take-home Lab #3 Week 8.

Page 1: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

CS1020Week 8: 12th March 2015

Page 2: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Week 8

ContentsSit-in lab #2Common MistakesTake-home Lab #3

Page 3: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Week 8

Sit-in Lab #2

Set A – Construct Set B – Evaluation

Page 4: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Objectives

• Collections– Use array of objects, and java.util.Arraylist– Length vs size vs capacity– Handle insertion, sequential searching efficiently

• Problem solving– Design algorithm without changing structure, behaviour– Think of objects in terms of: the data they have, and the

services they provide

Week 8 4

Page 5: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Understand Question

buildUnit( , )

✓10 20

30 40 50

Week 8 5

Page 6: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Design Classes (1/2)

Sequentialsearching

Week 8 6

Page 7: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Design Classes (Set A: 2/2)

: Construct

: ArrayList <Building>

+ size() 2

: Building

2

: Building

1

For efficiency, store (logical) size!

size == 2capacity == 2

size == 1capacity == 3

Week 8 7

Page 8: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Design Classes (Set B: 2/2)

: Evaluate

: ArrayList <Category>

+ size() 2

: Category

2

: Category

1

For efficiency, store (logical) size!

size == 2capacity == 2

size == 1capacity == 3

A B

C

D E

Week 8 8

Page 9: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Insertion (Set A: 1/2)

: Construct

: ArrayList <Building>

+ size() 2

: Building

2

: Building

1

1

How to add Building in sequence?How to add Unit in sequence?

Week 8 9

Page 10: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Insertion (Set B: 1/2)

: Evaluate

: ArrayList <Category>

+ size() 2

: Category

2

: Category

1

1

How to add Category in sequence?How to add Offense in sequence?

:Category

A B

C

D E

Week 8 10

Page 11: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

c) Unit[] temp = new Unit[ _arr.length + 1];// copy _arr’s units to temptemp[_arr.length] = givenUnit;_arr = temp;

a) _arr[size++] = givenUnit;

Insertion (Set A: 2/2)

: Building

2

How to add Unit efficiently?

b) int currIdx = 0;for (Unit curr : _arr) { if (curr == null) _arr[currIdx] = givenUnit; currIdx++;}

d) All of the abovee) None of the above

Week 8 11

Page 12: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

c) Offence[] temp = new Offense[ _arr.length + 1];// copy _arr’s units to temptemp[_arr.length] = givenOffence;_arr = temp;

a) _arr[size++] = givenOffence;

Insertion (Set B: 2/2)

: Building

2

How to add Offence efficiently?

b) int currIdx = 0;for (Offence curr : _arr) { if (curr == null) _arr[currIdx] = givenUnit; currIdx++;}

d) All of the abovee) None of the above

A B

Week 8 12

Page 13: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Searching (Set A: 1/5)

: Construct

: ArrayList <Building>

+ size() 2

: Building

2

: Building

3

How to prevent redundant searches?

buildUnit( , )

Week 8 13

Page 14: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Searching (Set B: 1/5)

: Construct

: ArrayList <Category>

+ size() 2

: Category

2

: Category

3

How to prevent redundant searches?recordOffence( category, offence)

A B

C D E

Week 8 14

Page 15: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Searching (Set A: 2/5)

: Construct

: ArrayList <Building>

+ size() 2

: Building

2

: Building

3

buildUnit( , )Find the first matching buildingFind the first matching unit within

Abstraction: I don’t care how Building finds the 1st matching Unit

Week 8 15

Page 16: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Searching (Set B: 2/5)

: Evaluate

: ArrayList <Category>

+ size() 2

: Category

2

: Category

3

Find the first matching categoryFind the first matching offence within

Abstraction: I don’t care how Category finds the 1st matching Offence

recordOffence( category, offence)

Week 8 16

Page 17: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Searching (Set A: 3/5)

: Construct

: ArrayList <Building>

+ size() 2

: Building

2

: Building

3

buildUnit( , )Unit selected = null; // why initialize?

For each Building currBldg : _buildings If currBldg.hasName(bldgName) selected = currBldg.( find first matching unit within )()

… Attempt to build unitWhat’s wrong with this?

/* Find the first matching building */

Week 8 17

Page 18: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Searching (Set B: 3/5)

: Evaluate

: ArrayList <Category>

+ size() 2

: Category

2

: Category

3

Offence selected = null; // why initialize?

For each Category currCat : _categories If currCat.hasName(offenceName) selected = currCategory.( find first matching unit within )()

… Attempt to build unitWhat’s wrong with this?

/* Find the first matching category */

recordOffence( category, offence)

Week 8 18

Page 19: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Searching (Set A: 4/5)

findUnitByName( blueCar )For each Unit currUnit : _units If currUnit.getName().equals( blueCar ) return currUnitreturn null

: Building

2

What’s wrong with this?

Week 8 19

Page 20: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Searching (Set B: 4/5)

findOffenseByName( offenceA )For each Offence currOffence : _offenses If currOffence.getName().equals( offenceA ) return currOffencereturn null

: Building

2

What’s wrong with this?

A B

Week 8 20

Page 21: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Searching (Set A: 5/5)

findUnitByName( blueCar )Unit match = nullFor each Unit currUnit : _units If currUnit.getName().equals( blueCar ) match = currUnitreturn match

: Building

3

What’s wrong with this?

/* Find the first matching unit */

Week 8 21

Page 22: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Searching (Set B: 5/5)

findUnitByName( offenseA )Offence match = nullFor each Offense currOffsense : _offenses If currOffense.getName().equals( offenseA ) match = currOffensereturn match

: Building

3

What’s wrong with this?

/* Find the first matching offense */

A B C

Week 8 22

Page 23: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Common Mistakes (1/2)

• Inefficient implementation of array-based collection– Re-creating array on insertion– Finding the first null element as the location to insert to– Searching continues after first match is found

• Incorrect searching in array-based collection– Searching past the logical “end” of the array

• Results in NullPointerException or undesired behaviour

– Using equals() method to compare each Building object in the ArrayList<Building> with building NAME, a String

Week 8 23

Page 24: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Common Mistakes (2/2)

• Fail to visualize how problem should be solved– “I must create getters/accessors for this to work…”– Failing to view classes as templates of objects that store

data and provide services• “Don’t know what this method is for”• “Don’t know which method to place this functionality in”• “Don’t know which method to invoke”

(When writing real software, changing what a public method does will likely affect other programs)

Week 8 24

Page 25: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Next Step (1/3)

• Understand relationship between:– Collection– List– RandomAccess– ArrayList/Vector

• Simulate your own ArrayList to understand how java.util.ArrayList works! See next page…(You should have done this during recess week)

Week 8 25

Page 26: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Next Step (2/3)

• Implement an array-based Collection supporting– Efficient insertion, proportional to 1 operation– Sequential search and removal, proportional to N

operationswhen we have N elements in the array

• Implement an array-based List supporting– Insertion into a given index in the middle of the list– Removing from a given index in the middle of the list

Week 8 26

Page 27: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Next Step (3/3)

• Implement your own LinkedList<E> and ListNode<E> from scratch

• Think:– Why use LinkedList when there’s ArrayList?– Why use ArrayList when there’s array?– Which operations are more efficient in LinkedList, and

which are more efficient in ArrayList?

Week 8 27

Page 28: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Week 828

Take-home Lab #3

Exercise 1 – Flip The List

Page 29: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Simple Algorithm (1/11)

A B C D E

Starting index to be flipped

Keep a pointer to link the last index

Week 8 29

Page 30: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Simple Algorithm (2/11)

A B C D E

Pointer 1 Pointer 2 Pointer 3

Week 8 30

Page 31: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Simple Algorithm (3/11)

A B C D E

NULL

Pointer 1 Pointer 2 Pointer 3

Week 8 31

Page 32: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Simple Algorithm (4/11)

A B C D E

NULL

Pointer 1 Pointer 2 Pointer 3

Week 8 32

Page 33: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Simple Algorithm (5/11)

A B C D E

NULL

Pointer 1 Pointer 2 Pointer 3

Week 8 33

Page 34: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Simple Algorithm (6/11)

A B C D E

NULL

Pointer 1 Pointer 2Pointer 3

Week 8 34

Page 35: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Simple Algorithm (7/11)

A B C D E

NULL

Pointer 1 Pointer 2 Pointer 3

Week 8 35

Page 36: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Simple Algorithm (8/11)

A B C D E

NULL

Pointer 1 Pointer 2 Pointer 3

Week 8 36

Page 37: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Simple Algorithm (9/11)

A B C D E

NULL

Pointer 1 Pointer 2

Week 8 37

Page 38: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Simple Algorithm (10/11)

A B C D E

NULL

Pointer 1 Pointer 2

Week 8 38

Page 39: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Simple Algorithm (11/11)

A B C D E

NULL

Pointer 1 Pointer 2

Week 8 39

Page 40: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Exception

A B C D E

Start Index

Start Index

Week 8 40

Page 41: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Week 841

Take-home Lab #3

Exercise 2 – Kallang Wave

Page 42: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Week 842

Kallang Wave Simulate a wave throughout an entire stadium

Multiple rows are not simulated Simulation runs from left to right Members of the audience are represented by

ASCII characters Different characters represent different states,

not individuals

Page 43: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Week 843

Audience

Page 44: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Week 844

||,,,

Audience as ASCII

,||,,,,||,,,,|||,,,|

Page 45: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Week 845

Circular Linked List Stadium is round need a “round” linked list Must use circular linked list

Either from skeleton or self-made Given CircularLinkedList class is incomplete Bonus marks for efficient use

Will need modification of data structure

Page 46: CS1020 Week 8: 12 th March 2015. Contents  Sit-in lab #2  Common Mistakes  Take-home Lab #3 Week 8.

Week 846

END OF FILE