In essence, this problem asks you to implement (really "port") Unicalc from Racket into Java. You'll do this by implementing a Quantity class, representing Unicalc's quantity lists. That, in turn, will use your OpenList as its fundamental data structure. This is natural, because OpenList is a Java implementation of Racket lists!
This problem asks you to implement Unicalc in Java.
The implementation will be in the Quantity class.
In order to
ensure that toString and other important methods are uniform,
there is a starter file for Quantity.java
in the hw7.zip archive:
hw7.zip
In that zip archive are three files:
Notes on Quantity
{ 9.8, [meter], [second, second], 0.1 }
The above Quantity object represents 9.8 meters per second squared with an
uncertainty of 0.1.
Note that when a Quantity is printed, it is surrounded by curly braces.
{ 42.0, [], [], 0.0 }
Here, we are illustrating the provided toString function, along with the OpenList class you wrote this week. The curly braces are used to help distinguish objects of type Quantity from objects of type OpenList.
cancel( [m,s], [kg,s] ) returns [m]
cancel( [m,s], [kg,m,s] ) returns []
cancel( [m,s], [kg] ) returns [m,s]
cancel( [m,s], [] ) returns [m,s]
So, cancel does not treat its input lists the same! It's trying
to cancel elements from the first input, L that are present
in the second input, M.
String fL = (String)L.first;
String fM = (String)M.first;
From there, you can test conditions such as fL.equals(fM) and
fL.compareTo(fM). Based on those tests, your code will return
the appropriate value.
private double m; // the multiplier
private double u; // the uncertainty
private OpenList N; // the numerator, an OpenList of Strings (units)
private OpenList D; // the denominator, also an OpenList of Strings (units)
public Quantity(double m) // constructor for a "plain-number" QL
public Quantity(double m, OpenList N, OpenList D) // constructor with 0.0 unc.
public Quantity(double m, OpenList N, OpenList D, double u) // "standard" constructor
public String toString() // for printing objects of type Quantity
public static OpenList getSmallDB() // gets a pre-prepared database of units
public boolean equals( Object o ) // the equals method for any Object
public boolean equals( Quantity q ) // the equals method for comparing Quantity objects
public static void pl( Object o ) // printing for lazy (or sane) folks!
public static void pl() // ditto!
public static void main(String[] a) // main method
// a constructor with a different order of inputs...
public Quantity(double m, double u, OpenList N, OpenList D)
// These are ALMOST translations from earlier weeks' Unicalc code
//
// Note that norm_unit, norm, and add take in a DB, rather than using a global DB
// cancel removes elements from L, if they're in M - it's NOT symmetric!
public static OpenList cancel( OpenList L, OpenList M )
// simplify cancels common units from N to D and vice-versa
public static Quantity simplify(Quantity Q);
// multiplies two Quantity lists
public static Quantity multiply(Quantity Q1, Quantity Q2);
// raises Q to the pth power (p ≥ 0)
public static Quantity power(Quantity Q, int p)
// divides two Quantity lists
public static Quantity divide(Quantity Q1, Quantity Q2);
// returns the normalized QL equivalent to unit
public static Quantity norm_unit(String unit, OpenList DB);
// returns the norm_unit QL equivalent to Q
public static Quantity norm(Quantity Q, OpenList DB);
// adds two Quantity lists *after* normalizing them
public static Quantity add(Quantity Q1, Quantity Q2, OpenList DB);
// If the units (afer normalizing) do not match, return the following
// valid Quantity: { 0.0, ["error:add"], [], 0.0 }
// negation (we can implement subtraction via add and negate)
public static Quantity negate(Quantity Q);
Presuming the numerators and denominators are sorted The constructors of Quantity should make sure that the numerator and denominator units are in alphabetical (sorted) order. Our starter file provides this functionality. You will need mergesort working in your OpenList class.
Testing your code For testing we've provided a set of tests that are commented out in main.
In addition, the file QuantityTester.java
has a set of tests that you'll be able to compile and run
once Quantity's methods are written.