lecture4 - CS116 OBJECTORIENTED PROGRAMMINGII LECTURE4...

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

View Full Document Right Arrow Icon
CS 116 OBJECT ORIENTED  PROGRAMMING II LECTURE 4 GEORGE KOUTSOGIANNAKIS Copyright:  2010Illinois Institute of Technology/George Koutsogiannakis 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
Arrays – Single Dimension Topics Copying Array Values. Enumerations. Searching  Arrays. Selection Sort . 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
Copying Array Values  (cont’d)   A separate copy of the array has been created. 5
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 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
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 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
Background image of page 9

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

View Full DocumentRight Arrow Icon
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 
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/27/2010 for the course CS 116 taught by Professor Koutsogiannakis during the Summer '08 term at Illinois Tech.

Page1 / 31

lecture4 - CS116 OBJECTORIENTED PROGRAMMINGII LECTURE4...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online