Lecture 6 b Last time: array declaration and instantiationarray declaration and instantiation array referencearray reference bounds checkingbounds checking.

Slides:



Advertisements
Similar presentations
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Advertisements

Outline Polymorphic References Polymorphism via Inheritance Polymorphism via Interfaces Sorting Searching Event Processing Revisited File Choosers and.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
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.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
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.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Reference … and Misc Other Topics Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Chapter 9: Arrays and Strings
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Arrays in Java Selim Aksoy Bilkent University Department of Computer Engineering
Static Class Members Wrapper Classes Autoboxing Unboxing.
C++ for Engineers and Scientists Third Edition
Chapter 6: Arrays Java Software Solutions Third Edition
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
INF 523Q Chapter 5: Enhancing Classes. 2 b We can now explore various aspects of classes and objects in more detail b Chapter 5 focuses on: object references.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java Software.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition.
© 2004 Pearson Addison-Wesley. All rights reserved October 13, D Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor:
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
Programming in Java (COP 2250) Lecture Chengyong Yang Fall, 2005.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
© 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
1 Enhancing Classes  Now we can explore various aspects of classes and objects in more detail  Chapter 5 focuses on: object references and aliases passing.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
1 Sorting b Sorting is the process of arranging a list of items into a particular order b There must be some value on which the order is based b There.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 Objects for Organizing Data -- Introduction zAs our programs get more sophisticated, we need assistance organizing large amounts of data zChapter 6 focuses.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition.
Arrays Chapter 7.
Chapter 6: Arrays Java Software Solutions
Chapter 5: Enhancing Classes
More Object Oriented Programming
Sorts.
Data Structures and Algorithms
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
Chapter 5: Enhancing Classes
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Java Classes Aliases & Null & This
Object Oriented Programming Review
Outline Creating Objects The String Class The Random and Math Classes
Sorting and Searching -- Introduction
Arrays.
Presentation transcript:

Lecture 6 b Last time: array declaration and instantiationarray declaration and instantiation array referencearray reference bounds checkingbounds checking initialization at declarationinitialization at declaration arrays of objectsarrays of objects b Today: object references revisited - section 5.1object references revisited - section 5.1 arrays as parameters - section 6.1arrays as parameters - section 6.1 multidimensional arrays - section 6.4multidimensional arrays - section 6.4 sorting - section 6.3sorting - section 6.3 Sections 6.1, 6.2

2 References b Recall from Chapter 2 that an object reference holds the memory address of an object ChessPiece bishop1 = new ChessPiece(); bishop1

3 Assignment Revisited b The act of assignment takes a copy of a value and stores it in a variable b For primitive types: num2 = num1; Before num1 5 num2 12 After num1 5 num2 5

4 Reference Assignment b For object references, assignment copies the memory location: bishop2 = bishop1; Before bishop1bishop2 After bishop1bishop2 bishop1 and bishop2 are now aliases

5 Garbage Collection b When an object no longer has any valid references to it, it can no longer be accessed by the program b It is useless, and therefore called garbage b Java performs automatic garbage collection periodically, returning an object's memory to the system for future use b In other languages, the programmer has the responsibility for performing garbage collection

Passing Objects to Methods b Parameters in a Java method are passed by value b This means that a copy of the actual parameter (the value passed in) is stored into the formal parameter (in the method header) b Passing parameters is essentially an assignment b When an object is passed to a method, the actual parameter and the formal parameter become aliases of each other

7 Arrays as Parameters b An entire array can be passed to a method as a parameter b Like any other object, the reference to the array is passed, making the formal and actual parameters aliases of each other b Changing an array element in the method changes the original b An array element can be passed to a method as well, and will follow the parameter passing rules of that element's type

Passing Objects to Methods b What you do to a parameter inside a method may or may not have a permanent effect (outside the method) b See ParameterPassing.java (page 226) ParameterPassing.java b See ParameterTester.java (page 228) ParameterTester.java b See Num.java (page 230) Num.java b Note the difference between changing the reference and changing the object that the reference points to

9 Two-Dimensional Arrays b A one-dimensional array stores a simple list of values b A two-dimensional array can be thought of as a table of values, with rows and columns b A two-dimensional array element is referenced using two index values b To be precise, a two-dimensional array in Java is an array of arrays  See TwoDArray.java (page 299) TwoDArray.java

10 Multidimensional Arrays b An array can have as many dimensions as needed, creating a multidimensional array b Each dimension subdivides the previous one into the specified number of elements  Each array dimension has its own length constant b Because each dimension is an array of array references, the arrays within one dimension could be of different lengths

11 The static Modifier b In Chapter 2 we discussed static methods (also called class methods) that can be invoked through the class name rather than through a particular object  For example, the methods of the Math class are static  To make a method static, we apply the static modifier to the method definition  The static modifier can be applied to variables as well b It associates a variable or method with the class rather than an object

12 Static Methods public static int triple (int num) { int result; result = num * 3; return result; } class Helper Because it is static, the method could be invoked as: value = Helper.triple (5);

13 Static Methods b The order of the modifiers can be interchanged, but by convention visibility modifiers come first  Recall that the main method is static; it is invoked by the system without creating an object b Static methods cannot reference instance variables, because instance variables don't exist until an object exists b However, they can reference static variables or local variables

14 Static Variables b Static variables are sometimes called class variables b Normally, each object has its own data space b If a variable is declared as static, only one copy of the variable exists private static float price; b Memory space for a static variable is created as soon as the class in which it is declared is loaded

Static Variables b All objects created from the class share access to the static variable b Changing the value of a static variable in one object changes it for all others b Static methods and variables often work together b See CountInstances.java (page 233) CountInstances.java b See MyClass.java (page 234) MyClass.java

16 Sorting b Sorting is the process of arranging a list of items into a particular order b There must be some value on which the order is based b There are many algorithms for sorting a list of items b These algorithms vary in efficiency b We will examine two specific algorithms: Selection SortSelection Sort Insertion SortInsertion Sort

17 Selection Sort b The approach of Selection Sort: select one value and put it in its final place in the sorted listselect one value and put it in its final place in the sorted list repeat for all other valuesrepeat for all other values b In more detail: find the smallest value in the listfind the smallest value in the list switch it with the value in the first positionswitch it with the value in the first position find the next smallest value in the listfind the next smallest value in the list switch it with the value in the second positionswitch it with the value in the second position repeat until all values are placedrepeat until all values are placed

18 Selection Sort b An example: original: original: smallest is 1: smallest is 1: smallest is 2: smallest is 2: smallest is 3: smallest is 3: smallest is 6: smallest is 6:  See SortGrades.java (page 289) SortGrades.java  See Sorts.java (page 290) -- the selectionSort method Sorts.java Sorts.java

19 Insertion Sort b The approach of Insertion Sort: Pick any item and insert it into its proper place in a sorted sublistPick any item and insert it into its proper place in a sorted sublist repeat until all items have been insertedrepeat until all items have been inserted b In more detail: consider the first item to be a sorted sublist (of one item)consider the first item to be a sorted sublist (of one item) insert the second item into the sorted sublist, shifting items as necessary to make room to insert the new additioninsert the second item into the sorted sublist, shifting items as necessary to make room to insert the new addition insert the third item into the sorted sublist (of two items), shifting as necessaryinsert the third item into the sorted sublist (of two items), shifting as necessary repeat until all values are inserted into their proper positionrepeat until all values are inserted into their proper position

20 Insertion Sort b An example: original: original: insert 9: insert 9: insert 6: insert 6: insert 1: insert 1: insert 2: insert 2:  See Sorts.java (page 290) -- the insertionSort method Sorts.java