|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

04-14-2008, 07:32 PM
|
 |
Member
|
|
Join Date: Apr 2008
Posts: 36
|
|
|
[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
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
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!
__________________
-- To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. --
Cheer up, the worst has yet to come...
|
|

04-14-2008, 10:01 PM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 840
|
|
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. 
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. !
Got a little Capt'n in you? (drink responsibly)
|
|

04-14-2008, 10:17 PM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 17
|
|
|
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, 05:13 AM
|
 |
Member
|
|
Join Date: Apr 2008
Posts: 36
|
|
|
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.........
__________________
-- To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. --
Cheer up, the worst has yet to come...
|
|

04-15-2008, 05:23 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 840
|
|
Originally Posted by bobleny
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.. 
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. !
Got a little Capt'n in you? (drink responsibly)
|
|

04-15-2008, 05:58 AM
|
 |
Member
|
|
Join Date: Apr 2008
Posts: 36
|
|
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
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.
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....
__________________
-- To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. --
Cheer up, the worst has yet to come...
Last edited by bobleny : 04-15-2008 at 06:01 AM.
|
|

04-15-2008, 06:18 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 840
|
|
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
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
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:
The area of the cube is: 1000
The area of the Square is: 100

__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. !
Got a little Capt'n in you? (drink responsibly)
Last edited by CaptainMorgan : 04-15-2008 at 06:20 AM.
|
|

04-15-2008, 07:11 AM
|
 |
Member
|
|
Join Date: Apr 2008
Posts: 36
|
|
|
Hey, it works! Thanks!
I never would have thought to that...
__________________
-- To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. --
Cheer up, the worst has yet to come...
|
|

04-15-2008, 07:35 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 840
|
|
My pleasure. 
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. !
Got a little Capt'n in you? (drink responsibly)
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|