• Post Reply Bookmark Topic Watch Topic
  • New Topic

problems assigning values to the elements of a two dimensional array  RSS feed

 
Mohammed Azeem
Ranch Hand
Posts: 56
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I'm now ready to code for my own simple scenarios - just to enhance my skills really. But I've already run into a problem which should be simple to solve but I just can't think how.

I'm storing the two points of a rectangle as an 2 x 2 integer array. The points are: P1(100,100) and P2(300,300). The 2x2 array has been declared as rectangle[][].

The problem comes when I try to assign a value to the first element: rectange[0][0] = 100; Netbeans thinks the array rectangle is a class and reports a "cannot find symbol" error.

I just can't see where I've gone wrong. I've been struggling for two days!

The code is below:

 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please post the complete code and the error message.
 
Mohammed Azeem
Ranch Hand
Posts: 56
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou Praveen. Its my ever post to anything and I am amazed of such a quick response.

I have a logical plan for this trivial project. I know that there are classes and methods out there that would achieve what I want to do more easily but I just wanted to do this as a learning exercise.

The plan is:

* define a rectangle by specifiying the coordinates of the opposite corners.
* display the rectangle
* perform a linear transformation upon the rectangle (i.e rotate it, translate, scale it, reflect it or shear it)
* display the transformed rectangle.


However, I have not written any further lines of code since I cannot resolve the problem netbeans has flagged up.

Please two screenshots of the code: one displaying the error.

Sorry, the screenshots are on my desktop so can't upload them to here.




 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your plan seems fine. You dont need to upload screenshots, you can just post the text of the full class that is throwing up the error. I'm sure these 4 lines are just a part of the full MyPanel class.
 
Mohammed Azeem
Ranch Hand
Posts: 56
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I haven't got more code to show you but I will post the code that class MyPanel would have been if Netbeans hadn't flagged up this error.

Give me an hour or so.

I didn't see the point of writing any more code until I had figured out why Netbeans thought that an array I had specifically declared was a class.

Speak to you soon.
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh! This is an incomplete program Mohammed, a standard Java program's syntax is as below


Can you see that you are missing several parts here (including braces). Thats why you are getting some errors.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammed Azeem wrote:
I didn't see the point of writing any more code until I had figured out why Netbeans thought that an array I had specifically declared was a class.


Wait a second.... Are you saying that you posted the complete program? You didn't post a snippet?


If so, then your issue is... You can't just have code anywhere. Within a class, they have to be a declaration of the class, or part of an initializer, constructor, or method. In your code, line 7 doesn't satisfy any of these conditions.

Also, you are missing a closing brace for your class.

Henry
 
Mohammed Azeem
Ranch Hand
Posts: 56
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yes Praveen.

MyPanel was a complete yesterday with these methods: public int[][] dorotate(double angle) and a paint method public void paint(Graphics g).

MyPanel was passed to the contentPane of a JApplet and the applet was called by a browser.

The dorotate() method returned a transformed integer array object which held the coordinates of the transformed rectangle.

However I deleted all the lines and started over when I could not resolve the problem I have describe in this post. The problem persisted whether or not the rest of the code was there.

Is the assignment statement rectangle[0][0] = 100; (the x-coordinate of the first corner) correct? I think it is, but I can't understand why Netbeans thinks it is not.

Thankyou Praveen, I really appreciate your help.



 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without seeing the full code and the error, I can only guess here on in...Anyways, as Henry has pointed out, the statement rectangle[0][0] = 100 is at a wrong place. Its an assignment statement and is an executable piece of code and this should not go into the declaration area(declaration of variables). It would be fine if you had done something like this,

 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My, there seems to be a lot of confusion in this discussion.
Apart from the fact that there is no such thing as a 2D array, only an array of arrays, you seem really confused. It sounds to me as if you had simply dived in to write code, without thinking about the design or anything. I suggest you
  • Stop using NetBeans for the time being.
  • Move the rectangles out of the panel class.
  • Create a rectangle class.
  • Consider what the attributes of a rectangle are.
  • Work out how to write a class which represents a rectangle. It will be a simple case of constructor, fields, getXXX methods, toString equals and hashCode.
  • Consider whether to make the rectangle class mutable or immutable. You might need setXXX methods, too.
  • Get that lot working from the command line, so you can print out something like:
    Rectangle at (123, 234) width 345 height 456
    Then you can transfer those details to a GUI and display it.

    Only now should you consider rotation, etc. Are you are going to use an AffineTransform to rotate your rectangle, or are you going to apply the transform yourself with matrix arithmetic?
     
    Campbell Ritchie
    Marshal
    Posts: 55772
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Why do people assume you want an int[2][2] in the first place? That is not the way to describe a rectangle, but one way. There are other ways; you might define the coordinates of one corner, width and height.
     
    Mohammed Azeem
    Ranch Hand
    Posts: 56
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    OK. Right. Now I am getting something!

    That's really helpful Praveen and Campbell.

    I think I might run with the idea that the assignment statement rectangle[0][0] = 100; is in the wrong place. Suggestions from both of you have given me some glimmers of ideas.

    Campbell I have drawn rectangles using the method drawRect(x, y, width, height) of Graphics class previous experiments - again using an JApplet to display them.
    This time I wanted to define a rectangle as a 2x2 matrix or if you prefer an array of arrays just in order to get some experience of coding matrix multiplication to effect the rotation transform. I agree its not the simplest definition of a rectangle.

     
    Campbell Ritchie
    Marshal
    Posts: 55772
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    But put the coordinates into a rectangle class. I don’t think it matters whether you use x, y, width, height or x1, y1, x2, y2. Both are valid ways to define a rectangle. And each has its advantages; you display x, y, w, h, and you can use x1, y1, x2, y2 for matrix arithmetic. Each has its disadvantages, too.

    Agree. Yes, you are getting somewhere.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!