Solved

Testing an implemented Comparable

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
A short article about problems I had with the new location API and permissions in Marshmallow
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

856 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