Still Aggregation and Composition

Slides:



Advertisements
Similar presentations
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Advertisements

Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
A Singleton Puzzle: What is Printed? 1 public class Elvis { public static final Elvis INSTANCE = new Elvis(); private final int beltSize; private static.
Composition (Part 2) 1. Class Invariants  class invariant  some property of the state of the object that is established by a constructor and maintained.
ArrayList, Multidimensional Arrays
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Static Attributes and Inheritance  static attributes behave the same as non-static attributes in inheritance  public and protected static attributes.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
COMP 121 Week 8: Generic Collections. Objectives To understand type variables and how they are used in generic programming To be able to implement and.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
CSE 1201 Object Oriented Programming ArrayList 1.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
The ArrayList Data Structure Standard Arrays at High Speed!
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays Chapter 7.
CMSC 202 ArrayList Aug 9, 2007.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Java Arrays and ArrayLists COMP T1 #5
Sixth Lecture ArrayList Abstract Class and Interface
Aggregation and Composition
Chapter 9 More on Objects and Classes
Objects First with Java CITS1001
Copying ArrayLists Copied Section of
Programming in Java Lecture 11: ArrayList
The ArrayList Class An ArrayList is a complex data structure that allows you to add or remove objects from a list and it changes size automatically. The.
Can store many of the same kind of data together
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Arrays versus ArrayList
CMSC 202 ArrayList Aug 9, 2007.
ArrayList Collections.
תירגול 9 אובייקטים.
Data Structures and Database Applications Arrays And Lists
Can store many of the same kind of data together
Dynamic Data Structures and Generics
Object Oriented Programming in java
CS2011 Introduction to Programming I Objects and Classes
CMSC 202 ArrayList Aug 9, 2007.
Grouped Data Arrays, and Array Lists.
slides created by Ethan Apter
ArrayLists 22-Feb-19.
Can store many of the same kind of data together
Based on slides by Alyssa Harding & Marty Stepp
Java SE 7 One and Multi Dimensional Arrays Module 6
CSE 143 Lecture 2 ArrayIntList, binary search
Review: libraries and packages
slides created by Ethan Apter and Marty Stepp
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
Java Coding 6 David Davenport Computer Eng. Dept.,
Arrays.
Presentation transcript:

Still Aggregation and Composition Collections as Fields Still Aggregation and Composition

Motivation often you will want to implement a class that has-a collection as a field a university has-a collection of faculties and each faculty has-a collection of schools and departments a receipt has-a collection of items a contact list has-a collection of contacts from the notes, a student has-a collection of GPAs and has- a collection of courses a polygonal model has-a collection of triangles* *polygons, actually, but triangles are easier to work with

What Does a Collection Hold? a collection holds references to instances it does not hold the instances 100 client invocation dates 200a d1 500a d2 600a d3 700a ... 200 ArrayList object ArrayList<Date> dates = new ArrayList<Date>(); Date d1 = new Date(); Date d2 = new Date(); Date d3 = new Date(); dates.add(d1); dates.add(d2); dates.add(d3);

Test Your Knowledge What does the following print? ArrayList<Point> pts = new ArrayList<Point>(); Point p = new Point(0., 0., 0.); pts.add(p); p.setX( 10.0 ); System.out.println(p); System.out.println(pts.get(0)); Is an ArrayList<X> an aggregation of X or a composition of X?

Student Class (from notes) a Student has-a string id a Student has-a collection of yearly GPAs a Student has-a collection of courses 1 1 List<Double> Student Set<Course> gpas courses 4 1 id * Double String Course

PolygonalModel Class a polygonal model has-a List of Triangles aggregation 1 PolygonalModel List<Triangle> tri * Triangle

PolygonalModel class PolygonalModel { private List<Triangle> tri; public PolygonalModel() { this.tri = new ArrayList<Triangle>(); }

PolygonalModel public void clear() { // removes all Triangles this.tri.clear(); } public int size() { // returns the number of Triangles return this.tri.size();

Collections as Fields when using a collection as an attribute of a class X you need to decide on ownership issues does X own or share its collection? if X owns the collection, does X own the objects held in the collection?

X Shares its Collection with other Xs if X shares its collection with other X instances, then the copy constructor does not need to create a new collection the copy constructor can simply assign its collection [notes 5.3.3] refer to this as aliasing

PolygonalModel Copy Constructor 1 public PolygonalModel(PolygonalModel other) { // implements aliasing (sharing) with other // PolygonalModel instances this.tri = other.tri; } public List<Triangle> getTriangles() { return this.tri; alias: no new List created alias: no new List created

PolygonalModel p2 = new PolygonalModel(p1); 100 client invocation p1 200a p2 500a ... 200 PolygonalModel object tri 700a 500 700 ArrayList<Triangle> object 1000a 1100a ... 1000 Triangle object 1100

Test Your Knowledge Suppose that the PolygonalModel copy constructor makes an alias of the list of triangles. Suppose you have a PolygonalModel p1 that has 100 Triangles. What does the following code print? PolygonalModel p2 = new PolygonalModel(p1); p2.clear(); System.out.println( p2.size() ); System.out.println( p1.size() );

X Owns its Collection: Shallow Copy if X owns its collection but not the objects in the collection then the copy constructor can perform a shallow copy of the collection a shallow copy of a collection means X creates a new collection the references in the collection are aliases for references in the other collection

X Owns its Collection: Shallow Copy the hard way to perform a shallow copy of a list named dates shallow copy: new List created but elements are all aliases ArrayList<Date> sCopy = new ArrayList<Date>(); for(Date d : dates) { sCopy.add(d); } add adds an alias of d to sCopy

X Owns its Collection: Shallow Copy the easy way to perform a shallow copy of a list named dates ArrayList<Date> sCopy = new ArrayList<Date>(dates);

PolygonalModel Copy Constructor 2 public PolygonalModel(PolygonalModel other) { // implements shallow copying this.tri = new ArrayList<Triangle>(other.tri); } shallow copy: new List created, but no new Triangle objects created

PolygonalModel p2 = new PolygonalModel(p1); 100 client invocation p1 200a p2 500a ... 200 PolygonalModel object tri 700a 500 800a 700 ArrayList<Triangle> object 1000a 1100a ... 800 1000 Triangle object 1100

Test Your Knowledge Suppose that the PolygonalModel copy constructor makes a shallow copy of the list of triangles. Suppose you have a PolygonalModel p1 that has 100 Triangles. What does the following code print? PolygonalModel p2 = new PolygonalModel(p1); p2.clear(); System.out.println( p2.size() ); System.out.println( p1.size() );

Test Your Knowledge Suppose that the PolygonalModel copy constructor makes a shallow copy of the list of triangles. Suppose you have a PolygonalModel p1 that has 100 Triangles. What does the following code print? PolygonalModel p2 = new PolygonalModel(p1); Triangle t1 = p1.getTriangles().get(0); Triangle t2 = p2.getTriangles().get(0); System.out.println(t1 == t2);

X Owns its Collection: Deep Copy if X owns its collection and the objects in the collection then the copy constructor must perform a deep copy of the collection a deep copy of a collection means X creates a new collection the references in the collection are references to new objects (that are copies of the objects in other collection)

X Owns its Collection: Deep Copy how to perform a deep copy of a list named dates ArrayList<Date> dCopy = new ArrayList<Date>(); for(Date d : dates) { dCopy.add(new Date(d.getTime()); } deep copy: new List created and new elements created new Date created that is a copy of d

PolygonalModel Copy Constructor 3 public PolygonalModel(PolygonalModel other) { // implements deep copying this.tri = new ArrayList<Triangle>(); for (Triangle t : other.getTriangles()) { this.tri.add(new Triangle(t)); } deep copy: new List created, and new Triangle objects created new Triangle created that is a copy of t

PolygonalModel p2 = new PolygonalModel(p1); 100 client invocation p1 200a p2 500a ... 200 PolygonalModel object tri 700a 500 800a 700 ArrayList<Triangle> object 1000a 1100a ... 800 2000a 2100a 1000 Triangle object 1100 continued on next slide

2000 Triangle object ... 2100

Test Your Knowledge Suppose that the PolygonalModel copy constructor makes a deep copy of the list of triangles. Suppose you have a PolygonalModel p1 that has 100 Triangles. What does the following code print? PolygonalModel p2 = new PolygonalModel(p1); p2.clear(); System.out.println( p2.size() ); System.out.println( p1.size() );

Test Your Knowledge Suppose that the PolygonalModel copy constructor makes a deep copy of the list of triangles. Suppose you have a PolygonalModel p1 that has 100 Triangles. What does the following code print? PolygonalModel p2 = new PolygonalModel(p1); Triangle t1 = p1.getTriangles().get(0); Triangle t2 = p2.getTriangles().get(0); System.out.println(t1 == t2); System.out.println(t1.equals(t2));

Arrays

Arrays in Java an array is a container object that holds a fixed number of values of a single type the length of an array is established when the array is created https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Arrays to declare an array you use the element type followed by an empty pair of square brackets double[] collection; // collection is an array of double values collection = new double[10]; // collection is an array of 10 double values https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Arrays to create an array you use the new operator followed by the element type followed by the length of the array in square brackets double[] collection; // collection is an array of double values collection = new double[10]; // collection is an array of 10 double values https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Arrays the number of elements in the array is stored in the public field named length double[] collection; // collection is an array of double values collection = new double[10]; // collection is an array of 10 double values int n = collection.length; // the public field length holds the number of elements https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Arrays the values in an array are called elements the elements can be accessed using a zero-based index (similar to lists and strings) https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Arrays the elements can be accessed using a zero-based index (similar to lists and strings) collection[0] = 100.0; collection[1] = 100.0; collection[2] = 100.0; collection[3] = 100.0; collection[4] = 100.0; collection[5] = 100.0; collection[6] = 100.0; collection[7] = 100.0; collection[8] = 100.0; collection[9] = 100.0; // set all elements to equal 100.0 collection[10] = 100.0; // ArrayIndexOutOfBoundsException https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Array vs ArrayList under most circumstances, you should use ArrayList instead of an array however, arrays are a part of the Java language and it is important that you understand how to use them advantages of ArrayList grows in size automatically when needed provides many useful methods