Hello all,
I'm taking classes for Java and doing pretty well for a newbie with no previous software programming experience (making html webpages just don't count hehe). I'm having a little trouble with one of my assignments that looks pretty simple. I know the concept and what must be accomplished but I'm not quite sure how to go about it. Here's what I need to do...
I have a driver program and a class program. The gist of it is that I need to create a method that compares two objects (each containing a rational number) and if they are close enough together, will output 0 because they are considered even (to within 0.0001 of each other). Correspondingly, if they are more then 0.0001 apart it outputs -1 and 1 respectively. My problem is that I'm not sure how to compare two class objects within my method. I know how to compare variables and string objects, but not such large objects like class instantiations. The method I created is called "compareTo" and has obvious problems (though the logic may be correct?). I have posted both the class and driver programs below. Before I forget, the class program implements the "Comparable" interface. Here's a link to it's API definition
Don't worry about the other methods in the class program, they all seem to work =) Any ideas would be appreciated!
Thanks,
Jisoo22
class program
public class Rational implements Comparable
{
private int numerator, denominator;
private double tolerance = 0.0001;
//-----------------------------------------------------------------
// Sets up the rational number by ensuring a nonzero denominator
// and making only the numerator signed.
//-----------------------------------------------------------------
public Rational (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();
}
//-----------------------------------------------------------------
// Compares two rational numbers and if they are within tolerance
// are considered even (or 0). If not, then -1 or 1 is outputted
// to show that the first rational number is less than or greater
// than the second. Currently a work in progress.
//-----------------------------------------------------------------
public Rational compareTo ( Rational ob1, Rational ob2 )
{
int obValue = 0;
double compared = ob1 - ob2;
if (compared <= tolerance)
{
return obValue;
}
else if ( compared > tolerance )
{
obValue = obValue + 1;
return obValue;
}
else
{
obValue = obValue - 1;
return obValue;
}
}
//-----------------------------------------------------------------
// Adds this rational number to the one passed as a parameter.
// A common denominator is found by multiplying the individual
// denominators.
//-----------------------------------------------------------------
public Rational add (Rational op2)
{
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int sum = numerator1 + numerator2;
return new Rational (sum, commonDenominator);
}
//-----------------------------------------------------------------
// Subtracts the rational number passed as a parameter from this
// rational number.
//-----------------------------------------------------------------
public Rational subtract (Rational op2)
{
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int difference = numerator1 - numerator2;
return new Rational (difference, commonDenominator);
}
//-----------------------------------------------------------------
// Multiplies this rational number by the one passed as a
// parameter.
//-----------------------------------------------------------------
public Rational multiply (Rational op2)
{
int numer = numerator * op2.getNumerator();
int denom = denominator * op2.getDenominator();
return new Rational (numer, denom);
}
//-----------------------------------------------------------------
// Divides this rational number by the one passed as a parameter
// by multiplying by the reciprocal of the second rational.
//-----------------------------------------------------------------
public Rational divide (Rational op2)
{
return multiply (op2.reciprocal());
}
//-----------------------------------------------------------------
// Returns the reciprocal of this rational number.
//-----------------------------------------------------------------
public Rational reciprocal ()
{
return new Rational (denominator, numerator);
}
//-----------------------------------------------------------------
// Returns the numerator of this rational number.
//-----------------------------------------------------------------
public int getNumerator ()
{
return numerator;
}
//-----------------------------------------------------------------
// Returns the denominator of this rational number.
//-----------------------------------------------------------------
public int getDenominator ()
{
return denominator;
}
//-----------------------------------------------------------------
// Determines if this rational number is equal to the one passed
// as a parameter. Assumes they are both reduced.
//-----------------------------------------------------------------
public boolean equals (Rational 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;
}
//-----------------------------------------------------------------
// 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 / common;
denominator = denominator / common;
}
}
//-----------------------------------------------------------------
// Computes and returns the greatest 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;
}
}
driver program
public class RationalNumbers
{
//-----------------------------------------------------------------
// Creates some rational number objects and performs various
// operations on them.
//-----------------------------------------------------------------
public static void main( String [] args ){
Rational r1 = new Rational( 1, 10000 ); // .0001
Rational r2 = new Rational( 1, 10000 ); // .0001
Rational r3 = new Rational( 2, 10000 ); // .0002
Rational r4 = new Rational( 3, 10000 ); // .0003
System.out.println( "should be 0:" + r1.compareTo( r2 ) );
System.out.println( "should be 0:" + r1.compareTo( r3 ) ); //in tolerance
System.out.println( "should be 0:" + r1.compareTo( r1 ) );
System.out.println( "should be -1:" + r1.compareTo( r4 ) );
System.out.println( "should be 1:" + r4.compareTo( r1 ) );
} //main
}
I'm taking classes for Java and doing pretty well for a newbie with no previous software programming experience (making html webpages just don't count hehe). I'm having a little trouble with one of my assignments that looks pretty simple. I know the concept and what must be accomplished but I'm not quite sure how to go about it. Here's what I need to do...
I have a driver program and a class program. The gist of it is that I need to create a method that compares two objects (each containing a rational number) and if they are close enough together, will output 0 because they are considered even (to within 0.0001 of each other). Correspondingly, if they are more then 0.0001 apart it outputs -1 and 1 respectively. My problem is that I'm not sure how to compare two class objects within my method. I know how to compare variables and string objects, but not such large objects like class instantiations. The method I created is called "compareTo" and has obvious problems (though the logic may be correct?). I have posted both the class and driver programs below. Before I forget, the class program implements the "Comparable" interface. Here's a link to it's API definition
Don't worry about the other methods in the class program, they all seem to work =) Any ideas would be appreciated!
Thanks,
Jisoo22
class program
public class Rational implements Comparable
{
private int numerator, denominator;
private double tolerance = 0.0001;
//-----------------------------------------------------------------
// Sets up the rational number by ensuring a nonzero denominator
// and making only the numerator signed.
//-----------------------------------------------------------------
public Rational (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();
}
//-----------------------------------------------------------------
// Compares two rational numbers and if they are within tolerance
// are considered even (or 0). If not, then -1 or 1 is outputted
// to show that the first rational number is less than or greater
// than the second. Currently a work in progress.
//-----------------------------------------------------------------
public Rational compareTo ( Rational ob1, Rational ob2 )
{
int obValue = 0;
double compared = ob1 - ob2;
if (compared <= tolerance)
{
return obValue;
}
else if ( compared > tolerance )
{
obValue = obValue + 1;
return obValue;
}
else
{
obValue = obValue - 1;
return obValue;
}
}
//-----------------------------------------------------------------
// Adds this rational number to the one passed as a parameter.
// A common denominator is found by multiplying the individual
// denominators.
//-----------------------------------------------------------------
public Rational add (Rational op2)
{
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int sum = numerator1 + numerator2;
return new Rational (sum, commonDenominator);
}
//-----------------------------------------------------------------
// Subtracts the rational number passed as a parameter from this
// rational number.
//-----------------------------------------------------------------
public Rational subtract (Rational op2)
{
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int difference = numerator1 - numerator2;
return new Rational (difference, commonDenominator);
}
//-----------------------------------------------------------------
// Multiplies this rational number by the one passed as a
// parameter.
//-----------------------------------------------------------------
public Rational multiply (Rational op2)
{
int numer = numerator * op2.getNumerator();
int denom = denominator * op2.getDenominator();
return new Rational (numer, denom);
}
//-----------------------------------------------------------------
// Divides this rational number by the one passed as a parameter
// by multiplying by the reciprocal of the second rational.
//-----------------------------------------------------------------
public Rational divide (Rational op2)
{
return multiply (op2.reciprocal());
}
//-----------------------------------------------------------------
// Returns the reciprocal of this rational number.
//-----------------------------------------------------------------
public Rational reciprocal ()
{
return new Rational (denominator, numerator);
}
//-----------------------------------------------------------------
// Returns the numerator of this rational number.
//-----------------------------------------------------------------
public int getNumerator ()
{
return numerator;
}
//-----------------------------------------------------------------
// Returns the denominator of this rational number.
//-----------------------------------------------------------------
public int getDenominator ()
{
return denominator;
}
//-----------------------------------------------------------------
// Determines if this rational number is equal to the one passed
// as a parameter. Assumes they are both reduced.
//-----------------------------------------------------------------
public boolean equals (Rational 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;
}
//-----------------------------------------------------------------
// 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 / common;
denominator = denominator / common;
}
}
//-----------------------------------------------------------------
// Computes and returns the greatest 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;
}
}
driver program
public class RationalNumbers
{
//-----------------------------------------------------------------
// Creates some rational number objects and performs various
// operations on them.
//-----------------------------------------------------------------
public static void main( String [] args ){
Rational r1 = new Rational( 1, 10000 ); // .0001
Rational r2 = new Rational( 1, 10000 ); // .0001
Rational r3 = new Rational( 2, 10000 ); // .0002
Rational r4 = new Rational( 3, 10000 ); // .0003
System.out.println( "should be 0:" + r1.compareTo( r2 ) );
System.out.println( "should be 0:" + r1.compareTo( r3 ) ); //in tolerance
System.out.println( "should be 0:" + r1.compareTo( r1 ) );
System.out.println( "should be -1:" + r1.compareTo( r4 ) );
System.out.println( "should be 1:" + r4.compareTo( r1 ) );
} //main
}