Solved

Testing an implemented Comparable

Posted on 2006-11-06
4
142 Views
Last Modified: 2010-04-16
I have the following class

public class RationalNumber
{
      private int numerator, denominator;
      
      //-------------------------------------------------------------------------------------
      //Constructor: Sets up the rational numebr by ensuring a nonzero denominator and making
      //only the numerator signed.
      //-------------------------------------------------------------------------------------
      public RationalNumber (int numer, int denom)
      {
            if (denom == 0)
                  denom = 1;
                  
                  //Make the numerator "store" the sign
                  if (denom < 0)
                  {
                        numer = numer * -1;
                        denom = denom * -1;
                  }
                  
                  numerator = numer;
                  denominator = denom;
                  
                  reduce();
                  
      }
      
      //-------------------------------------------------------------------------------------
      // Returns the numerator of this rational number
      //-------------------------------------------------------------------------------------
      public int getNumerator ()
      {
            return numerator;
      }
      
      //-------------------------------------------------------------------------------------
      // Returns the denominator of this rational number.
      //-------------------------------------------------------------------------------------
      public int getDenominator ()
      {
            return denominator;
      }
      
      //-------------------------------------------------------------------------------------
      // Returns the reciprocal of this rational number
      //-------------------------------------------------------------------------------------
      public RationalNumber reciprocal ()
      {
            return new RationalNumber (denominator, numerator);
      }
      
      //-------------------------------------------------------------------------------------
      // Adds this rational number to the one passed a a parameter. A common denominator
      // is found by multiplying the individual denominators
      //-------------------------------------------------------------------------------------
      public RationalNumber add (RationalNumber op2)
      {
            int commonDenominator = denominator * op2.getDenominator();
            int numerator1 = numerator * op2.getDenominator();
            int numerator2 = op2.getNumerator() * denominator;
            int sum = numerator1 + numerator2;
            
            return new RationalNumber (sum, commonDenominator);
            
      }
      
      //-------------------------------------------------------------------------------------
      // Subtracts the rational number passed as a parameter from this rational number
      //-------------------------------------------------------------------------------------
      public RationalNumber subtract (RationalNumber op2)
      {
            int commonDenominator = denominator * op2.getDenominator();
            int numerator1 = numerator * op2.getDenominator();
            int numerator2 = op2.getNumerator() * denominator;
            int difference = numerator1 - numerator2;
            
            return new RationalNumber (difference, commonDenominator);
      
      }
      
      //--------------------------------------------------------------------------------------
      // Multiplies this rational number by the one passed as a parameter
      //--------------------------------------------------------------------------------------
      public RationalNumber multiply (RationalNumber op2)
      {
            int numer = numerator * op2.getNumerator();
            int denom = denominator * op2.getDenominator();
            
            return new RationalNumber (numer, denom);
            
      }
      
      //--------------------------------------------------------------------------------------
      // Divides this rational number by the one passes as a parameter by multiplying by the
      // reciprocal of the second rational.
      //--------------------------------------------------------------------------------------
      public RationalNumber divide (RationalNumber op2)
      {
            return multiply (op2.reciprocal());
      }
      
      //--------------------------------------------------------------------------------------
      // Divides this rational number by the one passed as a parameter by multiplying by the
      // reciprocal of the second rational
      //--------------------------------------------------------------------------------------
      public boolean iLike (RationalNumber op2)
      {
            return ( numerator == op2.getNumerator() &&
                              denominator == op2.getDenominator() );
      }
      
      //--------------------------------------------------------------------------------------
      // Returns this rational number as a string
      //--------------------------------------------------------------------------------------
      public String toString ()
      {
            String result;
            
            if (numerator == 0)
                  result = "0";
         else
                  if (denominator == 1)
                     result = numerator + "";
                  else
                        result = numerator + "/" + denominator;
            
            return result;
            
      }
      
      //--------------------------------------------------------------------------------------
      //
      
            public int compare(Object o)
            {
                  double tolerance = 0.0001;
                  RationalNumber other = (RationalNumber)o;
                  double thisFrac = this.asDecimal();
                  double otherFrac = other.asDecimal();
                  
                  if (Math.abs(thisFrac - otherFrac) < tolerance)
                  
                  {
                        return 0; //equal
                  }
                  
                  else
                  {
                        return 1;
                        
                  }
                  
            }

                  
      //---------------------------------------------------------------------------------------
      // Returns the Fractional for the objects
      //---------------------------------------------------------------------------------------            
      public double asDecimal()
            {
                  return numerator / denominator;
            }

      //---------------------------------------------------------------------------------------
      // Reduces this rational number by dividing both the numerator and the denominator by
      // their greatest common divisor.
      //---------------------------------------------------------------------------------------
      private void reduce ()
      {
            if (numerator != 0)
            {
                  int common = gcd (Math.abs(numerator), denominator);
                  
                  numerator = numerator = numerator / common;
                  denominator = denominator / common;
                  
            }
            
      }
      
      //---------------------------------------------------------------------------------------
      // Computes and returns th greates common divisor of the two positive parameters.
      // Uses Euclid's algorithm.
      //---------------------------------------------------------------------------------------
      private int gcd (int num1, int num2)
      {
            while (num1 != num2)
                  if (num1 > num2)
                        num1 = num1 - num2;
                  else
                        num2 = num2 - num1;
                        
                  return num1;
      }
      
}



I want to test the comparable methods that were set up. I created the following driver class but am unsure how to proceed. Please advise.

public class RationalNumber_Driver
{
      
      public static void main (String[] args)
      {
            
            RationalNumber r1 = new RationalNumber (6, 8);
            RationalNumber r2 = new RationalNumber (1, 3);
            
            System.out.println (r1);
            System.out.println (r2);
            
      }
      
}

How can I code from here to test the methods?
0
Comment
Question by:msheppard74
  • 2
  • 2
4 Comments
 
LVL 19

Accepted Solution

by:
VoteyDisciple earned 500 total points
ID: 17882872
I'm thinkin'... create two objects with different values, compare 'em with the methods you wrote, and see if the results are right.

I mean, you could do something fancy like make an array of 'em and call a sort algorithm that relies on the Comparable interface, but... I'm lazy.  (-:
0
 

Author Comment

by:msheppard74
ID: 17882895
Didn't I just create two objects in this driver class?

My problem is how to compare them with the methods I wrote! ;)

0
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17882916
Well, call the method on one of the objects, passing the other in as an argument, and output the result.
0
 

Author Comment

by:msheppard74
ID: 17882971
public class RationalNumber_Driver
{
      
      public static void main (String[] args)
      {
            
            RationalNumber r1 = new RationalNumber (4, 10);
            RationalNumber r2 = new RationalNumber (1, 3);
            RationalNumber r3;
            
            System.out.println (r1);
            System.out.println (r2);
            
            if (r1.compare(r2) == 0)
                  System.out.println ("The rational numbers are equal");
      }
      
}

OK I tested this an it returned that the numbers are equal. Can you test and concur?? Pretty pretty please??
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now