Question

Polynomial Using LinkedList class of Java Language Description: Implement a polynomial class using a LinkedList defined...

                         Polynomial Using LinkedList class of Java Language

Description: Implement a polynomial class using a LinkedList defined in Java


(1) Define a polynomial that has the following methods for Polynomial

a. public Polynomial() 

        POSTCONDITION: Creates a polynomial represents 0

b. public Polynomial(double a0)

        POSTCONDITION: Creates a polynomial has a single x^0 term with coefficient a0

c. public Polynomial(Polynomial p)

        POSTCONDITION: Creates a polynomial is the copy of p


d. public void add_to_coef(double amount, int exponent)

     POSTCONDITION: Adds the given amount to the coefficient of the specified exponent.
     Note: the exponent is allowed to be greater than the degree of the polynomial   
      example: if p = x + 1, after p.add_to_coef(1, 2), p = x^2  + x + 1
          

e.  public void assign_coef(double coefficient, int exponent)
     POSTCONDITION: Sets the coefficient for the specified exponent.
     Note: the exponent is allowed to be greater than the degree of the polynomial   

f. public double coefficient(int exponent) 
     POSTCONDITION: Returns coefficient at specified exponent of this polynomial. 
     Note: the exponent is allowed to be greater than the degree of the polynomial   
     e.g. if p = x + 1; p.coeffcient(3) should return 0

g. public double eval(double x)
     POSTCONDITION: The return value is the value of this polynomial with the given value for the variable x.
     Do not use power method from Math, which is very low efficient 

h. public boolean equals (Object p)

        POSTCONDITION: return true if p is a polynomial and it has same terms as this polynomial

i. public string toString()

        POSTCONDITION: return the polynomial as a string like “2x^2 + 3x + 4”
        
        Important only non-zero terms 

j. public Polynomial add(Polynomial p)
     POSTCONDITION:
                this object and p are not changed   
                return a polynomial that is the sum of   p and this polynomial 


k. public Polynomial multiply(Polynomial p)
     POSTCONDITION: 
     this object and p should not be changed
     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.


 
0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class Polynomial {

        class PolyNode {
                double coeff;
                int degree;
                PolyNode next;

                public PolyNode(double coeff, int degree) {
                        this.coeff = coeff;
                        this.degree = degree;
                }
        }

        private PolyNode head; // points to the first Node of a Polynomial

        public Polynomial() // DO NOT MODIFY THIS CONSTRUCTOR
        {
                head = null;
        }

        public Polynomial(Polynomial p) // a "copy" constructor
        {
                // get the node of list
                PolyNode n = p.head;
                while (n != null) {
                        // when we add, it automatically makes the element head, if there is
                        // only element
                        add_to_coef(n.coeff, n.degree);

                        // go to next element
                        n = n.next;
                }
        }

        public Polynomial(double a0) {
                head = null;
                add_to_coef(a0, 0);
        }
        
        public void assign_coef(double coefficient, int exponent) {
                PolyNode tmp = head;
                
                while(tmp != null) {
                        if(tmp.degree == exponent) {
                                tmp.coeff = coefficient;
                                return;
                        }
                        tmp = tmp.next;
                }
                
                // if node is not found, create new node
                add_to_coef(coefficient, exponent);
        }
        
        public double coefficient(int exponent) {
                PolyNode tmp = head;
                
                while(tmp != null) {
                        if(tmp.degree == exponent) {
                                return tmp.coeff;
                        }
                        tmp = tmp.next;
                }
                
                return 0;
        }
        
        public double eval(double x) {
                double val = 0;
                PolyNode tmp = head;
                
                while(tmp != null) {
                        val += Math.pow(x, tmp.degree) * tmp.coeff;
                        tmp = tmp.next;
                }
                
                return val;
        }

        /**
         * Creates a new Term and Node containing it and inserts it in its proper place
         * in this Polynomial (i.e. in ascending order by exponent)
         */
        public void add_to_coef(double amount, int exponent) {
                if (amount == 0)
                        return;

                // create node
                PolyNode n = new PolyNode(amount, exponent);

                // if it is the first element
                if (head == null) {
                        head = n;
                } else {
                        // if it has same exponent as head
                        if (head.degree == exponent) {
                                head.coeff = (head.coeff + amount);
                                return;
                        } else if (head.degree < exponent) {
                                // it need to be inserted before head
                                n.next = head;
                                head = n;
                                return;
                        }

                        // loop like p will be the previous node, and we will be comparing
                        // p.next inside
                        // keep previous node information is required as we need to add this
                        // node in between
                        PolyNode p = head;
                        while (p.next != null) {

                                if (exponent == p.degree) {
                                        // if same exponent term is there
                                        p.coeff = (p.coeff + amount);
                                        return;
                                } else if (exponent > p.next.degree) {
                                        // loop till we get some lower exponent position
                                        n.next = p.next;
                                        p.next = n;
                                        return;
                                }

                                // move to next
                                p = p.next;
                        }

                        if (p.degree == exponent) {
                                p.coeff = (p.coeff + amount);
                                return;
                        }

                        // add to the last
                        p.next = n;
                }
        }

        String getTerm(double coefficient, int exponent) {
                String s = "+";
                if (coefficient < 0) {
                        s = "";
                }
                if (coefficient == 1 && exponent == 1)
                        return s + "x";
                else if (exponent == 0)
                        return s + String.valueOf(coefficient);
                else if (coefficient == 1)
                        return s + "x^" + String.valueOf(exponent);
                else if (exponent == 1)
                        return s + String.valueOf(coefficient) + "x";
                else
                        return s + String.valueOf(coefficient) + "x^" + String.valueOf(exponent);
        }

        /**
         * Returns a polynomial as a String in this form: x + 3x^2 + 7x^3 + x^5
         */
        public String toString() {
                String s = "";
                PolyNode node = head;
                String seperator = "";
                // for each term, do add
                while (node != null) {
                        s += seperator + getTerm(node.coeff, node.degree);
                        node = node.next;
                        seperator = " ";
                }
                return "(" + s + ")";
        }

        /**
         * Multiply this Polynomial by another Polynomial
         */
        public Polynomial multiply(Polynomial p) {
                Polynomial result = new Polynomial();
                PolyNode p1 = head;
                // traverse for each term of p1
                while (p1 != null) {

                        PolyNode p2 = p.head;

                        // traverse for each term of p2
                        while (p2 != null) {
                                // add the new term to result polynomial
                                result.add_to_coef(p1.coeff * p2.coeff, p1.degree + p2.degree);

                                // advance p2
                                p2 = p2.next;
                        }

                        // advance p1
                        p1 = p1.next;
                }
                return result;
        }

        /**
         * Add this Polynomial and another Polynomial
         */
        public Polynomial add(Polynomial p) {

                // create a copy of original polynomial
                // we will add new node to directly this.
                Polynomial result = new Polynomial(this);

                PolyNode p1 = p.head;
                // our add Term method, add the coefficients, if exponent is same,
                // else it will create a new term
                while (p1 != null) {
                        result.add_to_coef(p1.coeff, p1.degree);
                        p1 = p1.next;
                }

                return result;
        }

        /**
         * subtract this Polynomial and another Polynomial
         */
        public Polynomial minus(Polynomial p) {

                // create a copy of original polynomial
                // we will add new node to directly this.
                Polynomial result = new Polynomial(this);

                PolyNode p1 = p.head;
                // our add Term method, add the coefficients, if exponent is same,
                // else it will create a new term
                // we are changing the sign of coefficient here, so that it can be added
                while (p1 != null) {
                        result.add_to_coef(-1 * p1.coeff, p1.degree);
                        p1 = p1.next;
                }

                return result;
        }

        public boolean equals(Polynomial p) {
                PolyNode p1 = this.head;
                PolyNode p2 = p.head;

                while (p1 != null || p2 != null) {
                        if ((p1.coeff != p2.coeff) || (p1.degree != p2.degree)) {
                                return false;
                        }
                        p2 = p2.next;
                        p1 = p1.next;
                }
                return true;
        }
}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Add a comment
Know the answer?
Add Answer to:
Polynomial Using LinkedList class of Java Language Description: Implement a polynomial class using a LinkedList defined...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can anyone add the 1) add_to_coef, add, multiply, coefficient, eval and equals method in the main...

    Can anyone add the 1) add_to_coef, add, multiply, coefficient, eval and equals method in the main function..so can take it from text file.... or 2) make main function with user input for all methods mentioned in the program. import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; public class Polynomial { //array of doubles to store the coefficients so that the coefficient for x^k is stored in the location [k] of the array. double coefficients[]; static int size; //fube the following...

  • Create a class to represent a term in an algebraic expression. As defined here, a term...

    Create a class to represent a term in an algebraic expression. As defined here, a term consists of 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. Your class...

  • The second lab continues with the Polynomial class from the first lab by adding new methods...

    The second lab continues with the Polynomial class from the first lab by adding new methods for polynomial arithmetic to its source code. (There is no inheritance or polymorphism taking place yet in this lab.) Since the class Polynomial is designed to be immutable, none of the following methods should modify the objects this or other in any way, but return the result of that arithmetic operation as a brand new Polynomial object created inside that method. public Polynomial add(Polynomial...

  • develop a java program using linkedlist to implement priority queue ADT with following operations: void insert(double...

    develop a java program using linkedlist to implement priority queue ADT with following operations: void insert(double priority); int delete_min(); public boolean isEmpty(); double findMin(); double deleteMin() EmptyPQException()

  • Java Programming. please give me answer and explain in details. Question 1 (c) (4 marks] For...

    Java Programming. please give me answer and explain in details. Question 1 (c) (4 marks] For each of the methods equals and hashCode in the following class, explain whether or not they are implemented correctly (with respect to their specification in the object class and any additional specification in this class). If they are not implemented correctly. show how they may be rewritten to address the problems that you have identified. *An immutable representation of a term in a polynomial....

  • JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList...

    JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList Class (some methods included). Remember, you will use the LinkedList Class that we developed in class not Java’s LinkedList Class. You will add the following method to the LinkedList Class: printEvenNodes – this is a void method that prints Nodes that have even indices (e.g., 0, 2, 4, etc). Create a LinkedListDemo class. Use a Scanner Class to read in city names and store...

  • a Java code Complete the provided code by adding a method named sum() to the LinkedList...

    a Java code Complete the provided code by adding a method named sum() to the LinkedList class. The sum() method should calculate the sum of all of the positive numbers stored in the linked list. The input format is the number of items in the list, followed by each of the items, all separated by spaces. Construction of the linked list is provided in the template below. The output should print the sum of the positive values in the list....

  • Horner: Given the coefficients of a polynomial a0, a1, . . . , an, and a...

    Horner: Given the coefficients of a polynomial a0, a1, . . . , an, and a real number x0, find P(x0), P′ (x0), P′′(x0), P(3)(x0), . . . , P(n) (x0) Sample input representing P(x) = 2 + 3x−x 2 + 2x 3 , x0 = 3.5: 3 2 3 -1 2 3.5 the first number is the degree of the polynomial (n), the coefficients are in order a0, a1, . . . , an, the last number is x0....

  • java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then...

    java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then output comes like this 5 4 3 2 1 Stack is empty. here is the code that i'm going to use class Stack<T> implements Iterator<T> {    LinkedList<T> list;       public Stack() {        list = new LinkedList<T>();    }       public boolean isEmpty() {        return list.isEmpty();   ...

  • Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to...

    Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT