CS1102 Tut 1 ADTs - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

CS1102 Tut 1 ADTs

Description:

Question : In parameter passing in Java methods (1) Java pass by value ... public class Tester. static Person b = new Person(); public static void main(String[]args) ... – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 39
Provided by: dcsa
Category:
Tags: adts | cs1102 | tester | tut

less

Transcript and Presenter's Notes

Title: CS1102 Tut 1 ADTs


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

2
Test yourself
  • Question In parameter passing in Java methods
  • (1) Java pass by value
  • (2) Java pass by reference
  • (3) Java pass by value for primitives and
    reference for non-primitives

3
Bonus Question
  • public class Tester
  • static Person b new Person()
  • public static void main(Stringargs)
  • Person a new Person()
  • a.setName("Fred")
  • b.setName("Peter")
  • changeName_1(a)
  • System.out.println(a.getName() " "
    b.getName())
  • changeName_2(a)
  • System.out.println(a.getName() " "
    b.getName())
  • public static void changeName_1(Person a)
  • b new Person()
  • b.setName("David")
  • a b

class Person private String name
public void setName(String name)
this.name name
public String getName()
return this.name
4
Last week..
  • What is the output ?
  • To understand this problem, we must understand
    how Java handle variable names
  • When we declare a non-primitive variable, the
    variable contains the address to the object and
    NOT the object itself
  • Recall Primitive variables are int, double,
    float etc..

5
Last week..
  • Person a new Person(Michael)
  • //When the new keyword is used, a new object is
    created. Variable a is updated to store the
    address of this object
  • Person temp a
  • //In this step, the address of object pointed to
    by a is copied into temp. The object is not
    copied

6
Bonus Question
  • public class Tester
  • static Person b new Person()
  • public static void main(Stringargs)
  • Person a new Person()
  • a.setName("Fred")
  • b.setName("Peter")
  • changeName_1(a)
  • System.out.println(a.getName() " "
    b.getName())
  • changeName_2(a)
  • System.out.println(a.getName() " "
    b.getName())
  • public static void changeName_1(Person a)
  • b new Person()
  • b.setName("David")
  • a b

class Person private String name
public void setName(String name)
this.name name
public String getName()
return this.name
7
Question 1
8
Question 1
9
Question 1
  • a.setName(Fred)
  • b.setName(Peter)
  • changeName_1(a)
  • //a.name Fred, b.name David
  • changeName_2(a)
  • public static void changeName_2(Person a)
  • a.setName("John")
  • //output John David

10
Question 1
11
How this relates to LinkedList
  • class Listnode String value ListNode
    next public Listnode(String s) value
    s

Next just stores the address of the next
listnode.
12
How this relates to LinkedList
  • Listnode n1 new Listnode(a)
  • Listnode n2 new Listnode(b)
  • Listnode n3 new Listnode(c)
  • n1.next n2
  • n1.next.next n3

What is n1.next ?
13
Another example
head
temp
  • head.next.next.prev.next.next.prev temp
  • This is an assignment operation!
  • You are setting the previous pointer of some node
    to temp!

14
Another example
head
temp
  • head.next.next.prev.next.next.prev temp
  • How do you determine which node?
  • Step by step

15
Another example
head
temp
  • head.next.next.prev.next.next.prev temp
  • head is node 3

16
Another example
head
temp
  • head.next.next.prev.next.next.prev temp
  • Head.next is node 12

17
Another example
head
temp
  • head.next.next.prev.next.next.prev temp
  • head.next.next is node 32

18
Another example
head
temp
  • head.next.next.prev.next.next.prev temp
  • head.next.next.prev is node 12

19
Another example
head
temp
  • head.next.next.prev.next.next.prev temp
  • head.next.next.prev.next.next is node 35
  • So, essentially the statement is saying node
    35.prev temp

20
Group assignment
  • Question 1 Group 4
  • Question 2 Group 1
  • Question 3 Group 2
  • Question 4 Group 3
  • Question 5 Group 4
  • Question 6 Group 1

21
q1
  • Consider an ADT List of integers. Write the
    method
  • int addList(List aList) throws ListOutOfBoundsExce
    ption
  • that computes the sum of the integers in the list
    aList. The operations on list are
  • createList()
  • isEmpty ()
  • size ()
  • add (newPosition, newItem)
  • remove (index)
  • removeAll ()
  • get (index)

22
q1 solution
  • int addList(List aList) throws ListOutOfBoundsExce
    ption
  • // ----------------------------------------------
    ------------
  • // Computes the sum of the integers in the list
    aList.
  • // Precondition aList is a list of integers.
  • // Postcondition The sum of the integers in
    aList is returned.
  • // ----------------------------------------------
    ------------
  • int sum 0, nextItem
  • for(int i 1 i lt aList.size() i)
  • nextItem aList.get(i)
  • sum nextItem
  • // end for
  • return sum
  • // end AddList

23
q2
  • Implement the method swap (aList, i, j) that
    interchanges the items currently in positions i
    and j of a list. Define the method in terms of
    the operations of the ADT List so that it is
    independent of any particular implementation of
    the list. Throw an exception ListIndexOutOfBoundsE
    xception if i and j is out of range.

24
q2
  • What are the useful operations you can use in the
    ADT list ?
  • get()
  • remove()
  • add()
  • set()

25
q2 solution
  • void swap(List aList, int i, int j) throws
    ListOutOfBoundsException
  • // ----------------------------------------------
    ---------
  • // Swaps the ith and the jth items in the list
    aList.
  • // Precondition aList is a list of type
    Object.
  • // Postconditions The ith and jth items are
    swapped in aList
  • // ----------------------------------------------
    ---------
  • Object first, second
  • first aList.get(i)
  • second aList.get(j)
  • aList.remove(i)
  • aList.add(i, second)
  • aList.remove(j)
  • aList.add(j, first)
  • // end Swap

26
q2 solution - alternative
  • void swap(List aList, int i, int j) throws
    ListOutOfBoundsException
  • // ----------------------------------------------
    ---------
  • // Swaps the ith and the jth items in the list
    aList.
  • // Precondition aList is a list of type
    Object.
  • // Postconditions The ith and jth items are
    swapped in aList
  • // ----------------------------------------------
    ---------
  • Object first, second
  • first aList.get(i)
  • second aList.get(j)
  • aList.set(i, second)
  • aList.set(j, first)
  • // end Swap

27
q3
  • Use the method swap that you wrote in Question 2
    to write a method that reverses the order of the
    items in a list aList.

28
q3 solution
  • void reverseList(ListReferenceBased aList) throws
  • ListOutOfBoundsException
  • // ----------------------------------------------
    --------
  • // Reverses the order of the items in list
    aList.
  • // Preconditions none.
  • // Postconditions The contents of aList are in
    reverse order.
  • // ----------------------------------------------
    --------
  • int high aList.size()
  • for(int low 1 low lt high low, high--)
  • swap(aList, low, high)
  • // end ReverseList

29
q4 q5
  • Identify the operations in an ADT.
  • If you are writing the ADT to manage some data,
    what are the operations you need?

30
q4
  • In mathematics, a set is a group of distinct
    items. Specify operations as a part of the ADT
    set.

31
q4 solution
  • Some ADT Set operations (Operate on this set)
  • createSet() // creates an empty set
  • isEmpty() // Is "this" set empty?
  • setSize() // returns the no. of elements of
    "this" set
  • isSubsetOf(set2) // Is set2 a subset of this
    set?
  • isAnElement(item) // Is Item an element of
    this set?
  • setUnion(set2) // union of set2 and this set
  • setIntersection(set2) // intersection of set2
    and this set
  • isMemberOf (item) // Is item a member of set
  • isProperSubsetOf (set2) // Is set2 a proper
    subset of this set ?
  • setEqual (set2) // Is set 2 equal
    to this set?
  • setDifference (set2) // return the set
    difference btwn two sets
  • addElement (item) // Insert an element into
    the set

32
q5
  • Specify operations that are as part of the ADT
    Fraction. Include typical operations such as
    addition, subtraction and reduce (reduce fraction
    to lowest terms).

33
q5 solution
  • Some ADT Fraction operations
  • createFraction() // creates a fraction with a
    value of 0
  • createFraction(num, denom) // creates fraction
    num/denom
  • setFraction(num, denom) // sets fraction
    num/denom
  • getNumerator() // returns the fraction's
    numerator
  • getDenominator() // returns the fraction's
    denominator
  • addFraction(secondFraction) // returns the sum
  • subtractFraction(secondFraction)// returns the
    difference
  • multiplyFraction(secondFraction) // returns the
    product
  • divideFraction(secondFraction) // returns the
    quotient
  • reduceToLowestTerms() // Reduce numerator and
    denominator
  • // to lowest terms

34
q6
  • Implement ADT Set that you proposed in Q4 using
    ArrayList of Integers.
  • Think of the algorithm before you implement it!

35
q6
  • addElement(Integer o)
  • Add an integer into the set only if the set does
    not already have the element
  • isSubsetOf(Set s2)
  • How do you check that set s2 is a subset of the
    current set ?

36
q6 solution (1)
  • import java.util.
  • public class Set
  • private ArrayList ltIntegergt v
  • public Set () v new ArrayList ltIntegergt ()
  • // addElement into this set
  • public void addElement (Integer o) if
    (!v.contains (o)) v.add (o)
  • // determines whether this Set is empty
  • public boolean isEmpty() return (v.size ()
    0)
  • // returns the number of elements in this Set
    (the cardinality)
  • public int setSize() return v.size ()

37
q6 solution (2)
  • // determines whether set2 is a subSet of this
    set
  • public boolean isSubsetOf(Set set2)
  • boolean subset true
  • int i 0
  • while (subset i lt set2.v.size())
  • if (!v.contains (set2.v.get (i)))
    subset false
  • return subset
  • // determines if Item is an element of this
    set
  • public boolean isAnElement(Integer item)
  • return v.contains (item)

38
q6 solution (3)
  • // inserts all items in set2 not in this set
    into this set
  • public void setUnion(Set set2)
  • for (int i 0 i lt set2.v.size() i)
  • if (!v.contains (set2.v.get (i))) v.add
    (set2.v.get (i))
  • // this set will contain common elements of
    this set and set2
  • public void setIntersection(Set set2)
  • ArrayList ltIntegergt temp new ArrayList
    ltIntegergt ()
  • for (int i 0 i lt v.size () i)
  • if (set2.v.contains (v.get (i))) temp.add
    (v.get (i))
  • v new ArrayList ltIntegergt ()
  • for (int i 0 i lt temp.size () i) v.add
    (temp.get (i))
Write a Comment
User Comments (0)
About PowerShow.com