Question
Java please. Please create a test class for each of the two classes provided below. Your task is to complete the test classes. Proper naming
Java please. Please create a test class for each of the two classes provided below.
Your task is to complete the test classes. Proper naming conventions and use of annotations are required. You must create tests for each required method (you do not need to test Poly.printPoly()) in Poly and PolyCalc according to the format described in Task 2.1. It may be better to complete this problem outside of EduTools.
You do not need to cover every possible case with each method. Provide a reasonable number of cases and any special cases you can think of. Maybe you will find an error in the code!
PolyCalc class
public final class PolyCalc { public static Poly differentiate(Poly p) { double[] coeffs = p.getCoeffs(); double[] newCoeffs = new double[coeffs.length]; for (int i = 0; i < coeffs.length - 1; i++) { newCoeffs[i + 1] = (coeffs.length - 1 - i) * coeffs[i]; } return new Poly(newCoeffs); } public static Poly integrate(Poly p) { double[] coeffs = p.getCoeffs(); double[] newCoeffs = new double[coeffs.length + 1]; for (int i = 0; i < coeffs.length; i++) { newCoeffs[i] = coeffs[i] / (coeffs.length - i); } return new Poly(newCoeffs); } }
Poly class
public class Poly { private final double[] coeffs; private final int order; public Poly(double[] coeffs) { this.coeffs = coeffs.clone(); order = coeffs.length - 1; } public double evaluate(double x) { double result = 0; for (int i = 0; i <= order; i++) { result += coeffs[i] * Math.pow(x, order - i); } return result; } public void printPoly() { StringBuilder sb = new StringBuilder(); for (int i = 0; i < order; i++) { if (coeffs[i] != 0) { if (sb.length() > 0 && coeffs[i] > 0) { sb.append(" + ").append(Math.abs(coeffs[i])).append("x^").append(order - i); } else if (sb.length() > 0 && coeffs[i] < 0) { sb.append(" - ").append(Math.abs(coeffs[i])).append("x^").append(order - i); } else { sb.append(coeffs[i]).append("x^").append(order - i); } } } if (coeffs[order] != 0) { if (coeffs[order] < 0 && sb.length() > 0) { sb.append(" - ").append(Math.abs(coeffs[order])); } else if (coeffs[order] > 0 && sb.length() > 0) { sb.append(" + ").append(Math.abs(coeffs[order])); } else { sb.append(coeffs[order]); } } System.out.println(sb.toString()); } public double[] getCoeffs() { return coeffs; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started