## General Policies:

Do NOT share code. You will not receive help on the day a program is due or after the due date, SO DO NOT PROCRASTINATE!!!

Program 1 counts for 11% of your grade.

## Background:

Documentation for Additional Classes (in Javadoc format)

Before the invention of computers, the word "computer " referred to a person who performed mathematical calculations. Human computers calculated nth roots of numbers for various values of n, logarithms, trajectories, areas under curves, and many other things. Your task is to write a program to do some tasks that human computers used to perform.

If you study calculus, you learn how to compute derivatives and integrals for many mathematical functions. Here you will teach the computer to do these computations for relatively simple mathematical functions, polynomials. The polynomials we will consider have one variable, x; x^3 + 3 x^2 + 3 x + 1 is an example of a polynomial of degree 3.

Computing the derivative of a polynomial is a straightforward task. The degree of the derivative will be one less than the degree of the original polynomial. For each term of the original polynomial except for the constant term, a term for the derivative polynomial will be determined as follows. The coefficient is computed by multiplying the degree of the term by the coefficient of the term, and the degree will be one less than the degree of the term in the original polynomial. For the above example, we get 3 x^2 + 6 x + 3 as the derivative. The act of computing a derivative is often called differentiation.

Evaluating a derivative at a given x value is equivalent of computing the slope of the tangent line to the function at the given point. For example, evaluating the derivative of x^3 + 3 x^2 + 3 x + 1 at 2.5 yields 3 (2.5)^2 + 6 (2.5) + 3, which equals 36.75. Another way to approximate the slope of the tangent line is to calculate the difference quotient. Given an x value and a distance h, evaluate the function at x+h, evaluate the function at x, compute the difference, and divide by h. The limit of this result as h approaches 0 is the derivative of the function at the given h value.

Given a polynomial, a value x, and a tolerance, you will determine a value of h which causes the absolute value of the difference between the difference quotient and the polynomial's derivative at x to be less than or equal to the tolerance. How can this be done? Evaluate the derivative at x. You can initialize h to be 1. Compute the difference quotient. If the absolute value of the difference between the difference quotient and the derivative is larger than the tolerance, update h by dividing its old value by 2, recompute the difference quotient, and repeat the process. Note that the difference quotient needs to be calculated using the original polynomial.

Computing an indefinite integral of a polynomial is also a straightforward task. The degree of the indefinite integral will be one more than the degree of the original polynomials. For each term of the original polynomial, a term for the indefinite integral polynomial will be determined as follows. The coefficient is computed by dividing the coefficient of the term by one more than the degree of the term, and the degree will be one more than the degree of the term in the original polynomial. For the beginning polynomial above, we get 1/4 x^4 + 3/3 x^3 + 3/2 x^2 + 1/1 x, which is 0.25 x^4 + x^3 + 1.5 x^2 + x. What happens if we integrate the derivative that we computed earlier? We get 3/3 x^3 + 6/2 x^2 + 3/1 x, which is x^3 + 3 x^2 + 3 x. This is the original polynomial, except for the constant term. The act of computing an integral is often called integration.

Computing the area under a polynomial between two points involves integrating the polynomial, evaluating the integral at the second point, evaluating the integral at the first point, and determining the mathematical difference between them.

## Program Execution:

Documentation for the Poly class

Note that Poly has two constructors. You will be using the constructor which takes one int parameter, which represents the degree of the polynomial. Call your file PolyProgram.java. It is tempting to complete this entire program in the main method. I want you to write lots of short methods to get practice in creating, passing, and using objects. I will be grading you on how well you break the problem down into separate, short, static methods. To give you an idea of what I will be looking for, here are some sample method signatures that I expect you to implement and to use:

public static Poly readPoly(int degree) //produces a polynomial of the given degree by asking the user to enter suitable coefficients
public static String displayPoly(Poly poly) //displays the polynomial (nonzero coeffs only)
public static Poly integrate(Poly poly) //produces a polynomial corresponding to an indefinite integral of the given polynomial
public static double areaUnderCurve(Poly poly, double startX, double endX) // computes the (non-negative) area under the given polynomial between startX and endX
public static Poly differentiate(Poly poly) //produces a polynomial corresponding to the derivative of the given polynomial
public static double derivativeAtX(Poly poly, double x) // computes the derivative of the given polynomial at the given x value
public static double getH(Poly poly, double x, double tolerance) // computes a value of h for which the absolute value of the percentage error (discussed in Lab 2) does not exceed the tolerance

The parameters are as follows:

• poly is your polynomial
• degree is the highest power of x with a nonzero coefficient
• startX is the left endpoint of the interval
• endX is the right endpoint of the interval
• x is the value at which a suitable polynomial should be evaluated
Other tasks:
• Compare doubles to each other using a reasonable tolerance.
• Format numerical output with the DecimalFormat class.
• Obtain needed input from the user using the Keyboard class.
• Use loops.

The following shows some sample runs with expected outputs.

Sample 1

```Please enter the degree for the polynomial with which you wish to work: 3
Enter the coefficient for the degree 0 term: 1
Enter the coefficient for the degree 1 term: 3
Enter the coefficient for the degree 2 term: 3
Enter the coefficient for the degree 3 term: 1
The polynomial that you entered is 1.000 x^3 + 3.000 x^2 + 3.000 x + 1.000
What do you wish to do with the polynomial?
Enter 1 to compute and display an indefinite integral.
Enter 2 to determine and display the area under the polynomial between two endpoints.
Enter 3 to compute and display the derivative of the polynomial.
Enter 4 to determine and display the derivative of the polynomial at a point.
Enter 5 to determine and determine a value of h for which the difference quotient is close enough to the derivative at a given point.
Enter your choice: 1
An indefinite integral of 1.000 x^3 + 3.000 x^2 + 3.000 x + 1.000 is 0.250 x^4 + 1.000 x^3 + 1.500 x^2 + 1.000 x
```

Sample 2

```Please enter the degree for the polynomial with which you wish to work: 3
Enter the coefficient for the degree 0 term: 1
Enter the coefficient for the degree 1 term: 3
Enter the coefficient for the degree 2 term: 3
Enter the coefficient for the degree 3 term: 1
The polynomial that you entered is 1.000 x^3 + 3.000 x^2 + 3.000 x + 1.000
What do you wish to do with the polynomial?
Enter 1 to compute and display an indefinite integral.
Enter 2 to determine and display the area under the polynomial between two endpoints.
Enter 3 to compute and display the derivative of the polynomial.
Enter 4 to determine and display the derivative of the polynomial at a point.
Enter 5 to determine and determine a value of h for which the difference quotient is close enough to the derivative at a given point.
Enter your choice: 2
Please enter the left endpoint for the interval. 1
Please enter the right endpoint for the interval. 5
The area under 1.000 x^3 + 3.000 x^2 + 3.000 x + 1.000 from x=1 to x=5 is 320.000
```

Sample 3

```Please enter the degree for the polynomial with which you wish to work: 3
Enter the coefficient for the degree 0 term: 1
Enter the coefficient for the degree 1 term: 3
Enter the coefficient for the degree 2 term: 3
Enter the coefficient for the degree 3 term: 1
The polynomial that you entered is 1.000 x^3 + 3.000 x^2 + 3.000 x + 1.000
What do you wish to do with the polynomial?
Enter 1 to compute and display an indefinite integral.
Enter 2 to determine and display the area under the polynomial between two endpoints.
Enter 3 to compute and display the derivative of the polynomial.
Enter 4 to determine and display the derivative of the polynomial at a point.
Enter 5 to determine and determine a value of h for which the difference quotient is close enough to the derivative at a given point.
Enter your choice: 3
The derivative of 1.000 x^3 + 3.000 x^2 + 3.000 x + 1.000 is 3.000 x^2 + 6.000 x + 3.000
```

Sample 4

```Please enter the degree for the polynomial with which you wish to work: 3
Enter the coefficient for the degree 0 term: 1
Enter the coefficient for the degree 1 term: 3
Enter the coefficient for the degree 2 term: 3
Enter the coefficient for the degree 3 term: 1
The polynomial that you entered is 1.000 x^3 + 3.000 x^2 + 3.000 x + 1.000
What do you wish to do with the polynomial?
Enter 1 to compute and display an indefinite integral.
Enter 2 to determine and display the area under the polynomial between two endpoints.
Enter 3 to compute and display the derivative of the polynomial.
Enter 4 to determine and display the derivative of the polynomial at a point.
Enter 5 to determine and determine a value of h for which the difference quotient is close enough to the derivative at a given point.
Enter your choice: 4
Please enter the x-value for which you wish to compute the derivative: 2.5
The derivative of 1.000 x^3 + 3.000 x^2 + 3.000 x + 1.000 at x=2.5 is 36.750
```

Sample 5

```Please enter the degree for the polynomial with which you wish to work: 3
Enter the coefficient for the degree 0 term: 1
Enter the coefficient for the degree 1 term: 3
Enter the coefficient for the degree 2 term: 3
Enter the coefficient for the degree 3 term: 1
The polynomial that you entered is 1.000 x^3 + 3.000 x^2 + 3.000 x + 1.000
What do you wish to do with the polynomial?
Enter 1 to compute and display an indefinite integral.
Enter 2 to determine and display the area under the polynomial between two endpoints.
Enter 3 to compute and display the derivative of the polynomial.
Enter 4 to determine and display the derivative of the polynomial at a point.
Enter 5 to determine and determine a value of h for which the difference quotient is close enough to the derivative at a given point.
Enter your choice: 5
Please enter the x-value for which you wish to approximate the derivative using the difference quotient: 2.5
Please enter the desired tolerance: 0.01
A value of h achieving the desired tolerance is 0.0004882812
```
To use the starting files listed below, simply place them in the same directory with your PolyProgram.java file. Starting files:

Note that you are not getting the code for the Poly class!

Program Specification:

• Instantiation and use of pre-defined classes
• Poly
• Keyboard
• String
• DecimalFormat
• Use of expressions with attention to operator precedence
• arithmetic
• relational
• logical
• Use of loops
• for
• while
• Use of conditional statements
• if/else
• Object parameters
• Comparison of floating point numbers using a tolerance
• Creation and use of helper methods to simplify problem solving
• Program testing and discovery

Program Documentation:
Follow the documentation guidelines. You do not have to worry about item 2 for this program, however.

Submission:
Electronic Submission: All files needed to run your program!
Comment and Print Out:

• PolyProgram.java