Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * File Name: Polynomial.java
- * Email Address: domingu9@pnw.edu
- * Program: This program implements a polynomial class using a LinkedList defined in Java
- * @author Enrique Dominguez
- * @version 11/13/18
- */
- import java.util.LinkedList;
- import java.util.ListIterator;
- 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;
- }
- //
- public double getCoefficient()
- {
- return coefficient;
- }
- public int getExponent()
- {
- //
- return 0;
- }
- public TermNode getNext()
- {
- return null;
- }
- public void setCoefficient(double newCoeff)
- {
- }
- public int setExponent()
- {
- return 0;
- }
- public void setNext(TermNoode newNext)
- {
- }
- // other methos.
- }
- public class Polynomial {
- // instance variables of Polynomial
- // first is the term of the Polynomial with the highest degree
- // except the constant term, only non-zero terms are stored.
- private TermNode first;
- /**Postcondition: Creates a polynomial which is 0.
- * **/
- public Polynomial() {
- first = new TermNode(0,0, null);
- }
- /**Postcondition: 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);
- }
- /** Postcondition: Creates a copy of Polynomial p
- * @param p the Polynomial which is to be copied.
- * **/
- public Polynomial(Polynomial p) {
- TermNode copyHead;
- TermNode copyTail;
- copyHead = new TermNode(p.first.coefficient, p.first.exponent, null);
- copyTail = copyHead;
- while (p.first.next != null) {
- p.first = p.first.next;
- copyTail = new TermNode(p.first.coefficient, p.first.exponent, p.first.next);
- copyTail = copyTail.next;
- }
- first = copyHead;
- }
- /** Postcondition: 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) {
- if (amount == 0)
- {
- return;
- }
- if (exponent > first.exponent)
- {
- first = new TermNode(exponent, amount, first);
- }else if (exponent == first.exponent){
- first.coefficient += amount;
- }else {
- TermNode cur = first;
- while (cur != null)
- {
- cur = cur.next;
- if (exponent > cur.exponent)
- {
- first.next = new
- TermNode(exponent, amount, first.next);
- break;
- }else{
- cur = cur.next;
- }
- }
- }
- }
- /** Postcondition: 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) {
- }
- /** Postcondition: 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) {
- if (exponent > this.function.getExponent()) {
- throw new IllegalArgumentException("Coefficient is greater than teh highest exponent within polynomial");
- } else {
- if (this.function.getExponent() == exponent) {
- return this.function.getCoefficient();
- } else {
- TermNode index = TermNode.expSearch(this.function, exponent);
- if (index != null) {
- return index.getCoefficient();
- } else {
- return 0;
- }
- }
- }
- }
- /** @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 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 null;
- }
- /**@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) {
- return p;
- }
- /** Postcondition: 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) {
- return p;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement