public class TDReal implements ThreeDecimal
{
  private double value = 0.0;   //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 TDReal implementation");

    int temp = (int)(val*1000);
    this.value = (double)temp/(double)(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).
                                      Then we divide by 100 to retrieve the 3 decimals
                                      we were looking for.
                                      For instance, 0.23456 -> 234.56 -> 234 -> 0.234 */
  }

  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 TDReal implementation");

    return this.value;
  }

  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 TDReal implementation");

    TDReal max = new TDReal();          //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)
  {
	TDReal test1 = new TDReal();
	TDReal test2 = new TDReal();

    //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("Testingmax() - 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("Testingmax() - 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("Testingmax() - equal value: Should be 0.555, actual: " + test1.max(test2.getValue())); 
  }
}
