package username.functions; /** * Functions that represent that product of two functions. * E.g., f(x) = g(x) * h(x). * * @author Samuel A. Rebelsky * @version 1.0 of April 2006 */ public class ProductUIF implements UnaryIntegerFunction { // +--------+----------------------------------------------- // | Fields | // +--------+ /** The first function in the product. */ UnaryIntegerFunction fun1; /** The econd function in the product. */ UnaryIntegerFunction fun2; // +--------------+----------------------------------------- // | Constructors | // +--------------+ /** Build a new function that returns f1(x)*f2(x). */ public ProductUIF(UnaryIntegerFunction f1, UnaryIntegerFunction f2) { this.fun1 = f1; this.fun2 = f2; } // ProductUIF(UnaryIntegerFunction,UnaryIntegerFunction) // +------------------+------------------------------------- // | Standard Methods | // +------------------+ public String toString() { String str1 = fun1.toString(); String str2 = fun2.toString(); if (str1.length() > 1) { str1 = "(" + str1 + ")"; } if (str2.length() > 1) { str2 = "(" + str2 + ")"; } return str1 + "*" + str2; } // toString // +----------------+--------------------------------------- // | Public Methods | // +----------------+ public int apply(int x) { return this.fun1.apply(x) * this.fun2.apply(x); } // apply(int) } // ProductUIF