public class TDFraction implements ThreeDecimal
{
  private int numerator = 0;   //initialized
  private int denominator = 1000; //initialized

  public void setValue(double val)
  //pre: val is a number between 0 and 1
  //post: stores the first 3 decimals of val
  {
    /* Uncomment below to have the concole print what method is being called */
    //System.out.println("the setValue method has been called using the TDFraction implementation");


    this.numerator = (int)(val*1000); /*multiplies the original number by 1000
                                      so all of the digits we're looking for
                                      are now in the 100, 10 and 1 positions
                                      then cast as int to truncate the remaining
                                      decimals (if any).
                                      Therefore, if our number is 0.23456, numerator
                                      stores (int)(234.56) = 234. The denominator is
                                      automatically stored as 1000, and we need not
                                      reduce the fraction into lowest terms. */
  }

  public double getValue()
  //pre: setValue has been called with valid input
  //post: returns the value stored by ThreeDecimal in type double
  {
    /* Uncomment below to have the concole print what method is being called */
    //System.out.println("the getValue method has been called using the TDFraction implementation");

    return (double)numerator / (double)denominator;
  }

  public ThreeDecimal max(ThreeDecimal other)
  //pre: other is not null
  //post: returns a ThreeDecimal whose value is the max value of this and other
  {
    /* Uncomment below to have the concole print what method is being called */
    //System.out.println("the max method has been called using the TDFraction implementation");

    TDFraction max = new TDFraction();  //it's okay to use 'new' as we're in the
                                        //actual implementing class
    if (this.getValue() >= other.getValue()) //if this is bigger than other
                                             //copy the value from this into max
    {
      max.setValue(this.getValue());
    }
    else                                     //otherwise, copy the value from
    {                                        //other into max
      max.setValue(other.getValue());
    }
    return max;                              //return the maximum
  }


public static void main(String[] args)
  {
	TDFraction test1 = new TDFraction();
	TDFraction test2 = new TDFraction();

    //testing setValue/getValue - perfect input
	test1.setValue(0.976);
	System.out.println("Testing set/getValue() - perfect input: Should be 0.976, actual: " + test1.getValue());
    //testing setValue/getValue - only 1 decimal
	test1.setValue(0.8);
	System.out.println("Testing set/getValue() - only 1 decimal: Should be 0.8, actual: " + test1.getValue()); 
    //testing setValue/getValue - more than 3 decimals
	test1.setValue(0.47896);
	System.out.println("Testing set/getValue() - more than 3 decimals: Should be 0.478, actual: " + test1.getValue());

    //testing max() - this > other
	test1.setValue(0.999);
	test2.setValue(0.777);
	System.out.println("Testing max() - this > other: Should be 0.999, actual: " + test1.max(test2.getValue()));
    //testing max() - other > this
	test1.setValue(0.666);
	test2.setValue(0.888);
	System.out.println("Testing max() - other > this: Should be 0.888, actual: " + test1.max(test2.getValue()));
    //testing max() = this has the same value as other
	test1.setValue(0.555);
	test2.setValue(0.555);
	System.out.println("Testing max() - equal value: Should be 0.555, actual: " + test1.max(test2.getValue()));
  }
}
