Oregon State University home page
OSU Physics Department home page
  PH.265 MAIN PAGE         HOME    

Understanding arrays and applets

You should create an applet which draws the graph of cos function, and its integral. This applet should use arrays to perform this task. There will be 6 arrays: three "double" ones, which will store "logical" x coordinates, and the corresponding coordinates of the cos function, and its integral, and three "integer" arrays which will store the "screen" coordinates.

  1. Start from the Applet1 and modify its method paint in the following way:
  2. Create a new integer variable nMax=100.
  3. Create three double arrays, nMax points each. Call one array xArr, the second yArr, and the last one intArr.
  4. Create three integer arrays, nMax points each. Call them xIArr, yIArr, and intIArr.
  5. Add a new integer variable: iInd; create the new double variable dx, and initialize it with the value 3./nMax. Note that now dx represents a "step" in logical coordinates from one graph point to the next one.
  6. Replace the parameters in the world2win call (still in method paint) so that the top and bottom "logical" coordinates would be -1 and 1, and left and right coordinates would be 0 and 3.
  7. Edit the function f(x) so it will return cos(x)
  8. Return to the method paint. Right after the world2win call add three statements which initialize first elements of your double arrays (xArr, yArr, and intArr). Initialize first elements of xArr and intArr with 0, and first elements of yArr with cos(0). Note that the first element of an array xArr is xArr[0]; the same is true for all other arrays.
  9. right after this initialization add a loop: for(iInd=1; iInd<nMax, iInd=iInd+1){…} ; the "…" should do the following:
    1. setup the logical coordinate x as xArr[iInd]=xArr[iInd-1]+dx;
    2. calculate the iInd-th "y" coordinate
    3. now, as you have both yArr[iInd] and yArr[iInd-1] calculated, calculate intArr[iInd] as intArr[iInd-1]+dx*(yArr[iInd]+yArr[iInd-1])/2.
    4. Write a comment, explaining why the previous line calculates the integral of a function stored in yArr
  10. after the previous loop, add another one: for(iInd=0; iInd<nMax, iInd=iInd+1){…}; Here the "…" should convert the "logical" coordinates xArr[iInd], yArr[iInd], and intArr[iInd] into screen ones xIArr[iInd], yIArr[iInd], and intIArr[iInd]. Note that only "x" coordinate is "horizontal", while both "y" and "int" are "vertical".
  11. replace the existing drawing loop with
    for(iInd=1; iInd<nMax, iInd=iInd+1){
    g.drawLine(xIArr[iInd-1],yIArr[iInd-1],xIArr[iInd],yIArr[iInd]);
    g.drawLine(xIArr[iInd-1],intIArr[iInd-1],xIArr[iInd],intIArr[iInd])
    }
    ;
  12. Add a comment explaining how the drawing works now
  13. Add two lines for coordinate axes.
  14. put both applet (class file), and html file in public_html folder on ONID server. To do this, start for example, gftp (in xwidows menu go to "Menu"->Internet->data exchange->gftp, enter the server name ftp.onid.orst.edu, enter your ONID username, and password. Then drag and drop your files to the public_html folder on ONID (if you don't have public_html folder, create one).
    You should be able to start the applet via the link: http://www.onid.orst.edu/~USERNAME/HTML_FILE_NAME, or
    http://www.oregonstate.edu/~USERNAME/HTML_FILE_NAME (let me know if this doesn't work).
    Send me a link to your page via e-mail (viktor.podolskiy@physics.oregonstate.edu). Send a source (Java) code in the same e-mail.

top

 

download pdf

E-mail: Viktor.Podolskiy@physics.oregonstate.edu
Office: Weniger 401A
Office Phone: 541-737-1702; Fax: 541-737-1683