RU beehive logo ITEC dept promo banner
ITEC 120
2010fall
ibarland

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs

lect12c
arrays of objects

 - discuss hw09-soln.java


   [Files before: lect12c-playlist/;]
 - An array of Songs: Show the pictures involved with:
new Song[10]
Song[] album;

album = new Song[10];
album[0] = new Song(...);
album[1] = new Song(...);
...

// Write a loop to find the total play-time of all Songs:
double timeSoFar = 0.0;
for (int i=0;  i < album.length;  ++i) {
  timeSoFar += album[i].length;
  }

// N.B. Don't confuse album.length with album[i].length -- 
//    entirely different beasts!  
// (And, it was dumb luck those fields have the same name.
//   Still, a common confusion is writing things like
//   `album.isCopyrighted` or `i.artist`.)
- Aliasing: Consider a variable songOfTheYear. What happens when we `songOfTheYear.setIsCopyrighted(false)` -- note that the array doesn't change, but the things it refers to *do* change. "When Barack Obama dyes his hair blue, so does The President of the U.S." **** Reached here, Nov.19 ****
- Create a class which *contains* an array of Song, as well as has its own name, and a creator field. Task: write *Playlist* methods for total running time; write a method containsCopyrighted. - Another possible example: A Brood has a hideout(String), a name, and an array of all its Vampire members. We could have not only a variable "vampireOfTheMonth", but even an array of past winners. - swapping two elements of an array. - We can write a method swapArrayElements( double[], int, int ) but not swapLocalVariables(int,int). Why not? The picture explains all! Assigning through a reference behaves *very differently* from assigning to a local variable. (Some languages have two different names for the assignment operator "=" -- one which can be used for local variables, and one which can be used for a [reference-to-an-]objects's fields.

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs


©2010, Ian Barland, Radford University
Last modified 2010.Nov.19 (Fri)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme