Programming Tools & Computer Science I

CSC 185 & 201, Spring 2013, Northern Virginia Community College

Due:

Apr 1, 11:59PM

To submit:

Send an email to me at jal2016@email.vccs.edu with subject CSC 201 HW6, containing your your answers attached in a zip file.

Directions:

The course materials have a project set up in homework/201/hw6 that you should start with or copy java files from.

  1. Create a class LoggedArray that inherits from ArrayList<String>, and prints a message whenever a new entry is added to the array.

    Here's an example of usage:

    package csc201.hw6;
    
    /** Example of LoggedArray usage.
     */
    public class LoggedArrayTest {
        public static void main(String[] args) {
            LoggedArray array = new LoggedArray(System.out);
            array.add("one"); // Should print something like "Adding 'one' to list."
            array.add("two");
            array.add("three");
        }
    }
    

  2. Create a class LoggedArray2 that is similar to your solution from #1, but does not inherit from ArrayList<String>. Rather, choose one of the following options:

    1. Use an ArrayList<String> as a member (that is, use delegation rather than inheritance), and implement the following methods:

      • boolean add(String s)
      • void set(int i, String s)
      • String get(int i)
    2. Implement the Collection<String> interface, and define your class in a way that you can construct a logged array that wraps up any desired collection object. This option is more work, but will define a class that can be used anywhere a collection is expected.

  3. Consider the following abstract class used for 1-D numerical integration (computing the area underneath a curve described by a function of 1 variable):

    /*
     * 
     */
    package csc201.hw6;
    
    /** Abstract class for numerical integration.
     *
     */
    public abstract class AbstractIntegrator {
    
        // Number of subdivisions for integration.
        private int N;
    
        /** Construct an integrator.
         * 
         * @param n Number of subdivisions.
         */
        public AbstractIntegrator(int n) {
            N = n;
        }
    
        /** Integrate (compute area underneath) function on a given interval.
         * 
         * @param f Function to integrate
         * @param a Left interval endpoint
         * @param b Right interval endpoint
         * @return Area estimate
         */
        public double integrate(Function f, double a, double b) {
            double delta = (b-a)/N;
            double sum = 0;
            for (int i = 0; i < N; i++) {
                double left = a + delta * i;
                double right = left + delta;
                sum += height(f, left, right);
            }
            return sum * delta;
        }
    
        /** Compute height of function in given interval.
         * 
         * @param f Function to integrate
         * @param left left endpoint of interval
         * @param right right endpoint of interval
         * @return Measure of height of function 
         */
        public abstract double height(Function f, double left, double right);
                
    }
    

    /*
     * 
     */
    package csc201.hw6;
    
    /**
     * Interface that represents a mathematical function of 1 variable.
     */
    public interface Function {
    
        /**
         * Compute the value of the function
         *
         * @param x function input
         * @return function output
         */
        double call(double x);
    }