[Solved]Test Methods Using Junit Class Import Javaio Import Javautil Import Javatext Import Javama Q37288356
test the methods by using Junitclass
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
// Polynomial ADT
class Polynomial {
// arraylist to store coefficients of polynomial
ArrayList<Integer> p;
public Polynomial() {
p = new ArrayList<Integer>();
}
// function to get degree of polynomial
public int degree() {
return p.size() – 1;
}
// get cofficient of the power
public int getCoefficient(int power) {
if (power < 0 || power > this.p.size() – 1)
throw new IllegalArgumentException(“Invalid power.”);
return this.p.get(power);
}
// change the coefficient of the power to the input
public void changeCoefficient(int coefficient, int power) {
// if power is 0 throws IllegalArgumentException
if (power < 0)
throw new IllegalArgumentException(“Power can’t benegative.”);
// if power is less than current degree of polnomial simply changeit
if (power < this.p.size()) {
this.p.set(power, coefficient);
} else { // else extend the polynomial and set it accordingly
for (int i = p.size(); i <= power; i++) {
this.p.add(0);
}
this.p.set(power, coefficient);
}
}
// multiply the polynomial with a constant
public void multiply(int constant) {
for (int i = 0; i < this.p.size(); i++) {
this.p.set(i, this.p.get(i) * constant);
}
}
// return the derivative of the polynomial without chaing theprevious object
public Polynomial derivative() {
Polynomial derivative = new Polynomial();
for (int i = 1; i< this.p.size(); i++) {
if(this.p.get(i) != 0) {
derivative.changeCoefficient(this.p.get(i) * i, i-1);
}
}
return derivative;
}
// toString method to print the polynomial in readableform
@Override
public String toString() {
if(p.size() < 1) return “”;
StringBuilder sb = new StringBuilder();
sb.append(this.p.get(this.p.size() – 1) + “x^” +this.degree());
for (int i = this.p.size() – 2; i >= 0; i–) {
if (p.get(i) != 0) {
if (i > 0) sb.append( (this.p.get(i) < 0 ? ” – ” : ” + “) +(Math.abs(this.p.get(i)) == 1 ? “” : Math.abs(this.p.get(i))) +”x^” + i);
else sb.append((this.p.get(i) < 0 ? ” ” : ” + “) +this.p.get(i));
}
}
return sb.toString();
}
}
public class Test {
public static void main(String[] args) {
// create the polynomial object with degree undefinded
Polynomial p = new Polynomial();
// change the coefficient of power 5 to 4
p.changeCoefficient(4, 5);
// add coefficient 7 to power 3
p.changeCoefficient(7, 3);
p.changeCoefficient(-1, 2);
p.changeCoefficient(9, 0);
// prints the polynomial
System.out.println(p);
// prints degree of polynomial
System.out.println(p.degree());
// print coefficient of the power 3
System.out.println(p.getCoefficient(3));
System.out.println(p.getCoefficient(4));
// change the coefficient of power 7
p.changeCoefficient(-3, 7);
System.out.println(p);
// get the 0th coefficient that is constant of polynomial
System.out.println(p.getCoefficient(0));
// multiply polynomial by a constant 5
p.multiply(5);
System.out.println(p);
// print the derivative of the polynomial
System.out.println(p.derivative());
}
}
Expert Answer
Answer to test the methods by using Junit class import java.io.*; import java.util.*; import java.text.*; import java.math.*; impo… . . .
OR

