According to the assignment#1 we are required to create a class to represent
polynomials of the following form:
anxn + . . . a2x2
+ a1x + a0 where maximum degree is 5.
The program is a simple one. I used the algorithm from the assignment
requirement for addition, subtraction and multiplication by constant. My
program was designed to enable mathematical operations of two polynomials
with different degrees, i.e. up to 5. It can also check the result of mathematical
operations and return the appropriate polynomial. The whole program
is composed of two classes: class Polynomial and class test.
Description of the Algorithm and Overview of the Program
The problem is solved by implementing two classes; namely Polynomial and test.
The Polynomial Class
The polynomial class has two constructor methods. The first is a function
that initializes an object with coefficients for a polynomial. The second
constructor function takes no arguments and initializes the polynomial
to some default value, representing the 0 polynomial.
The Polynomial class is responsible from mathematical operations andable
to receive coefficients data from outside and construct polynomial objects.
The mathematical operation functions are addition of two polynomials, subtraction
of two polynomials and multiplication of a polynomial by a constant. The
polynomial class has also addtional methods to print the polynomial
on the screen.
Polynomial had two instance variables to store the degree of a polynomial and the array of coefficients respectively.
One private method is provided to perform addition or subtraction operations of coefficients of two polynomials. I put addition and subtraction in one method because they are basically the same operation and mostly are handled in the same way.
The class also has following public methods.
addPol Method: The method
accepts an argument of type Polynomial, add it to the local polynomial
and return a result of type Polynomial:
anxn + . . . a2x2
+ a1x + a0
+ bnxn + . . . b2x2 + b1x
+ b0
= (an+bn)xn + . . . (a2+b2)x2
+ (a1+b1)x + (a0+b0)
subPol method. The method performs
subtraction of two polynomials.
anxn + . . . a2x2
+ a1x + a0
- bnxn + . . . b2x2 + b1x
+ b0
= (an-bn)xn + . . . (a2-b2)x2
+ (a1-b1)x + (a0-b0)
multPol method. The method performs
multiplication of a polynomial by a constant:
c * anxn + . . . a2x2 +
a1x + a0
= c*anxn + . . . c*a2x2
+ c*a1x + c*a0
ToString Method Print the current
polynomial to the screen on the command line.
readPolCof MEthod: Read as many
coefficient as the degree of the polynomial plus one from the command line.
Test Class
Test class prompts user for inputs. Test class interprets
the user inputs to perform polynomial operations. Based on given operation
it calls the Polynomial object methods. The followings are basic operations
test class support;
parseInput Method perform user interaction
mentioned above.
R(X): Read polynomial called X and make an instance of polynomial
class. Then calls readPolCof method of the instance object.
X+Y: Perform additions of two polynomials called X and
Y respectively. The result printed on the screen.
X-Y: Perform subtraction of two polynomials called X and
Y respectively. The result printed on the screen.
c*X : Perform constant multiplication with polynomial X.
val(X): Print the polynomial X on the screen.
q or Q: Exit