Results 1 to 3 of 3
  1. #1
    eindoofus is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default Did I compile this wrong?

    I was able to compile some code fine on ideone.com with only one import statement but when I try to compile the files on my local computer I get some errors. I'm not sure if I'm doing something wrong.

    Here is some more info:

    I copy and pasted the following two classes into the ideone.com web-based compiler and I didn't run into any problems upon compiling:

    Java Code:
    /****************************************
    *****************************************/
    /**
     * A class for numeric progressions.
     */
    
    import java.math.BigInteger;
    // public class Progression {  CHANGED IN ORDER TO COMPILE ON IDEONE.COM
    class Progression {
      /** First value of the progression.  */
      protected BigInteger first;
    
      /** Current value of the progression.  */
      protected BigInteger cur;
    
      /** Default constructor.  */
      Progression() {
        cur = first = BigInteger.ZERO;
      }
    
      /** Resets the progression to the first value.
       * 
       * @return first value
       */
      protected BigInteger firstValue() {
        cur = first;
        return cur;
      }
    
      /** Advances the progression to the next value.
       *
       * @return next value of the progression
       */
      protected BigInteger nextValue() {
        
        cur = cur.add(BigInteger.ONE);
        return cur; // default next value
      }
    
      /** Prints the first n values of the progression.
       * 
       * @param n number of values to print
       */
      public void printProgression(int n) {
        System.out.print(firstValue());
        for (int i = 2; i <= n; i++) 
          System.out.print(" " + nextValue());
        System.out.println(); // ends the line
      }
    
       
    }
    
    /****************************************
    *****************************************/
    /**
     * Arithmetic progression.
     */
    
    
    class ArithProgression extends Progression {
    
      /** Increment. */
      protected BigInteger inc;
    
      // Inherits variables first and cur.
    
      /** Default constructor setting a unit increment. */
      ArithProgression() {
        this(BigInteger.ONE);
      }
    
      /** Parametric constructor providing the increment. */
      ArithProgression(BigInteger increment) {
        inc = increment; 
      }
    
      /** Advances the progression by adding the increment to the current value.
       * 
       * @return next value of the progression
       */
      protected BigInteger nextValue() {
        cur = cur.add(inc);
        return cur;
      }
    
      //  Inherits methods firstValue() and printProgression(int).
    }
    However after I pasted the code into two java files on my local computer and tried to compile them:

    Progression.java
    Java Code:
    /**
     * A class for numeric progressions.
     */
    
    import java.math.BigInteger;
    // public class Progression {  CHANGED IN ORDER TO COMPILE ON IDEONE.COM
    class Progression {
      /** First value of the progression.  */
      protected BigInteger first;
    
      /** Current value of the progression.  */
      protected BigInteger cur;
    
      /** Default constructor.  */
      Progression() {
        cur = first = BigInteger.ZERO;
      }
    
      /** Resets the progression to the first value.
       * 
       * @return first value
       */
      protected BigInteger firstValue() {
        cur = first;
        return cur;
      }
    
      /** Advances the progression to the next value.
       *
       * @return next value of the progression
       */
      protected BigInteger nextValue() {
        
        cur = cur.add(BigInteger.ONE);
        return cur; // default next value
      }
    
      /** Prints the first n values of the progression.
       * 
       * @param n number of values to print
       */
      public void printProgression(int n) {
        System.out.print(firstValue());
        for (int i = 2; i <= n; i++) 
          System.out.print(" " + nextValue());
        System.out.println(); // ends the line
      }
    
       
    }
    ArithProgression.java
    Java Code:
    /**
     * Arithmetic progression.
     */
    
    
    class ArithProgression extends Progression {
    
      /** Increment. */
      protected BigInteger inc;
    
      // Inherits variables first and cur.
    
      /** Default constructor setting a unit increment. */
      ArithProgression() {
        this(BigInteger.ONE);
      }
    
      /** Parametric constructor providing the increment. */
      ArithProgression(BigInteger increment) {
        inc = increment; 
      }
    
      /** Advances the progression by adding the increment to the current value.
       * 
       * @return next value of the progression
       */
      protected BigInteger nextValue() {
        cur = cur.add(inc);
        return cur;
      }
    
      //  Inherits methods firstValue() and printProgression(int).
    }
    "javac Progression.java" compiles fine but "javac ArtihProgression.java" spits out the following problems:

    ArithProgression.java:13: cannot find symbol
    symbol : class BigInteger
    location: class ArithProgression
    protected BigInteger inc;
    ^
    ArithProgression.java:23: cannot find symbol
    symbol : class BigInteger
    location: class ArithProgression
    ArithProgression(BigInteger increment) {
    ^
    ArithProgression.java:31: cannot find symbol
    symbol : class BigInteger
    location: class ArithProgression
    protected BigInteger nextValue() {
    ^
    ArithProgression.java:19: cannot find symbol
    symbol : variable BigInteger
    location: class ArithProgression
    this(BigInteger.ONE);
    ^
    4 errors

    Am I compiling this wrong or do I have to put "import java.math.BigInteger;" into every file that extends "Progression.java" as well?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,313
    Blog Entries
    7
    Rep Power
    25

    Default

    Quote Originally Posted by eindoofus View Post
    Am I compiling this wrong or do I have to put "import java.math.BigInteger;" into every file that extends "Progression.java" as well?
    You have to import that class in every file that just even mentions the BigInteger class; the compiler doesn't guess nor assumes anything so you simple have to import that class to tell the compiler what class you're talking about.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    6

    Default

    or if the number of occurences are small, you can just write java.math.BigInteger each time instead of importing it (if the import bothers you that much :P)

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. would this compile?
    By stringkilla in forum New To Java
    Replies: 10
    Last Post: 10-24-2010, 03:27 PM
  3. Can't Compile
    By sidk47 in forum JavaServer Pages (JSP) and JSTL
    Replies: 7
    Last Post: 06-15-2010, 04:43 PM
  4. doesn't compile?!
    By jon80 in forum New To Java
    Replies: 8
    Last Post: 06-14-2008, 05:42 PM
  5. Not able to compile
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-09-2008, 11:13 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
  •