CS1102 Tut 2 ADTs - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

CS1102 Tut 2 ADTs

Description:

class Man extends Mammal. public void printCharacteristic ... Mammal m2 = new Man(); m2.printCharacteristic(); m2.printMoreCharacteristic ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 39
Provided by: soc128
Category:
Tags: adts | cs1102 | mammal | tut

less

Transcript and Presenter's Notes

Title: CS1102 Tut 2 ADTs


1
CS1102 Tut 2 ADTs
  • Max Tan
  • tanhuiyi_at_comp.nus.edu.sg
  • S15-03-07 Tel65164364

2
First 15 minutes
  • Any questions regarding tutorial/lecture
    materials?
  • Any feedback regarding Tutorials/Labs?
  • Your graded lab is up. You only have FIVE
    submissions, be sure to do extensive testing on
    your own machine before submission!
  • CS1102 Help session on Monday 5 7pm.

3
First 15 minutes
  • Lecture 2 Data abstraction slide 86

Why can we define a List type to a ArrayList
object ? What happens when we call the add
method in aList?
4
First 15 minutes
  • ArrayList is a subclass of List this means that
    ArrayList is a special kind of list!
  • Polymorphism
  • So ArrayList is a List!
  • But is List an ArrayList?
  • ArrayList a new List()

Incompatible type error!
5
First 15 minutes
  • Lecture 2 Data abstraction slide 86

Why can we define a List type to a ArrayList
object ? What happens when we call the add
method in aList?
6
First 15 minutes
  • add() is a method defined in the List class
  • This means that all subclasses of List will
    either inherit the method OR override the method
    with their own implementation of add
  • In compile time, the aList object is determined
    to be a ArrayList, and the add method in
    ArrayList is used (assuming it overrides
    List.add)

7
First 15 minutes
class eg1 public static void
main(Stringargs) Mammal m1 new
Mammal() m1.printCharacteristic() Mammal
m2 new Man() m2.printCharacteristic() m2.p
rintMoreCharacteristic()
  • class Mammal
  • public void printCharacteristic()
  • System.out.println("Warm Blooded")
  • class Man extends Mammal
  • public void printCharacteristic()
  • System.out.println("Warm Blooded and is
    intelligent")
  • public void printMoreCharacteristic()
  • System.out.println("Can code in Java!")

A man is a subclass of a mammal, hence a man is a
mammal. Is mammal a man ?
8
Question 1
  • About planning before writing code!
  • Think of use cases the interaction that you
    need to perform on the game
  • In this question, you need to write the ADTs for
    Minesweeper as well as the Tiles in the grid

9
Question 1
  • What can be performed on the game?
  • We consider only left click and right click
    actions on the grid
  • You should localize the methods into the correct
    ADTs.

10
Question 1
  • Three classes
  • In UserInterface, Minesweeper and Tile
  • You only need to write for Minesweeper and Tile.
    UserInterface should be interchangable, so all
    methods should be in Minesweeper

11
Question 1 - UserInterface
  • Before waiting for mouse click
  • 1. Check victory
  • 2. Check lost
  • 3. Get the representation for each tile could
    be flagged, or uncovered mine or etc
  • 4. Get the mine counter and display the grid

12
Question 1
  • class Minesweeperboolean hasWon()
  • boolean hasLost()
  • String getTileString(int x, int y)
  • int getMineCounter()

13
Question 1
  • Left click
  • Uncovers a tile if it is not uncovered. If its
    neighbors has mines, count the number of mines in
    its neighbors and display it.
  • How many functions is required?

14
Question 1
  • Minesweeper
  • boolean hasWon()
  • boolean hasLost()
  • String getTileString(int x, int y)
  • int getMineCounter()
  • boolean isUncovered(int x, int y)
  • void uncover(int x, int y)
  • int getNeighborMines(int x, int y)

15
Question 1
  • Right click
  • Toggles the flag status on a tile if it is not
    uncovered yet

16
Question 1
  • Minesweeper
  • boolean hasWon()
  • boolean hasLost()
  • String getTileString(int x, int y)
  • int getMineCounter()
  • boolean isUncovered(int x, int y)
  • void uncover(int x, int y)
  • int getNeighborMines(int x, int y)
  • int toggleFlag(int x, int y)

17
Question 1
  • How did you store the tiles?
  • A two dimensional array is easy!
  • What functions are needed in the Tile class? Look
    back at the Minesweeper class to determine the
    algorithm for each method!

18
Question 1
  • For example boolean isUncovered(int x, int y)
  • Algorithm Go to the gridxy to access the
    tile, and ask the tile if it is uncovered.
  • Hence Tile needs a isUncovered() method!

19
Question 1
  • class Tile
  • int toggleFlag()
  • void uncover()
  • public boolean isUncovered()
  • public string getTileString()
  • bool isMine()

20
Question 1
  • Why is getNeighborMines() in the Minesweeper
    class instead of the Tile class ?
  • The tiles do not store its neighbors indices!
    But the Minesweeper class knows! (Because an
    array is used to store all the tiles)

21
Question 1
  • In this question, the Tile class is abstract to
    everyone who needs to use the Minesweeper class.
  • Only need to know about the Minesweeper class
  • Can your interface be changed? Interface should
    only accept inputs and give outputs and perform
    nothing else your Minesweeper ADT should
    reflect that.

22
Question 1
  • Lets take a look at the command line version of
    the game!

23
Question 2
  • About Java defined ADTs
  • To give you some idea of how a good ADT should
    abstract implementation details from its users!

24
Question 2
  • class ClassList
  • private ArrayListltStringgt cl
  • public ClassList()
  • cl new ArrayListltStringgt()
  • public void add(String name)
  • cl.add(name)
  • public void delete(String name)
  • for(int i 0 i lt cl.size() i)
  • String comparedName cl.get(i)
  • if(comparedName.equals(name))
  • cl.remove(i)

25
Question 2
  • class ClassList
  • private VectorltStringgt cl
  • public ClassList()
  • cl new VectorltStringgt()
  • public void add(String name)
  • cl.add(name)
  • public void delete(String name)
  • for(int i 0 i lt cl.size() i)
  • String comparedName cl.get(i)
  • if(comparedName.equals(name))
  • cl.remove(i)

26
Question 2
  • Both Vector and ArrayList inherits from
    AbstractList with defined add and delete methods!
  • Does your method names and parameters have to
    change when you change your underlying data
    structure?
  • For the Array Implementation, we move on to
    question 3 as it is similar

27
Question 3
  • Poses the challenges when writing an ADT

28
Question 3
  • With an underlying data structure to be an array
    what size will your array be?
  • What happens when you remove and element in the
    middle of the array?
  • What happens when the array is full?

29
Question 3
  • Removing an element from the middle of the array
  • You would have to push the elements at the back
    to fill up the gap!

Size() 8
Size() 7
30
Question 3
  • When array is full
  • Create a new array with twice the size of the old
    one and copy all the old data into the larger
    array
  • Hard coding a limit is not acceptable!

31
Question 4
  • What is an ADT?
  • Represented by an interface that shields an
    implementation
  • Users are concerned with the interface only
  • Imagine your mobile phone as an ADT

32
Question 4
  • Lets look at how it is stated in your mobile
    phone manual..

33
Question 4
  • Dial the sequence of numbers, and press the green
    call button. If call succeeds you will see the
    connected word on your screen.

34
Question 4
  • How to dial a number
  • callNumber(int x)
  • //Input a number
  • //Output boolean //representing connection
    //success or failure

35
Question 4
  • Does your manual tell you the details of how it
    works?

36
Question 4
  • On entering the number, the number is stored as a
    string. The numbers are first checked for country
    code using a stringtokenizer to tokenizer into
    three tokens and then and finally if there
    are no problems, the connected word is displayed

37
Question 4
  • How to dial a number
  • callNumber(int x)
  • //Input a number
  • //When the number is passed, it //is stored as a
    string, the //numbers are checked for area //code
    using a stringtokenizer //to tokenize into 3
    tokens //representing..

BAD!!!
38
Question 4
  • Are you really interested to know the details?
  • You just want to make a call!
  • And that is the purpose of an ADT to allow the
    users to use it without caring about the
    implementation details!
Write a Comment
User Comments (0)
About PowerShow.com