This lab explores mathematical functions via Java applets, Java programs that can produce graphics in a browser. As before, make a directory for this lab and keep a file of notes. Mail this file to me when you're done with the lab. Exercises with starts (*) aren't critical to the susbsequent exercises. If things are going slowly, it's safe to skip these for now; come back to them if there's time. 1) Copy the file ~lorenz/cs35-2/lab3/Function.java into your directory; study, compile and run it. Study its output. 2) Copy the file ~lorenz/cs35-2/lab3/Plotter.java into your directory and compile it. Also copy the file ~lorenz/cs35-2/lab3/applet.html to your directory. Load this latter file into your "hotjava" browser (type "hotjava" in a unix window). You should see a graph of the program's Linear object. 3) Provide a constructor in the Linear class to scale the function by an integer coefficient; i.e. in addition to the current constructor that makes "f(x) = x", create one to make "f(x) = a*x" 4*) Provide yet another constructor to make "f(x) = a*x+k". It should have two parameters (a,k). 5) Test your Linear constructors by creating additional Linear objects and placing them in the_functions array and printing them out. Reload applet.html to see the functions plotted together. 6*) After creating a Function object (or subclass thereof), you may set its color by modifying its .color field. Valid colors are in the Java Color class; e.g. Color.red, Color.blue, Color.cyan. (http://www.javasoft.com/products/jdk/1.1/docs/api/java.awt.Color.html) Set different color fields on the various Linear objects. Recompile Function.java and reload applet.html 7) Create subclasses of Function for a) Quadratic functions, f(x) = x^2 b) Exponential functions, f(x) = b^x c) Logarithmic functions, f(x) = log x (log base 2 is available as Main.log2(int x)) What quickly happens to the integer values of the f(x)=b^x computation? How do you explain this? Plot these functions; use different colors if you like. (You must always recompile and then reload applet.html into hotjava.) 8*) How would you compute f(x)=x^2 without using multiplication? (Hint: use nested loops.) 9) Clean up the main method: keep only the declaration of the_functions and the comments. 10) Copy the file ~lorenz/cs35-2/lab3/RndArray.java into your directory and compile it. The class RndArray can be instantiated to an object that contains an integer array of length n initialized with the numbers from 0 to n-1 in a random order. 11) Create a subclass of Function in (Function.java) called Search. Here's most of the code: class Search extends Function { Search(int n) { pts = new int[nPts]; /* compute the function */ pts[0] = 0; for (int x = 1; x < n; x++) { int j; /* declare and new a RndArray of size x */ /* find position j of the element x/2 */ pts[x] = j; } } } Upon construction, a search object should, for every point x in 0...nPts, create a RndArray of size x. Furthermore, after creating this array, it should scan the array from index 0 until it finds the element holding the median value; i.e. x/2. The number of loop iterations required to find this median value should be recorded in the pts array. The position of this median element (equivalently the number of loop iterations) loosely corresponds to the average-case running time of searching in an unordered array. 12) Plot a Search object. Also plot Linear objects corresponding to "f(x) = x" and to "f(x) = x/2". Where does the running time of Search seem to fall? 13*) Refine your average-case analysis by sampling more (e.g. 10) random arrays per Search point. This should lead to a straighter line in the plot. 14*) Inspect the Plotter.java code and try and understand its operation even though there are package methods we haven't seen before.