Results 1 to 9 of 9
- 04-14-2008, 06:32 PM #1
[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...
- 04-14-2008, 09:01 PM #2
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)
- 04-14-2008, 09:17 PM #3
Member
- Join Date
- Dec 2007
- Posts
- 17
- Rep Power
- 0
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.
- 04-15-2008, 04:13 AM #4
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...
- 04-15-2008, 04:23 AM #5
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. :)
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.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.........
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)
- 04-15-2008, 04:58 AM #6
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
My book really disappointed me in this chapter. The only thing it told me was: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); } }
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 05:01 AM.
-- www.firemelt.net --
Cheer up, the worst has yet to come...
- 04-15-2008, 05:18 AM #7
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
CubeJava 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); } }
Now your output, what I assume based on the inputs, shows: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()); } }
:)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 05: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)
- 04-15-2008, 06:11 AM #8
Hey, it works! Thanks!
I never would have thought to that...-- www.firemelt.net --
Cheer up, the worst has yet to come...
- 04-15-2008, 06:35 AM #9
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
-
Programm Error: cannot find symbol Help?
By junix in forum New To JavaReplies: 2Last Post: 12-10-2007, 05:30 AM -
cannot find symbol class error
By po0oker in forum New To JavaReplies: 5Last Post: 10-31-2007, 02:52 PM -
Error: cannot find symbol
By silvia in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:39 AM -
Error: cannot find symbol
By cachi in forum AWT / SwingReplies: 1Last Post: 08-06-2007, 08:12 PM -
Error: cannot find symbol constructor
By zoe in forum New To JavaReplies: 1Last Post: 07-24-2007, 08:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks