# program help needed

kat brooks

Greenhorn

Posts: 2

posted 11 years ago

I am having a problem with program i am trying to finish. This is kind of lengthy so I apologize ahead of time. Here is what the program is supposed to do:

We are restricting ourselves to polynomials that only contain nonnegative, integral powers of x. In this program, you will find the roots of a polynomial of arbitrary, nonnegative degree (the degree is specified by the user). The degree of a polynomial is the highest power of x with a nonzero coefficient. In the example above, the degree of the polynomial is 5.

You will use the secant method to locate a zero of a given polynomial. The secant method uses the following iterative (repeating/looping) equation to identify a zero of a function:

xi + 1 = xi - [f(xi)*(xi - 1 - xi)/(f(xi - 1) - f(xi))]

Thus, you need two starting values of x, x0 and x1, in order to generate subsequent values of x that get closer and closer to a zero for the function. The iterative formula requires that you use the result of the first computation as an input for the second computation. That is, x2 is computed from x1 and x0, while x3 is computed from x2 and x1, and so forth.

but the poly structure is not needed. that is kept in a class by itself that this program feeds into.

Program Execution:

Javadoc for Program 1

Repeat the iterative process described above until:

the user specified number of iterations have been performed

you have identified a zero within the tolerance 0.00001

If a zero was found within the specified tolerance, report the x-coordinate corresponding to that zero. If no zeroes were found within the specified tolerance, report that no zeroes were found.

Call your file Secant.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 two sample method signatures that I expect you to implement and to use:

public static double secant(Poly poly, double x0, double x1, int iter) //returns the result of the secant method

public static Poly getPoly(int degree) //user inputs the coefficients of the powers of x from 0 to degree

The parameters are as follows:

poly is your polynomial,

x0 and x1 are the starting function values for the root finding (that is, evaluate your polynomial at these two points to get your search started),

iter is the number of iterations requested,

and degree is the highest power of x with a nonzero coefficient.

Of course, you should have more methods than just these two.

Other tasks:

Compare doubles to zero and to each other using a reasonable tolerance (use 0.00001).

Format numerical output with the DecimalFormat class.

Obtain needed input from the user using the Keyboard class.

Use at least one while loop and one for loop.

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

while

for

Use of conditional statements

if/else

switch

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.

Any help woudl be appreciated!!

We are restricting ourselves to polynomials that only contain nonnegative, integral powers of x. In this program, you will find the roots of a polynomial of arbitrary, nonnegative degree (the degree is specified by the user). The degree of a polynomial is the highest power of x with a nonzero coefficient. In the example above, the degree of the polynomial is 5.

You will use the secant method to locate a zero of a given polynomial. The secant method uses the following iterative (repeating/looping) equation to identify a zero of a function:

xi + 1 = xi - [f(xi)*(xi - 1 - xi)/(f(xi - 1) - f(xi))]

Thus, you need two starting values of x, x0 and x1, in order to generate subsequent values of x that get closer and closer to a zero for the function. The iterative formula requires that you use the result of the first computation as an input for the second computation. That is, x2 is computed from x1 and x0, while x3 is computed from x2 and x1, and so forth.

but the poly structure is not needed. that is kept in a class by itself that this program feeds into.

Program Execution:

Javadoc for Program 1

Repeat the iterative process described above until:

the user specified number of iterations have been performed

you have identified a zero within the tolerance 0.00001

If a zero was found within the specified tolerance, report the x-coordinate corresponding to that zero. If no zeroes were found within the specified tolerance, report that no zeroes were found.

Call your file Secant.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 two sample method signatures that I expect you to implement and to use:

public static double secant(Poly poly, double x0, double x1, int iter) //returns the result of the secant method

public static Poly getPoly(int degree) //user inputs the coefficients of the powers of x from 0 to degree

The parameters are as follows:

poly is your polynomial,

x0 and x1 are the starting function values for the root finding (that is, evaluate your polynomial at these two points to get your search started),

iter is the number of iterations requested,

and degree is the highest power of x with a nonzero coefficient.

Of course, you should have more methods than just these two.

Other tasks:

Compare doubles to zero and to each other using a reasonable tolerance (use 0.00001).

Format numerical output with the DecimalFormat class.

Obtain needed input from the user using the Keyboard class.

Use at least one while loop and one for loop.

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

while

for

Use of conditional statements

if/else

switch

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.

Any help woudl be appreciated!!

kat brooks

Greenhorn

Posts: 2

posted 11 years ago

what i am having problems with these methods.

public static double secant(Poly poly, double x0, double x1, int iter) //returns the result of the secant method

public static Poly getPoly(int degree) //user inputs the coefficients of the powers of x from 0 to degree

how do i set up the user input to get the Poly file to read them?

so far through the java api i have found that:

int degrees() //returns the largest power with a non-zero coeficient.

double evaluate(double x)//evaluates the poly at the specified location

double getCoeff(int power) //returns the coeffficient of the specified power

void setCoeff(int power, double coeff)// allows the coefficient of tthe specified power to be set.

this helps but it doesn't explain how i set this up to give the poly these values.

public static double secant(Poly poly, double x0, double x1, int iter) //returns the result of the secant method

public static Poly getPoly(int degree) //user inputs the coefficients of the powers of x from 0 to degree

how do i set up the user input to get the Poly file to read them?

so far through the java api i have found that:

int degrees() //returns the largest power with a non-zero coeficient.

double evaluate(double x)//evaluates the poly at the specified location

double getCoeff(int power) //returns the coeffficient of the specified power

void setCoeff(int power, double coeff)// allows the coefficient of tthe specified power to be set.

this helps but it doesn't explain how i set this up to give the poly these values.

Barry Gaunt

Ranch Hand

Posts: 7729

posted 11 years ago

Surely the getPoly(int degree) method has to make use of the Keyboard class. What can the Keyboard class do? Hopefully it can prompt the user and read some strings. The strings can be parsed into floating point numbers and stored in a suitable data structure representing the degree+1 coefficients of the polynomial.

[ September 25, 2005: Message edited by: Barry Gaunt ]

[ September 25, 2005: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch

Getting someone to think and try something out is much more useful than just telling them the answer.