COMPUTER 2430 Object Oriented Programming and Data Structures I

Slides:



Advertisements
Similar presentations
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.
Advertisements

Stacks, Queues, and Linked Lists
Public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public.
EC-241 Object-Oriented Programming
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
CS 2430 Day 14. Agenda for today The Bag class No mixing! What if we have a bunch of String s that we want to store in our program? We can’t use the.
Arrays An array is a collection of variables, all of the same type. An array is created with the new operator. The size of an array must be specified at.
25-Jun-15 Vectors. 2 Vectors and arrays A Vector is like an array of Object s Differences between arrays and Vector s: Arrays have special syntax; Vector.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
Part 2. Searching Arrays Looking for a specific element in an array E.g., whether a certain score (85) is in a list of scores Linear search Binary search.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.
Arrays Construct array: new double[10] Store in variable of type double[] double[] data = new double[10];
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching.
CS 2430 Day 12. Agenda for today Container class example: DateList Growable container classes.
CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18.
1 Java linked list. Java linked list - definition ▪ Often in programming we are required to systematically store some type of information. A prime example.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
CS 1430: Programming in C++ 1. Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s)
Java linked list.
Java Programming Persistent Data Types. Persistent Data Structure A persistent data structure is a data structure having an internal state that never.
CS 367 Introduction to Data Structures Charles N. Fischer Fall s367.html.
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,
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
Linked Data Structures
The List ADT.
Computing with C# and the .NET Framework
Chapter 8 – Arrays and Array Lists
Chapter 5 Ordered List.
Efficiency of in Binary Trees
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 302 Week 11 Jim Williams, PhD.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS Week 8 Jim Williams, PhD.
Chapter 5 Ordered List.
Data Structures ADT List
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 2430 Object Oriented Programming and Data Structures I
Outline Late Binding Polymorphism via Inheritance
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
Adapted from Pearson Education, Inc.
CSE 373: Data Structures and Algorithms
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
COMPUTER 2430 Object Oriented Programming and Data Structures I
Grouped Data Arrays, and Array Lists.
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
ArrayLists 22-Feb-19.
Collections Framework
OBJECT ORIENTED PROGRAMMING II LECTURE 4 GEORGE KOUTSOGIANNAKIS
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 2430 Object Oriented Programming and Data Structures I
Stacks CS-240 Dick Steflik.
ArrayLists.
ArrayLists 27-Apr-19.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Tree.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Presentation transcript:

COMPUTER 2430 Object Oriented Programming and Data Structures I

Container Classes Bag Set Sorted List . . .

Class Bag Container Can grow, but never shrink The order of array elements is not maintained May or may not allow duplicate elements

public class Bag { private final int INIT_SIZE = 400000; private final int GROWBY = 500; private Object[] bag = new Object[INIT_SIZE]; private int numObjects = 0; public void addObject( Object obj ) public boolean removeObject( Object obj ) private int find ( Object obj ) @Override public String toString () private void grow () }

public class CS2430Sx { public static void main(String[] args) Bag myBag = new Bag(); Object obj = new Object(); // Valid? myBag.addObject(obj); myBag.addObject(new Date()); myBag.addObject(new Student(“Frank”)); }

public class Bag { private Object[] bag = new Object[INIT_SIZE]; private int numObjects = 0; public void addObject( Object obj ) if (numObjects == bag.length) grow(); bag[numObjects ++] = obj; } private void grow () int size = bag.length + GROWBY; Object[] temp = new Object[size]; for (int i = 0; i < numObjects; i ++) temp[i] = bag[i]; bag = temp;

public class Bag { . . . // Allow duplicate? // Yes public void addObject( Object obj ) if (numObjects == bag.length) grow(); bag[numObjects ++] = obj; }

public class Bag { . . . // What if duplicates are not allowed? public boolean addObject( Object obj ) if (find(obj) != -1) return false; if (numObjects == bag.length) grow(); bag[numObjects ++] = obj; return true; } private void grow () private int find( Object obj )

It works for all classes: Polymorphism! public class Bag { private Object[] bag = new Object[INIT_SIZE]; /** Finds and returns the index of element in array bag that equals obj. @param obj the object to be found @return the index of the element if found, -1 otherwise */ private int find( Object obj ) for (int i = 0; i < numObjects; i ++) if (bag[i].equals(obj)) return i; return -1; } It works for all classes: Polymorphism!

public class Bag { private final int INIT_SIZE = 400000; private final int GROWBY = 500; private Object[] bag = new Object[INIT_SIZE]; private int numObjects = 0; // Maintain order public boolean removeObject( Object obj ) int index = find (obj); if (index > -1) -- numObjects; for (int i = index; i < numObjects; i ++) bag[i] = bag[i + 1]; return true; } return false;

// Maintain order public boolean removeObject( Object obj ) { int index = find (obj); if (index > -1) delete(index); return true; } return false; // Quiz 2 // Index is in the range and the operation will be successful! public void delete ( int index ) -- numObjects; for (int i = index; i < numObjects; i ++) bag[i] = bag[i + 1];

public class Bag { private final int INIT_SIZE = 400000; private final int GROWBY = 500; private Object[] bag = new Object[INIT_SIZE]; private int numObjects = 0; // Do not maintain order public boolean removeObject( Object obj ) int index = find (obj); if (index > -1) bag[index] = bag[-- numObjects]; return true; } return false;

// Remove one object from the array public boolean removeObject( Object obj ) { int index = find (obj); if (index > -1) delete(index); return true; } return false;

// Remove all objects from the array that equal obj public int removeObject( Object obj ) { int count = 0; int index = find (obj); while (index > -1) delete(index); count ++; index = find(obj); } return count;

Any Other Meaningful Bag Methods? public class Bag { private final int INIT_SIZE = 10000; private final int GROWBY = 100; private Object[] bag = new Object[INIT_SIZE]; private int numObjects = 0; public void addObject( Object obj ) private void grow () public boolean removeObject( Object obj ) public int find ( Object obj ) @Override public String toString () } Any Other Meaningful Bag Methods?

public class Bag { private final int INIT_SIZE = 10000; private final int GROWBY = 100; private Object[] bag = new Object[INIT_SIZE]; private int numObjects = 0; . . . public boolean isEmpty() return numObjects == 0; } public int getCount () return numObjects;

public class Bag { private Object[] bag = new Object[INIT_SIZE]; public Object getItem ( Object obj ) int index = find(obj); if ( index != -1 ) return bag[index]; return null; } Why we need this method?

GolfLeague theLeague = new GolfLeague(); GolfLeagueMember member = new SeniorMember(“Qi”, 80); System.out.println(member.toString()); // The scores and handicap for Qi has changed . . . member = theLeague.getItem(member);

SortedList Class The data declaration can be the same as that for the Bag. private Object[] list = new Object[INIT_SIZE]; private int num = 0; private final int GROWBY = 3; All the Bag methods we wrote would also work as SortedList methods, but the list must be in sorted order.

Method add Assume in ascending order. Where to insert a new object? index … 0 1 i i+1 num-1 length-1 Assume in ascending order. Where to insert a new object? list[0] < obj list[1] < obj . . . list[i] < obj list[i+1] > obj Need a comparison method!

Method add Find the position to be added Move items down one position index 0 1 2 num-1 length-1 … New item … Find the position to be added Move items down one position Add the new item Update num

Method delete Find the item (index) to be deleted 0 1 2 num-1 length-1 … … Find the item (index) to be deleted Move items up one position Update num

Sort Method

Search Method

Quiz 2

Lab 3 Submission

Prog 2