#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Creating a program to do math functions with fractions, ran into a compiling issue.


    Hi, guys. I'm taking Intro to Java in college at the moment, and I'm supposed to be creating a program that will add, subtract, multiply, and divide two different fractions with two different constructors. I got it working just fine in Netbeans 7.2 (with the latest JDK), but for some reason, when I compile it in command prompt, I get quite a few "error: cannot find symbol" messages. To elaborate, they also include this:

    "symbol: class Fraction
    location: class TestFraction"

    which I have read online elaborates on what parts are confusing the compiler. The errors are apparently surrounding my Fraction declarations (Fraction a, Fraction b, Fraction x, and Fraction y) at the beginning of my "TestFraction" code and every usage of those fractions in the rest of my code.

    To be as thorough as possible, the Fraction code is supposed to be the two constructors and four math functions, while the TestFraction code is supposed to have two new Fractions to test whether the Constructors and all of the functions are working properly.

    If the denizens of this forum could help me out, I would greatly appreciate it. I've been pulling my hair out over this thing for hours now, and it's probably going to be something obvious that I'm overlooking.

    Here's the Fraction code first:

    Code:
    package fraction;
    
    public class Fraction
    {
        private int numerator;
        private int denominator;
        private int y;
    
        public Fraction () //This is the first constructor
        {
            numerator = 0;
            denominator = 1;
        }
    
        public Fraction(Fraction y) //This is the second constructor.
        {
            numerator = 9;
            denominator = 1;
        }
    
        public Fraction (int numerator, int denominator)
        {
            this.numerator = numerator;
            this.denominator = denominator;
        }
    
        public Fraction add(Fraction a)
        {
            int newNumerator = ((a.numerator*denominator) +
                    (numerator*a.denominator));
            int newDenominator = denominator*a.denominator;
            Fraction x = new Fraction (newNumerator, newDenominator);
            return x;
        }
    
        public Fraction subtract(Fraction a)
        {
            int newNumerator = ((numerator*a.denominator) -
                    (a.numerator*denominator));
            int newDenominator = denominator*a.denominator;
            Fraction x = new Fraction (newNumerator, newDenominator);
            return x;
        }
    
        public Fraction multiply(Fraction a)
        {
            int newNumerator = numerator*a.numerator;
            int newDenominator = denominator*a.denominator;
            Fraction x = new Fraction (newNumerator, newDenominator);
            return x;
        }
    
        public Fraction divide(Fraction a)
        {
            int newNumerator = numerator*a.denominator;
            int newDenominator = denominator*a.numerator;
            Fraction x = new Fraction (newNumerator, newDenominator);
            return x;
        }
    
        public void print()
        {
            System.out.println(this.numerator + "/" + this.denominator);
        }
        
    }
    Now, the TestFraction code:

    Code:
    package fraction;
    
    public class TestFraction
    {
        public static void main(String[] args)
    
        {       
            Fraction a = new Fraction(3,1);
            Fraction b = new Fraction(7,4);
            Fraction x = new Fraction();
            Fraction y = new Fraction();
    
            System.out.println("Our two custom fractions are:");
            a.print();
            b.print();
            
            //This is our test of our first constructor with the first fraction.        
    
            System.out.println("Sum of a and First Constructor:");
            x = new Fraction();
            x = x.add(a);
            x.print();
        
            System.out.println("Subtraction of a and First Constructor:");
            x = new Fraction();
            x = x.subtract(a);
            x.print();
    
            System.out.println("Multiplication of a and First Constructor:");
            x = new Fraction();
            x = x.multiply(a);
            x.print();
    
            System.out.println("Division of a and First Constructor:");
            x = new Fraction();
            x = x.divide(a);
            x.print();
    
            //This is a test of the first constructor with our second fraction.
    
            System.out.println("Sum of b and First Constructor:");
            x = new Fraction();
            x = x.add(b);
            x.print();
    
            System.out.println("Subtraction of b and First Constructor:");
            x = new Fraction();
            x = x.subtract(b);
            x.print();
    
            System.out.println("Multiplication of b and First Constructor:");
            x = new Fraction();
            x = x.multiply(b);
            x.print();
    
            System.out.println("Division of b and First Constructor:");
            x = new Fraction();
            x = x.divide(b);
            x.print();
    
            //Here, we test the second constructor with our first fraction.
    
            System.out.println("Sum of a and Second Constructor:");
            y = new Fraction (y);
            y = a.add(y);
            y.print();
    
            System.out.println("Subtraction of a and Second Constructor:");
            y = new Fraction (y);
            y = a.subtract(y);
            y.print();
    
            System.out.println("Multiplication of a and Second Constructor:");
            y = new Fraction (y);
            y = a.multiply(y);
            y.print();
    
            System.out.println("Division of a and Second Constructor:");
            y = new Fraction (y);
            y = a.divide(y);
            y.print();
    
            //Now to test the Second Constructor with the second Fraction.)
    
            System.out.println("Addition of b and Second Constructor:");
            y = new Fraction (y);
            y = b.add(y);
            y.print();
    
            System.out.println("Subtraction of b and Second Constructor:");
            y = new Fraction (y);
            y = b.subtract(y);
            y.print();
    
            System.out.println("Multiplication of b and Second Constructor:");
            y = new Fraction (y);
            y = a.multiply(y);
            y.print();
    
            System.out.println("Division of b and Second Constructor:");
            y = new Fraction (y);
            y = a.divide(y);
            y.print();
    
            
        }
    
    
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    I get quite a few "error: cannot find symbol" messages.
    That usually means the compiler can not find a definition for the symbol in the error message.
    Check the spelling.
    Make sure the class definitions are on the classpath. Where is the definition of the Fraction class? Can the compiler find it?


    Please post the full text of the error messages.
    Last edited by NormR; September 20th, 2012 at 12:48 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0
    Here's the full error message:

    Code:
    TestFraction.java:8: error: cannot find symbol
            Fraction a = new Fraction(3,1);
            ^
      symbol:   class Fraction
      location: class TestFraction
    TestFraction.java:8: error: cannot find symbol
            Fraction a = new Fraction(3,1);
                             ^
      symbol:   class Fraction
      location: class TestFraction
    TestFraction.java:9: error: cannot find symbol
            Fraction b = new Fraction(7,4);
            ^
      symbol:   class Fraction
      location: class TestFraction
    TestFraction.java:9: error: cannot find symbol
            Fraction b = new Fraction(7,4);
                             ^
      symbol:   class Fraction
      location: class TestFraction
    TestFraction.java:10: error: cannot find symbol
            Fraction x = new Fraction();
            ^
      symbol:   class Fraction
      location: class TestFraction
    TestFraction.java:10: error: cannot find symbol
            Fraction x = new Fraction();
                             ^
      symbol:   class Fraction
      location: class TestFraction
    TestFraction.java:11: error: cannot find symbol
            Fraction y = new Fraction();
            ^
      symbol:   class Fraction
      location: class TestFraction
    TestFraction.java:11: error: cannot find symbol
            Fraction y = new Fraction();
                             ^
      symbol:   class Fraction
      location: class TestFraction
    TestFraction.java:20: error: cannot find symbol
            x = new Fraction();
                    ^
      symbol:   class Fraction
      location: class TestFraction
    TestFraction.java:25: error: cannot find symbol
            x = new Fraction();
                    ^
      symbol:   class Fraction
      location: class TestFraction
    TestFraction.java:30: error: cannot find symbol
            x = new Fraction();
                    ^
      symbol:   class Fraction
      location: class TestFraction
    TestFraction.java:35: error: cannot find symbol
            x = new Fraction();
                    ^
      symbol:   class Fraction
      location: class TestFraction
    TestFraction.java:42: error: cannot find symbol
            x = new Fraction();
                    ^
      symbol:   class Fraction
      location: class TestFraction
    TestFraction.java:47: error: cannot find symbol
            x = new Fraction();
                    ^
      symbol:   class Fraction
      location: class TestFraction
    TestFraction.java:52: error: cannot find symbol
            x = new Fraction();
                    ^
      symbol:   class Fraction
      location: class TestFraction
    TestFraction.java:57: error: cannot find symbol
            x = new Fraction();
                    ^
      symbol:   class Fraction
      location: class TestFraction
    TestFraction.java:64: error: cannot find symbol
            y = new Fraction (y);
                    ^
      symbol:   class Fraction
      location: class TestFraction
    TestFraction.java:69: error: cannot find symbol
            y = new Fraction (y);
                    ^
      symbol:   class Fraction
      location: class TestFraction
    TestFraction.java:74: error: cannot find symbol
            y = new Fraction (y);
                    ^
      symbol:   class Fraction
      location: class TestFraction
    TestFraction.java:79: error: cannot find symbol
            y = new Fraction (y);
                    ^
      symbol:   class Fraction
      location: class TestFraction
    TestFraction.java:86: error: cannot find symbol
            y = new Fraction (y);
                    ^
      symbol:   class Fraction
      location: class TestFraction
    TestFraction.java:91: error: cannot find symbol
            y = new Fraction (y);
                    ^
      symbol:   class Fraction
      location: class TestFraction
    TestFraction.java:96: error: cannot find symbol
            y = new Fraction (y);
                    ^
      symbol:   class Fraction
      location: class TestFraction
    TestFraction.java:101: error: cannot find symbol
            y = new Fraction (y);
                    ^
      symbol:   class Fraction
      location: class TestFraction
    24 errors
    Spelling is fine, and the classpath is set to where they are stored (I've tried changing it around, but when I do, it just won't run anything or even return an error).
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    the classpath is set to where they are stored
    The classpath needs to point to the folder that CONTAINS the package folder,
    not to the folder containing the class files.
    The path to the class files: <classpath><package>
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0
    I just went back and looked at the assignment, and I cannot use any sort of package. I'm not sure if the assignment is possible with that, but I'm told that if I have any sort of subdirectory, it's an automatic zero.

    I appreciate your help, but I may just have to accept defeat on this one.

    (But in reply to your message, I set the classpath to the directory that contains the package folder, and it's unfortunately still not running.)

    EDIT: Unless I can leave the java and class files in a single folder and the package system still work, I mean. I don't think we've gotten there in class yet, so the package setup is a byproduct of my NetBeans usage. I might just look into doing this assignment another way.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    I cannot use any sort of package
    Then why is this in your code:
    Code:
    package fraction;
    You are using a package. Both classes are in that package.

    Try removing the package statement and putting the two java source files in the same folder.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0
    I just went back and looked at the assignment and saw that little bit about subdirectories. If I had seen it before, I wouldn't have started out using a package.

    Anyway, I did what you said, but no luck. Netbeans gives me an error that says "Could not find or load main class fraction.TestFraction", and running javac in cmd doesn't return anything.

    Again, thanks, but I think I'll take the hit on this assignment and start trying to get the next one done early just in case I run into any problems.
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    "Could not find or load main class fraction.TestFraction"
    See that "fraction." part? That is the package name.
    You need to tell the IDE to forget about packages.
    Or you need to learn how to use packages if you are going to use an IDE.

IMN logo majestic logo threadwatch logo seochat tools logo