import java.util.*; /** Our internal representation of a number in the X0 language. * See http://www.radford.edu/itec380/2019spring-ibarland/Project * * @author Ian Barland * @version 2014.Nov.04 */ public record Num(double x) implements Value, Expr { public Value eval() { return this; } // Arg, `Value.java` has this (same) implemenation as default method, but javac // complains that we're implementing Expr w/o defining 'eval'. public String toString() { // Let's make ints look better than, say, "43.0": // Omit trailing 0's after decimal, rounding to 9 places: return formatter.format(this.x); } public static Num parse( Scanner s, String punctuation ) { return new Num( UtilIan.nextDoubleSplittingBy(s,punctuation) ); } private static java.text.DecimalFormat formatter = new java.text.DecimalFormat("#.#########"); /** Return the Java double this Value represents * (for use by other primitives in our language.) * @return the Java double this Value represents. */ double doubleValue() { return this.x; } @Override /** Compare two Numbers, but with some tolerance. */ public boolean equals( /* Num this, */ Object that) { if (this==that) { return true; } else if (that==null) { return false; } else if (this.getClass() != that.getClass()) { return false; } else { Num thatt = (Num) that; return UtilIan.equalsApprox(this.x,thatt.x); } } }