Homework 7
Grouping More Objects
|
|
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:
- name - The name of the image, a String.
- 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:
- create a group of images,
- display the group, either sequentially or in random order,
- 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.
- An ArrayList is useful to group objects by associating an
integer index with an object. For example, the figure at right
associates an integer index 0, 1 or 2 with name of an image in the
album.
- A HashMap is useful to group objects by associating an arbitrary
key with an object . For example, the figure at right associates the
name of an image in the album with a filename.
Assignment
- Download and run the HW7.exe file to extract the
initial project files.
- Open the HW7 project. The class view should appear as at right.
- Test by creating a PictureViewer object. Use picture parameter "Fishing.jpg"
and delay parameter of 6.
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
- Create a new class named Album.
- 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; |
- Add fields.
- Add one ArrayList field. This will hold the names of the
images as Strings.
- Add one HashMap field. The keys are the image names and
the values the image filenames.
- Add a Album constructor that initializes the ArrayList
and HashMap objects.
- Add a mutator method addImage that adds an image.
The addImages method has two parameters: the name and filename
of the image. The name is stored in the ArrayList. The name
is the HashMap key and filename is the corresponding
HashMap value.
- Test by:
- Create an Album object.
- Add two images ("fishing.jpg" and "outback.gif") using addImage.
- 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");
} |
- Test by creating an Album instance and calling the createImages
method.
- Add a call to createImages() at the end of the Album constructor
to automatically initialize an Album for testing.
- Add an accessor method getSize that returns the size of the
album.
- Test by:
- Create a Album instance
- Call the getSize method.
- Add an accessor method getName to return the name at
the index specified in the method parameter. The method has one int
parameter.
- Note that a name is stored in an ArrayList must be cast
as a String object (see notes and page 96).
- Test by:
- Create a Album instance
- Call the getName(2) method.
- Add an accessor method getFileName to return the filename
corresponding a name specified in the method parameter. The method has one
String
parameter.
- Note that a filename is stored in a HashMap must be cast
as a String object (see notes and page 96).
- Test by:
- Create a Album instance
- Call the getFileName("Fish") method. It should return
the filename "Fishing.jpg".
- 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 key identifying the filename
to view.
- Use getFileName() to get filename; getFileName("Fish")
returns "Fishing.jpg" as the filename.
- Use the PictureViewer constructor to view the image
filename. The following views "Fishing.jpg" for 4 seconds.
- new PictureViewer( "Fishing.jpg", 4);
- Test by:
- Create an Album instance
- Call the view("Fish") method to view the "Fishing.jpg"
file.
- Add a printAlbum method to print the name and
filename of all images in the album.
- Recall that the name is in the ArrayList.
- Use getFileName() to get filename; getFileName("Fish")
returns "Fishing.jpg" as the filename.
- Test by:
- Create an Album instance
- Call the printAlbum method.
- Add a viewAlbum method to view all images in the album.
- Recall that the name is in the ArrayList.
- Use view() method to view an image.
- Test by:
- Create an Album instance
- Call the viewAlbum method.
- 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.

- Use the Random class method nextInt() to generate
a random integer; you will need a random number between 0 and the
size of the album, remember getSize().
- Recall that the name is in the ArrayList and filename
is in the HashMap; use view(name) to view an image.
- Test by:
- Create an Album instance
- Call the randomViewAlbum(10) method to randomly view
10 images.
- Add a basic skin graphical interface for your Album.
- Test by:
- Create a AlbumSkin object.
- Click on an image name in the AlbumSkin window to view
the image.
- Capture the AlbumSkin window (Alt Prnt Scrn) and paste into Word
for printing.
Turn in
- First Page - Contains your name, date and
Homework 7. Staple all pages together.
- Album Class, Terminal and AlbumSkin window printouts.