Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mapio committed Oct 31, 2024
1 parent 85a43c7 commit 02c2404
Showing 1 changed file with 1 addition and 40 deletions.
41 changes: 1 addition & 40 deletions src/main/java/it/unimi/di/prog2/s09/SparsePoly.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public record Term(int coeff, int degree) {
/**
* Builds a term.
*
* @throws NegativeExponentException if if {@code n} < 0.
* @throws NegativeExponentException if {@code n} < 0.
*/
public Term { // using the compact constructor
if (degree < 0)
Expand Down Expand Up @@ -197,43 +197,4 @@ public SparsePoly minus() {
for (Term t : terms) lst.add(new Term(-t.coeff, t.degree));
return new SparsePoly(lst);
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof SparsePoly)) return false;
SparsePoly other = (SparsePoly) obj;
return terms.equals(other.terms);
}

@Override
public int hashCode() {
return terms.hashCode();
}

@Override
public String toString() {
if (degree() > 0) {
StringBuilder sb = new StringBuilder("SparsePoly: ");
int pos = terms.size() - 1;
Term t = terms.get(pos);
if (t.coeff < -1) sb.append("-" + (-t.coeff));
else if (t.coeff == -1) sb.append("-");
else if (t.coeff > 1) sb.append(t.coeff);
sb.append("x" + (t.degree > 1 ? "^" + t.degree : ""));
while (--pos >= 0) {
t = terms.get(pos);
if (t.degree == 0) break;
if (t.coeff < -1) sb.append(" - " + (-t.coeff));
else if (t.coeff == -1) sb.append(" - ");
else if (t.coeff == 1) sb.append(" + ");
else sb.append(" + " + t.coeff);
sb.append("x" + (t.degree > 1 ? "^" + t.degree : ""));
}
if (t.degree == 0)
if (t.coeff > 0) sb.append(" + " + t.coeff);
else if (t.coeff < 0) sb.append(" - " + (-t.coeff));
return sb.toString();
} else return "SparsePoly: " + (terms.isEmpty() ? 0 : terms.get(0).coeff);
}
}

0 comments on commit 02c2404

Please sign in to comment.