Executive Summary [READY]

This is the third graded lab for CS2102.

1. Stated Aims

  1. Learn how to write a derived class from scratch
  2. Learn how to define the details of a linked list (both the Node class and the class representing the linked list)
  3. Problem solve a computation.

2. Background knowledge

  1. Understand concept of derived class
  2. Understand concept of method invocations to derived classes.

3. Setup

If you are in the Lab, perform the routine KH 202 setup or AK 120D setup; for now, ignore the "advanced setup" options. If you would like to set up  Eclipse on your personal computer, then (a) install Java JDK 1.5 if you need to as described in tools section (here); and (b) install Eclipse as described in tools section (here).

Make sure you have read pp. [423-446] from the textbook.

Download the provided lab fragments from the Examples directory in Eclipse or from here. You have been given a base class Function and derived class SquareFunction. You also have the skeletal outline of a FunctionNode and FunctionList class. Finally, there is a FunctionListTest test case that I have provided, which you must complete.

4. Individual Tasks

Your challenge is to develop some classes that can be used to experiment with functional composition. Specifically, in mathematics, it is possible to compose functions F(x) and G(x) to create the function (F o G)(x) = F (G (x) ). For example, assume F(x) is the function "x+2" and G(x) is the function "x*5". Now G(2) is 10, and F(10) is 12. Thus (F o G)(2) = F (G (2) ) which equals 12.

Now, how does this relate to linked lists? Well, in this lab, you are going to create a linked list of Functions. Each Function computes an int value given a single int as the parameter. Assume that F and G are as above, and a new Function H is defined as "x-8". Then (F o G o H)(x) = F (G ( H (x) ) ) which means (F o G o H) (2) is equal to F ( G ( H ( 2) ) ). H(2) is equal to -6, G(-6) is equal to -30, and F(-30) is equal to -28. Thus (F o G o H) (2) is equal to -28.

The linked list arises when you consider the sequence H --> G --> F. Thus, if you could construct a linked list with the nodes representing Functions in this order, then you could compute the function (F o G o H) (2). Note that the order at first glance appears to be backwards: But it is designed to make the lab straightforward.

4.1 Hand in the answer to the following question(s) in lab

4.2 Design FunctionNode and FunctionList so you can create a Linked List of Functions

You have been provided with a base Function class:

public class Function {

   /** Compute f(x) where f is the identify function.
    *  @param x value in the domain for f(x)
    *  @return range of the function f(x) where f(x) is the identify function.
    */
   public int compute (int x) {
     return x;
   }
}

a derived SquareFunction class:

public class SquareFunction extends Function {

  /** Compute f(x) where f is x^2
   *  @param x value in the domain for f(x)
   *  @return range of the function f(x) where f(x) is x^2
   */
  public int compute (int x) {
    return x * x;
  }
}

and a FunctionListTest JUnit test case:

public class FunctionListTest extends TestCase {

  /** These tests should run *as is*. */
  public void testSimpleExample () {
    FunctionList list = new FunctionList();

    Function id1 = new Function();       // the identify function F(x) = x
    Function id2 = new SquareFunction(); // the square function G(x) = x^2
    Function id3 = new SquareFunction(); // again square H(x) = x^2

    list.add(id1); // now F(x)
    assertEquals(10, list.compute(10));

    list.add(id2); // now G(F(x))
    assertEquals(100, list.compute(10));

    list.add(id3); // now H(G(F(x)))
    assertEquals(10000, list.compute(10));
  }

}

Once you have completed the implementation for FunctionNode and FunctionList, you should be able to execute the JUnit test case FunctionListTest. Once the JUnit test case succeeds, you are done with part 1 of the lab.

4.3 Design two new Function derived classes

Design the class TripleFunction which extends Function and provides an implementation whose compute(x) method returns "3*x". Design the class DivideByFunction that extends Function and provides an implementation whose compute(x) method returns "x/n" where n is an integer. Note that for the DivideByFunction class, you will have to define a constructor that takes a single argument DivideByFunction(int n) that is to be stored as an instance variable.

Within the FunctionListTest class, there is an empty test case, testYourCode(), that you must fill in. Your goal is to come up with a sequence that enables the second test case in the FunctionListTest file to succeed. Note that I would like you to use a DivideByFunction instance that computes the function "x/2".

public class FunctionListTest extends TestCase {

   public void testYourCode() {
     FunctionList list = new FunctionList();

     // insert your code here

     assertEquals (96, list.compute(8));
   }

}

You can solve this problem by using three objects from the available derived classes -- TripleFunction, DivideByFunction, SquareFunction.

5. Follow-up Tasks

Your goal is to turnin the assignment file(s) found in the lab3 package by November 15th at 11:59 PM. The name of the assignment you should use is "lab3". Don't forget to sign in for the lab and turn in your questions. You won't receive a grade unless you (a) submit your code via turnin and; (b) hand in these questions during the lab section.

Change History

Date Reason/Change
11/15/2006 Ready to go
11/14/2006 Initial page