Homework 7
Grouping More Objects 

powered by FreeFind

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.

Assignment

  1. Download and run the HW7.exe file to extract the initial project files.
    1. Open the HW7 project. The class view should appear as at right.
    2. 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).
     

  2. 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.
      • 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.
         
    4. Add a Album constructor that initializes the ArrayList and HashMap objects.
       
    5. 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.
           
    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");
      }
      • 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.
         
    7. Add an accessor method getSize that returns the size of the album.
      • Test by:
        • Create a Album instance
        • Call the getSize method.
           
    8. 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.
           
    9. 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".
           
    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 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.
           
    11. 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.
           
    12. 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.
           
    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.
      • 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.
           
    14. 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

  1. First Page - Contains your name, date and Homework 7. Staple all pages together.
  2. Album Class, Terminal and AlbumSkin window printouts.