The Vector Class. Vector Class Write a Vector class. This will be a container class that will hold objects. It should allow the following operations:

Slides:



Advertisements
Similar presentations
1 Linked Lists Continued Lecture 5 Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists ADS2 Lecture 5.
Advertisements

1 - Recursion on linked lists Lecture 7 ADS2 Lecture 7.
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Linked Lists Chapter 4.
Section 2.5 Single-Linked Lists. A linked list is useful for inserting and removing at arbitrary locations The ArrayList is limited because its add and.
Container Classes A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as.
Building a Linked List in Java. Linked List In the Procedural Paradigm a linked list consisted of: –A pointer to the head of the list –Nodes (in dynamic.
Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
1 CS2200 Software Development Lecture 27: More Testing A. O’Riordan, 2008 K. Brown,
CHAPTER 8 Lists. 2 A list is a linear collection Adding and removing elements in lists are not restricted by the collection structure We will examine.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
1 Dynamic Arrays  Why Dynamic Arrays?  A Dynamic Array Implementation  The Vector Class  Program Example  Array Versus Vector.
Information Hiding and Encapsulation
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
Ranga Rodrigo. Class is central to object oriented programming.
13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Chapter 7 More Lists. Chapter 7: More Lists 7.1 – Circular Linked Lists 7.2 – Doubly Linked Lists 7.3 – Linked Lists with Headers and Trailers 7.4 – A.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Writing JavaDocs Mimi Opkins CECS 274 Copyright (c) Pearson All rights reserved.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
CSC 205 Programming II Lecture 18 The Eight Queens Problem.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Arrays Construct array: new double[10] Store in variable of type double[] double[] data = new double[10];
 A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
© Paul Ammann, 2008 Design by Contract Paul Ammann CS/SWE 332.
IBM TSpaces Lab 3 Transactions Event Registration.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Using a List Is Like Using a Vending.
Introduction to Data Structures and Algorithms
Chapter 5 Linked Lists II
Static?. Static Not dynamic class Widget { static int s; int d; // dynamic // or instance // variable }
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Linked Lists Chapter 4. Linked Structures: Motivations Arrays have fixed size –Problematic for data structures of arbitrary size Arrays order items physically.
1 Java linked list. Java linked list - definition ▪ Often in programming we are required to systematically store some type of information. A prime example.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
CSE 143 Lecture 4 More ArrayIntList : Pre/postconditions; exceptions; testing reading: slides created by Marty Stepp and Hélène Martin
Documentation Javadocs. Design/Documentation An essential ingredient of good Object Oriented programming is known as design by contract. This means that.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Java linked list.
Puzzle 2 1  what does the following program print? public class Puzzle02 { public static void main(String[] args) { final long MICROS_PER_DAY = 24 * 60.
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
Linked Lists CS 367 – Introduction to Data Structures.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Linked Data Structures
The need for Programming Languages
CHAPTER 4: Linked Structures
Sixth Lecture ArrayList Abstract Class and Interface
Sequences 5/10/2018 2:01 PM Linked Lists Linked Lists.
Single-Linked Lists.
Software Development Java Classes and Methods
CC 215 Data Structures Stack ADT
Binary Search one reason that we care about sorting is that it is much faster to search a sorted list compared to sorting an unsorted list the classic.
Data Structures ADT List
slides created by Ethan Apter
Initializing Objects.
Linked Lists [AJ 15].
slides created by Ethan Apter
The Basics of Recursion
Lists.
Data Structures & Algorithms
Recursive Objects Singly Linked Lists.
slides created by Ethan Apter and Marty Stepp
Presentation transcript:

The Vector Class

Vector Class Write a Vector class. This will be a container class that will hold objects. It should allow the following operations: –addAdd an object to the end of the Vector –findDetermine if an object is in the Vector –getGet a certain element (by number) from the Vector –insertInsert a new element into a certain position –deleteDelete a certain element from the Vector –sizeReturns size of Vector

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 i=0;i<v.size();i++) System.out.println (v.get(i)); false Moe Larry Shemp Curly

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:

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; }

LLNode // class LLNode (continued) public void setNext(LLNode n) { next = n; } public LLNode getNext() { return next; } } // LLNode

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 It’s also a good place to include things that you always forget (e.g. closing braces)

Basics public class Vector { private LLNode head; private int count; /* */ /** Basic constructor. Precondition: None. Postcondition: Vector instantiated with size set to zero. */ public Vector() { setHead(null); count = 0; }

Basics /** Sets head reference. Precondition: Instantiation. Postcondition: head reference set to newhead New head reference */ private void setHead(LLNode newhead) { head = newhead; }

Basics /** Returns head reference. Precondition: Instantiation. Postcondition: No change to Head reference */ private LLNode getHead() { return head; }

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!

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 o The object to be added. */ public void add(Object o) { } // add

Design by Contract /** Finds the location of an object in the Vector. Precondition: Vector has been instantiated. Postcondition: The Vector will be o The object to be located. Note: The target object must return true when tested with 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

Design by Contract /** Gets the n-th element of the Vector and returns it. Precondition: Vector has been instantiated and should have at least n+1 elements. Postcondition: The Vector will be n The number of the object to be located. Note: The first object in the Vector is numbered The desired object. If the Vector is not at least n+1 elements long null will be returned. */ public Object get(int i) { return null; } // get

Design by Contract /** Inserts an object into the Vector at the specified position. If the Vector is not long enough acts like add Precondition: Vector has been instantiated. Postcondition: The Vector will have one additional element. It will either be in position specified or in the last element in the Vector (whichever is o The object to be 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

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

Design by Contract /** Returns the size of the Vector. Precondition: Vector has been instantiated. Postcondition: Vector will be Size of the Vector. */ public int size() { return 0; } // delete } // Vector

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

/** 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 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)

/** Add helper. Called by add. 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 current Current head of list 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

/** Finds the location of an object in the Vector. Precondition: Vector has been instantiated. Postcondition: The Vector will be o The object to be located. Note: The target object must return true when tested with 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

/* */ /* 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, k+1); } // find helper

/** Gets the nth element of the Vector and returns it. Precondition: Vector has been instantiated and should have at least n+1 elements. Postcondition: The Vector will be n The number of the object to be located. Note: The first object in the Vector is numbered The desired object. If the Vector is not at least n+1 elements long null will be returned. */ public Object get(int i) { if(getHead() == null) return null; else return get(getHead(), i, 0); } // get

/* */ /* 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, k+1); } // get helper

/** Inserts an object into the Vector at the specified position. If the Vector is not long enough acts like add. Precondition: Vector has been instantiated. Postcondition: The Vector will have one additional element. It will either be in position specified or in the last element in the Vector (whichever is o The object to be 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) { if((getHead() == null) || (i == 0)) { LLNode temp = new LLNode(o); temp.setNext(getHead()); setHead(temp); count++; } else insert(getHead(), o, i, 1); } // insert

/* */ /* 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, k+1); } // insert helper

/** 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 n The number of the object to be deleted. Note: The first object in the Vector is numbered 0. */

public void delete(int n) { /* Check to make sure element to be deleted is in Vector */ if(n = 0) { if(n == 0) { setHead(getHead().getNext()); count--; } else { delete(getHead(), n, 1); } } // delete

/** Delete helper. Deletes the n-th element of the Vector. Precondition: Vector has been instantiated. Postcondition: The Vector will have the n-th element current Head of current list n The number of the object to be deleted. Note: The first object in the Vector is numbered 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

private void delete(LLNode current, int n, int k){ if(n == k) { current.setNext(current.getNext().getNext()); count--; } else { delete(current.getNext(), n, k+1); } } // delete helper

/** Returns the size of the Vector. Precondition: Vector has been instantiated. Postcondition: Vector will be Size of the Vector. */ public int size() { return count; } // size

/** Convert vector into string listing. Precondition: Instantiation. Postcondition: No change to Representation of Vector as list. */ public String toString() { String retVal = ""; for(int i=0; i < size(); i++) { retVal += get(i) + "\n"; } return retVal; }

/** 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"));

/** 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");

/** 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());

/* 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 } // Vector