CS239 - Advanced Programming James Madison University
In-Lab Programming Portion of the Final Examination Spring 2011

This work complies with the JMU Honor Code.

Name: _______________________ Signature: _______________________ Section: _______

Instructions: Answer both of the following questions and submit them using SUBMIT. Be careful to SUBMIT the assignment for your section.

You must work entirely on your own, but you may use the documentation for the Java API and you may compile and execute your solutions. You may receive partial credit and, as always, you may not receive full credit even if your answers pass the SUBMIT tests.

  1. (20 points) Harbern Industries (HI) has developed a new imaging technology to locate rare earth minerals. Using this technology, rare earth minerals show up in images as pixels that are very bright (i.e., have a large sum of the red, green, and blue components).

    You have been "volunteered" to complete the software for this sensor. With that in mind, complete the findMax() method in the following ColorSensor class. Your solution should not be recursive.

    import java.awt.*;
    import java.io.*;

    public class ColorSensor
    {
    /**
    * The entry-point of the application
    *
    * @param args The command line arguments (left, top, width, height, file)
    */
    public static void main(String[] args)
    {
    Color[][] image;
    int height, left, max, top, value, width;
    String name;

    // Process the command line arguments
    left = Integer.parseInt(args[0]);
    top = Integer.parseInt(args[1]);
    width = Integer.parseInt(args[2]);
    height = Integer.parseInt(args[3]);
    name = args[4];

    // Load the image
    image = ImageStorage.loadImage(name);

    // Perform the calculations
    max = findMax(image, left, top, width, height);

    // Print the result
    System.out.println("Maximum: " + max);
    }



    /**
    * Finds the pixel in the given rectangle that
    * has the maximum sum of the
    * R, G, and B components and returns that sum.
    * * @param image The image to use * @param left The coordinate of the left side of the rectangle * @param top The coordinate of the top side of the rectangle * @param width The width of the rectangle * @param height The height of the rectangle * @return The maximum sum in the rectangle */ private static int findMax(Color[][] image, int left, int top, int width, int height) { } }

  2. (15 points) Harbern Industries (HI) has created a retirement plan for its employees. When an employee creates a retirement account, she/he promise to make a deposit (of a fixed amount) at the beginning of every year and HI promises to add an interest payment to the account at the end of the year. The interest rate is determined when the account is created and never changes. The interest payment is equal to the interest rate multiplied by the balance in the account at the beginning of the year.

    If the deposit is denoted by d, the interest rate is denoted by r, and the value of the retirement account at the end of year n be denoted by Vn , it follows that:

    V 0 = d
    V 1 = d + (1 + r)V 0
    .
    .
    .
    Vn = d + (1 + r)V n-1

    With that in mind, complete the valueIn() method in the following RetirementAccount class. Your solution must be recursive.

    public class RetirementAccount
    {
    private double deposit, rate;

    /**
    * Explicit Value Constructor
    *
    * @param deposit The deposit made (at the beginning of) each year
    * @param rate The interest rate (which is guaranteed)
    */
    public RetirementAccount(double deposit, double rate)
    {
    this.deposit = deposit;
    this.rate = rate;
    }


    /**
    * Calculates the value of this RetirementAccount in
    * a given number of years
    *
    * @param years The number of years
    */
    public double valueIn(int years)
    {
























    }
    }

  3. The Color class has the following public methods:

        Color(int r, int g, int b) // Constructs a Color 

    int getBlue() // Gets the value of the blue component [0,255]

    int getGreen() // Gets the value of the blue component [0,255]

    int getRed() // Gets the value of the blue component [0,255]

    The following partial implementations of the ColorSensor and RetirementAccount classes exist:

    Your code will be tested with the following classes and data files: