The Vector Class - PowerPoint PPT Presentation

About This Presentation
Title:

The Vector Class

Description:

v.insert('Shemp', 1); v.delete(2); for (int i=0;i v.size();i ) System. ... Shemp. Curly. Design. Implementation will be easily accomplished with a linked list. ... – PowerPoint PPT presentation

Number of Views:101
Avg rating:3.0/5.0
Slides: 39
Provided by: BillL161
Category:
Tags: class | shemp | vector

less

Transcript and Presenter's Notes

Title: The Vector Class


1
The Vector Class
2
Vector Class
  • Write a Vector class. This will be a container
    class that will hold objects. It should allow the
    following operations
  • add Add an object to the end of the Vector
  • find Determine if an object is in the Vector
  • get Get a certain element (by number) from the
  • Vector
  • insert Insert a new element into a certain
    position
  • delete Delete a certain element from the Vector
  • size Returns size of Vector

3
Typical Operation
  • Vector v
  • v new Vector()
  • v.add(Larry)
  • v.add(Moe)
  • v.add(Curly)
  • System.out.println
  • (v.find(Ned))
  • System.out.println
  • (v.get(1))
  • v.insert(Shemp, 1)
  • v.delete(2)
  • for
  • (int i0iltv.size()i)
  • System.out.println
  • (v.get(i))
  • false
  • Moe
  • Larry
  • Shemp
  • Curly

4
Design
  • Implementation will be easily accomplished with a
    linked list.
  • We'll assume we have available an LLNode which
    holds objects and has get/setData and get/setNext
    methods.
  • We'll start with the basic shell and the fields
    we'll need

5
LLNode
  • class LLNode
  • private Object data
  • private LLNode next
  • public LLNode(Object o)
  • setData(o)
  • setNext(null)
  • public void setData(Object o)
  • data o
  • public Object getData()
  • return data

6
LLNode
  • // class LLNode (continued)
  • public void setNext(LLNode n)
  • next n
  • public LLNode getNext()
  • return next
  • // LLNode

7
Things to notice
  • As you get used to any language you will learn a
    basic shell or template from which to start
    programs.
  • Creating and using such a shell will sometimes
    aid in getting started
  • Its also a good place to include things that you
    always forget (e.g. closing braces)

8
Basics
  • public class Vector
  • private LLNode head
  • private int count
  • / -------------------------------------------
    /
  • / Basic constructor.
  • ltBRgtltBgtPreconditionlt/Bgt None.
  • ltBRgtltBgtPostconditionlt/Bgt Vector
    instantiated
  • with size set to zero.
  • /
  • public Vector()
  • setHead(null)
  • count 0

9
Basics
  • / Sets head reference.
  • ltBRgtltBgtPreconditionlt/Bgt Instantiation.
  • ltBRgtltBgtPostconditionlt/Bgt head reference
    set
  • to newhead
  • _at_param newhead New head reference
  • /
  • private void setHead(LLNode newhead)
  • head newhead

10
Basics
  • / Returns head reference.
  • ltBRgtltBgtPreconditionlt/Bgt Instantiation.
  • ltBRgtltBgtPostconditionlt/Bgt No change to
    Vector
  • _at_return Head reference
  • /
  • private LLNode getHead()
  • return head

11
Things to Notice
  • Make the documentation work for you as opposed to
    being just a dumb requirement that you do after
    the real work is done.
  • Create the documentation in the form of Javadocs
  • Create this documentation in a working i.e.
    compilable template
  • As each method is written compile and test!

12
Design by Contract
  • / Adds an object to the end of the Vector.
  • Precondition Vector has been
    instantiated.
  • Postcondition The last item in the
    Vector
  • will be the item added and the Vector
    will be
  • one element longer.
  • _at_param o The object to be added.
  • /
  • public void add(Object o)
  • // add

13
Design by Contract
  • / Finds the location of an object in the
    Vector.
  • Precondition Vector has been
    instantiated.
  • Postcondition The Vector will be
    unchanged.
  • _at_param o The object to be located. Note
    The
  • target object must return true when
    tested
  • with o.equals(target)
  • _at_return An value indicating the location
    of
  • the item. The first element is numbered
    0. A
  • -1 indicates element not found.
  • /
  • public int find(Object o)
  • return 0
  • // find

14
Design by Contract
  • / Gets the n-th element of the Vector and
  • returns it.
  • Precondition Vector has been
    instantiated
  • and should have at least n1 elements.
  • Postcondition The Vector will be
    unchanged.
  • _at_param n The number of the object to be
  • located. Note The first object in the
    Vector
  • is numbered 0.
  • _at_return The desired object. If the Vector
    is
  • not at least n1 elements long null will
    be
  • returned.
  • /
  • public Object get(int i)
  • return null
  • // get

15
Design by Contract
  • / Inserts an object into the Vector at the
  • specified position. If the Vector is not
    long
  • enough acts like add.
  • _at_see add
  • ltBgtPreconditionlt/Bgt Vector has been
  • instantiated.
  • ltBgtPostconditionlt/Bgt The Vector will
    have one
  • additional element. It will either be in
  • position specified or in the last element
    in
  • the Vector (whichever is smaller).
  • _at_param o The object to be inserted.
  • _at_param n The position into which the
    object is
  • to be inserted. Note The first object in
    the
  • Vector is numbered 0.
  • /
  • public void insert(Object o, int i)
  • // insert

16
Design by Contract
  • / Deletes the n-th element of the Vector.
  • Precondition Vector has been
    instantiated
  • and the n-th element is present.
  • Postcondition The Vector will have the
    n-th
  • element deleted unless the original
    Vector
  • was not long enough to have the n-th
    element.
  • _at_param n The number of the object to be
  • deleted. Note The first object in the
    Vector
  • is numbered 0.
  • /
  • public void delete(int n)
  • // delete

17
Design by Contract
  • / Returns the size of the Vector.
  • Precondition Vector has been
    instantiated.
  • Postcondition Vector will be unchanged.
  • _at_return Size of the Vector.
  • /
  • public int size()
  • return 0
  • // delete
  • // Vector

18
Things to Notice
  • Now we go back in a code incrementally
  • Write a method
  • Add some code to the test main to test it
  • TEST IT

19
  • / Adds an object to the end of the Vector.
  • ltBRgtltBgtPreconditionlt/Bgt Vector has been
  • instantiated.
  • ltBRgtltBgtPostconditionlt/Bgt The last item in
    the
  • Vector will be the item added and the
    Vector
  • will be one element longer.
  • _at_param o The object to be added.
  • /
  • public void add(Object o)
  • if(getHead() null)
  • setHead(new LLNode(o))
  • count
  • else
  • add(getHead(), o)
  • // add

Typical error...had o instead of new LLNode(o)
20
  • / Add helper. Called by add. Adds an object
    to
  • the end of the Vector.
  • ltBRgtltBgtPreconditionlt/Bgt Vector has been
  • instantiated.
  • ltBRgtltBgtPostconditionlt/Bgt The last item
    in the
  • Vector will be the item added and the
    Vector
  • will be one element longer.
  • _at_param current Current head of list

  • (recursive).
  • _at_param o The object to be added. /
  • private void add(LLNode current, Object o)
  • if(current.getNext() null)
  • current.setNext(new LLNode(o))
  • count
  • else
  • add(current.getNext(), o)
  • // add helper

21
  • / Finds the location of an object in the
    Vector.
  • ltBRgtltBgtPreconditionlt/Bgt Vector has been
  • instantiated.
  • ltBRgtltBgtPostconditionlt/Bgt The Vector will
    be
  • unchanged.
  • _at_param o The object to be located. Note
    The
  • target object must return true when
    tested
  • with o.equals(target)
  • _at_return An value indicating the location
    of
  • the item. The first element is numbered
    0. A
  • -1 indicates element not found. /
  • public int find(Object o)
  • if(getHead() null)
  • return -1
  • else
  • return find(getHead(), o, 0)
  • // find

22
  • / -------------------------------------------
    --/
  • / find helper
    /
  • / -------------------------------------------
    --/
  • private int find(LLNode current, Object o,
    int k)
  • if(current.getData().equals(o))
  • return k
  • else if(current.getNext() null)
  • return -1
  • else
  • return find(current.getNext(), o,
    k1)
  • // find helper

23
  • / Gets the nth element of the Vector and
    returns it.
  • ltBRgtltBgtPreconditionlt/Bgt Vector has been
  • instantiated and should have at least n1
    elements.
  • ltBRgtltBgtPostconditionlt/Bgt The Vector will be
    unchanged.
  • _at_param n The number of the object to be
    located. Note The first object in the Vector is
    numbered 0.
  • _at_return The desired object. If the Vector is
    not at least n1 elements long null will be
    returned. /
  • public Object get(int i)
  • if(getHead() null)
  • return null
  • else
  • return get(getHead(), i, 0)
  • // get

24
  • / -------------------------------------------
    --/
  • / get helper
  • / -------------------------------------------
    --/
  • private Object get(LLNode current, int i, int
    k)
  • if(i k)
  • return current.getData()
  • else if(current.getNext() null)
  • return null
  • else
  • return get(current.getNext(), i,
    k1)
  • // get helper

25
  • / Inserts an object into the Vector at the
    specified position. If the Vector is not long
    enough acts like add.
  • ltBRgtltBgtPreconditionlt/Bgt Vector has been
    instantiated.
  • ltBRgtltBgtPostconditionlt/Bgt The Vector will have
    one
  • additional element. It will either be in
    position specified or in the last element in the
    Vector (whichever is smaller).
  • _at_param o The object to be inserted.
  • _at_param n The position into which the object is
  • to be inserted. Note The first object in the
    Vector is numbered 0. /

26
  • public void insert(Object o, int i)
  • if((getHead() null) (i 0))
  • LLNode temp new LLNode(o)
  • temp.setNext(getHead())
  • setHead(temp)
  • count
  • else
  • insert(getHead(), o, i, 1)
  • // insert

27
  • / -------------------------------------------
    --/
  • / insert helper
  • / -------------------------------------------
    --/
  • private void insert(LLNode current, Object o,
  • int i, int k)
  • if((current.getNext() null) (i
    k))
  • LLNode temp new LLNode(o)
  • temp.setNext(current.getNext())
  • current.setNext(temp)
  • count
  • else
  • insert(current.getNext(), o, i, k1)
  • // insert helper

28
  • / Deletes the n-th element of the Vector.
  • ltBRgtltBgtPreconditionlt/Bgt Vector has been
  • instantiated and the n-th element is
    present.
  • ltBRgtltBgtPostconditionlt/Bgt The Vector will
  • have the n-th element deleted unless the
  • original Vector was not long enough to
    have
  • the n-th element.
  • _at_param n The number of the object to be
  • deleted. Note The first object in the
    Vector
  • is numbered 0. /

29
  • public void delete(int n)
  • / Check to make sure element to be
    deleted
  • is in Vector /
  • if(n lt size() n gt 0)
  • if(n 0)
  • setHead(getHead().getNext())
  • count--
  • else
  • delete(getHead(), n, 1)
  • // delete

30
  • / Delete helper. Deletes the n-th element
    of
  • the Vector.
  • ltBRgtltBgtPreconditionlt/Bgt Vector has been
  • instantiated.
  • ltBRgtltBgtPostconditionlt/Bgt The Vector will
  • have the n-th element deleted.
  • _at_param current Head of current list
  • (recursive).
  • _at_param n The number of the object to be
  • deleted. Note The first object in the
    Vector
  • is numbered 0.
  • _at_param k Current count.
  • /

Note Javadocs do not by default include private
items. However this can be overridden so two
versions of Javadocs could be produced one for
internal and one for external use
31
  • private void delete(LLNode current, int n,
    int k)
  • if(n k)
  • current.setNext(current.getNext().getNext
    ())
  • count--
  • else
  • delete(current.getNext(), n, k1)
  • // delete helper

32
  • / Returns the size of the Vector.
  • ltBRgtltBgtPreconditionlt/Bgt Vector has been
  • instantiated.
  • ltBRgtltBgtPostconditionlt/Bgt Vector will be
  • unchanged.
  • _at_return Size of the Vector. /
  • public int size()
  • return count
  • // size

33
  • / Convert vector into string listing.
  • ltBRgtltBgtPreconditionlt/BgtInstantiation.
  • ltBRgtltBgtPostconditionlt/BgtNo change to
    Vector.
  • _at_return Representation of Vector as list.
  • /
  • public String toString()
  • String retVal ""
  • for(int i0 i lt size() i)
  • retVal get(i) "\n"
  • return retVal

34
  • / Test main. /
  • public static void main(String args)
  • Vector v new Vector()
  • System.out.println("Should be -1 "

  • v.find("Test"))
  • v.add("Test")
  • v.add("Test2")
  • System.out.println("Should be 0 "

  • v.find("Test"))
  • System.out.println("Should be 1 "

  • v.find("Test2"))
  • System.out.println("Should be -1 "

  • v.find("Missing"))

35
  • / Test main. /
  • / Test get /
  • System.out.println("Should be Test "

  • v.get(0))
  • System.out.println("Should be Test2 "

  • v.get(1))
  • if(v.get(2) null)
  • System.out.println("Passed get 2
    test")
  • else
  • System.out.println("Failed get 2
    test")

36
  • / Test main. /
  • / insert test /
  • v.insert("NewHead", 0)
  • v.insert("NewOne", 1)
  • System.out.println("Start Traversal")
  • System.out.println(v)
  • System.out.println("End Traversal")
  • System.out.println("Size " v.size())

37
  • / delete test /
  • v new Vector()
  • v.add("Zero")
  • v.add("One")
  • v.add("Two")
  • v.add("Three")
  • v.add("Four")
  • v.add("Five")
  • System.out.println("Starting
    vector\n"v)
  • v.delete(0)
  • System.out.println("Deleted first\n"v)
  • v.delete(4)
  • System.out.println("Deleted last\n"v)
  • v.delete(-v.size())
  • v.delete(v.size())
  • System.out.println("Should be no
    change\n"v)
  • v.delete(1)
  • System.out.println("Two should be
    gone\n" v)
  • // main

38
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com