This need to be done in java using linked list public class Polynomial { // this
ID: 3602398 • Letter: T
Question
This need to be done in java using linked list public class Polynomial { // this is a nested static class, it defines a type // all the instance varaibles can be access directly inside Polynomial class even though they have // private modifier private static class TermNode{ private double coefficient; private int exponent; private TermNode next; public TermNode(int exp, double coeff, TermNode nextTerm ) { coefficient= coeff; exponent = exp; next = nextTerm; } } // instance variables of Polynomial // first is the term of the Polynomial with the highest degree private TermNode first; /**Postcondition:</b> Creates a polynomial which is 0. * **/ public Polynomial() { first = new TermNode(0,0, null); } /**Postcondition:</b> Creates a polynomial which has a single term a0*x^0 * @param a0 The value to be set as the coefficient of the constant (x^0) term. * **/ public Polynomial(double a0) { first = new TermNode(0,a0,null); } /**<b>Postcondition:</b> Creates a copy of Polynomial p * @param p the Polynomial which is to be copied. * **/ public Polynomial(Polynomial p) { } /**<b>Postcondition:</b> Adds the given amount to the coefficient of the specified exponent. * @param amount The amount to be added to the coefficient. * @param exponent The degree of the term whose coefficient is to be modified. * (1) Note that the exponent can be arbitrary * (2) If you want, you can assume the amount is not 0, however, it is possible that * after you add the amount, the coefficient becomes 0, in which case, you should delete the TermNode * **/ public void add_to_coef(double amount, int exponent) { } /**<b>Postcondition:</b> Sets the coefficient of a specified term to a specified value. * @param coefficient The new value of the coefficient. * @param exponent The degree of the term whose coefficient is to be modified. * (1) Note that the exponent can be arbitrary * (2) The coefficient may be 0 * **/ public void assign_coef(double coefficient, int exponent) { } /** <b>Postcondition:</b> Returns coefficient at specified exponent of this polynomial. * @param exponent The exponent of the term whose coefficient is sought. * @return The coefficient of the term. * @throws Exception if the degree of the activating polynomial is less than that of the requested term. * **/ public double coefficient(int exponent) { } /** @return The value of this Polynomial with the given value for the variable x. * @param x The value at which the Polynomial is to be evaluated. * using Horner's method to evaluation * see the link here * https://en.wikipedia.org/wiki/Horner%27s_method * ***/ public double eval(double x) { TermNode t = first; double sum = t.coefficient; int curExponent = t.exponent; while(t.next!=null) { } return sum; } /**@return True if p and this polynomial is same * @param p The polynomial to be tested for equality. * */ public boolean equals (Object obj) { if(obj instanceof Polynomial) { } return false; } /**@return Returns a string representing the polynomial expression with coefficients displayed to the tenths place, * omitting any coefficients that are zero. * If all coefficients are 0, then the zero function is reported. * **/ public String toString() { } /**@return Returns a Polynomial that is the sum of p and this Polynomial. * @param p The Polynomial to be added to the activating Polynomial. * **/ public Polynomial add(Polynomial p) { } /**<b>Postcondition:</b> Returns a new polynomial obtained by multiplying this term and p. For example, if this polynomial is 2x^2 + 3x + 4 and p is 5x^2 - 1x + 7, then at the end of this function, it will return the polynomial 10x^4 + 13x^3 + 31x^2 + 17x + 28. @param p The polynomial to be multiplied. @return The product of the activating Polynomial and p. **/ public Polynomial multiply(Polynomial p) { } }
Explanation / Answer
// Java code for Linked List implementation
import java.util.*;
public class Test
{
public static void main(String args[])
{
// Creating object of class linked list
LinkedList<String> object = new LinkedList<String>();
// Adding elements to the linked list
object.add("A");
object.add("B");
object.addLast("C");
object.addFirst("D");
object.add(2, "E");
object.add("F");
object.add("G");
System.out.println("Linked list : " + object);
// Removing elements from the linked list
object.remove("B");
object.remove(3);
object.removeFirst();
object.removeLast();
System.out.println("Linked list after deletion: " + object);
// Finding elements in the linked list
boolean status = object.contains("E");
if(status)
System.out.println("List contains the element 'E' ");
else
System.out.println("List doesn't contain the element 'E'");
// Number of elements in the linked list
int size = object.size();
System.out.println("Size of linked list = " + size);
// Get and set elements from linked list
Object element = object.get(2);
System.out.println("Element returned by get() : " + element);
object.set(2, "Y");
System.out.println("Linked list after change : " + object);
}
}
// Java code for Linked List implementation
import java.util.*;
public class Test
{
public static void main(String args[])
{
// Creating object of class linked list
LinkedList<String> object = new LinkedList<String>();
// Adding elements to the linked list
object.add("A");
object.add("B");
object.addLast("C");
object.addFirst("D");
object.add(2, "E");
object.add("F");
object.add("G");
System.out.println("Linked list : " + object);
// Removing elements from the linked list
object.remove("B");
object.remove(3);
object.removeFirst();
object.removeLast();
System.out.println("Linked list after deletion: " + object);
// Finding elements in the linked list
boolean status = object.contains("E");
if(status)
System.out.println("List contains the element 'E' ");
else
System.out.println("List doesn't contain the element 'E'");
// Number of elements in the linked list
int size = object.size();
System.out.println("Size of linked list = " + size);
// Get and set elements from linked list
Object element = object.get(2);
System.out.println("Element returned by get() : " + element);
object.set(2, "Y");
System.out.println("Linked list after change : " + object);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.