Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PolynomialTester.java /** A class to to test the Polynomial class. */ public class PolynomialTester { public static void main(String[] args) { Polynomial p = new

PolynomialTester.java

/** A class to to test the Polynomial class. */ public class PolynomialTester { public static void main(String[] args) { Polynomial p = new Polynomial(new Term(-10, 0)); p.print(); System.out.println(" Expected: - 10.0"); p.add(new Polynomial(new Term(-1, 1))); p.print(); System.out.println(" Expected: - 1.0x - 10.0"); p.add(new Polynomial(new Term(9, 7))); p.print(); System.out.println(" Expected: 9.0x^7 - 1.0x - 10.0"); p.add(new Polynomial(new Term(5, 10))); p.print(); System.out.println(" Expected: 5.0x^10 + 9.0x^7 - 1.0x - 10.0");

Polynomial q = p.multiply(p); q.print(); System.out.println(" Expected: 25.0x^20 + 90.0x^17 + 81.0x^14 - 10.0x^11 - 100.0x^10 - 18.0x^8 - 180.0x^7 + 1.0x^2 + 20.0x + 100.0"); } }

Polynomial.java

import java.util.LinkedList; import java.util.ListIterator; /** A class to represent a polynomial. */ public class Polynomial { . . .

/** Constructs an empty polynomial */ public Polynomial() { . . . }

/** Constructs a new polynomial with the given term @param t the term to initialize the polynomial with */ public Polynomial(Term t) { . . . }

/** Adds the polynomial such that the terms are in sorted order from highest power to lowest @param p the polynomial to add */ public void add(Polynomial p) { . . .

}

/** Multiplies the given polynomial with this one and returns the result @param p the polynomial to multiply @return this * p */ public Polynomial multiply(Polynomial p) { . . .

}

/** Prints the polynomial "nicely" so that it reads from highest term to lowest and doesn't have a leading "+" if the first term is positive. */ public void print() { . . .

Term.java } }

/** A class to represent an algebraic term. */ public class Term { private double coefficient; private int power;

/** A constructor to initialize a single term with a given coefficient and power @param coefficent the coefficient @param power the power */ public Term(double coefficient, int power) { this.coefficient = coefficient; this.power = power; }

/** @return the coefficient */ public double getCoefficient() { return coefficient; }

/** @return the power */ public int getPower() { return power; }

/** Multiplies two coefficient together and returns the result @param t the other term @return this * t as a term */ public Term multiply(Term t) { return new Term(coefficient * t.coefficient, power + t.power); }

/** Adds the term to this term if the powers are the same @param t the term to attempt to add */ public void addIfSamePower(Term t) { if (t.power == power) { coefficient += t.coefficient; } }

/** Returns a string representation of the term with a ^ representing the exponent @return a string representation of a term */ public String toString() { if (power == 0) { return Math.abs(coefficient) + ""; } else if (power == 1) { return Math.abs(coefficient) + "x"; } else { return Math.abs(coefficient) + "x^" + power; } } }

image text in transcribed

IN JAVA comment code Write a class Polynomial that stores a polynomial such as p(x) 5x0+9x7-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 Supply methods to add, multiply, and print polynomials. Supply a constructor that makes a polynomial from a single term. 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 Polynomia (new Term(5, 10) Then compute p(x) x p(x) compute px)x plx) Polynomial q p.multiply(p); q.print()

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

Makers And Takers The Rise Of Finance And The Fall Of American Business

Authors: Rana Foroohar

1st Edition

0553447238, 978-0553447231

Students also viewed these Databases questions