Solved

Testing an implemented Comparable

Posted on 2006-11-06
4
146 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This is about my first experience with programming Arduino.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

773 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