// Function.java // // Rahul Simha // Spring 2008 // Updated 2015 import java.awt.*; import java.awt.geom.*; import java.util.*; import java.text.*; public class Function { String name = "Func"; String xLabel = "x"; Vector points = new Vector(); double minX, maxX, minY, maxY; public Function (String name) { if (name != null) this.name = name; } public Function (String name, String xLabel) { if (name != null) this.name = name; if (xLabel != null) this.xLabel = xLabel; } public String getName () { return name; } public void add (double x, double y) { // Find right place for it. int k = points.size(); for (int i=0; i 8) { df = new DecimalFormat ("#.##E0"); } else { df = new DecimalFormat (s); } return df.format (x); } public void show () { SimplePlotPanel.makePlotFrame (points, name, 600, 480); } public static void show (Function ... fSet) { ArrayList pointSets = new ArrayList (); ArrayList names = new ArrayList (); for (Function F: fSet) { pointSets.add (F.points); names.add (F.name); } SimplePlotPanel.makePlotFrame (pointSets, names, fSet[0].xLabel, 600, 480); } public static void show (ArrayList fSet) { ArrayList pointSets = new ArrayList (); ArrayList names = new ArrayList (); for (Function F: fSet) { pointSets.add (F.points); names.add (F.name); } SimplePlotPanel.makePlotFrame (pointSets, names, fSet.get(0).xLabel, 600, 480); } public static void main (String[] argv) { //test1(); test2 (); } static void test1 () { Function F = new Function ("Test"); F.add (1, 2); F.add (2, 4); F.add (3, 6); System.out.println ("F(1.5)=" + F.get(1.5) + " F(2.2)=" + F.get(2.2) + " F(3.0)=" + F.get(3.0) + " F(5)=" + F.get(5.0)); F.show (); } static void test2 () { Function F = new Function ("Test1"); for (double x=0; x<=10; x+=0.1) { F.add (x, 3*x+5); } Function G = new Function ("Test2"); for (double x=0; x<=10; x+=0.1) { G.add (x, 4*x+5); } Function zero = new Function ("Zero"); for (double x=0; x<=10; x+=0.1) { zero.add (x, 0); } double d = F.distance (G, 100); System.out.println ("dist=" + d); double d2 = F.distance (G, 1000); System.out.println ("dist2=" + d2); double d3 = F.distance (zero, 1000); System.out.println ("dist3=" + d3); } }