Results 1 to 11 of 11
  1. #1
    Jtrtoday is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default Cannot Find symbol error!

    In my program for adding subtractin multiplying and dividing fractions the RationalTest.java file wont complie, the Fraction.java does complie
    I get the compile error in the RationalTest.java pointing at all the F's on these lines: 21,22,23

    Any help would be appreciated thanks!

    Java 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);
    	}
    }
    Java 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; }	
    
    }
    Last edited by Jtrtoday; 02-14-2012 at 10:35 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,407
    Rep Power
    20

    Default Re: Cannot Find symbol error!

    Delete any .class files that may be left over from a previous compile. Then recompile the classes.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Jtrtoday is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default Re: Cannot Find symbol error!

    I deleted all my previous .class files and recomplied getting the same errors..

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,407
    Rep Power
    20

    Default Re: Cannot Find symbol error!

    Copy and paste the errors here.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Jtrtoday is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default Re: Cannot Find symbol error!

    Here are all the errors hope this helps

    Plain Code:
    [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
    Last edited by Jtrtoday; 02-14-2012 at 11:06 PM.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,407
    Rep Power
    20

    Default Re: Cannot Find symbol error!

    Did you compile Fraction first?

    Are the .java files in a folder named rationaltest?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Jtrtoday is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default Re: Cannot Find symbol error!

    Yes I compiled Fraction.java first, Even if i delete all the class files and recompile Fraction frist I still get same errors when compiling RationalTest.java
    Last edited by Jtrtoday; 02-14-2012 at 11:10 PM.

  8. #8
    Jtrtoday is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default Re: Cannot Find symbol error!

    No their in a folder called Rational2

  9. #9
    Jtrtoday is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default Re: Cannot Find symbol error!

    I moved the files to a new folder and named it RationalTest, and recomiplied with out sucuess im getting the same error.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,407
    Rep Power
    20

    Default Re: Cannot Find symbol error!

    Your package name is rationaltest, not RationalTest. The folder name has to match the package name and you have to compile from the package root: the folder that contains the rationaltest folder.

    You need to go through this Lesson: Packages (The Java™ Tutorials > Learning the Java Language)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    Jtrtoday is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default Re: Cannot Find symbol error!

    Thank you very much i got it fixed

Similar Threads

  1. Cannot find symbol error
    By cannonal in forum New To Java
    Replies: 3
    Last Post: 01-22-2012, 08:09 PM
  2. Cannot find symbol error?
    By Josep_16 in forum New To Java
    Replies: 2
    Last Post: 06-19-2011, 07:14 PM
  3. Cannot find symbol error
    By rajivjoshi in forum New To Java
    Replies: 3
    Last Post: 05-31-2010, 10:13 AM
  4. error cannot find symbol
    By jcoon3 in forum New To Java
    Replies: 3
    Last Post: 09-27-2009, 10:56 PM
  5. Replies: 9
    Last Post: 10-18-2008, 08:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •