Homework 7
Grouping More Objects

Modified

Overview - The purpose of the homework is to practice grouping objects using ArrayList and HashMap. An Album class that groups and displays digital images to create various slide shows will be implemented. The Album will display selected images using a provided PictureViewer class. A simple, graphical user interface to the Album is provided by the AlbumSkin class.

Problem description - An album groups images. The images consist of two parts:

  1. name - The name of the image, a String.
  2. filename - The name of the image file, a String.

For example, the name "Fish" associates an image stored as filename "Fishing.jpg".

For this assignment, an Album is a grouping of an arbitrary number of images and the methods to:

  1. create a group of images,
  2. display the group, either sequentially or in random order,
  3. and to print the name and filename of the images in the group.

The album will use an ArrayList to group image names; a HashMap will group image filenames as in the figure at right.

 

Example using the ArrayList to locate the HashMap filename

Consider an ArrayList named aL and a HashMap hM with the following values:

ArrayList  aL
 
0 Title
1 Outback
2 Fish
        HashMap  hM
 
Fish Fishing.jpg
Title Title.jpg
Outback Outback.jpg

The values aL are the keys of hM.

aL.get(1) is "Outback"

hM.get("Outback") is "Outback.jpg"

hM.get( aL.get( 1 ) ) is "Outback.jpg"

Accessing all the values of an ArrayList is simple because we know the indices must be 0, 1, 2, ....

To print aL values "Title", "Outback" and "Fish":

for ( int i=0; i< aL.size(); i++)
     System.out.println( aL.get( i ) ); 

Accessing all the values of a HashMap is more difficult because the keys could be any String: "Title", "Ray", "*&%".

But we've defined aL to hold the keys of hM, then the HashMap values can be accessed by hM.get( aL.get( i ) ).

To print all hM values "Title.jpg, "Outback.jpg" and "Fishing.jpg":

for ( int i=0; i< aL.size(); i++)
     System.out.println( hM.get( aL.get( i ) ) ); 

 

We will organize the photo album by:

To display a photo number 2, the friendly name at index 2 "Outback" is the HashMap key associated with the filename "Outback.jpg".

aL.get(2) is "Outback"

hM.get("Outback") is "Outback.jpg"

hM.get( aL.get( 2 ) ) is "Outback.jpg"

 

Getting started

Assignment

Album class
  1. Create a new class named Album.
     
  2. To use ArrayList, Random, HashMap and Iterator classes, add the first lines of Album exactly as:
    import java.util.HashMap;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.Random;
  3. Add fields.
  4. Add a Album constructor that initializes the ArrayList and HashMap fields.
     
  5. Add a mutator method addImage that adds an image. The addImage method has two String parameters: the name and filename of the image.
    1. The name is stored in the ArrayList.
    2. The name is the HashMap key and filename is the corresponding HashMap value.

    Test by:

  6. To speed up the testing process we'll automate of adding images. Add the following mutator method createImages that adds images to the album; you can copy and paste.
     public void createImages() {
        addImage("Fish", "Fishing.jpg");
        addImage("Dive", "Dive.jpg");
        addImage("Rocking","Rocking.jpg");
        addImage("Local Color","local.jpg");
        addImage("Outback","Outback.gif");
    }
  7. Add an accessor method getSize that returns the size of the album, the size of the ArrayList.
  8. Add an accessor method getName to return the name at the ArrayList index specified in the method parameter. The method has one int  parameter.
  9. Add an accessor method getFileName to return the filename corresponding a name in the HashMap specified in the method parameter. The method has one String  parameter.
  10. Add the method view to view a single image. The method has one String parameter, the name of the image (not the filename) to view. Recall that the name is a HashMap key identifying the filename to view.
  11. Add a printAlbum method to print the name and filename of all images in the album to the terminal window.
  12. Add a viewAlbum method to view all images in the album.
  13. Add a randomViewAlbum method to randomly view a given number of images in the album. The method has one int parameter, the number of images to randomly view.
  14. Add a basic skin graphical interface for your Album.
  15. Add some of your photos

Turn in

C201 OnCourse Dropbox - Create a HW7 folder in the C201 OnCourse DropBox. Copy the following:

    1. Album JAVA file.
    2. A Word file with Screen shot of AlbumSkin with one of your images. Use Alt Prnt Scrn to capture the window, then paste into Word.