// Quantity.java // Name: // Date: // Comments /** class: Quantity * represents a Quantity list within the unicalc application * that is, a numeric value ("multiplier"), * a Numerator (N), a list of Strings (units), and * a Denomindator (D), another OpenList of Strings (units) * N or D may be the OpenList.emptyList; */ class Quantity { private double m; // the quantity private OpenList N; // the numerator private OpenList D; // the denominator /* Quantity, the constructor * inputs: a multiplier, m, a numerator list, N, and * a denominator list, D * outputs: the new Quantity object */ public Quantity(double m, OpenList N, OpenList D) { this.m = m; this.N = N; this.D = D; } /* toString yields a String representation - note the { } curly brackets * inputs: none * outputs: the String */ public String toString() { return "{ " + this.m + ", " + this.N + ", " + this.D + " }"; } /* main, the starting point for program control in this Quantity class * inputs: the usual (not used) * outputs: none (void) */ public static void main(String[] args) { System.out.println("\n"); OpenList DB = getDB(); System.out.println("DB is " + DB); System.out.println("\n"); Quantity Q1 = new Quantity( 1.0, OpenList.list("m", "m", "s"), OpenList.list("m","s","s") ); System.out.println("Q1 is " + Q1); System.out.println("\n"); // tests to use -- uncomment once each method is written... /* System.out.println("simplified, Q1 is " + simplify(Q1)); Quantity Q2 = new Quantity( 42.0, OpenList.list("kg", "m", "s"), OpenList.list("m","s","s") ); Quantity QM = multiply(Q1, Q2); System.out.println("QM is " + QM); Quantity QD12 = divide(Q1, Q2); Quantity QD21 = divide(Q2, Q1); Quantity QD11 = divide(Q1, Q1); System.out.println("QD12 is " + QD12); System.out.println("QD21 is " + QD21); System.out.println("QD11 is " + QD11); System.out.println("conv_unit(\"watt\", DB) is " + conv_unit("watt", DB)); System.out.println("conv_unit(\"joule\", DB) is " + conv_unit("joule", DB)); System.out.println("conv_unit(\"meter\", DB) is " + conv_unit("meter", DB)); System.out.println("norm_unit(\"meter\", DB) is " + norm_unit("meter", DB)); System.out.println("norm_unit(\"joule\", DB) is " + norm_unit("joule", DB)); Quantity Q3 = new Quantity( 42.0, OpenList.list("joule", "meter"), OpenList.list("watt") ); System.out.println("norm(Q3, DB) is " + norm(Q3, DB)); */ System.out.println("\n"); System.out.println("End of the Quantity class main."); } public static OpenList getDB() { OpenList DB, EL; DB = EL = OpenList.emptyList; DB = DB.cons(OpenList.list("watt", new Quantity(1.0, OpenList.list("joule"), OpenList.list("second")))); DB = DB.cons(OpenList.list("volt", new Quantity(1.0, OpenList.list("joule"), OpenList.list("coulomb")))); DB = DB.cons(OpenList.list("ohm", new Quantity(1.0, OpenList.list("volt"), OpenList.list("ampere")))); DB = DB.cons(OpenList.list("joule", new Quantity(1.0, OpenList.list("kg","meter","meter"), OpenList.list("second","second")))); DB = DB.cons(OpenList.list("coulomb", new Quantity(1.0, OpenList.list("ampere","second"), EL))); DB = DB.cons(OpenList.list("mph", new Quantity(1.0, OpenList.list("mile"), OpenList.list("hour")))); DB = DB.cons(OpenList.list("minute", new Quantity(60.0, OpenList.list("second"), EL))); DB = DB.cons(OpenList.list("hour", new Quantity(60.0, OpenList.list("minute"), EL))); DB = DB.cons(OpenList.list("inch", new Quantity(0.0253995, OpenList.list("meter"), EL))); DB = DB.cons(OpenList.list("mile", new Quantity(5280.0, OpenList.list("foot"), EL))); DB = DB.cons(OpenList.list("foot", new Quantity(12.0, OpenList.list("inch"), EL))); return DB; } }