### CS171, Homework 2

• Preparation

• To do this project, you must obtain a copy of the prepared files with these commands:

 ``` mkdir ~/cs171/hw2 cp /home/cs171000/Handouts/hw2/*.java ~/cs171/hw2 ```

• Overview of the assignment

• This project consists of 2 parts:

• Part 1:

• You are given the following FunctionClass program:

 ``` public class FunctionClass { public double f( double x ) { return x*x; // f(x) returns x2 } } ```

• You must write a method:

 ``` public static double approxIntegral(FunctionClass G, double a, double b, int n) ```

that use the rectangle rule to compute an approximation of the integral of the function f(x) = x2.

The function f(x) is a method that is part of the input parameter G.

So you can compute the function value in x using the call:

 ``` G.f(x) ```

• Part 2:

• In part 2, you must use the method that you have written in part 1 to compute the integral of a different function

Requirement:

 You cannot make any changes to your program file written for part 1

Hint: use polymorphism

• Pre-requisite: the Rectangle Rule method

• We will use the Rectangle Rule method in this assignment.

• The Rectangle Rule method is an algorithm used to find/compute an approximation of a definite integral of a function.

• The Rectangle Rule method was discussed in CS170 --- see: click here

• Briefly:

• The definite integral is the area under the function f(x) between the interval [a,b]:

• We can approximate the integral with the sum of the area of nr rectangles:

• The width of each rectangle is the same and is equal to (b − a)/n

The height of each rectangle is equal to the function value at the left corner of the rectange:

• Here is the Java code of the Rectangle Rule method:

 ``` 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); } } ```

• Part 1: adapt the Rectangle Rule algorithm

• The Rectangle Rule algorithm is given as a main program.

• In the first part of the project, you are to change the Rectangle Rule algorithm into a method form.

The method is named:

 ``` public static double approxIntegral( FunctionClass G, double a, double b, int n ) ```

that computes:

 The definite integral of the function G.f(x) = x2 that is defined in the class FunctionClass. The integral interval interval is [a,b]. The definite integral is aproximated using n rectangles. You can adapt the Rectangle Rule algorithm (see above).

• You need to use these files in part 1:

• FunctionClass.java: the FunctionClass class contains the method f(x) that you need to integrate:

 ``` public class FunctionClass { public double f( double x ) { return x*x; // f(x) = x^2 } } ```

Do not make any changes to the program FunctionClass.java !!!

• RectangleMethod.java: the RectangleMethod class contains the method approxIntegral( FunctionClass G, double a, double b, int n ) that you need to complete for part 1 of the project:

 ``` public class RectangleMethod { public static double approxIntegral( FunctionClass G, double a, double b, int n ) { /* --------------------------------------------------- Write this method for part 1 Note: G.f(x) will compute the function value in x --------------------------------------------------- */ } } ```

• testProg.java: is the test program for part 1

Do not make any changes to the test program testProg.java !!!

• After completing the approxIntegral() method, you can run the test program as follows:

 To compile:   javac testProg.java           To run:          java testProg

• Correct output of part 1:

 ``` 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 ```

• Part 2: use your Rectangle Rule algorithm to integrate the function f(x) = x4

• Important:

 Make sure your part 1 is correct before doing part 2. (When you run the testProg, you can see verify that your approxIntegral() is correct.)

• Part 2:

You will need to use these files in part 2:

 MyFunctionClass.java            MyTestProg.java RectangleMethod.java --- do not make any changes to this program file !!!

In part 2 of the assignment, you are to write 2 programs:

• Write the method f(x) in the class MyFunctionClass:

 This method returns the value x4         (It's very simple !)

• Write the main method is a test program myTestProg.java that calls the approxIntegral() method from Part 1 to compute the integrals of x4 using the same values of a, b and n as the test program testProg.java

I have put in some print statements in the main method already:

 ``` public static void main( String[] args ) { double r; // Write a statement to compute the integral for interval [0, 10] // using 100000000 rectangles r = .... System.out.println("Integral of x^4 from 0 to 10 = " + r ); System.out.println("The correct answer is: 19999.99950000431"); // Write a statement to compute the integral for interval [-1, 1] // using 100000000 rectangles r = .... System.out.println("Integral of x^4 from -1 to 1 = " + r ); System.out.println("The correct answer is: 0.3999999999990907"); } ```

All you need to do is figure out how you can make the method RectangleMethod.approxIntegral() compute the correct integral.

 Do not make any changes to the RectangleMethod.approxIntegral() method. You must use it as is (i.e., the same solution you got from part 1) Hint: use polymorphism (that's the point of this asignment !!!)

• After completing these 2 programs, you can run the test program as follows:

 To compile:   javac MyTestProg.java           To run:          java MyTestProg

 ``` Integral of x^4 from 0 to 10 = 19999.99950000431 The correct answer is: 19999.99950000431 Integral of x^4 from -1 to 1 = 0.3999999999990907 The correct answer is: 0.3999999999990907 ```

Important note:

 Do not try to do part 2 by using the files from part 1. The files testProg,java and FunctionClass.java must not be edited. (You can use them by cutting and pasting Java code from the file(s) into the new program files MyTestProg,java and MyFunctionClass.java)

• Turn in

• After you have successfully completed and tested your program, you must turn your file in for grading.

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 ```