Page 1 of 1

Reputation:

# AddRationals.java:16: cannot find symbol : class Rational

Posted 13 July 2010 - 07:08 PM

I'm sure this is simple, but it's the little things that make my brain pop still. Anyhow, the error is:

symbol : class Rational
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;

{
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)
{
}
else
{
}
}
}

}

```

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

```

Is This A Good Question/Topic? 0

## Replies To: AddRationals.java:16: cannot find symbol : class Rational

### #2 n8wxs

• --... ...-- -.. . -. ---.. .-- -..- ...

Reputation: 972
• Posts: 3,878
• Joined: 07-January 08

## Re: AddRationals.java:16: cannot find symbol : class Rational

Posted 13 July 2010 - 07:32 PM

It compiles for me using NetBeans 6.9. I had to add static keywords

```public class AddRationals {
static Rational rFirst = new Rational(1, 2);
static Rational rTotal = new Rational();
long rSum = 0;
...

```

because the code as written attempts to access non-static variables from a static context.

I also changed if (i = 1) { to if (i == 1) {

This post has been edited by n8wxs: 13 July 2010 - 07:34 PM

### #3 Kediil

Reputation: -1
• Posts: 11
• Joined: 29-June 10

## Re: AddRationals.java:16: cannot find symbol : class Rational

Posted 13 July 2010 - 07:37 PM

I'm using jGrasp. I have this assignment, one more, and a final, then I never have to touch programming again. All of you who not only *can* do it, but do it for a living, you are wonderful special people, and I am not gifted in such a way. ;p I'm guessing maybe I should suck it up and download NetBeans so I don't have to worry about it, but I have no idea how to use the program. I'm not getting any errors for 'static variable referenced from a non-static context', though I have in the past. Just the error (and like errors) as I mentioned previously. - Did chance the = to ==, thanks. ^..^

If you have any idea what I might be able to do in jGrasp, I would appreciate it, though I'm downloading the other now. ><,

n8wxs, on 13 July 2010 - 06:32 PM, said:

```public class AddRationals {
static Rational rFirst = new Rational(1, 2);
static Rational rTotal = new Rational();
long rSum = 0;
...

```

because the code as written attempts to access non-static variables from a static context.

I also changed if (i = 1) { to if (i == 1) {

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,955
• Joined: 06-March 08

## Re: AddRationals.java:16: cannot find symbol : class Rational

Posted 13 July 2010 - 07:40 PM

JGrasp has NOTHING to do with your problem. It is JUST a fancy Java editor
Java is Java and that's it... you can write the same code with Notepad and you will have the same problem

### #5 Kediil

Reputation: -1
• Posts: 11
• Joined: 29-June 10

## Re: AddRationals.java:16: cannot find symbol : class Rational

Posted 13 July 2010 - 07:42 PM

That's lovely to know. Unfortunately, it doesn't help me much with my problem and I've been trying to figure this out for quite a while. Do you happen to know what I need to do in order to fix the issue? I would really appreciate it if you do.

pbl, on 13 July 2010 - 06:40 PM, said:

JGrasp has NOTHING to do with your problem. It is JUST a fancy Java editor
Java is Java and that's it... you can write the same code with Notepad and you will have the same problem

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,955
• Joined: 06-March 08

## Re: AddRationals.java:16: cannot find symbol : class Rational

Posted 13 July 2010 - 07:55 PM

A lots of errors... you should at least post code that compiles

```	if(i = 1)
{
}

```

should be of(i == 1)
where are rFirst and rTotal defined

This method
```
public int compareTo( Rational secondRational )

```

does not override any method
remove the @override keyword

This post has been edited by pbl: 13 July 2010 - 08:40 PM

### #7 Kediil

Reputation: -1
• Posts: 11
• Joined: 29-June 10

## Re: AddRationals.java:16: cannot find symbol : class Rational

Posted 13 July 2010 - 08:02 PM

I'm seeking help *because* it won't compile. If I could get it to compile, I wouldn't be asking for help with it. I'm not a super genius, and I'm horrible at programming on a good day.

The if, changed to if(i == 1) is only meant to display that message IF it's on the first iteration. Else handling all the iterations after it. I'm not sure what an 'of' is at all, much less why I would do a "--"

I think in the next part you're asking where rFirst and rTotal are defined. They are defined at the top, inside the class, before the method.

The compareTo bit is part of my teacher's code and we are not allowed to alter it.

pbl, on 13 July 2010 - 06:55 PM, said:

A lots of errors... you should at least post code that compiles

```	if(i = 1)
{
}

```

should be of(i -- 1)
where are rFirst and rTotal defined

This method
```
public int compareTo( Rational secondRational )

```

does not override any method
remove the @override keyword

### #8 n8wxs

• --... ...-- -.. . -. ---.. .-- -..- ...

Reputation: 972
• Posts: 3,878
• Joined: 07-January 08

## Re: AddRationals.java:16: cannot find symbol : class Rational

Posted 13 July 2010 - 08:09 PM

What is the "file" name of the rational class file?

### #9 Kediil

Reputation: -1
• Posts: 11
• Joined: 29-June 10

## Re: AddRationals.java:16: cannot find symbol : class Rational

Posted 13 July 2010 - 08:14 PM

jGrasp automatically saves them by their class name, so the Rational class is saved as "Rational" or with the extension "Rational.java".

I think that's what you're asking.

n8wxs, on 13 July 2010 - 07:09 PM, said:

What is the "file" name of the rational class file?

### #10 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,955
• Joined: 06-March 08

## Re: AddRationals.java:16: cannot find symbol : class Rational

Posted 13 July 2010 - 08:39 PM

you can say and scream how much as you want but
```if(i = 1)

```

is not a valid Java statement. It is in C/C++ but not in Java
if check for a boolean expression and i=1 is NOT a valid boolean expression while i==1 is

### #11 Kediil

Reputation: -1
• Posts: 11
• Joined: 29-June 10

## Re: AddRationals.java:16: cannot find symbol : class Rational

Posted 13 July 2010 - 08:46 PM

I neither said nor screamed. As a matter of fact, I plainly said, to you and to another, that I *CHANGED* that from "if(i = 1)" to "if(i == 1)".

Regardless. This has absolutely NO BEARING on the fact that the CLASS is not being recognized, as in the original and ongoing error

symbol : class Rational
Rational rTotal = new Rational(1, 2);

pbl, on 13 July 2010 - 07:39 PM, said:

you can say and scream how much as you want but
```if(i = 1)

```

is not a valid Java statement. It is in C/C++ but not in Java
if check for a boolean expression and i=1 is NOT a valid boolean expression while i==1 is

### #12 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,955
• Joined: 06-March 08

## Re: AddRationals.java:16: cannot find symbol : class Rational

Posted 13 July 2010 - 08:54 PM

for now,you are a beginner in Java ?, remove all your package statements from all your code
make it works an later on you'll learn how to use it/them

### #13 Kediil

Reputation: -1
• Posts: 11
• Joined: 29-June 10

## Re: AddRationals.java:16: cannot find symbol : class Rational

Posted 13 July 2010 - 08:57 PM

I am an extreme beginner. If I remove the "package rationalpackage;" from my code, it gives me the error :

class file contains wrong class: ratioanlpackage.Rational
Please remove or make sure it appears in the correct subdirectory of the classpath.

I am not allowed to alter the Rational class file's code.

pbl, on 13 July 2010 - 07:54 PM, said:

for now,you are a beginner in Java ?, remove all your package statements from all your code
make it works an later on you'll learn how to use it/them

### #14 Kediil

Reputation: -1
• Posts: 11
• Joined: 29-June 10

## Re: AddRationals.java:16: cannot find symbol : class Rational

Posted 13 July 2010 - 09:12 PM

Both as amazing and stupid as it seems. I went and downloaded NetBeans anyhow, because, what the heck, I've been screwing with this code long enough to try *anything*. And it compiled JUST FINE. Good god, there is a *reason* I will never be a programmer. If only my school weren't forcing me. >< Had some other alterations that I hadn't posted to get the adding done right. Works fine now. Darned compilers, or whatever the issue was. ;p Thank you for your help anyhow!

pbl, on 13 July 2010 - 07:54 PM, said:

for now,you are a beginner in Java ?, remove all your package statements from all your code
make it works an later on you'll learn how to use it/them