Question: How do I code the following in my interface, class and JUnit: A method evaluate that takes a double-precision decimal number and returns a double-precision
How do I code the following in my interface, class and JUnit: A method evaluate that takes a double-precision decimal number and returns a double-precision result.
Current Code:
// Polynomial.java (THIS IS THE INTERFACE) public interface Polynomial { //Adds the resulting term to the Polynomial. void addTerm(int coeff, int power); //Removes any and all terms in the Polynomial with the given power. void removeTerm(int power); //Returns the degree of the Polynomial. int getDegree(); //Returns the coefficient of a term with the given power. int getCoefficient(int power); //Returns true if this and the given Polynomial are the same, otherwise false is returned. boolean isSame(Polynomial p); //Add this Polynomial to the given Polynomial and returns the resulting Polynomial. Polynomial add(Polynomial p); }import java.util.ArrayList; /** * This is the Polynomial Implementation class that implements the Polynomial interface. */ public class PolynomialImpl implements Polynomial { //Instance variable private final ArrayList terms; /** * This is the constructor. */ public PolynomialImpl() { terms = new ArrayList(); } /** * This method adds the resulting term to the Polynomial. * @param coeff the coefficient of the term. * @param power the power of the term (the exponent). */ @Override public void addTerm(int coeff, int power) { //Check if the coefficient is not zero (0) if (coeff != 0) { //Create a Term with the given coefficient and power Term newTerm = new Term(coeff, power); //Add newTerm to the Polynomial. addTerm(newTerm, 0); } } /** * This method is the helper method for addTerm(int coeff, int power) method. * * @param newTerm new term to add * @param termNum term index in the terms list */ private void addTerm(Term newTerm, int termNum) { if (termNum >= terms.size()) { // Add term at termNum in the terms list this.terms.add(termNum, newTerm); } else { // Check if term at termNum in terms list has power greater than the given power if (this.terms.get(termNum).power > newTerm.power) { addTerm(newTerm, termNum + 1); } else if (this.terms.get(termNum).power == newTerm.power) { // Add coefficient of newTerm to the term at termNum this.terms.get(termNum).coeff += newTerm.coeff; // Check if the coefficient of the term at termNum is zero (0) // If it is, then remove the term at termNum if (this.terms.get(termNum).coeff == 0) { this.terms.remove(termNum); } } else { // If control enters this block, it means that a term with the given power // does not exists. Therefore add newTerm at termNum this.terms.add(termNum, newTerm); } } } /** * This method removes any and all terms in the Polynomial with the given power. * @param power the power of the term to remove */ @Override public void removeTerm(int power) { removeTerm(power, 0); } /** * This method is a helper for removeTerm(int power) method. * @param power the power of the term to remove * @param termNum term index in the terms list */ private void removeTerm(int power, int termNum) { // Check if termNum is greater than or equal to terms.size if (termNum >= terms.size()) { return; } else { // Check if the term at termNum is terms list has power greater han the given power if (this.terms.get(termNum).power > power) { removeTerm(power, termNum + 1); } else if (this.terms.get(termNum).power == power) { //Remove term at termNum this.terms.remove(termNum); } } } /** *This method computes the degree of the Polynomial. * @return return of the degree value */ @Override public int getDegree() { //Check if terms list is not empty if (!this.terms.isEmpty()) { return this.terms.get(0).power; } else { throw new IllegalArgumentException("Polynomial has no terms. "); } } /** * This method computes the coefficient of a term with the given power. * @param power the power of the term to find * @return coefficient */ @Override public int getCoefficient(int power) { return getCoefficient(power, 0); } /** * This method is a helper method for getCoefficient(int power) method. * @param power the power of the term to find * @param termNum the term index in the terms list * @return the value of 0 */ private int getCoefficient(int power, int termNum) { //Check if termNum is greater than or equal to terms.size if (termNum >= terms.size()) { return 0; } //Check if term at termNum in terms list has power same as the given power if (this.terms.get(termNum).power == power) { return this.terms.get(termNum).coeff; } else if (this.terms.get(termNum).power > power) { return getCoefficient(power, termNum + 1); } else { return 0; } } /** * This method computes true if this and the given Polynomial are the same, otherwise false. * @param p the Polynomial to compare with this Polynomial * @return when p is 0 or false. */ @Override public boolean isSame(Polynomial p) { //Check if degree of this Polynomial is the same as that of the given Polynomial if (getDegree() == p.getDegree()) { //Check if size terms lost of both this and p are the same if (this.terms.size() == ((PolynomialImpl) p).terms.size()) { return isSame(p, 0); } } //Default return return false; } /** * This method is a helper method for isSame(Polynomial p) method. * @param p the Polynomial to compare with this Polynomial. * @param termNum the term index in the terms list. * @return the p value or false. */ private boolean isSame(Polynomial p, int termNum) { //Check if termNum is greater than or equal to this.terms.size or p.terms.size if (termNum = ((PolynomialImpl) p2).terms.size()) { return; } //Add term at termNum in p2 to p1 p1.addTerm(((PolynomialImpl) p2).terms.get(termNum).coeff, ((PolynomialImpl) p2).terms.get(termNum).power); //Move to the next term add(p1, p2, termNum + 1); } @Override public String toString() { //Check if terms ArrayList is empty if (terms.isEmpty()) { return "0"; } else { //Polynomial in string form StringBuilder sb = new StringBuilder(); //Append all terms for (int i = 0; i import org.junit.Before; import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; /** * This is teh JUnit test for Polynomial. */ public class PolynomialJUnit { //Polynomial objects Polynomial p1; Polynomial p2; /** * This method create Polynomial objects p1 and p2. * @throws Exception "Polynomial has no terms." */ @Before public void setUp() throws Exception { p1 = new PolynomialImpl(); p2 = new PolynomialImpl(); } /** *This method test the addTerm method of the Polynomial class. */ @Test public void testAddTerm() { //Add terms to p1 p1.addTerm(3, 4); assertEquals("3x^4", p1.toString()); p1.addTerm(0, 7); assertEquals("3x^4", p1.toString()); p1.addTerm(-4, 0); assertEquals("3x^4 - 4", p1.toString()); p1.addTerm(2, 1); assertEquals("3x^4 + 2x^1 - 4", p1.toString()); p1.addTerm(-5, 3); assertEquals("3x^4 - 5x^3 + 2x^1 - 4", p1.toString()); //Add terms to p2 p2.addTerm(4, 0); assertEquals("4", p2.toString()); p2.addTerm(2, 2); assertEquals("2x^2 + 4", p2.toString()); p2.addTerm(2, 3); assertEquals("2x^3 + 2x^2 + 4", p2.toString()); p2.addTerm(10, 7); assertEquals("10x^7 + 2x^3 + 2x^2 + 4", p2.toString()); } /** *This method test the removeTerm method of the Polynomial class. */ @Test public void testRemoveTerm() { //Add terms to p2 p2.addTerm(4, 0); p2.addTerm(2, 2); p2.addTerm(2, 3); p2.addTerm(10, 7); //Remove term with power 7 from p2 p2.removeTerm(7); assertEquals("2x^3 + 2x^2 + 4", p2.toString()); //Remove term with power 10 from p2 p2.removeTerm(10); assertEquals("2x^3 + 2x^2 + 4", p2.toString()); } /** *This method test the getDegree method of the Polynomial class. */ @Test public void testGetDegree() { //Add terms to p2 p2.addTerm(4, 0); p2.addTerm(2, 2); p2.addTerm(2, 3); p2.addTerm(10, 7); //Test getDegree assertEquals(7, p2.getDegree()); //Remove term with power 7 from p2 p2.removeTerm(7); assertEquals(3, p2.getDegree()); } /** * This method test the getCoefficient method of the Polynomial class. */ @Test public void testGetCoefficient() { //Add terms to p1 p1.addTerm(3, 4); p1.addTerm(0, 7); p1.addTerm(-4, 0); p1.addTerm(2, 1); p1.addTerm(-5, 3); //Test get coefficient assertEquals(0, p1.getCoefficient(12)); assertEquals(3, p1.getCoefficient(4)); assertEquals(-4, p1.getCoefficient(0)); } /** * This method test the isSame method of the Polynomial class. */ @Test public void testIsSame() { //Add terms to p1 p1.addTerm(3, 4); p1.addTerm(0, 7); p1.addTerm(-4, 0); p1.addTerm(2, 1); p1.addTerm(-5, 3); //Add terms to p2 p2.addTerm(4, 0); p2.addTerm(2, 2); p2.addTerm(2, 3); p2.addTerm(10, 7); //Test isSame method for p1 and p2 assertTrue(p1.isSame(p2)); assertTrue(p2.isSame(p1)); //Add terms to p2 p2.addTerm(11, 7); //Test isSame method for p1 and p2 assertFalse(p1.isSame(p2)); assertFalse(p2.isSame(p1)); } /** * This method test the add method of the Polynomial class. */ @Test public void testAdd() { //Add terms to p1 p1.addTerm(-4, 0); //Add terms to p2 p2.addTerm(4, 0); //Test add method assertEquals("0", (p1.add(p2)).toString()); //Add terms to p1 p1.addTerm(3, 4); p1.addTerm(0, 7); p1.addTerm(2, 1); p1.addTerm(-5, 3); //Add terms to p2 p2.addTerm(2, 2); p2.addTerm(2, 3); //Test add method assertEquals("3x^4 - 3x^3 + 2x^2 + 2x^1", (p1.add(p2)).toString()); } /** * This method test the toString method of the Polynomial class. */ @Test public void testToString() { //Test String assertEquals("0", p1.toString()); //Add term to p1 p1.addTerm(0, 7); //Test toString assertEquals("0", p1.toString()); //Add term to p1 p1.addTerm(0, 1); //Test toString assertEquals("0", p1.toString()); //Add term to p1 p1.addTerm(-11, 0); //Test toString assertEquals("-11", p1.toString()); //Add term to p1 p1.addTerm(-12, 5); //Test toString assertEquals("-12x^5 - 11", p1.toString()); } }


An example of such a polynomial is f(x)=3x 5x+ 2x 4. This polynomial has four terms. The degree of a polynomial is defined as the highest power of the variable in a term. In the above example, the degree of the polynomial is 4. The polynomials we deal with have only positive, integral powers. The coefficients of our polynomials are also integral numbers. Tlvo polynomials are the same if they contain the same terms. Several algebraic operations are possible with polynomials. The simplest one is evaluating the polynomial for a specific value of the variable. For example, for a polynomial f(x)=3x 5x + 2x 4, its value at x=2.5 is defined as: f/2.5) = 3 * (2.5) 5* (2.5) + 2 * (2.5) 4 which is 40.0625. Polynomials can be added together to create a new polynomial. The addition is performed by combining all the terms and adding the coefficients of the terms with the same power. For example 3x 5x + 2x 4 + 2x + 2x + 4 = 3x - 3x + 2x+ 2x. The degree of the sum is the maximum of the degrees of the two polynomials. am What to do . . . . Design an interface Polynomial that defines the above operations. This is your polynomial abstract data type and specifies the contract Polynomial(s) will adhere to. Specifically this interface should have the following method signatures: A method add term that takes a coefficient and a power (both integral numbers) and adds the resulting term to the polynomial. (This will enable you to build a polynomial term-by-term.) It should throw an IzlegalArgumentException if a negative power is passed to it. A method remove term that takes a power and removes any and all terms in the polynomial with that power. A method getDegree that returns the degree of this polynomial. A method geticaefficient that takes a power and returns the coefficient for the term with that power. A method evaluate that takes a double-precision decimal number and returns a double- precision result. A method add that takes another Polynomial object and returns the polynomial obtained by adding the two polynomials. Any implementation should ensure that this method does not mutate either polynomial. The implementation may assume that the given Polynomial is the of the same concrete class as this object; if it is a different class, the method may throw an attegal ArgumentException Now implement this interface in a class Batxnamiatort. Beyond implementing the Polynomial interface, this implementation should have the following features/obey these constraints: . . This class should store the polynomial using the a linked list with nodes. This representation must be implemented by you (i.e. you are not allowed to use existing List classes in Java). . This class should store only terms with non-zero coefficients. This class should store the polynomial terms in decreasing order of their powers. This class should have a constructor with no parameters that creates a polynomial with no terms, i.e. the polynomial 0 (think about how to represent this?). This class should have a second constructor that takes a polynomial as a string, parses it and creates the polynomial accordingly. The string contains the polynomial, with each term separated by a space. The following examples should work with your constructor: "4x^3 +3x^1 -5" . o o "-3x^4 -2x^5 -5 +11x^1" Hint: Break the string into substrings and process. You may find the Scannerand String classes helpful to do this. Furthermore one can convert "23" into the integer 23 by using Integerularsent ("23"). . While you are free to write helper methods, you are not allowed to write any other public methods other than those in the interface and the above two constructors. . This class should include a testring method that returns a string that contains the polynomial. The following examples should help you infer the required format. Note that the spacing and placement in the String representations below are purposeful and not a formatting error: o 5x2 + 4x 2 creates the string "5x^2 +4x^1 -2" o -50x3 + x2 + 3 creates the string "-50x^3 +1x^2 +3" 4x + 2x5 3x2 10 creates the string "2x^5 -3x^2 +4x^1 -10" o . Write tests that thoroughly test this implementation. As always, it is recommended to write the test before completing the ReixaemiatInnt implementation. 3.Hints: How to tackle the assignment Start by creating empty interfaces and classes for polynomials and nodes. Now select a method from the ADT and implement it end-to-end. Write tests for it before writing the implementation, and when your implementation passes your tests, move on to the next method. Tackle the parsing constructor at the end: you should not need it to test other methods. For the add method: one approach is to think about how you can "accumulate the result. There are other approaches; do not feel compelled to follow this line of thinking. NB: These are hints and suggestions. You are not required to follow these suggestions if you are more comfortable with an alternate approach. An example of such a polynomial is f(x)=3x 5x+ 2x 4. This polynomial has four terms. The degree of a polynomial is defined as the highest power of the variable in a term. In the above example, the degree of the polynomial is 4. The polynomials we deal with have only positive, integral powers. The coefficients of our polynomials are also integral numbers. Tlvo polynomials are the same if they contain the same terms. Several algebraic operations are possible with polynomials. The simplest one is evaluating the polynomial for a specific value of the variable. For example, for a polynomial f(x)=3x 5x + 2x 4, its value at x=2.5 is defined as: f/2.5) = 3 * (2.5) 5* (2.5) + 2 * (2.5) 4 which is 40.0625. Polynomials can be added together to create a new polynomial. The addition is performed by combining all the terms and adding the coefficients of the terms with the same power. For example 3x 5x + 2x 4 + 2x + 2x + 4 = 3x - 3x + 2x+ 2x. The degree of the sum is the maximum of the degrees of the two polynomials. am What to do . . . . Design an interface Polynomial that defines the above operations. This is your polynomial abstract data type and specifies the contract Polynomial(s) will adhere to. Specifically this interface should have the following method signatures: A method add term that takes a coefficient and a power (both integral numbers) and adds the resulting term to the polynomial. (This will enable you to build a polynomial term-by-term.) It should throw an IzlegalArgumentException if a negative power is passed to it. A method remove term that takes a power and removes any and all terms in the polynomial with that power. A method getDegree that returns the degree of this polynomial. A method geticaefficient that takes a power and returns the coefficient for the term with that power. A method evaluate that takes a double-precision decimal number and returns a double- precision result. A method add that takes another Polynomial object and returns the polynomial obtained by adding the two polynomials. Any implementation should ensure that this method does not mutate either polynomial. The implementation may assume that the given Polynomial is the of the same concrete class as this object; if it is a different class, the method may throw an attegal ArgumentException Now implement this interface in a class Batxnamiatort. Beyond implementing the Polynomial interface, this implementation should have the following features/obey these constraints: . . This class should store the polynomial using the a linked list with nodes. This representation must be implemented by you (i.e. you are not allowed to use existing List classes in Java). . This class should store only terms with non-zero coefficients. This class should store the polynomial terms in decreasing order of their powers. This class should have a constructor with no parameters that creates a polynomial with no terms, i.e. the polynomial 0 (think about how to represent this?). This class should have a second constructor that takes a polynomial as a string, parses it and creates the polynomial accordingly. The string contains the polynomial, with each term separated by a space. The following examples should work with your constructor: "4x^3 +3x^1 -5" . o o "-3x^4 -2x^5 -5 +11x^1" Hint: Break the string into substrings and process. You may find the Scannerand String classes helpful to do this. Furthermore one can convert "23" into the integer 23 by using Integerularsent ("23"). . While you are free to write helper methods, you are not allowed to write any other public methods other than those in the interface and the above two constructors. . This class should include a testring method that returns a string that contains the polynomial. The following examples should help you infer the required format. Note that the spacing and placement in the String representations below are purposeful and not a formatting error: o 5x2 + 4x 2 creates the string "5x^2 +4x^1 -2" o -50x3 + x2 + 3 creates the string "-50x^3 +1x^2 +3" 4x + 2x5 3x2 10 creates the string "2x^5 -3x^2 +4x^1 -10" o . Write tests that thoroughly test this implementation. As always, it is recommended to write the test before completing the ReixaemiatInnt implementation. 3.Hints: How to tackle the assignment Start by creating empty interfaces and classes for polynomials and nodes. Now select a method from the ADT and implement it end-to-end. Write tests for it before writing the implementation, and when your implementation passes your tests, move on to the next method. Tackle the parsing constructor at the end: you should not need it to test other methods. For the add method: one approach is to think about how you can "accumulate the result. There are other approaches; do not feel compelled to follow this line of thinking. NB: These are hints and suggestions. You are not required to follow these suggestions if you are more comfortable with an alternate approach Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
