2

Okay, so I'm very new to Java, and I have no prior programming experience. I'm going through the Java tutorial, and everything is going alright until I run into a problem in the "Objects" section the tutorial.

The title of the program is Create Object Demo. The goal is to find the width, height, and area of one rectangle, and the new position of another. You do all of this using the premise of "Creating an object." The creating the object part is the problem.

Here's the original code:

public class CreateObjectDemo { public static void main(String[] args) { // Declare and create a point object and two rectangle objects. Point originOne = new Point(23, 94); Rectangle rectOne = new Rectangle(originOne, 100, 200); Rectangle rectTwo = new Rectangle(50, 100); // display rectOne's width, height, and area System.out.println("Width of rectOne: " + rectOne.width); System.out.println("Height of rectOne: " + rectOne.height); System.out.println("Area of rectOne: " + rectOne.getArea()); // set rectTwo's position rectTwo.origin = originOne; // display rectTwo's position System.out.println("X Position of rectTwo: " + rectTwo.origin.x); System.out.println("Y Position of rectTwo: " + rectTwo.origin.y); // move rectTwo and display its new position rectTwo.move(40, 72); System.out.println("X Position of rectTwo: " + rectTwo.origin.x); System.out.println("Y Position of rectTwo: " + rectTwo.origin.y); } } 

I run the program, and this is my error message:

CreateObjectDemo:.java:6: error: cannot find symbol Point originOne = new Point(23, 94); ^ symbol: class Point location: class CreateObjectDemo CreatObjectDemo.java:6: error: cannot find symbol Point originOne = new Point(23, 94); ^ 

The full code and process is also located here

Likewise, the error message points to the word "Point" and "Rectangle" in the same fashion and claims that it "cannot find [the] symbol."

Any help would be greatly appreciated. I've been struggling with this error for a couple of days now. Thanks.

2
  • You need to download the Point and Rectangle classes (in the link that you specified), add import statements as appropriate, and then include them in your classpath when you compile. That particular error is the compiler saying, "Wait, you're referencing these classes but I don't know what they are!" Commented Jul 29, 2013 at 1:22
  • 1
    Nobody leaves the room! Who got the symbol? Commented Jul 29, 2013 at 1:27

4 Answers 4

3

You will have to import the classes Point and Rectangle to your class. Add the following two lines on the top of your class, after the package line.

import java.awt.Point; import java.awt.Rectangle; 

If you are using Eclipse then just do CtrlShiftO, this will import the required classes for you.

Sign up to request clarification or add additional context in comments.

11 Comments

I would probably just import java.awt.*; and be done with it.
No, I made the same mistake. He's following a demo which is using a custom Point and rectangle class. It may be that the awt point and rectangle will work, but may not. he needs to download the other classes to follow the tutorial precisely, as suggested by Roddy in comments.
the shortcut neo suggests will import the exact class types required @Bohemian also importing a specific class type makes the dependencies easier to see.
Thanks @WilliamMorrison. I assumed it is the awt classes the OP is referring to.
Agree with you @WilliamMorrison :-)
|
1

As Roddy suggests, you need to download and include the Point and Rectangle classes in the tutorial you are following. If you place those classes in the same directory as your CreateObjectDemo you'll not need import statements.

Exactly what's happening is the compiler is trying to convert your Java source code into byte code which the JVM (Java Virtual Machine) can interpret. For code to compile, all classes must be found. As the Point and Rectangle classes cannot be found, your code can't compile. Class Point and Rectangle are referred to as dependencies of class CreateObjectDemo for this reason. CreateObjectDemo won't work without them, it is dependent on Point and Rectangle.

Fixing this is easy, just make sure your CreateObjectDemo class knows where the Point and Rectangle classes are.

Comments

0

For the CreateObject Demo there are two other classes in the tutorial you need to put in the same directory as the demo: the Point Class, and the Rectangle Class. They are described a couple of paragraphs down from the code of the demo.

So in netbeans and eclipse, put them in the same directory as CreateObjectDemo.java.

So in netbeans, go to File->New File->Java->Java Class->Class Name: Point etc

You do not need to import anything.

Hope this helps.

Comments

0

The tutorial is not learning about GUI but just showing how to calculate the rectangle area using a method, so it is not necessary to import java.awt in the CreateObjectDemo.class. Just make sure that the Point and Rectangle classes are put in the same package with the CreateObjectDemo class, the error will surely disappear.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.