2 Replies - 658 Views - Last Post: 19 May 2012 - 05:16 AM Rate Topic: -----

#1 zesverige  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-May 12

Cannot Find Symbol Error! in main method.

Posted 19 May 2012 - 03:48 AM

Hello!,

I have some code here, and i am receiving the the "Cannot find symbol error", its driving me crazy.. my assignment told me to place it in the main method. Is that not the main? (sorry if stupid question, but i am new to java!), i have attempted to move the main method into the other class, but i am still getting the error, then i put under constructor which also doesn't work..sorry i am slightly confused :D.

The code is below.. please take a look!

Thanks for your time.

-ZeZverige

package lab5;

import java.util.*;
import java.awt.*;
import java.applet.*;


public class Lab5 {

        abstract class DrawableShape {

            private int x;
            private int y;

            // constructor
            DrawableShape(int newx, int newy) {
                moveTo(newx, newy);
            }

            // accessors for x & y
            int getX() {
                return x;
            }

            int getY() {
                return y;
            }

            void setX(int newx) {
                x = newx;
            }

            void setY(int newy) {
                y = newy;
            }

            // move the x & y position
            void moveTo(int newx, int newy) {
                setX(newx);
                setY(newy);
            }

            void rMoveTo(int deltax, int deltay) {
                moveTo(getX() + deltax, getY() + deltay);
            }

            // virtual draw method
            abstract void draw();
            
        }
        
        class Square extends DrawableShape {
   private int width;
   private int height;

   // constructor
   Square(int newx, int newy, int newwidth, int newheight) {
      super(newx, newy);
      setWidth(newwidth);
      setHeight(newheight);
   }

   // accessors for the width & height
   int getWidth() { return width; }
   int getHeight() { return height; }
   void setWidth(int newwidth) { width = newwidth; }
   void setHeight(int newheight) { height = newheight; }

   // draw the square
   void draw() {
      System.out.println("Drawing a Rectangle at:(" + getX() + ", " + getY() +
         "), width " + getWidth() + ", height " + getHeight());
   }
}
    }



and i have the main also-


package lab5;

public class drawing {

    public static void main(String[] args) {

        Square sq = new Square(5);  // <--------- THIS IS THE ERROR  "Square" is redlining.
        sq.moveBy(7, 7);
        sq.draw();

    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: Cannot Find Symbol Error! in main method.

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Cannot Find Symbol Error! in main method.

Posted 19 May 2012 - 04:05 AM

Since the class Square is a nested class of class Lab5, you'll have to import it as in:

import lab5.Lab5.Square;

Alternatively, you could refer to it with its fully qualified name,

lab5.Lab5.Square sq = new lab5.Lab5.Square(5);

You'll then have other problems with Square's constructors and methods, but let us know if you need help with those. You might also reconsider your design and determine whether Square should be a nested class.
Was This Post Helpful? 0
  • +
  • -

#3 zesverige  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-May 12

Re: Cannot Find Symbol Error! in main method.

Posted 19 May 2012 - 05:16 AM

View PostGregBrannon, on 19 May 2012 - 04:05 AM, said:

Since the class Square is a nested class of class Lab5, you'll have to import it as in:

import lab5.Lab5.Square;

Alternatively, you could refer to it with its fully qualified name,

lab5.Lab5.Square sq = new lab5.Lab5.Square(5);

You'll then have other problems with Square's constructors and methods, but let us know if you need help with those. You might also reconsider your design and determine whether Square should be a nested class.


Thanks for the input, am i just replacing the "Square sq = new Square(5);" line with "lab5.Lab5.Square sq = new lab5.Lab5.Square(5);"

Cheers
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1