Arrays of Objects.

Slides:



Advertisements
Similar presentations
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
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.
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.
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Designing an ADT The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT What data does.
Chapter 10 Introduction to Arrays
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
Chapter 7 – Arrays.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Arrays Clark Savage Turner, J.D., Ph.D. Copyright © 2000 by C Scheftic. All rights reserved. These notes do rely heavily.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
Wecksten, Mattias Arrays Based on templates from Lewis & Loftus.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
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.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
Topic 3 The Stack ADT.
Chapter 3 Introduction to Collections – Stacks Modified
1 Objects for Organizing Data -- b As our programs get more sophisticated, we need assistance organizing large amounts of data. Chapter 6 focuses on: array.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
7. Arrays. Topics Declaring and Using Arrays Some Array Algorithms Arrays of Objects Variable Length Parameter Lists Two-Dimensional Arrays The ArrayList.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Chapter 7 Arrays: Part 2. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Outline Declaring and Using Arrays Arrays of Objects Variable Length Parameter.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 7 Arrays 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
1 Arrays An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 7 Arrays: Part 1 of 2. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of.
Dr. Sahar Shabanah Lecture 3: Arrays. Data Structures Data structures are organized collections of information and a set of operations used to manage.
© 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Chapter 7 Arrays 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
1 Objects for Organizing Data -- Introduction zAs our programs get more sophisticated, we need assistance organizing large amounts of data zChapter 6 focuses.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Chapter 7 Arrays…. 7-2 Arrays An array is an ordered list of values An array of size N is indexed from.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Lecture 5 array declaration and instantiation array reference
Arrays of Objects October 9, 2006 ComS 207: Programming I (in Java)
Chapter VII: Arrays.
Chapter 4 Linked Structures.
COS 312 DAY 14 Tony Gauvin.
Chapter 8 – Arrays and Array Lists
Parameter Lists & Command Line Arguments
7.6 Common Array Algorithm: Filling
Tracing Objects and Local Variables
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Chapter 7: Arrays.
Sorts.
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.
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
ITunes Lab Copyright © 2012 Pearson Education, Inc.
Fall 2018 CISC124 12/3/2018 CISC124 or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod.
Chapter 8 Classes User-Defined Classes and ADTs
Outline Creating Objects The String Class The Random and Math Classes
Arrays of Objects October 8, 2007 ComS 207: Programming I (in Java)
Arrays.
CS100A Lect. 12, 8 Oct More About Arrays
CHAPTER 3: Collections—Stacks
Presentation transcript:

Arrays of Objects

String[] words = new String[25]; Arrays of Objects The elements of an array can be object references The following declaration reserves space to store 25 references to String objects String[] words = new String[25]; It does NOT create the String objects themselves Each object stored in an array must be instantiated separately An array can contain objects, and each of those objects could have several variable and the methods that use them. When we store objects in an array, each element is a separate object. That is, an array of objects is really an array of object references.

GradeRange.java The GradeRange program creates an array of String objects called grades, which stores the possible grades and their numeric lowest value, and then prints them. For example, the lowest grade you can have to get an A is 95. Sometimes two arrays with corresponding elements like this are called parallel arrays. Parallel arrays can be tricky because they can get out of synch with each other.

Arrays of Objects Objects can have arrays as instance variables Many useful structures can be created with arrays and objects The software designer must determine carefully an organization of data and objects that makes sense for the situations

Tunes.java It is important to remember that creating an array and creating the objects that are stored in the array are two separate steps. In the Tunes program we see these two steps. The Tunes class contains a main method that creates, changes and looks at a CD collection. Each CD added to the collection has a title, artist, purchase price, and number of tracks.

CDCollecton.java The CDCollection class contains an array of CD objects representing the collection. It counts the CDs in the collection and their combined value. It also keeps track of the size of the collection array so that a larger array can be created if too many CDs are added to the collection. The collection array is instantiated in the CD Collection constructor. Every time a CD is added to the connection, using the addCD method, a new CD object is crated and a reference to it is stored in the collection array. Each time a CD is added to the collection, we check to see whether we have filled up the collection array. If we didn’t check this, we would get an exception when we tried to store a new CD object at an invalid index. If the array is full, the private increaseSize method is invoked, which first creates an array that is twice the as big as the current collection array. Each CD in the collection is then copied into the new array. Finally, the collection reference is set to the larger array. This means we never run out of room in our CD collection. The user of the CDCollection object, the main method, never has to worry about running out of space because it is all handle internally.

CD.java The toString method of the CDCollection class returns a report summarizing the collection. The report is created, in part, using calls to the toString method of each CD object stored in the collection. The CD class contains the CD constructor as well as the toString method.