AddFractions.java:16: cannot find symbol
symbol : class Rational
location: class rationalpackage.AddFractions
Rational rTotal = new Rational(1, 2);
The files are in the same folder, with proper pathways. I have other multi-class programs that work just fine. I'm not sure why it's not connecting right..
The code I'm working on where this error is found is :
(Simple code, a loop to create rationals and add them together using Rationals.java)
/*Using the Rational class we created, write a program that adds and then displays the sum of the fractions:
1/2 + 2/3 + 3/4 + ... + 20/21.
Use a loop to create and add these fractions.
Do NOT have 20 lines of code to create these 20 fractions.
Hint: if you use the class Rational as we discussed in class, an integer overflow will occur.
Therefore, change all of the int's to long's (except for the return value in compareTo( ) ).
*/
package rationalpackage;
public class AddRationals
{
Rational rFirst = new Rational(1, 2);
Rational rTotal = new Rational();
long rSum = 0;
public static void main( String[] args )
{
for(int i = 1; i <= 20; i++)
{
int num = 1 + i;
int den = 2 + i;
Rational rToAdd = new Rational(num, den);
if(i = 1)
{
System.out.println(rFirst + " + " + rToAdd + " = " + rFirst.add(rToAdd));
rTotal = rFirst.add(rToAdd);
}
else
{
System.out.println("Adding " + rToAdd + " to the previous sum of " + rTotal + " = " + rTotal.add(rToAdd));
this.rTotal = rTotal.add(rToAdd);
}
}
}
}
And this is the code for Rational.java that our teacher gave us to work off of :
package rationalpackage;
/**
* Creates a class to handle rational numbers
* @author Tim Farage
*/
public class Rational implements Comparable<Rational>
{
private long num; //an integer, positive or negative or zero
private long den; //a positive integer
/**
* Construct a default ctor for class Rational
*/
public Rational()
{
//create the fraction 0
num = 0;
den = 1;
}
//////////////////////////////////////////////////////////////////////
/**
* this ctor creates a Rational number from an long
* @param anInteger long
*/
public Rational( long anInteger )
{
num = anInteger;
den = 1;
}
//////////////////////////////////////////////////////////////////////
/**
* Construct a rational with specified n and d.
* It will be stored in simplest form, and the den
* will be positive.
* @param n long
* @param d long
*/
public Rational( long n, long d )
{
if ( d == 0 )
{
//this is not allowed so we'll just set the fraction itself
//to 0. However, the best thing to do here is to throw
//an exception.
num = 0;
den = 1;
return;
}
else if ( n == 0 ) //the fraction is 0 so set den to 1
{
num = 0;
den = 1;
return;
}
//get the greatest common divisor of n and d so
//we can store the fraction in simplest form
//Note that to get here neither n nor d is 0.
long gcd = gcd( n, d );
num = n / gcd;
den = d / gcd;
//If den is negative, multiply den and num by -1,
//so that there is no negative number on the bottom.
if ( den < 0 )
{
den *= -1;
num *= -1;
}
}
//////////////////////////////////////////////////////////////////////
/**
* copy ctor - can use this to copy the data from one Rational to another
* For example:
* Rational ra = new Rational( 3, 4 );
* Rational rb = new Rational( ra ); //rb will be another Rational
* object that is equal to 3/4.
* @param aRational Rational
*/
public Rational( Rational aRational )
{
num = aRational.num;
den = aRational.den;
}
////////////////////////////////
/**
* Uses a recursive form Euclid's gcd algorithm.
* @param n a non-zero long
* @param d a non-zero long
* @return the positive greatest common divisor of n and d. If either is 0,
* returns -1.
*/
public static long gcd( long n, long d )
{
if ( n == 0 || d == 0 )
{
return -1; //error code
}
//obtain the absolute values of n and d
d = Math.abs( d );
n = Math.abs( n );
long remainder = n % d;
//boundard condition of the recursion
if ( remainder == 0 )
{
return d;
}
else
{
return gcd( d, remainder );
}
} // end gcd( )
//////////////////////////////////////////////////////////////////////
/**
* @return long the numerator of the fraction
*/
public long getNumerator()
{
return num;
}
///////////////////////////////////////////////////////////////////////
/**
* @return long the denominator of the fraction
*/
public long getDenominator()
{
return den;
}
//////////////////////////////////////////////////////////////////////
/**
* Add a rational number to this rational
* @param secondRational Rational
* @return Rational
*/
public Rational add( Rational secondRational )
{
long n = num * secondRational.den + den * secondRational.num;
long d = den * secondRational.den;
return new Rational( n, d ); //this will also simplify it
}
//////////////////////////////////////////////////////////////////////
/**
* Subtract a rational number from this rational
* @param secondRational Rational
* @return Rational
*/
public Rational subtract( Rational secondRational )
{
long n = num * secondRational.den - den * secondRational.num;
long d = den * secondRational.den;
return new Rational( n, d ); //this will also simplify it
}
//////////////////////////////////////////////////////////////////////
/**
* multiply a rational number by this rational
* @param secondRational Rational
* @return Rational
*/
public Rational multiply( Rational secondRational )
{
long n = num * secondRational.num;
long d = den * secondRational.den;
return new Rational( n, d ); //this will also simplify it
}
//////////////////////////////////////////////////////////////////////
/**
* divide a rational number by this rational
* @param secondRational Rational
* @return Rational
*/
public Rational divide( Rational secondRational )
{
long n = num * secondRational.den;
long d = den * secondRational.num;
return new Rational( n, d ); //this will also simplify it
}
//////////////////////////////////////////////////////////////////////
/**
* return this Rational number as a decimal
* @return double
*/
public double toDouble()
{
return (double)num / (double)den;
}
//////////////////////////////////////////////////////////////////////
/**
* Override the toString() method
* @return String
*/
@Override
public String toString()
{
return num + "/" + den;
}
//////////////////////////////////////////////////////////////////////
/**
* Override the equals method in the Object class
* @param aRational Object
* @return boolean
*/
@Override
public boolean equals( Object aRational )
{
if ( num == ( (Rational)aRational ).num
&&
den == ( (Rational)aRational ).den )
{
return true;
}
else
{
return false;
}
}
////////////////////////////////////////////////////////////////////////
/**
* Override the compareTo method in java.lang.Comparable
* @param secondRational Rational
* @return long
*/
@Override
public int compareTo( Rational secondRational )
{
Rational difference = this.subtract( secondRational );
if ( difference.getNumerator() > 0 )
{
return 1;
}
else if ( difference.getNumerator() < 0 )
{
return -1;
}
else
{
return 0;
}
} // end compareTo()
} // end class Rational

New Topic/Question
Reply
MultiQuote








|