CS1020 Week 8: 12 th March 2015. Contents Sit-in lab #2 Common Mistakes Take-home Lab #3 Week 8.
-
Upload
griffin-webster -
Category
Documents
-
view
217 -
download
0
Transcript of 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
Week 8
ContentsSit-in lab #2Common MistakesTake-home Lab #3
Week 8
Sit-in Lab #2
Set A – Construct Set B – Evaluation
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
Understand Question
buildUnit( , )
✓10 20
30 40 50
Week 8 5
Design Classes (1/2)
Sequentialsearching
Week 8 6
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
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
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
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
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
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
Searching (Set A: 1/5)
: Construct
: ArrayList <Building>
+ size() 2
: Building
2
: Building
3
How to prevent redundant searches?
buildUnit( , )
Week 8 13
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Week 828
Take-home Lab #3
Exercise 1 – Flip The List
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
Simple Algorithm (2/11)
A B C D E
Pointer 1 Pointer 2 Pointer 3
Week 8 30
Simple Algorithm (3/11)
A B C D E
NULL
Pointer 1 Pointer 2 Pointer 3
Week 8 31
Simple Algorithm (4/11)
A B C D E
NULL
Pointer 1 Pointer 2 Pointer 3
Week 8 32
Simple Algorithm (5/11)
A B C D E
NULL
Pointer 1 Pointer 2 Pointer 3
Week 8 33
Simple Algorithm (6/11)
A B C D E
NULL
Pointer 1 Pointer 2Pointer 3
Week 8 34
Simple Algorithm (7/11)
A B C D E
NULL
Pointer 1 Pointer 2 Pointer 3
Week 8 35
Simple Algorithm (8/11)
A B C D E
NULL
Pointer 1 Pointer 2 Pointer 3
Week 8 36
Simple Algorithm (9/11)
A B C D E
NULL
Pointer 1 Pointer 2
Week 8 37
Simple Algorithm (10/11)
A B C D E
NULL
Pointer 1 Pointer 2
Week 8 38
Simple Algorithm (11/11)
A B C D E
NULL
Pointer 1 Pointer 2
Week 8 39
Exception
A B C D E
Start Index
Start Index
Week 8 40
Week 841
Take-home Lab #3
Exercise 2 – Kallang Wave
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
Week 843
Audience
Week 844
||,,,
Audience as ASCII
,||,,,,||,,,,|||,,,|
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
Week 846
END OF FILE