/Users/lyon/j4p/src/bookExamples/ch06RefDataTypes/PolyExample.java

1    package bookExamples.ch06RefDataTypes; 
2     
3    /** 
4     * DocJava, Inc. 
5     * http://www.docjava.com 
6     * Programmer: dlyon 
7     * Date: Sep 13, 2004 
8     * Time: 3:42:58 PM 
9     */ 
10   interface Fun1 { 
11       public double f(double x); 
12   } 
13   class Sine implements Fun1 { 
14          public double f(double x){ 
15              return x+1; 
16          } 
17   } 
18   class FutureValue implements Fun1 { 
19       double rate = 0.07; 
20       double y = 30; 
21       FutureValue(double rate, double y) { 
22           this.rate = rate; 
23           this.y = y; 
24       } 
25       public double f(double loan) { 
26             return  (100000 + loan * 100000) * Math.pow(1+rate,y); 
27       } 
28   } 
29   public class PolyExample { 
30       public static void main(String[] args) { 
31           FutureValue fv = new FutureValue(0.03,30); 
32           print(fv); 
33           //print(new Sine()); 
34       } 
35    
36       public static void print(Fun1 fcn1) { 
37           for (double d=0; d < 1; d=d+0.1){ 
38               System.out.println("d,f(d)="+d+","+fcn1.f(d)); 
39           } 
40       } 
41    
42   } 
43