Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class Polynomial that stores a polynomial such as: p(x) = 5x^10 + 9x^7 -

ID: 3861840 • Letter: W

Question

Write a class Polynomial that stores a polynomial such as: p(x) = 5x^10 + 9x^7 - x -10 as a linked list of terms. A Term contains the coefficient and the power of x. For example, you would store p(x) as a LinkedList with 4 elements, each of which is an instance of a Term class (5,10), (9,7), (-1,1),(-10,0) Supply a constructor that makes a polynomial from a single term. Supply methods to add, multiply, and print polynomials.For example, the polynomial p can be constructed as Polynomial p = new Polynomial(new Term(-10, 0)); p.add(new Polynomial(new Term(-1, 1))); p.add(new Polynomial(new Term(9, 7))); p.add(new Polynomial(new Term(5, 10))); Then compute p(x) x p(x) as Polynomial q = p.multiply(p); q.print(); Include a JUnit test for each method that you implement.You should turn in four files:Polynomial.java PolynomialTest.java Term.java TermTest.java

Explanation / Answer

//================================== Term.java =============================

public class Term implements Comparable<Term> {
   private int coficient;
   private int power;
  
   public Term(int c ,int p){
       coficient = c;
       power = p;
   }
   public int getCoficient() {
       return coficient;
   }
   public void setCoficient(int coficient) {
       this.coficient = coficient;
   }
   public int getPower() {
       return power;
   }
   public void setPower(int power) {
       this.power = power;
   }
   public String toString(){
       String str ="";
       if(coficient==0)return "0";
       if(Math.abs(coficient)>1)
           str += ""+Math.abs(coficient);
       if(Math.abs(power)>1)
           str += "x^"+power;
       else if(Math.abs(power)==1)
           str += "x";
       return str;
   }
   @Override
   public int compareTo(Term t) {
       if(t.getPower()>getPower()) return 1;
       else if(t.getPower() < getPower()) return -1;
       else
           return 0;
   }
  
}

//============================ Polynomial.java ========================


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Polynomial {
  
   List<Term> terms;
   public Polynomial(){
       terms = new ArrayList<Term>();
   }
  
   public Polynomial(Term t){
       terms = new ArrayList<Term>();
       terms.add(t);
       Collections.sort(terms);
   }
  
   public void add(Term t){
       for(int i =0;i<terms.size();i++){
           Term t1 = terms.get(i);
           if(t1.getPower()==t.getPower()){
               t1.setCoficient(t1.getCoficient()+t.getCoficient());
               return;
           }
       }
       terms.add(t);
       Collections.sort(terms);
   }
   public Polynomial addWiht(Polynomial p2){
       Polynomial p = new Polynomial();
       for(int i=0;i<terms.size();i++){
           p.add(new Term(terms.get(i).getCoficient(), terms.get(i).getPower()));
       }
       for(int i=0;i<p2.terms.size();i++){
           p.add(new Term(p2.terms.get(i).getCoficient(), p2.terms.get(i).getPower()));
       }
       return p;
   }
   public Polynomial multiply(Polynomial p2){
       Polynomial p = new Polynomial();
       for(int i=0; i<terms.size();i++){
           for(int j=0;j<p2.terms.size();j++){
               Term t = mul(terms.get(i),p2.terms.get(j));
               p.add(t);
           }
       }
       return p;
  
   }
   private Term mul(Term t1,Term t2){
       return new Term(t1.getCoficient()*t2.getCoficient(),t1.getPower()+t2.getPower());
   }
   public void print(){
       System.out.println(toString());
   }
   public String toString(){
       String str ="";
       if(terms.size()>0){
           str += terms.get(0);
           if(terms.get(0).getCoficient()<0){
               str = "-"+str;
           }
       }
       else
           return str;
       for(int i=1;i<terms.size();i++){
           if(terms.get(i).getCoficient()<0)
               str += " - "+terms.get(i).toString();
           else
               str += " + "+terms.get(i).toString();
       }
       return str;
   }
}

//


public class PolynomialDriver {

   public static void main(String[] args) {
       Polynomial p1 = new Polynomial();
       p1.add(new Term(2, 1));
       p1.add(new Term(3,0));
       p1.add(new Term(-1,1));
       p1.add(new Term(-10,0));
       System.out.println("p1 : "+p1);
       Polynomial p2 = new Polynomial();
       p2.add(new Term(3, 1));
       p2.add(new Term(2,0));
       p2.add(new Term(-1,1));
       p2.add(new Term(-10,0));
       System.out.println("p2 : "+p2);
      
      
       System.out.println(" add : " +p1.addWiht(p2));
       System.out.println(" mul : " +p1.multiply(p2));

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote