CSC-207 Data Structures

MidTerm Exam, October 15, 2001

Dr. Leipold

 

NAME: __________________________

 

Please print this page, sign your name in the space above and hand it in with your exam.

 

 

A polynomial is a series of terms added together. Each term has a coefficient (a multiplier) and a power of a variable, say x. Here are some examples of polynomials:

 

a)         4x5 + 7x3 – x2 + 9

 

b)         -3x7 + 6x5 – 2x4 + 4x2 – 3x + 2

 

Notice that not all powers of x need to be present, but they can be “forced” to be present by making the coefficients zero. For example the first polynomial above could be written as:

 

            4x5 + 0x4 + 7x3 – x2 + 0x + 9x0

 

You are to write a specification and a class structure to handle a data structure that implements a polynomial. Before you start though, you must think. What are the operations that need to be done on the polynomial? Your book offers some and I will offer others.

 

Operations:

-       degree ( )

-       getCoefficient(power)

-       changeCoefficient(newCoefficient, power)

-       addPolynomial(polynomial)

-       subtractPolynomial(polynomial)

 

When writing the specification for these operations, you can follow those in your textbook for the first three and then write your own for the last two operations.

 

Now you must implement a class and data structure (actual Java code) to describe and include the operations for a general polynomial. First of all, if you think about it, a polynomial can be represented by an integer array that gives only the coefficients. For example the first polynomial example above could be placed into an array that looks like this

 

9

0

-1

7

0

4

 

 

 

 

 

Notice that the 9 is in position 0 in the array and is the coefficient for x0. The 0 in position 1 in the array is the coefficient for x1, etc.

 

As part of your exam, write down the array representation for the polynomial example given in b) above.

 

 

 

 

 

 

 

 

 

 

 

 

Now you must implement the class and the operations. Do not forget to include a constructor method to initialize the array to some set of nice values, say all zeroes.

 

Now think about the code you will have to write to implement the operations. How will you determine the degree of a polynomial? How will you get or change a coefficient? How will you add or subtract two polynomials? All of this code must be included in the class definition.

 

Also write a main program that calls each of the operations for a polynomial at least once.

 

You do not have to get it all working – but you do have to run both the main program and the class through the Java compiler to get rid of as many errors as possible. Attach your specifications, and listings of the class file and the main program to this page before handing it in. Remember that I will be looking for proper specifications, proper comment statements etc. So be thorough!!!