aboutsummaryrefslogtreecommitdiff
path: root/Week7/src/polynomial/Term.java
diff options
context:
space:
mode:
authorCamil Staps2015-03-17 21:57:28 +0100
committerCamil Staps2015-03-17 21:57:28 +0100
commitb0646c93d7a2aece16b099ab805af2d58753ac79 (patch)
treee204fb1265a2deed188e115647116e4c8a23226f /Week7/src/polynomial/Term.java
parentAdded comment about ordering on exponent (diff)
Various small enhancements
Diffstat (limited to 'Week7/src/polynomial/Term.java')
-rw-r--r--Week7/src/polynomial/Term.java10
1 files changed, 7 insertions, 3 deletions
diff --git a/Week7/src/polynomial/Term.java b/Week7/src/polynomial/Term.java
index 7cde927..60c6381 100644
--- a/Week7/src/polynomial/Term.java
+++ b/Week7/src/polynomial/Term.java
@@ -81,17 +81,21 @@ public class Term {
/**
* Converts a term into a readable representation the standard format is
* %fx^%d
+ * I changed the implementation to remove trailing zeroes from the coefficient.
*
* @return the string representing the term
*/
@Override
public String toString() {
+ String nice_coefficient = Double.toString(coefficient);
+ if (Math.round(coefficient) == coefficient)
+ nice_coefficient = Integer.toString((int) coefficient);
if (exponent == 0) {
- return String.format("%f", coefficient, exponent);
+ return String.format("%s", nice_coefficient, exponent);
} else if (exponent == 1) {
- return String.format("%fx", coefficient, exponent);
+ return String.format("%sx", nice_coefficient, exponent);
} else {
- return String.format("%fx^%d", coefficient, exponent);
+ return String.format("%sx^%d", nice_coefficient, exponent);
}
}