Results 1 to 9 of 9
  1. #1
    bobleny's Avatar
    bobleny is offline Member
    Join Date
    Apr 2008
    Posts
    50
    Rep Power
    0

    Default [SOLVED] Java Error: Cannot find Symbol...

    Please excuse the incredibly stupid nature of this application... It is the most boring and short application I have been instructed to write....

    The error is:
    Cannot find symbol
    Symbol: Constructor Square()
    Location: Class Square

    Cube.java
    Java Code:
    public class Cube extends Square
    {
        private int height;
        
        public Cube(int x)
        { // <-- NetBeans shows the error at this location!
    	   height = x;
        }
        
        public int area()
        {
            int area = (length * width * height);
            return area;
        }
    }

    Square.java
    Java Code:
    public class Square
    {
        protected int length;
        protected int width;
    
        public Square(int x, int y)
        {
            length = x;
            width = y;
        }
        
        public int area()
        {
            int area = (length * width);
            return area;
        }
    }

    I don't know what it is talking about, I'm sure I screwed up somehow... But, the constructor is clearly there...

    I'm sure it is something stupid but I've gone through over and over, and I just don't see the problem.

    Can anyone please help?

    Thanks!
    -- www.firemelt.net --
    Cheer up, the worst has yet to come...

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    The constructor is there, but look - the one that's there is not a default constructor(no parameters), but one that has an x and y of int for a parameter. The error tells you it can't find the default constructor.. for whatever reason.... I can't see why it's looking for it without more code, maybe another method is looking for it but it never will find it, hence the error. Look into a term or concept called overloaded for more help. Are the two constructors below equal?

    public Square() {}
    public Square(int x, int y) {...}

    Answer, no. :)
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  3. #3
    Jman is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default

    Hi bobleny.

    I managed to fix your script but like what CaptainMorgan said have a little read about overloaded and also what the [B]super[B] key word is used for. just to help you, when you extend a class the child class gets all the methods the parent class has. And if you realy cant get it I will show you where the problem is.

  4. #4
    bobleny's Avatar
    bobleny is offline Member
    Join Date
    Apr 2008
    Posts
    50
    Rep Power
    0

    Default

    Well, like the Captain said, simply adding a standard Square() solved the "problem"...

    But, I know you don't need to do that, I've seen many applications that don't have a standard constructor.......


    And as far as the super keyword goes, I thought that was only for when you want to call a method in the super class?
    Not that it matters, it doesn't work anyways.........
    -- www.firemelt.net --
    Cheer up, the worst has yet to come...

  5. #5
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Quote Originally Posted by bobleny View Post
    Well, like the Captain said, simply adding a standard Square() solved the "problem"...

    But, I know you don't need to do that, I've seen many applications that don't have a standard constructor.......
    The only reason the compiler is complaining about a default constructor is because somewhere in your code you are explicitly making a call to one. Many applications do not have default constructors - this is true. In these applications the assumption is safe to say that they're not making a call to a default constructor, otherwise they'd never compile. Find your call to the default constructor and you'll understand why the compiler is complaining. Remove the call and then the compiler won't complain. In your case, the call is from the constructor for Cube(or all subclasses for that matter), which will make calls to their parent's default constructor first. :)

    And as far as the super keyword goes, I thought that was only for when you want to call a method in the super class?
    Not that it matters, it doesn't work anyways.........
    Yes, calls to the parent class can be used via the keyword super. In your case, you can try super.area() to call the parent's area method.

    Hope this clear things up.. ;)
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  6. #6
    bobleny's Avatar
    bobleny is offline Member
    Join Date
    Apr 2008
    Posts
    50
    Rep Power
    0

    Default

    OK, I didn't know that subclasses call its superclass's default constructor.....

    Now, once Netbeans stopped complaining, I was able to test my application. It doesn't work. Go figure.... lol

    Here is the third and final file to this application:
    Test.java
    Java Code:
    public class Test
    {
        public static void main(String[] args)
        {
            Square square = new Square(10, 10);
            Cube cube = new Cube(10);
    
            int squareArea = square.area();
            int cubeArea = cube.area();
    
            System.out.print("The area of the cube is: "+ cubeArea +"\nThe area of the Square is: "+ squareArea);
        }
    }
    My book really disappointed me in this chapter. The only thing it told me was:
    super
    extends
    and, well that's about it.:confused:


    The exercise for this chapter also isn't very helpful.... This is what I got out of what they told me to do... (I have no idea as to why anyone would want to do this!)

    The objective is to calculate the surface area of the square with it's area method. Then compute the area for the cube with it's area method whilst inheriting the rest from it's supper class. (The only thing it could inherit is the width and length variables. And even then...)

    I'm confused, is there a way to build the cube object off of the square object?


    My results for Test.java is a 0 for the cube, and a 100 for the square.


    ----
    Maybe it's just me, but the way I code, I can't think of any time in which I would ever need to inherent another class. If it is so different that I need to write another class, then what are the chances of me "needing" to inherent a method....
    Last edited by bobleny; 04-15-2008 at 06:01 AM.
    -- www.firemelt.net --
    Cheer up, the worst has yet to come...

  7. #7
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    The problem is that when you make the call to the area() method which wants to know the value of the Square's length and width to compute the Cube's area, the result returned is zero. It's obvious this is not intended- but this is logically correct. Member primitives of a class are initialized to zero, and when you call area from cube, the square is not initialized with the value of the instantiated Square before it Square square = new Square(10, 10); To use these values(pardon my reformatting below.. it's a bad habit), I altered your Cube's area method to accept a Square parameter, which can now use the values to compute the correct answer. I'm sure you can do this a dozen different ways.. nevertheless. See below:
    Square
    Java Code:
    public class Square {
      protected int length;
      protected int width;
    
      public Square() {}
    
      public Square(int x, int y) {
        length = x;
        width = y;
      }
    
      public int area() {
        return (length * width);    
      }
    }
    Cube
    Java Code:
    public class Cube extends Square {
      private int height;
    
      public Cube(int x) { 
        height = x;
      }
    
      public int area(Square s) {
        return (s.length * s.width * height);
      }
      
      public static void main(String[] args) {
        Square square = new Square(10, 10);
        Cube cube = new Cube(10);
    
        System.out.print("The area of the cube is: "+ cube.area(square) +
                         "\nThe area of the Square is: "+ square.area());
      }   
    }
    Now your output, what I assume based on the inputs, shows:
    Java Code:
    The area of the cube is: [B]1000[/B]
    The area of the Square is: 100
    :)
    Last edited by CaptainMorgan; 04-15-2008 at 06:20 AM.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  8. #8
    bobleny's Avatar
    bobleny is offline Member
    Join Date
    Apr 2008
    Posts
    50
    Rep Power
    0

    Default

    Hey, it works! Thanks!

    I never would have thought to that...
    -- www.firemelt.net --
    Cheer up, the worst has yet to come...

  9. #9
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    My pleasure. :)
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

Similar Threads

  1. Programm Error: cannot find symbol Help?
    By junix in forum New To Java
    Replies: 2
    Last Post: 12-10-2007, 06:30 AM
  2. cannot find symbol class error
    By po0oker in forum New To Java
    Replies: 5
    Last Post: 10-31-2007, 03:52 PM
  3. Error: cannot find symbol
    By silvia in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:39 AM
  4. Error: cannot find symbol
    By cachi in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 09:12 PM
  5. Error: cannot find symbol constructor
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 09:24 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
  •