Homework 7
|
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:
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:

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:
- placing friendly names in an ArrayList which is simple to access by index.
- placing photo filenames in a HashMap which is simple to associate the friendly name with the filename.
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"
The PictureViewer has a single constructor with parameters of picture
(the String filename to view) and delay (the time delay in seconds
during which the image is viewed). 
Album class
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
Test by:
public void createImages() {
addImage("Fish", "Fishing.jpg");
addImage("Dive", "Dive.jpg");
addImage("Rocking","Rocking.jpg");
addImage("Local Color","local.jpg");
addImage("Outback","Outback.gif");
}

| <Fish, Fishing.jpg> <Dive, Dive.jpg> <Rocking, Rocking.jpg> <Local Color, local.jpg> <Outback, Outback.gif> |

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