JMU CS349 - Developing Multimedia
Gallery Help Policies Solutions Study-Aids Syllabus Tools
Sample Questions for the Midterm Exam


  1. Answer the Java review questions.
  2. Indicate the best match for each of the following:
    _____ Clipping
    _____ Cones
    _____ CYM
    _____ Kerning
    _____ Line
    _____ Translation
    1. The Iterator Pattern
    2. The use of different bearings for different glyphs
    3. A 0-dimensional shape
    4. A 1-dimensional shape
    5. A 2-dimensional shape
    6. Limiting the portion of an object that will be rendered
    7. A kind of affine transformation
    8. An additive color model
    9. A subtractive color model
    10. Color Sensors
  3. Answer each of the following in the space provided:
    Why is it so common to use a color model in which red, green and blue are the primary colors?


    Define the term "translation" (as it relates to affine transforms).


    What is a "convex shape"?


    Illustrate a mitred join.


    Provide an example of a ligature.


  4. Explain why it is much easier to convert a color photograph to gray-scale than it is to convert a gray-scale photograph to color.
  5. I've developed an email system called JMUmble. In this system, arriving messages are handled by a PostOffice object. Depending on how the system is configured at runtime, one or more objects might need to know when a message arrives. I have currently implemented several such classes: ScreenFlasher (which makes the entire screen flash -- you always know when a message has arrived), PopularityTimer (which starts a clock that show the amount of time since the most recent message arrived), and Mumbler (which uses speech generation to read the name of the person that sent the message -- this is where the system got its name). Use the observer pattern to develop a class model of this system (in UML). You do not need to include the attributes of each class, only the operations/methods. Include comments that describe each operation/method.
  6. Modify the following implementation of the GateListenerDatabase class so that it uses type-safe collections (and is, itself, type-safe).
    import java.util.*;
    
    /**
     * A database of GateListenerRecord objects
     *
     */
    public class GateListenerDatabase
    {
        private HashMap              db;
    
    
    
        /**
         * Construct a new GateListenerDatabase
         */
        public GateListenerDatabase()
        {
    	db = new HashMap();
        }
    
    
    
        /**
         * Add a GateListenerRecord to the database
         *
         * @param glr  The record to add
         * @return     true if successful and false otherwise
         */ 
        public boolean add(GateListenerRecord glr)
        {
    	boolean            added;
    	GateListenerRecord old;
    	String             key;
    
    
    	added = false;
    
    	key = glr.getHost()+":"+glr.getPort();
    	old = (GateListenerRecord)(db.get(key));
    
    	if (old != null)
            {
    	    added = false;
            }
    	else 
            {
    	    added = true;
    	    db.put(key, glr);
    	}
    
    	return added;
        }
    
    
    
    
        /**
         * Drop a GateListenerRecord from the database
         *
         * @param glr  The record to drop
         * @return     true if successful and false otherwise
         */
        public boolean drop(GateListenerRecord glr)
        {
    	boolean   dropped;
    	Object    old;
    	String    key;
    
    
    	key = glr.getHost()+":"+glr.getPort();
    	old = db.remove(key);
    
    	dropped = true;
    	if (old == null) dropped = false;
    
    	return dropped;
        }
    
    
    
        /**
         * Returns an Iterator containing all of the GateListenerRecord 
         * Objects in the database
         *
         * @return  The Iterator of records
         */
        public Iterator getAll()
        {
            // This must be changed!
    	return null;
        }
    
    }
        
  7. Modify your answer to the question above so that it is impossible to construct more than one instance of it. Use the singleton pattern.
  8. In Java, a Map object (e.g., a HashMap) can contain only one value for each key. Hence, in order to keep a mapping of, say, students to grades, one has to be creative. One approach is to create a ManyValuedMap class that does not provide all of the functionality or a Map but delegates to an object that implements Map. For example:
    import java.util.*;
    
    
    public class ManyValuedMap<K,V>
    {
        private HashMap<K,ArrayList<V>>       delegate;
    
        public ManyValuedMap()
        {
            delegate = new HashMap<K,ArrayList<V>>();
        }
    
        // Returns all of the values associated with a key (or null)
        public ArrayList<V> get(K key)
        
        // Adds the value to the ArrayList associated with the given key
        public V put(K key, V value)
    
        // Removes the value from the ArrayList associated with the given key
        public V remove(K key)
    }
        

    Implement this class.

  9. As an alternative to the implementation of the ManyValuedMap above, one could extend the HashMap class as follows:
    import java.util.*;
    
    
    public class ManyValuedMap<K,V> extends HashMap<K,V>
    {
        // Searches through all of the values in all of the keys
        public boolean containsValue(Object value)
    
        // Returns the first value for this key
        public V get(Object key)
    
        // Returns all of the values associated with a key (or null)
        public Iterator<V> getValues(K key)
        
        // Adds the value to the ArrayList associated with the given key
        public V put(K key, V value)
    
        // Throws UnsupportedOperationException
        public void putAll(Map<? extends K,? extends V> m) 
        
        // Removes the value from the ArrayList associated with the given key
        public V remove(Object key)
    
        // Returns a Collection of all of the values associated with all keys
        public Collection<V> values()
    }
        

    Implement this class.

  10. Rather than extendingd the HashMap class as above, one could decorate a Map as follows:
    import java.util.*;
    
    
    public class ManyValuedMap<K,V> implements Map<K,V>
    {
        private  Map<K,ArrayList<V>>       decorated;
    
        public ManyValuedMap(Map<K,ArrayList<V>> decorated)
        {
            this.decorated = decorated;
        }
    
        // All of the methods required in the Map interface should
        // delegate to decorated as needed
    
    
        // Returns all of the values associated with a key (or null)
        public Iterator<V> getValues(K key)
        {
           Collection<V>    values;
           
           values = decorated.get(key);
           
           if (values != null) return values.iterator();
           else                return null;       
        }
    }
        

    Implement this class.

  11. Suppose a GUI component has the following paint method:
          public void paint(Graphics g)
          {
            g.setColor(Color.black);
            g.fillRect(50,50,400,400);
    
            g.setXORMode(Color.white);
            g.fillRect(25,75,200,100);
          }
          

    Carefully illustrate, on the grid below, what would be drawn by this paint() method if it were called.

    grid.gif
  12. Given the following definition of a RectangleCanvas:
          public class RectangleCanvas extends JPanel
          {
            private AffineTransform       at, rotate, scale;
            private Rectangle2D.Double    r;
    
    
    
            public RectangleCanvas()
            {
        	  r = new Rectangle2D.Double(50.0, 50.0, 100.0, 100.0);
    
        	  rotate = AffineTransform.getRotateInstance(0.75, 100.0, 100.0);
        	  scale  = AffineTransform.getScaleInstance(3.0, 2.0);
        	  at     = new AffineTransform();
            }
    
    
    
            public void paint(Graphics g)
            {
        	  Graphics2D     g2;
    	  Shape          s;
    
    
    
    	  g2 = (Graphics2D)g;
    	  at.setToIdentity();
    	  at.preConcatenate(rotate);
    	  at.preConcatenate(scale);
    
    	  s = at.createTransformedShape(r);
    	  g2.draw(s);
            }
          }
          
    What will be rendered the first time a RectangleCanvas is painted? What will be rendered the second time it is painted?
  13. (Note: Your answer to this question must not use the multimedia system from the textbook.) Modify the RectangleCanvas class above so that it contains a second rectangle with parameters (50, 50, 100, 100). The original rectangle must be rendered first in opaque red and the "new" rectangle must be rendered second in 50% transparent blue. The background must be opaque white.
  14. (Note: Your answer to this question must not use the multimedia system from the textbook.) Write a public paint method that is passed a Graphics object, returns nothing, and draws a red rectangle centered in a solid black background.
  15. Given the following attributes:
        private JButton                   toggle;    
        private JLabel                    light;       
        
        private static final String       OFF   = "Off";
        private static final String       ON    = "On";
        private static final String       SPACE = " ";
        

    complete the following fragment so that toggle is 100 pixels by 50 pixels with its upper-left corner at (50,100) and light is 100 pixels by 50 pixels with its upper-left corner at (250,100).

           JPanel                    contentPane;       
                  
           contentPane = (JPanel)rootPaneContainer.getContentPane();   
           contentPane.setLayout(null);
           
    
           toggle      = new JButton();
           toggle.setText(ON);       
    
    
           light       = new JLabel();
           light.setText(SPACE);       
           light.setBackground(Color.BLACK);
           light.setOpaque(true);       
        
  16. What code do you need to add (to your answer to the question above) so that the object referred to by this can respond to toggle being pressed?
  17. Complete the following method so that the light will switch between black (when off) and white (when on). Specifically, when the ``On'' button is pressed the background color of light should be set to Color.WHITE and the text of toggle should be set to OFF. On the other hand, when the ``Off'' button is pressed the background color of light should be set to Color.BLACK and the text of toggle should be set to ON.
        public void actionPerformed(ActionEvent event)
        {
           String   actionCommand;
           
           actionCommand = event.getActionCommand();
    
    
    
    
    
        }
        
  18. Using your answers to the questions above, create a class named OnOffApplication that extends JApplication.
  19. Discuss how the composite pattern might be used with the ConvolveOp class. How does this compare with requiring the user to compose kernels "by hand"? Would it make sense to use the composite pattern with the BufferedImageOp class? Why or why not?
  20. Consider the following raster representation of a black-and-white source image (in which 0 represents black and 1 represents white):
    0 0 0 0
    0 1 1 0
    0 0 0 0
    0 0 0 0

    and the following 3x3 kernel:

    0 1 0
    0 0 0
    0 0 0

    What black-and-white "destination" image will result from applying a convolution with the above kernel to the above "source" image (when no operation is applied along the edges)?

  21. (Note: Your answer to this question must not use the multimedia system from the textbook.) The JMU Office of Resource Management has taken digital photographs of the front wall of all of the classrooms on campus, like the following:
    frontwall.gif

    and would like us to develop a GUI component that renders such an image. Prof. Bernstein has written part of a FrontWall class for this purpose. You must complete the renderBackground() method.

    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    
    /**
     * A GUI component that displays the front wall of a classroom
     */
    public class FrontWall extends JPanel
    {
        protected double               imageHeight, imageWidth;
        protected double               wallHeight, wallWidth;
        protected Image                image;                   
        
    
        /**
         * Default Constructor
         */
        public FrontWall()
        {
           super();
           ImageIcon     icon;
    
           // Load the background image
           icon        = new ImageIcon("frontwall.gif");
           image       = icon.getImage();
    
           // Get the width and height of the background image
           imageHeight = (double)image.getHeight(null);
           imageWidth  = (double)image.getWidth(null);
        }
    
    
    
        /**
         * Render this component
         *
         * @param g   The rendering engine to use
         */
        public void paint(Graphics g)
        {
           super.paint(g);       
           Dimension          d;
           Graphics2D         g2;
           
           
    
           g2 = (Graphics2D)g;
    
           // Determine the size of this component
           d = getSize();
           wallWidth  = (double)d.width;
           wallHeight = (double)d.height;
           
    
           renderBackground(g2);
        }
        
    
    
        /**
         * Render the background image on this component.  When rendered,
         * the image must fill this component (that is, it must be
         * wallWidth x wallHeight pixels), even if it changes its aspect
         * ratio.
         *
         * @param g2         The rendering engine to use
         */
        protected void renderBackground(Graphics2D g2)
        {
    
    
    
            // YOUR CODE HERE!
    
    
        }
        
    
    }
        
  22. (Note: Your answer to this question must use the multimedia system from the textbook.) Create a BufferedImageBinaryOp interface that contains the requirements of dual-source/single-destination operations performed on BufferedImage objects. Should this interface extend BufferedImageOp? Why or why not?
  23. (Note: Your answer to this question must use the multimedia system from the textbook.) Create a concrete DifferenceOp class that implements the BufferedImageBinaryOp interface in the previous question. The destination image `returned' by the filter() method must contain all of the pixels that are different in the two source images. Specifically, the pixel in the result should be black if the corresponding pixels in the two source images are "the same" and should be red if they are "different". This class must have an explicit value constructor that is passed a tolerance and a Metric that will be used by the filter() method to determine if the pixels are "different" or not. It must also have a default constructor that assumes a tolerance of 0.

Copyright 2023