Question
ILL GIVE YOU 5 STARS FOR YOUR HELP. (Please provide comments on each step so I know whats going on. thank you ) Programming Assignment
ILL GIVE YOU 5 STARS FOR YOUR HELP. (Please provide comments on each step so I know whats going on. thank you )
Programming Assignment #1
"Polynomials Using the ArrayList Class"
The Term Class
Create a class to represent a term in an algebraic expression. As defined here, a term consists of the variable x, 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, respectively. 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 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.
To get credit for this assignment, your Polynomial class must use an ArrayList-of-Term as its principal data structure
To get credit, you must not declare any additional instance variables or methods and must not modify any of the method declarations in any way (not necessary).
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)
Note that the Polynomial class has:
A constructor that creates an empty Polynomial (a Polynomial with 0 terms)
A method with signature
public void addTerm(int coefficient, int exponent)
which creates a Term and places it in its proper position in the Polynomial
The terms of the Polynomial are stored in ascending order by exponent (see above) so you never have 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 Polynomial p to this Polynomial 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 + 4x^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 must always be printed with terms collected
The Test Class
Test class PolynomialTester.java is available online. Make no changes to the test class.
Javadoc Comments
Make sure you properly document the Polynomial and Term classes and all public methods, including all parameters, and all return statements. Then run the javadoc utility to generate the help pages (Polynomial.html and Term.html).
For this assignment, the Javadoc comments and html files will count as a separate grade!
What to Upload to Moodle
Upload a zip file containing your NetBeans project folder and the output
See the Submitting Your Assignments document online
Due Date Tuesday, January 29th
Helpful Hints! Divide and Conquer!
Begin by coding the Term class and the Polynomial methods addTerm and toString. For now, have addTerm simply add each new Term at the end of the list. This will enable you to run the program and verify that your Term class is correct and that new Terms are indeed being 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
After doing the above, 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.
Lastly, code the collectTerms method. An algorithm will be discussed in class.
Other Considerations
Also make sure your classes contain proper internal documentation and meet all the style considerations discussed in class and online in Unit 1.
import java.util.ArrayList;
public class Polynomial
{
// do NOT modify this instance variable declaration
private ArrayList polynomial ;
public Polynomial() // constructor
{
// TO DO: write body of this constructor
}
public void addTerm(int coeff, int expo)
{
// TO DO: write body of this method
}
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
}
public Polynomial polyMultiply(Polynomial p)
{
// TO DO: write body of this method
// temporary return so class skeleton will compile and run
return null ;
}
public Polynomial polyAdd(Polynomial p)
{
// TO DO: write body of this method
// temporary return so class skeleton will compile and run
return null ;
}
} // end of Polynomial class definition =========================
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 + " p2 = " + p2 + " p1 + p2 = " +
nullTest);
nullTest = p1.polyMultiply(p2);
System.out.println(" p1 = " + p1 + " p2 = " + p2 + " p1 * 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);
}
}
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