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

------------------------------------------------------------------------ Here is

ID: 3597943 • Letter: #

Question

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

Here is some of the class code (It is a public Polynomial class with a static nested TermNode class with 3 pieces of data, the double coefficient, integer exponent, and next TermNode link):

public class Polynomial  
{
private TermNode first;

private static class TermNode
{
private double coefficient;
private int exponent;
private TermNode next;
  
public TermNode(double coeff, int exp, TermNode nextTerm)
{
coefficient = coeff;
exponent = exp;
next = nextTerm;
}
}
  
public Polynomial()
{
first = new TermNode(0,0, null);
}
  
public Polynomial(double a0)
{
first = new TermNode(a0,0,null);
}

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;
}

Explanation / Answer

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Polynomial {
private TermNode first;

private static class TermNode {
private double coefficient;
private int exponent;
private TermNode next;

public TermNode(double coeff, int exp, TermNode nextTerm) {
coefficient = coeff;
exponent = exp;
next = nextTerm;
}
}

public Polynomial() {
first = new TermNode(0, 0, null);
}

public Polynomial(double a0) {
first = new TermNode(a0, 0, null);
}

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;
}
  
/**@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() {
TermNode temp = first;
NumberFormat formatter = new DecimalFormat("#.0");
StringBuilder sb = new StringBuilder("");
while(temp != null) {
if (temp.coefficient != 0) {
sb.append(formatter.format(temp.coefficient))
.append("x^").append(temp.exponent).append(" ");
}
temp = temp.next;
}
if(sb == null || sb.toString().equals("")) {
sb.append("0");
}
return sb.toString();
}

/**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) {
TermNode temp = first;
TermNode prev = null;
while(temp != null) {
if (temp.exponent == exponent) {
temp.coefficient += amount;
if (temp.coefficient == 0) {
if (prev!=null) {
prev.next = temp.next;
temp = null;
}
else {
first = null;
}
}
break;
}
temp = temp.next;
}
}
}