mkdir ~/cs171/hw2 cp /home/cs171000/Handouts/hw2/*.java ~/cs171/hw2 |
|
|
public class RectangleMethod
{
public static void main(String[] args)
{
double a, b, w, sum, x_i;
int i, n;
// You need to provide the fillowing inputs
a = start of the interval
b = end of the interval
n = # rectangles used to approximate the integral
/* ---------------------------------------------------
The Rectangle Rule Algorithm
--------------------------------------------------- */
w = (b-a)/n; // Compute width
sum = 0.0; // Clear running sum
for ( i = 0; i < n; i++ )
{
x_i = a + i*w; // x_i = left corner point of rectangle
sum = sum + ( w * f(x_i) ); // w = width of rectangle i
// f(x_i) = height of rectangle i
}
System.out.println("Approximate integral value = " + sum);
}
}
|
The method is named:
public static double approxIntegral( FunctionClass G, double a, double b, int n ) |
that computes:
|
|
Test 1: Integral computed by your program = 333.33332833323936 The correct answer is: 333.33332833323936 Test 2: Integral computed by your program = 0.666666666666624 The correct answer is: 0.666666666666624 Test 3: Integral computed by your program = 11.666666791658349 The correct answer is: 11.666666791658349 |
|
You will need to use these files in part 2:
|
In part 2 of the assignment, you are to write 2 programs:
|
Important note:
|
Use the following command (from inside your cs171/hw2 project directory) to turn in your program:
cd ~/cs171/hw2 /home/cs171000/turnin RectangleMethod.java hw2 /home/cs171000/turnin MyFunctionClass.java hw2a /home/cs171000/turnin MyTestProg.java hw2b |