Lab #5: Arrays
by Bill

Welcome to this week's lab excitement.

Arrays, and How to Differentiate Them from Tree Swallows

Arrays provide a useful purpose: an indexed filing system for data. What kind of data? Any kind. you can make an array of integers, floats, Strings, your classes. Anything. And that's what makes them so handy—one way to index any kind of data.

Here is an example of an actual, living, breathing array. It's an array of integers that stores (in this case) my lowest test scores in the Math/CompSci Department:

int Test_Scores [] = new int [5];
Test_Scores [1] = 0;
Test_Scores [2] = 6;
Test_Scores [3] = 9;
Test_Scores [4] = 17;
Test_Scores [5] = -4; //Take-home test that I turned in late.

Here's another example: an array of String objects entitled, "Five MTV Hosts Who Annoy the Crap Out of Me."

String MTV_Host [] = new String[5];
MTV_Host [1]="Carson Daly";
MTV_Host [2]="Carson Daly";
MTV_Host [3]="Carson Daly";
MTV_Host [4]="Carson Daly";
MTV_Host [5]="Carson Daly";

And I'll see you at next year's MTV music awards. So where does that bring us? (NOTE: The above example is just one way to use arrays. We'll use another in this lab.

The Lab and All It's Glory

We're going to write a program from scratch, today. We don't need nobody else's pithy, sundry, tawdry or obfuscated code. Nope—we're rollin' our own.

Well, you are, at any rate.

Specifically, the assignment is to write a happy little program to handle radio station programing. The program will allow Station Managers to keep track of which songs are on the playlist, how many times they've been paid to play the song that week, and how many times they will actually have it played.

Most top-40 radio stations actually have less than 40 songs on their playlist. This one can have as many as you want—but have at least 5.

A menu will should presented to the user, which will allow them to select any song on the playlist to change. When the user selects one, they can then change any of the data for that particular song. There should also be an option to quit.

There's two basic steps in this dilly, which correspond to the two classes that will be written.

Part I: Create The Song Class

So, we'll need a Song class to hold all the information we'll need to keep track of for a song. Like al classes, this one will also require the methods that work with that data. So, what should each Song class store?

Data Type Initial value
Song name String "none"
Brand name (also known as the "artist") String "none"
Number of slot paid for int 0
Number of slots that will play int 0

No problems so far. But now we need those pesky methods do actually use that data. Specifically, we'll need:

A constructor to initialize all the values to default values.

A method to change their values.

Part II: The Program Class

The Song class will live inside a Program class which has the all-important Main method. This is, of course, how we've done all our programs thus far.

Data Class    
  Program Class  
     

Call the program class anything you want (something like "Playlist"), but please leave my name out of it. You can implement it as a separate file, or have both classes in the same file.

Your Main should do something akin to:

  1. Ask them which song they want to change (you could also tell them how many are in the playlist).
  2. Call the Change method for the song object corresponding to the one selected by the user.
  3. Lather, rinse, repeat.

Sample program output (red indicates user input):

Welcome to the playlist editor. Remember to play all songs requested by Death Row records.
Select a song (by number) to edit. Type zero to quit.

Song: 5

Brand Name: Britney Spears
Song Name: Bombastic Love
Slots paid for: 20
Slots allocated: 10

Song updated.

Song: 4

Brand Name: Britney Spears
Song Name: I'm Not A Girl, Not Yet A Woman
Slots paid for: 20
Slots allocated: 10

Song updated.

Song: 0

Session complete.

And that's pretty much it. (And yes, those really are track names from her new album.)

Conclusion: The Part at the End

Well, that's about it. Hopefully, that all went well, and we're all good. Now, we can all go back to our rooms and drink.

Be sure to tune in for next week's lab, "Class Hierarchies Even Marxist Could Love."