Question
Create a class to represent a term in an algebraic expression. As defined here, a term consists of an integer coefficient and a nonnegative integer
Create a class to represent a term in an algebraic expression. As defined here, a term consists of an integer coefficient and a nonnegative integer exponent. E.g.
in the term 4x2, the coefficient is 4 and the exponent 2
in -6x8, the coefficient is -6 and the exponent 8
Your class will have a constructor that creates a Term object with a coefficient and exponent passed as parameters, and accessor methods that return the coefficient and the exponent. Your class will also override toString to return a Term in this general form:
ax^b where a is the coefficient and b the exponent
with these special cases:
Case | Returns |
a = 1 | x^b |
b = 1 | ax |
b = 0 | a |
a = 1, b = 1 | x |
You may assume that the coefficient will not be zero.
The Generic Node Class
This has already been done, declared as an inner class in Polynomial. Do not modify it in any way. Note that it will not compile until the Term class is defined.
The Polynomial Class
A skeleton of the Polynomial class you are to use is on the class web site. All you have to do is write the bodies of the methods.
Do not declare any new instance variables or methods and do not modify any of the method declarations.
As defined here, a polynomial is a sequence of terms. E.g.
1.
2.
3.
The terms of polynomial 1 are (3,2), (4,4) and (1,6). The terms of polynomial 2 are (2,0), (5,2), (6,3) and (2,7). Polynomial 3 has only one term (4,10)
To receive credit for this assignment, you must use no data structures other than your own programmer-defined (you being the programmer) singly-linked list of generic Nodes
Note that the Polynomial class has:
A constructor that creates a null Polynomial (a Polynomial with 0 terms)
A copy constructor that creates a new Polynomial as an exact duplicate of an existing one (aka: a deep copy)
A method with signature
public void addTerm(int coefficient, int exponent)
which creates a Term and places it in its proper position in the linked list
The terms on the list are stored in ascending order by exponent (see III., above) so there is never a need to sort the list. Terms with the same exponent may be stored in any order but will appear after all terms with lesser exponents and before all terms with greater exponents
A method with signature
public Polynomial polyAdd(Polynomial p)
which adds this Polynomial to p and returns the sum
A method with signature
public Polynomial polyMultiply(Polynomial p)
which multiplies this Polynomial by p and returns the product
An overridden toString method that returns a String representation of a polynomial as a sum of terms. For example polynomial 1 above would have this String representation:
3x^2 + 48x^4 + x^6
A private method with signature
private void collectTerms()
which collects like terms of this Polynomial. E.g.
Before: x^2 + 3x^2 + 2x^2 + 3x^3 + 5x^4 + 2x^4
After: 6x^2 + 3x^3 + 7x^4
Polynomials should always be printed with like terms collected.
Helpful Hints! Divide and Conquer!
Begin by coding the Term class and Polynomial methods toString and addTerm. For now, have addTermsimply add each new Term at the head of the list or, if you prefer, at the end. This will enable you to run the program and verify that your Term class is correct and that new Terms are indeed being created and added to the list.
Code the polyAdd and polyMultiply methods. These are fairly straightforward and once completed, along with the temporary version of addTerm, you will have a majority of the credit in the bank. J
Modify the addTerm method so that each new Term is inserted in its proper place in the list. An algorithm will be discussed in class. Note that none of the other methods will need to be modified in any way. More credit in the bank.
Code the collectTerms method. An algorithm will be discussed in class.
Code the copy constructor.
Dont forget: With linked lists, crayons are more important than keyboards.
-------------------------------------------------------------------------------------------
Skeleton for Polynomial.java:
/** * A class to represent a polynomial as a sum of terms where each term has * the same variable, an int coefficient, and a nonnegative int exponent. * Polynomials may be added and multiplied */ public class Polynomial { private Node head ; // points to the first Node of a Polynomial /** * Default constructor creates a Polynomial with no terms */ public Polynomial() // DO NOT MODIFY THIS CONSTRUCTOR { head = null ; } /** * Creates a "deep copy" of a given Polynomial. I.e. a new Polynomial with * identical terms * @param p Polynomial to be copied */ public Polynomial(Polynomial p) // a "copy" constructor { // TO DO: write body of this method } /** * Creates a new Term and Node containing it and inserts it in its proper * place in this Polynomial (i.e. in ascending order by exponent) * @param coeff the coefficient of the new Term * @param expo the exponent of the new Term */ public void addTerm(int coeff, int expo) { // TO DO: write body of this method } /** * Returns a polynomial as a String in this form: x + 3x^2 + 7x^3 + x^5 * @return the polynomial as a String */ public String toString() { // TO DO: write body of this method // temporary return so class skeleton will compile and run return "Hi Mom!" ; } // collect terms of a Polynomial object. I.e. replace all terms having the // same exponent with a single term which is their sum private void collectTerms() { // TO DO: write body of this method } /** * Multiply this Polynomial by another Polynomial * @param p the other Polynomial * @return the Polynomial product */ public Polynomial polyMultiply(Polynomial p) { // TO DO: write body of this method // temporary return so class skeleton will compile and run return null ; } /** * Add this Polynomial and another Polynomial * @param p the other Polynomial * @return the Polynomial sum */ public Polynomial polyAdd(Polynomial p) { // TO DO: write body of this method // temporary return so class skeleton will compile and run return null ; } // Node class definition - DO NOT MODIFY! class Node{ private E info ; // each node stores an object of the // type-parameter class... private Node next ; // ...and a pointer to another node // Node Constructor // parameter x is an object of the type-parameter class Node(E x) { info = x; // set info portion to parameter passed next = null; // not necessary, null is default value } } // end of Node class definition ============================ } // end of Polynomial class definition =========================
-------------------------------------------------------
PolynomialTester.java
public class PolynomialTester { public static void main(String[] args) { Polynomial p1 = new Polynomial(); Polynomial p2 = new Polynomial(); Polynomial p0 = new Polynomial(); Polynomial nullTest = p1.polyAdd(p2); System.out.println(" p1 = " + p1 + ", np2 = " + p2 + "\tp1 + p2 = " + nullTest); nullTest = p1.polyMultiply(p2); System.out.println(" p1 = " + p1 + ", np2 = " + p2 + "\tp1 * p2 is " + nullTest); p1.addTerm(5, 2); p1.addTerm(4, 5); p1.addTerm(3, 3); p1.addTerm(1, 2); p1.addTerm(5, 6); p2.addTerm(3, 8); p2.addTerm(2, 5); p2.addTerm(1, 2); p0.addTerm(1, 2); p0.addTerm(5, 0); p0.addTerm(4, 1); System.out.println(" p0 = " + p0); Polynomial p3 = p1.polyAdd(p2); System.out.println(" p1 = " + p1 + " p2 = " + p2 + " p1+p2 = " + p3); Polynomial p4 = p1.polyMultiply(p2); System.out.println(" p1 = " + p1 + " p2 = " + p2 + " p1*p2 = " + p4); Polynomial p5 = p2.polyMultiply(p2); System.out.println(" p2 = " + p2 + " p2*p2 = " + p5); Polynomial p6 = p0.polyMultiply(p2); System.out.println(" p0 = " + p0 + " " + "p2 = " + p2 + " p0*p2 = " + p6); Polynomial p7 = p0.polyAdd(p2); System.out.println(" p0 = " + p0 + " " + "p2 = " + p2 + " p0+p2 = " + p7); p1 = p1.polyAdd(p2); System.out.println(" After p1 = p1+p2 p1 = " + p1); p2 = p2.polyMultiply(p2); System.out.println(" After p2 = p2*p2 p2 = " + p2); // Testing copy constructor Polynomial pCopy = new Polynomial(p1) ; System.out.println(" After copy p1 = " + p1); System.out.println("After copy pCopy = " + pCopy); p1.addTerm(10, 4); System.out.println(" After adding 10x^4 to p1, p1 = " + p1); System.out.println("But pCopy is still " + pCopy); } }
------------------------------------
Please be thorough with comments!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started