good morning everyone. I have a problem with my code written in JAVA and it is that it does not pass a test, specifically this one:
@Test
public void constructs1() {
Polynomial p;
p = new Polynomial(new float[]{1, 5});
assertEquals("x + 5", p.toString());
p = new Polynomial(new float[]{0, 0, 0});
assertEquals(new Polynomial("0"), p);
p = new Polynomial(new float[]{-6, 0, 0, 20, -8});
assertEquals("-6x^4 + 20x - 8", p.toString());
}
The problem is specifically with the first case and is that my code returns a “1x + 5” and what it asks is for it to be an “x + 5” I have tried to eliminate the 1 in front of the x in various ways but or not They eliminate them or simply eliminate the 1 and the x. This is my code for those who want to know:
import java.util.Arrays;
public class Polynomial {
private float[] coefficients;
public Polynomial(float[] cfs) {
this.coefficients = removeLeadingZeros(cfs);
}
public Polynomial() {
this(new float[]{0});
}
public Polynomial(String s) {
this(parseString(s));
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Polynomial that = (Polynomial) o;
return Arrays.equals(coefficients, that.coefficients);
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
for (int i = 0; i < coefficients.length; i++) {
if (coefficients[i] != 0) {
if (result.length() > 0) {
result.append(" + ");
}
if (coefficients[i] != 1 || i == 0) {
result.append(coefficients[i] == 1 && i != 0 ? "" : removeTrailingDecimal(coefficients[i]));
}
if (i < coefficients.length - 1) {
result.append("x");
if (i < coefficients.length - 2) {
result.append("^").append(coefficients.length - i - 1);
}
} else if (i == coefficients.length - 1 && coefficients[i] == 1) {
result.append("x");
}
}
}
return result.length() == 0 ? "0" : result.toString();
}
private static String removeTrailingDecimal(float number) {
String str = Float.toString(number);
if (str.endsWith(".0")) {
return str.substring(0, str.length() - 2);
}
return str;
}
private static float[] removeLeadingZeros(float[] arr) {
int firstNonZero = 0;
while (firstNonZero < arr.length && arr[firstNonZero] == 0) {
firstNonZero++;
}
return Arrays.copyOfRange(arr, firstNonZero, arr.length);
}
private static float[] parseString(String s) {
return null;
}
}
The important parts are the “removeLeadingZeros” “removeTrailingDecimal” and the “toString” trailing functions. I have tried to make functions that eliminate the 1 in front of the x but nothing, I have tried to add an if in the toString that eliminates the 1 in front of the x but nothing. I don’t know what to do.
The important things to know is that the “removeLeadingZeros” function is used to remove the leading 0s from an array of floats. Then the “removeTrailingDecimal” function removes the trailing decimal from a number if it is an integer, the booleans method checks if two polynomials are equal, and the toString converts the polynomial to a string representation.
Todas las preguntas deben estar escritas en inglés.
You might want to check stackoverflow.com/questions/25385173/…
The
i == 0
check inif (coefficients[i] != 1 || i == 0)
is meant to bei == coefficients.length-1
. You want a special check for the last, most “right” coefficient, which will be in the last array position.Hints: maybe loop all coefficients but the last one; the last one (the one without
x
) is then handled after the loop || you can declare the first constructor aspublic Polynomial(float... cfs) {
, so it can be called asnew Polynomial(1, 5)
, no need to explicitly create an array (the compiler will create it [or code to create it])