CS1102 Tutorial 2 ADTs

W
Document Sample
scope of work template
							CS1102 Tutorial 2 ADTs


     Max Tan
     tanhuiyi@comp.nus.edu.sg
     COM1-01-09 Tel:65164364


                                1
15 minute recap : ADT

   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…
                                             2
15 minute recap : ADT

           To add a friend, you simply
            tap the Menu button, then
            select Contacts, then select
            Add contact




                                       3
15 minute recap : ADT
           How to add a contact
           addContact(string s)

           //Method will add a contact
            //name into the ADT
           //Output boolean
            //representing success or
            //failure
                                          4
15 minute recap : ADT

   Does your manual tell you the
    implementation details of how it
    works?




                                       5
15 minute recap
           1. After user enters a name, we
            use the Scanner class to obtain
            the string
           2. I check the array to see if its
            full, if array is full, I double it
           3. Find the first empty spot and
            insert the user name there…
           Etc etc

                                              6
15 minute recap : ADT
           How to add a contact
           addContact(string s)

           //Input a number
           //When the name is added
            //we check that the array is
            //not full, if its full we…
                   BAD!!!                  7
15 minute recap : ADT
   Are you really interested to know the
    details?
   You just want to add a contact!
   And that is the purpose of an ADT – to allow
    the users to use it without caring about the
    implementation details!
   When creating an Abstract Data Type, you
    have to imagine what you need from it, and
    provide public methods to use the ADT

                                               8
tutorial format
   Today
   Group 1 = Question 2
   Group 2 = Question 3
   Group 3 = Question 4
   Group 4 = Question 1

   Next week, Group 1 = Q3, Group 2 = Q4,
    Group 3 = Q1, Group 4 = Q2 (add 1 to your
    question number every week)
                                                9
Q1

   Identify two ADTs specified in the text
    above and describe briefly in which
    class the respective objects will be
    instantiated.




                                          10
   Q1
Each order has information regarding the date of order. An order also
consists of one or more different items ..

        Actually there are 3 ADTs you can identify
        OrderADT or OrderList, this is a container
         for Order objects
        You can add, edit, delete, view orders by
         using the interface provided from the
         OrderADT



                                                                        11
   Q1
.. items with different properties like price and quantity. The user can add,
edit and delete the individual items in each order.

         Order which contains the “date” for that
          order and also a list of different Items
         Provide methods in the Order ADT to
          add/edit/delete/view the individual items

         Item with properties like price and quantity
         Provide methods in the Item ADT to
          edit/view the item

                                                                                12
Q1

   Question:
   What methods would you provide so
    that the user would be able to print
    ALL the items in ALL the Orders ?




                                           13
Q2 (a)
   .. Java Array as the data structure to store the
    contacts. ..provide public methods to add, edit,
    delete a contact. A contact contains the contact
    name and contact number information..
   How would you implement the add
    method?



                                                       14
Q2 (a)

   Two ways
   Always insert at the end (but you have
    to ensure that there are no gaps when
    deleting)
   Always insert into the first available
    slot
   Question: What are their advantages
    and disadvantages?
                                             15
Q2 (b)
   .. Java Array as the data structure to store the
    contacts. ..provide public methods to add, edit,
    delete a contact. A contact contains the contact
    name and contact number information..
   What would you do when the array is
    full?



                                                       16
Q2 (b)

   Simply double the size of the array and
    copy the contents of the old array into
    the new array
   Question: why double? Why not
    increase array by a fixed number?



                                          17
Q2 (c)
   .. Java Array as the data structure to store the
    contacts. ..provide public methods to add, edit,
    delete a contact. A contact contains the contact
    name and contact number information..
   How would you implement the edit
    method?



                                                       18
Q2 (c)

   edit(string oldContactName, string
    newContactName, int newContactNo)
   Search through all the contacts to find
    the contact we are interested in, then
    update it accordingly



                                              19
Q2 (d)
   .. Java Array as the data structure to store the
    contacts. ..provide public methods to add, edit,
    delete a contact. A contact contains the contact
    name and contact number information..
   How would you implement the delete
    method?



                                                       20
Q2 (d)

   Two methods depending on your add
    implementation
   If your Add method inserts at the end,
    then your delete method have to close
    gaps by shifting all next elements.
                      delete(e)           next add

                  q   w    e      r   t


                  q   w    r      t
                                                     21
Q2 (d)

   Two methods depending on your add
    implementation
   If your Add method inserts in the first
    empty spot, then just set the slot to
    empty
          delete(e)               next add

      q   w    e      r   t   u     g


      q   w           r   t   u     g
                                              22
Q2 (e)
   .. Java Array as the data structure to store the
    contacts. ..provide public methods to add, edit,
    delete a contact. A contact contains the contact
    name and contact number information..
   Identify a potential problem resulting
    from your implementation in (a) and
    how it would affect your
    implementation in (c) and (d).

                                                       23
Q2 (e)
   What happens if you try to insert multiple
    contacts with the same name?
   How would you know which is the correct
    contact to edit/delete later on?
   Solution would be to disallow multiple
    contacts with the same name
   (This is quite open ended so if your answer
    is something that makes sense, then it is
    correct)

                                                  24
Q3
   ..ArrayList < ArrayList <Integer> >.. Write the
    method int sumList(ArrayList<ArrayList <Integer>>
    aList) that computes the sum of all the integers in
    the list aList.




                                                      25
Q3

   If you are confused, try to think of the
    ArrayList as a box. So we have an
    ArrayList of ArrayLists…
                     The ArrayList stores many
                     ArrayList<Integer> object




                                                 26
Q3

   The ArrayList<Integer> is the small
    box


                           6

                   2
                      7
                   51



                                          27
Q3

   So the algorithm is, take out the small
    boxes one by one.
   For each small box, take out the
    integer objects one by one


                                   4


                                              28
Q3

   There is no limitations to how many
    level of boxes there can be




                                          29
Q3
int sumList(ArrayList< ArrayList<Integer> > aList) throws ListOutOfBoundsException
{
  int sum = 0;
  //Iterates through all the available ArrayLists.
  for(int i = 0; i < aList.size(); i++)
  {
        //Gets the individual ArrayList
                  ArrayList<Integer> currList = aList.get(i);
        //Iterate through all the available integers
                                                                     Iterate through   all the
        For(int j = 0; j < currList.size(); j++)                     small boxes
        {
           Integer currValue = currList.get(j);
           //Java also does auto-unboxing of the primitive
           //int value from the Integer object, so
           // sum += currValue; would be correct.
           sum += currValue.intValue();
        }
  }
  return sum;
}




                                                                                                 30
Q3
int sumList(ArrayList< ArrayList<Integer> > aList) throws ListOutOfBoundsException
{
  int sum = 0;
  //Iterates through all the available ArrayLists.
  for(int i = 0; i < aList.size(); i++)
  {
        //Gets the individual ArrayList
                  ArrayList<Integer> currList = aList.get(i);
        //Iterate through all the available integers
                                                                     Take out a      small box
        For(int j = 0; j < currList.size(); j++)
        {
           Integer currValue = currList.get(j);
           //Java also does auto-unboxing of the primitive
           //int value from the Integer object, so
           // sum += currValue; would be correct.
           sum += currValue.intValue();
        }
  }
  return sum;
}




                                                                                                 31
Q3
int sumList(ArrayList< ArrayList<Integer> > aList) throws ListOutOfBoundsException
{
  int sum = 0;
  //Iterates through all the available ArrayLists.
  for(int i = 0; i < aList.size(); i++)
  {
        //Gets the individual ArrayList
                  ArrayList<Integer> currList = aList.get(i);
        //Iterate through all the available integers
        for(int j = 0; j < currList.size(); j++)
        {                                                            Iterate through    all the
           Integer currValue = currList.get(j);
           //Java also does auto-unboxing of the primitive           integers in that   selected
           //int value from the Integer object, so                   small box
           // sum += currValue; would be correct.
           sum += currValue.intValue();
        }
  }
  return sum;
}




                                                                                                   32
Q3
int sumList(ArrayList< ArrayList<Integer> > aList) throws ListOutOfBoundsException
{
  int sum = 0;
  //Iterates through all the available ArrayLists.
  for(int i = 0; i < aList.size(); i++)
  {
        //Gets the individual ArrayList
                  ArrayList<Integer> currList = aList.get(i);
        //Iterate through all the available integers
        for(int j = 0; j < currList.size(); j++)
        {                                                            Get each integer   and sum
           Integer currValue = currList.get(j);
           //Java also does auto-unboxing of the primitive           it up
           //int value from the Integer object, so
           // sum += currValue; would be correct.
           sum += currValue.intValue();
        }
  }
  return sum;
}




                                                                                             33
Q3
int sumList(ArrayList< ArrayList<Integer> > aList) throws ListOutOfBoundsException
{
  int sum = 0;
  //Iterates through all the available ArrayLists.
  for(int i = 0; i < aList.size(); i++)
  {
        //Gets the individual ArrayList
                  ArrayList<Integer> currList = aList.get(i);
        //Iterate through all the available integers
        for(int j = 0; j < currList.size(); j++)
        {                                                               Get each integer and sum
           Integer currValue = currList.get(j);
           //Java also does auto-unboxing of the primitive              it up
           //int value from the Integer object, so
           // sum += currValue; would be correct.
           sum += currValue.intValue();
        }
  }
  return sum;
}
                                                           Note that Java can handle auto-unboxing
                                             of the primitive wrappers.
                                             To avoid confusion to yourself, you
                                             should always try to use the methods 34
                                             provided for the Java classes
Q4

   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


                                              35
Q4
     Game      Randomly                  Check for                   Win
     starts   generate 20                win/lose
               mines on                  condition
                the tiles



                                                                     Lose
                                        Read User
                                          Input




                            leftClickEvent           rightClickEve
                                 (x, y)                 nt(x, y)




                                                                            36
Q4

   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.



                                            37
Q4

   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

                                          38
Q4

   Lets imagine how the game might
    start
   Initialize the game board of 15 x 15
    tiles, and with a probability of 0.1 that
    a tile is a mine.
   (Of course the real game sets a fixed
    number of mines, but we try to keep
    things simple)
                                                39
Q4
   Constructor of the MineSweeper class to
    handle the size of grid
   Grid size does not change so use a 2
    dimensional array.
   Since each tile contains some information,
    we use a Tile ADT to represent this
    information.
   The constructor of the Tile class will take in
    the probability value to determine if a tile is a
    mine (another constructor could take in a
    isMine true/false flag).
                                                    40
Q4

   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
                                          41
Q4

class Minesweeper{
   boolean hasWon()
   boolean hasLost()
   String getTileString(int x, int y)
   int getMineCounter()
}

                                        42
Q4

   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?


                                              43
Q4
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)
}

                                        44
Q4

   Right click
   Toggles the flag status on a tile if it is
    not uncovered yet




                                                 45
Q4
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)
}

                                        46
Q4

   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!


                                            47
Q4

   For example :
    boolean isUncovered(int x, int y)
   Algorithm : Go to the grid[x][y] to
    access the tile, and ask the tile if it is
    uncovered.
   Hence Tile needs a isUncovered()
    method!

                                                 48
Q4

class Tile{
   int toggleFlag()
   void uncover()
   public boolean isUncovered()
   public string getTileString()
   bool isMine()
}
                                   49
Q4

   Why is getNeighborMines() in the
    Minesweeper class instead of the Tile
    class ?
   The tiles do not store its neighbor’s
    indices! But the Minesweeper class
    knows! (Because an array is used to
    store all the tiles)

                                            50
Q4

   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.
                                                      51
Q4

   There are countless ways to solve this
    problem
   Once you have the skeleton methods,
    it will be a simple process to just fill in
    the code in the methods!
   ADT exercise: Try implementing your
    MineSweeper ADT and then writing a
    command line interface for it.
                                                  52

						
Related docs