{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture4

lecture4 - CS116 OBJECTORIENTED PROGRAMMINGII LECTURE4...

This preview shows pages 1–11. Sign up to view the full content.

CS 116 OBJECT ORIENTED  PROGRAMMING II LECTURE 4 GEORGE KOUTSOGIANNAKIS Copyright:  2010Illinois Institute of Technology/George Koutsogiannakis 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
OOP Review In previous lecture we discussed: Declaring and Instantiating Arrays Entering Array Elements Accessing Array Elements Aggregate Array Operations Arrays of Objects. Creating Packages 2
Arrays – Single Dimension Topics Copying Array Values. Enumerations. Searching  Arrays. Selection Sort . 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Copying Array Values Example This code copies the values of all elements in an array named  cellBills  to an array named  billsBackup ,   both of   which have previously been  instantiated with the same  length : for ( int i = 0; i < cellBills.length; i++ ) { billsBackup[i] = cellBills[i]; } The effect of this  for  loop is shown on the next slide. See Example 8.7 CopyingArrayElements.java 4
Copying Array Values  (cont’d)   A separate copy of the array has been created. 5

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Changing an Array's Size An array's  length  instance variable is constant that is, arrays are assigned a constant size when they are  instantiated  To expand an array while maintaining its original values: 1. Instantiate an array with the new size and a  temporary name 2. Copy the original elements to the new array 3. Point the original array reference to the new array 4. Assign a  null  value to the temporary array reference 6
Comparing Arrays for Equality To compare whether the elements of two arrays are equal: 1. Determine if both arrays have the same length 2. Compare each element in the first array with the  corresponding element in the second array To do this, we'll use a flag variable and a  for  loop. 7

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Comparing  cellBills1  to  cellBills2 boolean isEqual = true; // flag variable if ( cellBills1.length != cellBills2.length ) isEqual = false; // sizes are different else { for ( int i = 0; i < cellBills1.length && isEqual; i++ ) { if ( Math.abs( cellBills1[i] - cellBills2[i] ) > 0.001 ) isEqual = false; // elements are not equal } } See Example 8.8 ComparingArrays.java 8
enum Types Enumerated Types are classes with  special properties. They have a finite number of instances (as  for example Days in previous slide). 9

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Useful  enum  Methods  Return value Method name and argument list int compareTo( Enum eObj )      compares two  enum  objects and returns a  negative number if  this  object is less than the  argument, a positive number if  this  object is  greater than the argument, and 0 if the two  objects are equal.  int ordinal( ) returns the numeric value of the  enum  object.  By default, the value of the first object in the
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 31

lecture4 - CS116 OBJECTORIENTED PROGRAMMINGII LECTURE4...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online