COMPUTER 2430 Object Oriented Programming and Data Structures I

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Why not just use Arrays? Java ArrayLists.
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.
Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
Lab class 10: 1. Analyze and implement the following merge-sorting program. //import java.lang.*; public class MergeSorter { /** * Sort the elements of.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
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.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
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.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
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.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Arrays And ArrayLists - S. Kelly-Bootle
Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Java – Part II Lecture Notes 4. Arrays An array is a data structure that groups and organizes data l Array is a list of values (int, double, aggregates)
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Midterm Review 22C:21 Computer Science II. Problem 1 A Set ADT represents some subset of {1,2,..., n} for some natural number n. It supports the operations.
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.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
Information and Computer Sciences University of Hawaii, Manoa
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.
COMP 103 Bitsets. 2 Sets, and more Sets!  Unsorted Array  Sorted ArrayO(n) for at least one of  Linked Listcontains, add, remove  Binary Search TreeO(log.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
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.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Java Programming Persistent Data Types. Persistent Data Structure A persistent data structure is a data structure having an internal state that never.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 15: Sets and Maps Java Software Structures: Designing and Using.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Linked Data Structures
Efficiency of in Binary Trees
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks.
Generic array list and casting C&K s7.7
COMPUTER 2430 Object Oriented Programming and Data Structures I
Java Software Structures: John Lewis & Joseph Chase
Priority Queues.
Lecture 2: Implementing ArrayIntList reading:
null, true, and false are also reserved.
Building Java Programs
Building Java Programs
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
ArraySet Methods and ArrayIterator
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
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
EE 422C Sets.
Lecture 2: Implementing ArrayIntList reading:
UML Class Example Sales - name: string - monthlyProfit : double[] public class Sales{ private String name; private double[] monthlyProfit; public.
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
Grouped Data Arrays, and Array Lists.
COMPUTER 2430 Object Oriented Programming and Data Structures I
ArrayLists 22-Feb-19.
COMPUTER 2430 Object Oriented Programming and Data Structures I
by Sriram Pemmaraju Unoversity of Iowa
Stacks CS-240 Dick Steflik.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Module 8 – Searching & Sorting Algorithms
COMPUTER 2430 Object Oriented Programming and Data Structures I
Presentation transcript:

COMPUTER 2430 Object Oriented Programming and Data Structures I

Sets A set is a collection of elements. Not sorted. No duplicates! Can grow. Examples A = {1, 2, 3, 4} B = {2, 5} C = {1, 3, 5, 7, 9, 3} Not a Set!

Set Class in Java public class Set { private final int INIT_SIZE = 5; private final int GROWBY = 5; private Object[] list = new Object[INIT_SIZE]; private int num = 0; // Do we need the default constructor? public Set() } // Constructor with a given size public Set(int size) list = new Object[size];

Copy Constructor public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; public Set() public Set(int size) // Copy constructor public Set(Set s) this.list = new Object[s.length]; for (int i = 0; i < s.num; i ++) this.list[i] = s.list[i]; this.num = s.num; }

public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; private int find( Object obj ) private void grow() public boolean add( Object obj ) if (find(obj) != -1) return false; if (num == list.length) grow(); list[num ++] = obj; // list[num] = obj; num ++; return true; }

public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; private int find( Object obj ) private void grow() public boolean remove( Object obj ) if (find(obj) == -1) return false; list[find(obj)] = list[num - 1]; num --; return true; }

public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; private int find( Object obj ) private void grow() public boolean remove( Object obj ) int index = find(obj); if (index == -1) return false; list[index] = list[num - 1]; num --; return true; }

public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; private int find( Object obj ) private void grow() public boolean remove( Object obj ) int index = find(obj); if (index == -1) return false; num --; list[index] = list[num]; return true; }

public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; private int find( Object obj ) private void grow() public boolean remove( Object obj ) int index = find(obj); if (index == -1) return false; list[index] = list[-- num]; return true; }

public class Set { private final int INIT_SIZE = 5; private final int GROWBY = 5; private Object[] list = new Object[INIT_SIZE]; private int num = 0; // constructors private int find( Object obj ) private void grow() public boolean add( Object obj ) public boolean remove( Object obj ) public boolean isEmpty() public int getCount() public Object getItem( Object obj ) }

New Methods for Set Class contains intersection union difference isSubsetOf . . .

public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; /** Usage examples: if (setA.contains(obj)) */ public boolean contains( Object obj ) return find(obj) != -1; } private int find( Object obj )

Set Intersection A = {1, 2, 3, 4} B = {2, 5} A  B = {x: x  A and x  B} = {2} = B  A

public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; /** Usage example: setC = setA.intersection(setB) Does not change either this or the parameter s! */ public Set intersection(Set s) Set temp = new Set(num); for (int i = 0; i < num; i ++) if (s.conatins(list[i])) temp.add(list[i]) return temp; }

public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; /** Usage example: setC = setA.intersection(setB) Does not change either this or the parameter s! */ public Set intersection(Set s) Set temp = new Set(s.num); for (int i = 0; i < s.num; i ++) if (this.conatins(s.list[i])) temp.add(s.list[i]) return temp; }

public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; /** Usage example: setC = setA.intersection(setB) Does not change either this or the parameter s! */ public Set intersection(Set s) Set temp = new Set(); // will it work? for (int i = 0; i < num; i ++) if (s.conatins(list[i])) temp.add(list[i]) return temp; }

Set Union A = {1, 2, 3, 4} B = {2, 5} A  B = {x: x  A or x  B} = {1, 2, 3, 4, 5} = B  A

Does it work? Other Better Way? public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; /** Usage example: setC = setA.union(setB) Does not change either this or the parameter s! */ public Set union(Set s) Set temp = new Set(this); for (int i = 0; i < s.num; i ++) if (!this.contains(s.list[i])) temp.add(s.list[i]) return temp; } Does it work? Other Better Way?

Better and Do it this Way! public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; /** Usage example: setC = setA.union(setB) Does not change either this or the parameter s! */ public Set union(Set s) Set temp = new Set(this); for (int i = 0; i < s.num; i ++) temp.add(s.list[i]) return temp; } Does it work? Better and Do it this Way!

Set Difference A = {1, 2, 3, 4} B = {2, 5} A – B = {x: x  A but x  B} = {1, 3, 4}  B- A B – A = {5}

public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; /** Usage example: setC = setA.difference(setB) Does not change either this or the parameter s! */ public Set difference(Set s) Set temp = new Set(this); for (int i = 0; i < s.num; i ++) temp.remove(s.list[i]) return temp; }

Subset For two sets A and B and any element x, if x  A then x  B Then A is a subset of B A  B or B  A A = {1, 2, 3, 4} B = {2, 5} C = {1, 3} B  A? C  A?

public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; /** Usage example: if (setA.isSubsetOf(setB)) Does not change either this or the parameter s! */ public boolean isSubsetOf(Set s) for (int i = 0; i < num; i ++) if (! s.contains(list[i]) return false; return true; }

Prog 2 Friday: Class in Lab 206