PDA

View Full Version : [Java] method problem



johnlouis
April 6th, 2007, 09:52 PM
I have a class named Rectangle.java. It is in a package "Geometry" together with Point.java and Line.java. But when I try to use Rectangle.java in my main program, MyRect.java, it gives me a "cannot find symbol" error, particularly the methods and sometimes the variables. I tried compiling just my Rectangle class and it compiled fine.. And I tried the Line and Point classes on another program and it works fine... well probably because the Line and Point classes are from a book.. :book: I am just starting out in Java. :)
Rectangle.java

package Geometry;
public class Rectangle{
public Point[] corner = new Point[4];
public Rectangle(){
corner[0].setPoints(0,0);
corner[1].setPoints(1,0);
corner[2].setPoints(0,1);
corner[3].setPoints(1,1);
}
public Rectangle(double point1_x,double point1_y,double point2_x, double point2_y){
corner[0].setPoints(point1_x, point1_y);
corner[3].setPoints(point2_x, point2_y);
corner[1].setPoints(point2_x, point1_y);
corner[2].setPoints(point1_x, point2_y);
}
public Rectangle(final Rectangle oldRect){
corner[0] = oldRect.corner[0];
corner[3] = oldRect.corner[3];
corner[1] = oldRect.corner[1];
corner[2] = oldRect.corner[2];
}
public double getWidth(){
return corner[0].distance(corner[1]);
}
public static void printRectangle(final Rectangle rect){
for(int i= 0;i<4;i++){
System.out.println("Corner"+(i+1)+" X: "+rect.corner[i].getX()+" Corner"+(i+1)+" Y: \n"+rect.corner[i].getY());
}
}
public String toString(){
return ("Name: "+name);
}
}MyRect.java

import Geometry.*;
public class MyRect{
public static void main(String[] args){
Rectangle myRect = new Rectangle(0.0,0.0,2.0,1.0);
printRectangle(myRect);
}
}and the errors:

MyRect.java:5: cannot find symbol
symbol : method printRectangle(Rectangle)
location: class MyRect
printRectangle(myRect);
^
1 error

johnlouis
April 7th, 2007, 11:31 AM
bump :)

Krilnon
April 7th, 2007, 03:11 PM
First of all, the errors that you listed won't exist given the code that you posted. You never use the Rectangle constructor with the 5th String argument, and that call to println doesn't exist, either.

Anyway, it kind of seems like you're having trouble with compilation, as your Rectangle class has the appropriate constructor, so it shouldn't be generating that error. Are you using an IDE or are you compiling directly with javac? Usually IDEs will handle packages for you, but if not, then you might not have placed your class files correctly. Rectangle.java should be in a folder named Geometry somewhere in your classpath, is it?

johnlouis
April 8th, 2007, 06:35 AM
oh crap, i'm sorry, the Rectangle.java file doesn't have the String parameter in the constructor anymore.. i updated it. But is that String argument causing problems? assuming i have a "My Rectangle" argument added to my 'new' line..

And i think my packages are in their correct place. I have another main program, which i copied from the book, in the same directory as MyRect.java using the package Geometry, and it worked fine. And I am using javac in the command line and I use the same command to compile it, with different source file name of course..

thanks for the response :)

Krilnon
April 8th, 2007, 02:52 PM
But is that String argument causing problems?

If you give a set of arguments to a constructor that doesn't match one of the defined constructors, then yes, problems/errors will exist.

You can't access the 'name' property anymore because you removed it from the class definition.

johnlouis
April 8th, 2007, 11:18 PM
ok i updated the codes above... i removed all the 'name' references but i still get the "cannot find symbol" problem..

Krilnon
April 9th, 2007, 01:14 AM
Rectangle.printRectangle(myRect);

johnlouis
April 9th, 2007, 10:05 PM
so i need to use the 'full name' of the printRectangle() method? i'll try that out later when i get to my PC.. i thought it would be OK because i imported the whole package..

Voetsjoeba
April 9th, 2007, 10:12 PM
so i need to use the 'full name' of the printRectangle() method? i'll try that out later when i get to my PC.. i thought it would be OK because i imported the whole package..

printRectangle is a static method, so you must call it using Rectangle.printRectangle. It has nothing to do with whether it's been imported or not.

Jeff Wheeler
April 9th, 2007, 10:18 PM
On the other hand, if you had imported the Geometry classes statically, you would be able to call static methods in a way similar to what you want.

http://java.sun.com/j2se/1.5.0/docs/guide/language/static-import.html

This is only available in 5.0, and I greatly despise it!

johnlouis
April 11th, 2007, 09:49 AM
i changed it to "Rectangle.printRectangle(myRect);", but i still get an error..

MyRect.java:5: cannot find symbol
symbol : method printRectangle(Rectangle)
location: class Rectangle
Rectangle.printRectangle(myRect);
^
1 error