tutorial 2 abstract data types 1. admin stuffs some of you request to change tutorial class… –...

11
Tutorial 2 Abstract Data Types 1

Upload: lambert-harper

Post on 12-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Tutorial 2 Abstract Data Types 1. Admin Stuffs Some of you request to change tutorial class… – Transfer within my own classes (T3, T4, T5, T6) is fine

Tutorial 2Abstract Data Types

1

Page 2: Tutorial 2 Abstract Data Types 1. Admin Stuffs Some of you request to change tutorial class… – Transfer within my own classes (T3, T4, T5, T6) is fine

Admin Stuffs• Some of you request to change tutorial class…

– Transfer within my own classes (T3, T4, T5, T6) is fine.– Transfer between different tutors requires permission.

• Ask Dr Tan Sun Teck’s permission for such changes.

• Refrain from asking lab assignments to me– I am not supposed to help you with debugging or finding the algorithm etc…

• Bidding system not working (yet) – If this system is not good, I will be forced to change it…

2

Page 3: Tutorial 2 Abstract Data Types 1. Admin Stuffs Some of you request to change tutorial class… – Transfer within my own classes (T3, T4, T5, T6) is fine

Abstract Data Type (ADT)• Data Structure:

– “Structure of Data”, structure makes life easier

• Algorithm:– Step by step way to solve certain problem

• Abstract Data Type– Data + Operations (Algorithms) to organize that data structure– Walls: We do not need to know the implementation details– Examples: Complex Number, Sphere, ColoredSphere:Sphere, List, etc.

• Wait!! Contradictions??– We are given so many implementation details here! Why bother with details?

• In fact, we are going to do that throughout this module – To give you a strong theoretical foundation…– Later on, you will just use these ADTs for your programming tasks

3

Page 4: Tutorial 2 Abstract Data Types 1. Admin Stuffs Some of you request to change tutorial class… – Transfer within my own classes (T3, T4, T5, T6) is fine

Implementing ADT in Java: Class• Java class is used to implement these ADT• This is the basic pattern of a class implementation:

– public class C { // save as C.java private (instance) attributes… public (instance) attributes, constructor, methods…}

• Sometimes we use “Interface”– interface C_interface { // signature of public methods are mentioned here

desired interfaces…}

– public class C implements C_interface { // a guarantee that we will implement the methods implementation must adhere the agreed interface, otherwise we have compile error}

• In main method:– public static void main(String[] args) {

C cInstance = new C();}

4

Page 5: Tutorial 2 Abstract Data Types 1. Admin Stuffs Some of you request to change tutorial class… – Transfer within my own classes (T3, T4, T5, T6) is fine

Object Oriented Programming• We learned these OOP concepts via these two questions:

– Q1: Programming using agreed interfaces; Abstraction provided by ADTs; Implementing ADTs in Java.

– Q2: Software re-use, A demonstration of “Integration” of (F1_2008); (F1_Driver, F1_Team, F1_Championship); and (F1_Race)

5

Page 6: Tutorial 2 Abstract Data Types 1. Admin Stuffs Some of you request to change tutorial class… – Transfer within my own classes (T3, T4, T5, T6) is fine

Student Presentation• T3

1. N/A2. N/A3. N/A

• T41. N/A2. N/A3. N/A

• T51. Tan Yan Hao2. Tan Yan Hao3. Tan Yan Hao

• T61. Wong Shiang Ker2. Nguyen Duy Hoang3. Nguyen Duy Hoang

6

Better participation level is expected for tutorial 3 onwards!

Page 7: Tutorial 2 Abstract Data Types 1. Admin Stuffs Some of you request to change tutorial class… – Transfer within my own classes (T3, T4, T5, T6) is fine

Question 1 & 2 - F1 (1)

The answer for these two questions are in the Java source codes! 7

Page 8: Tutorial 2 Abstract Data Types 1. Admin Stuffs Some of you request to change tutorial class… – Transfer within my own classes (T3, T4, T5, T6) is fine

8

Visit this Simulator (free) at Singtel Headquarter, Somerset

Page 9: Tutorial 2 Abstract Data Types 1. Admin Stuffs Some of you request to change tutorial class… – Transfer within my own classes (T3, T4, T5, T6) is fine

Question 1 & 2 - F1 (2)• Coded by your TA:

– F1_2008.java

• Coded by you:– Q1:

• F1_Driver.java• F1_Team.java• F1_Championship.java

– Q2:• F1_Race.java

9

The bridge between the two:

INTERFACE

Airbus 380 Integration Problem:http://news.airwise.com/story/view/1169812254.html

Page 10: Tutorial 2 Abstract Data Types 1. Admin Stuffs Some of you request to change tutorial class… – Transfer within my own classes (T3, T4, T5, T6) is fine

Q3 – Compact Arraya) How to add?

– Find first empty slot, insert it (bad). O(n)– Maintain an index of first empty slot, insert there, add the index by 1 (good). O(1)

b) How to add when full?– Create a new array, double the size, copy the entries to new array.

c) How to edit?– Search it from left to right, if found, replace the content.– As soon as you hit an empty slot, it means that the entry is not in the compact array.

d) How to delete?– Search it, delete the entry, shift everything on the right side of it to left by one! Tedious!

e) What are the potential issues with (b) and (d)? (see above)

10

Page 11: Tutorial 2 Abstract Data Types 1. Admin Stuffs Some of you request to change tutorial class… – Transfer within my own classes (T3, T4, T5, T6) is fine

That’s all for today• Next week, we will continue our lesson with Linked List.• Maybe a better data structure than “compact array”, especially for Q3.

– Remember that the “best” data structure depends on the given scenario!

11