Question: ( my java code is written below for the Polynomial class complete! I just NEED the main method to test all the methods in class!!!!)

( my java code is written below for the Polynomial class complete! I just NEED the main method to test all the methods in class!!!!) l. Write a single main method in JAVA to test ALL the methods and classes. You can read from the keyboard, or from the file. The format of the file should be: first line specifies the maximum degree, then followed by the terms, one per line. You can assume they are in the right order. For example: polynomialA.txt 100 (3,0) (-1, 3) 

-----------------------------------------------------------------------------------------------------------------------

import java.io.*; import java.util.Arrays; import java.lang.Math;

// (1) class is named Polynomial public class Polynomial { // (2) Use array of doubles to store the coefficients so that the coefficient for x^k is stored in the location [k] of the array. int degree = 0; double[] coeff; /* (3) define the following methods: a. constructor with no parameters represents a polynomial 0 */ public Polynomial() { degree = 0; coeff = new double[1]; coeff[0] = 0; } // b. Creates a polynomial with a single x^0 term with coefficient a0 public Polynomial(double a0) { degree=0; coeff = new double[1]; coeff[0]=a0; } // c. Copy constructor for polynomial p public Polynomial(Polynomial p) { degree = p.degree; coeff = new double[degree+1]; for(int i = 0; i <= degree; i++) { coeff[i] = p.coeff[i]; } } // d. Adds the given amount to the coefficient of the specified exponent. public void add_to_coef(double amount, int exponent) { if(exponent > degree) { double[] coeff1 = new double[exponent]; for(int i = 0; i <= degree; i++) { coeff1[i] = coeff[i]; } coeff = coeff1; } coeff[exponent] = coeff[exponent] + amount; }

// e. Sets the coefficient for the specified exponent. public void assign_coef(double coefficient, int exponent) { if(exponent > degree) { double[] coeff1 = new double[exponent]; { for(int i = 0; i <= degree; i++) { coeff1[i] = coeff[i]; } coeff = coeff1; } coeff[exponent] = coefficient; } } // f. Returns coefficient at specified exponent of this polynomial. public double coefficient(int exponent) { if(exponent > degree) return 0; else return coeff[exponent]; } // g. The return value is the value of this polynomial with the given value for the variable x. public double eval(double x) { double value = 0; for(int i = 0; i <= degree; i++) { double prod = 1; for(int j = 0; j < i; j++) { prod = prod * x; } value = value + coeff[i] * prod; } return value; } // h. Returns true if p is a polynomial and it has same terms as this polynomial public boolean equals (Object p) { if(!(p instanceof Polynomial)) return false; Polynomial p1 = (Polynomial)p; if(!(p1.degree == degree)) return false; for(int i = 0; i < degree; i++) { if(!(coeff[i] == p1.coeff[i])) return false; } return true; } // i. return the polynomial as a string like 2x^2 + 3x + 4 public String toString() { String result = ""; for(int i = degree; i >= 0; i--) { if(coeff[i] != 0) { if(i > 0) { result = result + coeff[i] + "x^" +i; result = result + "+"; } else { result = result + coeff[0]; } } } return result; } // j. return a polynomial that is the sum of p and this polynomial public Polynomial add(Polynomial p) { Polynomial ans; if(degree > p.degree) { ans = new Polynomial(this); for(int i = 0; i <= p.degree; i++) { ans.coeff[i] = ans.coeff[i] + p.coeff[i]; } return ans; } else { ans = new Polynomial(p); for(int i = 0; i <= degree; i++) { ans.coeff[i] = ans.coeff[i] + coeff[i]; } return ans; } } // k. Returns a new polynomial after multiplying this term and p public Polynomial multiply(Polynomial p) { Polynomial p1 = new Polynomial(); for(int i = 0; i < p.coeff.length; i++) for(int j = 0; j < this.coeff.length; j++) { p1.add_to_coef(p.coefficient(i) * this.coefficient(j), i+j); } return p1; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!