Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribedProvided code(solve under multiplyTerm method):

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(); } }

5. Polynomial.multiplyTerm (15 points) This is a private helper method used by the multiply function. The polynomial object multiplies each of its terms by an argument term and updates itself. The runtime of this method should be O(n)

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

MySQL/PHP Database Applications

Authors: Brad Bulger, Jay Greenspan, David Wall

2nd Edition

0764549634, 9780764549632

More Books

Students also viewed these Databases questions