## Programming Assignment 2

This is the second individual programming assignment in CPSC 124: Introductory Programming, Fall 1998. See the information page for that course for more information.

This is an individual programming assignment. You should not discuss the assignment with anyone, except the instructor of the course. You should not show your work to anyone else in the class or look at their work. By turning in the assignment, you are asserting that you have followed these rules.

The program is due in class on Wednesday, October 21. Start work on it now, so that you can come in for help if you need it. Remember that programs turned in up to one week late will lose 20% off their grade. Programs will not be accepted more than one week late.

You might or might not have studied Integral Calculus, but there there is one problem in that course that can be understood without knowing anything about calculus. The problem is to find the area under the graph of a function. Although this area can usually not be computed exactly without calculus, it can always be approximated numerically. In fact, even with the techniques of calculus available, there are many cases where the area cannot be computed exactly. In those cases, the only recourse is to calculate an approximation. In this assignment, you will write a program that can do such approximations. I will explain some of the background in class, but for the purposes of this assignment, all you have to do is implement the formulas that are defined below.

We are interested in the area that lies between the graph of a given function and an interval of the x-axis. Let's suppose that the function is f(x), the left endpoint of the interval is A, and the right endpoint of the interval is B. Choose a positive integer, N. In general, larger values of N will give better approximations. Define dx to be (B-A)/N. Then the area in question can be approximated as

```      dx * ( f(A+0*dx) + f(A+1*dx) + f(A+2*dx) + . . . + f(A+(N-1)*dx) )
```

Because of the "dots" in this formula, you can't just use it in a program. You need to use a for loop to add up all the values of f(A+i*dx) for i from 0 to N-1. This formula is the "left endpoint rule" for approximating area. There are several other rules that could be used. The "right endpoint rule" approximates the area with the sum

```      dx * ( f(A+1*dx) + f(A+1*dx) + f(A+2*dx) + . . . + f(A+N*dx) )
```

that is, as the sum of f(A+i*dx) for i going from 1 to N. The "trapezoid rule" approximates the area as the sum:

```      dx * ( f(A+1*dx) + f(A+2*dx) + f(A+3*dx) + . . . + f(A+(N-1)*dx) )
+  0.5 * dx * ( f(A) + F(B) )
```

And finally, the midpoint rule uses the sum

```      dx * ( f(A+(0.5)*dx) + f(A+(1.5)*dx) +
f(A+(2.5)*dx) + . . . + f(A+((N-1)+0.5)*dx) )
```

Your task for this programming assignment is to write subroutines to implement each of these rules for approximating the area, and to use them to approximate the area under the graph of a function entered by the user. The user also gets to select the values of A, B, and N.

In Lab 5, you will use a class named Expr to represent functions of x. Use this same class to represent a function in this program. (This means that instead of writing "f(X)" for the value of the function, you would write "f.value(X)".) The subroutine to compute the left-endpoint rule approximation for the area could then have the form:

```      static double leftEndpointRule(Expr f, double A, double B, int N) {
// A subroutine that approximates the area under the graph
// of f(x) on the interval with left endpoint A and right
// endpoint B.  It uses a "left-endpoint rule" sum with N terms.
.
.  // Compute the sum and return its value.
.
} // end leftEndpointRule
```

The body of the subroutine will only be about five or six lines long. The subroutines for the three other rules will be similar to this one.

The main routine of your program must give the user a way to set up all the data (f, A, B, and N), and to select which approximation rule to apply. It must work in a loop that continues getting data from the user and doing approximations until the user wants to quit. There is a compiled solution to this assignment on the computer in the file /home/cs124/RiemannSums.class. Run this program to see one one way of organizing the interaction with the user. You are not required to use exactly the same style of input/output, but you should at least give the user similar flexibility. You can run the program, set your working directory to /home/cs124 with the command "cd /home/cs124", then run the program with the command "java RiemannSums".

David Eck, 11 October, 1998