Cannot Find symbol error! On new Object
In my program for adding subtractin multiplying and dividing fractions the RationalTest.java file wont complie, the Fraction.java does complie
Any help would be appreciated thanks!
Code :
package rationaltest;
import java.util.Scanner;
public class RationalTest {
public static void main (String args[]) {
Scanner input = new Scanner(System.in);
int Choice, Num1, Num2, Den1, Den2;
System.out.printf("Enter numerator 1:");
Num1 = input.nextInt();
System.out.printf("Enter denominator 1:");
Den1 = input.nextInt();
System.out.printf("Enter numerator 2:");
Num2 = input.nextInt();
System.out.printf("Enter denominator 2:");
Den2 = input.nextInt();
Fraction f1 = new Fraction(Num1, Den1);
Fraction f2 = new Fraction(Num2, Den2);
Fraction result = new Fraction(1,1);
char operation = ' ';
System.out.printf("Enter precision:");
int Prec = input.nextInt();
do{
System.out.printf("\n1. Add\n2. Subtract\n3. Multiply\n4. Divide\n5. Exit\nChoice:");
Choice = input.nextInt();
switch (Choice) {
case 1: operation = '+'; result = f1.Add(f2); break;
case 2: operation = '-'; result = f1.Subtract(f2); break;
case 3: operation = '*'; result = f1.Mult(f2); break;
case 4: operation = '/'; result = f1.Div(f2); break;
}
System.out.printf("%s %c %s = %s (%s)\n",f1,operation,f2,result,
result.asDecimal(Prec));
} while(Choice != 5);
}
}
Code :
package rationaltest;
public class Fraction {
private int Numerator;
private int Denominator;
public Fraction() {
Numerator = 0;
Denominator = 1;
}
public Fraction(int NumValue, int DenValue){
if (DenValue == 0){
System.out.println("!!Error: denominator Zero, a default rational 0/1 is stored instead.");
Numerator = 0;
Denominator = 1;
}
else {
Numerator = NumValue;
Denominator = DenValue;
int divider = gcd(Numerator > 0 ? Numerator : -Numerator,
Denominator > 0 ? Denominator : -Denominator);
if (divider > 1) {
Numerator /= divider;
Denominator /= divider;
}
}
}
private static int gcd(int a,int b) {
if (b==0) return a;
return gcd(b,a%b);
}
public Fraction Add(Fraction x){
return new Fraction(this.Numerator*x.Denominator+x.Numerator*this.Denominator,
this.Denominator*x.Denominator);
}
public Fraction Subtract(Fraction x) {
return new Fraction(this.Numerator*x.Denominator - x.Numerator*this.Denominator,
this.Denominator*x.Denominator);
}
public Fraction Mult(Fraction x) {
return new Fraction(this.Numerator*x.Numerator,this.Denominator*x.Denominator);
}
public Fraction Div(Fraction x) {
return new Fraction(this.Numerator*x.Denominator,x.Numerator*this.Denominator);
}
public String toString() {
return String.format("%d/%d", this.Numerator, this.Denominator);
}
public String asDecimal(int Prec) {
return String.format("%." + Prec + "f\n",(((double) getNum()) / ( (double) getDen())));
}
public void setNum(int NumValue){ Numerator = NumValue; }
public void setDen(int DenValue){ Denominator = DenValue; }
public int getNum(){ return Numerator; }
public int getDen(){ return Denominator; }
}
Here are all the errors hope this helps
Code Text:
[jrauscher]$ javac RationalTest.java
RationalTest.java:25: cannot find symbol
symbol : class Fraction
location: class RationalTest.RationalTest
Fraction f1 = new Fraction(Num1, Den1);
^
RationalTest.java:25: cannot find symbol
symbol : class Fraction
location: class RationalTest.RationalTest
Fraction f1 = new Fraction(Num1, Den1);
^
RationalTest.java:26: cannot find symbol
symbol : class Fraction
location: class RationalTest.RationalTest
Fraction f2 = new Fraction(Num2, Den2);
^
RationalTest.java:26: cannot find symbol
symbol : class Fraction
location: class RationalTest.RationalTest
Fraction f2 = new Fraction(Num2, Den2);
^
RationalTest.java:27: cannot find symbol
symbol : class Fraction
location: class RationalTest.RationalTest
Fraction result = new Fraction(1,1);
^
RationalTest.java:27: cannot find symbol
symbol : class Fraction
location: class RationalTest.RationalTest
Fraction result = new Fraction(1,1);
^
6 errors
Re: Cannot Find symbol error! On new Object
try using the -d option with javac to tell it where the root of the output class path is:
javac - Java programming language compiler
. should be okay if you don't mind your classes being output to ./rationaltest
Re: Cannot Find symbol error! On new Object
I couldn't get javac -d to work.
Re: Cannot Find symbol error! On new Object
Describe where the source files are located and where you are issuing the javac command.
With the classes in packages, the classpath must point to the folder containing the package folder.
Re: Cannot Find symbol error! On new Object
Quote:
Originally Posted by
jtrtoday
I couldn't get javac -d to work.
You could alternatively comment out the 'package' statement at the top of your files. Your problem is caused by Java's package<->filepath mapping. When you compile Fraction.java the way you're doing it, you're getting a Fraction.class in the working directory, right? When you compile your main method class, javac is attempting to resolve references to 'Fraction' by looking in the same package you declare your main method class to be in: rationaltest. To find the class files necessary to check method names etc against, javac looks in <classpath>/rationaltest. You haven't specified your classpath, so it's looking in ./rationaltest - which I'm guessing does not yet exist.
You could work around the problem with
Code :
javac -d . Fraction.java
javac -d . RationalTest.java
or (maybe, untested, but I'd be surprised if it didn't work)
Code :
javac Fraction.java RationalTest.java
Norm is right - I doubt there's anyone contributing here who doesn't explicitly set their classpath one way or another. My build files always do, with a line such as:
Code :
javac -d $CLASSROOT -cp $CLASSROOT:$JARS <filespec-for-sources>
Re: Cannot Find symbol error! On new Object
Thanks you very much everyone I got it fixed