/** *********************************************************
* FractClass implements a Fraction ADT.
* Integrity Constraint: The object's value is the
* correct value of the last fraction Stored or computed.
* @author Jeff Offutt
* @version 1.2, 1/99
********************************************************* **/
public class FractClass
{
// Class variables
private static int CurNumFracs = 0;
// Class methods
/** *********************************************************
* Class Method : Returns the current number of fractions
* pre: null
* post: null
* @param None
* @return An integer, the current number of fractions
********************************************************* **/
public static int GetNumFracs ()
{
return (CurNumFracs);
}
// Public constructor methods ///////////////////////////////
/** *********************************************************
* Constructor : Sets fraction to 0/1
* pre: null
* post: fraction's value is 0/1; number of fractions is incremented
* @param None
* @return None
********************************************************* **/
public FractClass () //constructor
{
Numerator = 0;
Denominator = 1;
CurNumFracs++;
}
/** *********************************************************
* Constructor : Initialize a new fraction with supplied values
* pre: Den is not 0
* post: fraction's value is Num/Den; number of fractions is incremented
* @param Num Integer numerator
* @param Den Integer denominator
* @return None
********************************************************* **/
public FractClass (int Num, int Den) //constructor
{
Numerator = Num;
Denominator = Den;
CurNumFracs++;
}
// Public mutator methods ///////////////////////////////////
/** *********************************************************
* Mutator : Adds two fractions, F1 = F1+F2
* pre: Both denominators must be the same
* (checked with the exception ArithmeticException)
* post: Object's value is old-value+Right
* @param Right FractClass right-hand-side
* @return None
********************************************************* **/
public void Add (FractClass Right)
{
try
{
Numerator = Numerator + Right.Numerator;
if (Denominator != Right.Denominator)
throw new ArithmeticException ();
}
catch (ArithmeticException e)
{
System.out.println ("Fraction add error: Denominators must be the same.");
}
}
/** *********************************************************
* Mutator : Multiplies two fractions, F1 = F1*F2
* pre: null
* post: Object's value is old-value*Right
* @param Right FractClass right-hand-side
* @return None
********************************************************* **/
public void Mult (FractClass Right)
{
Numerator = Numerator * Right.Numerator;
Denominator = Denominator * Right.Denominator;
}
// Public accessor methods //////////////////////////////////
/** *********************************************************
* Accessor : Compares two fractions for equality
* pre: null
* post: null
* @param Right FractClass right-hand-side
* @return boolean (F1 == F2)
********************************************************* **/
public boolean equals (Object Right)
{
if (!(Right instanceof FractClass))
return (false);
FractClass rhs = (FractClass) Right;
if (Numerator == rhs.Numerator && Denominator == rhs.Denominator)
return (true);
else
return (false);
}
/** *********************************************************
* Accessor : Prints a fraction using println
* pre: null
* post: Fraction is printed
* @param None
* @return None
********************************************************* **/
public void PrintFrac ()
{
System.out.println (Numerator + " / " + Denominator);
}
/** *********************************************************
* Accessor : Converts a fraction to a string for println
* pre: null
* post: null
* @param None
* @return String representation of the value is returned
********************************************************* **/
public String toString ()
{
return (Numerator + " / " + Denominator);
}
/** *********************************************************
* Main program function, tests the ADT class
* @param None
* @return None
********************************************************* **/
public static void main (String argv[])
{
FractClass f1, f2, f3, f4, f5;
System.out.println ("Instances: " + FractClass.GetNumFracs ());
f1 = new FractClass (1, 2);
f2 = new FractClass (3, 4);
f3 = new FractClass (2, 4);
f4 = new FractClass ();
System.out.println ("Instances: " + FractClass.GetNumFracs ());
System.out.print ("f1: ");
f1.PrintFrac ();
System.out.print ("f2: ");
f2.PrintFrac ();
System.out.print ("f3: ");
f3.PrintFrac ();
System.out.print ("f4: ");
f4.PrintFrac ();
f2.Add (f3);
System.out.print ("f2: ");
f2.PrintFrac ();
f1.Mult (f3);
System.out.print ("f1: ");
f1.PrintFrac ();
System.out.println ("Instances: " + FractClass.GetNumFracs ());
System.out.print ("f1: ");
System.out.println (f1); // Implicit call to f1.toString()
f5 = new FractClass (2, 4);
System.out.print ("f5: ");
System.out.println (f5);
if (f3 == f5)
System.out.println ("f3 and f5 are the same.");
else
System.out.println ("f3 and f5 are not the same.");
if (f3.equals (f5))
System.out.println ("f3 and f5 have the same values.");
else
System.out.println ("f3 and f5 do not have the same values.");
if (f4.equals (f5))
System.out.println ("f4 and f5 have the same values.");
else
System.out.println ("f4 and f5 do not have the same values.");
f1.Add (f5);
System.out.print ("f1: ");
f1.PrintFrac ();
} // end main
/////////////////////////////////////////////////////////////
// Instance variables. //
/////////////////////////////////////////////////////////////
private int Numerator;
private int Denominator;
} // End FractClass