Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Provided code(solve where says add code): package assignments2018.a2template; import java.math.BigInteger; public class Polynomial { private SLinkedList polynomial; public int size() { return polynomial.size(); } private

image text in transcribedProvided code(solve where says add code):

package assignments2018.a2template;

import java.math.BigInteger;

public class Polynomial { private SLinkedList polynomial; public int size() { return polynomial.size(); } private Polynomial(SLinkedList p) { polynomial = p; } public Polynomial() { polynomial = new SLinkedList(); } // Returns a deep copy of the object. public Polynomial deepClone() { return new Polynomial(polynomial.deepClone()); } /* * TODO: Add new term to the polynomial. Also ensure the polynomial is * in decreasing order of exponent. */ public void addTerm(Term t) { } /**** ADD CODE HERE ****/ // Hint: Notice that the function SLinkedList.get(index) method is O(n), // so if this method were to call the get(index) // method n times then the method would be O(n^2). // Instead, use a Java enhanced for loop to iterate through // the terms of an SLinkedList. /* for (Term currentTerm: polynomial) { // The for loop iterates over each term in the polynomial!! // Example: System.out.println(currentTerm.getExponent()) should print the exponents of each term in the polynomial when it is not empty. } */ public Term getTerm(int index) { return polynomial.get(index); } //TODO: Add two polynomial without modifying either public static Polynomial add(Polynomial p1, Polynomial p2) { Polynomial p3= new Polynomial();

/**** ADD CODE HERE ****/ return null; } //TODO: multiply this polynomial by a given term. private void multiplyTerm(Term t) { /**** ADD CODE HERE ****/ } //TODO: multiply two polynomials public static Polynomial multiply(Polynomial p1, Polynomial p2) { /**** ADD CODE HERE ****/ return null; } //TODO: evaluate this polynomial. // Hint: The time complexity of eval() must be order O(m), // where m is the largest degree of the polynomial. Notice // that the function SLinkedList.get(index) method is O(m), // so if your eval() method were to call the get(index) // method m times then your eval method would be O(m^2). // Instead, use a Java enhanced for loop to iterate through // the terms of an SLinkedList.

public BigInteger eval(BigInteger x) { /**** ADD CODE HERE ****/ return new BigInteger("0"); } // Checks if this polynomial is same as the polynomial in the argument public boolean checkEqual(Polynomial p) { if (polynomial == null || p.polynomial == null || size() != p.size()) return false; int index = 0; for (Term term0 : polynomial) { Term term1 = p.getTerm(index); if (term0.getExponent() != term1.getExponent() || term0.getCoefficient().compareTo(term1.getCoefficient()) != 0 || term1 == term0) return false; index++; } return true; } // This method blindly adds a term to the end of LinkedList polynomial. // Avoid using this method in your implementation as it is only used for testing. public void addTermLast(Term t) { polynomial.addLast(t); } // This is used for testing multiplyTerm public void multiplyTermTest(Term t) { multiplyTerm(t); } @Override public String toString() { if (polynomial.size() == 0) return "0"; return polynomial.toString(); } }

3. Polynomial.add (10 points) This is a static method that adds two polynomials and returns a new polynomial as result. You may use any of the class methods. Be careful not to modify either of the two polynomials. The runtime of this method should be O(ni + n2) where n,n2 are the number of terms in the two polynomials being added. 3. Polynomial.add (10 points) This is a static method that adds two polynomials and returns a new polynomial as result. You may use any of the class methods. Be careful not to modify either of the two polynomials. The runtime of this method should be O(ni + n2) where n,n2 are the number of terms in the two polynomials being added

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Build It For The Real World A Database Workbook

Authors: Wilson, Susan, Hoferek, Mary J.

1st Edition

0073197599, 9780073197593

More Books

Students also viewed these Databases questions

Question

=+j Explain IHRMs role in global HR research.

Answered: 1 week ago

Question

=+j Describe an effective crisis management program.

Answered: 1 week ago