Skip to main content

problems assigning a value to an array element

3 replies [Last post]
azeemofwalsall
Offline
Joined: 2012-08-17
Points: 0

Hello everyone.

I'm a beginner at Java so please don't laugh!

I'm storing the coordinates of a rectangle in a 2x2 integer array but I can't seem to assign the individual elements. I re-read the textbooks and re-studied the java tutorials.

What am I doing wrong in the assignment? I can't get past this point.

Thankyou for looking at it. Hope you can help.

PS: The indents are lost when I upload this message to the forum - I don't know why.

Netbeans reports this error:

Cannot find symbol:
symbol: class rectangle
location: class myPanel

']' expect4ed

---------------------------------------------------------------
The code:

</pre>
/*
* Defines a rectangle: an array of 2 points in xy space.
* Performs a rotation transform
* Displays the rectange
* Displays a random number from 1 to 6 within the rectangle
*/

/**
*
* @author Azeem
*/

import javax.swing.JPanel;

public class MyPanel extends JPanel {
   
    int[][] rectangle = new int[2][2];
   
    rectangle[0][0] = 100; // x-coordinate of first corner

(Netbeans flags up an error with this assignment statement)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
azeemofwalsall
Offline
Joined: 2012-08-17
Points: 0

Thankyou pzweilin !

I have resolved the problem and along the way I have been forced to re-learn almost everything in Java - things that I thought I already understood !!

It's really true: mistakes teach you and sometimes make you question everything.

My first substantial program, it works, it feels great.

The broad plan (just to give me experience in coding matrix multiplication) was:

Define a class: MyRectangle which defines a rectangle object by specifying its two opposite corners.

Assign the coordinates of the four points to a (2 X 4) integer array (int rectarray[][])

Declare a method: doRotate() which applies a rotation transform fo 30 degrees.

Declare a method: displayoriginalrectangle() that displays the coordinates of the original recatangle

Declare a method: displayproductmatris() that displays the coordinates of the transformed rectangle

Define a class UseMyRectangle which created a rectangle object and then displayed (as text) the coordinates of the original rectangle and the rotated rectangle.

Thankyou for your reply. All replies are greatly appreciated.

Heres the finished code:

import static java.lang.System.out;
</pre>
<pre>public class MyRectangle {

</pre>    
<pre>    int x1, y1, x2, y2, x3, y3, x4, y4;

</pre>    
<pre>    int[][] rectarray = new int[2][4];

    int[][] productmatrix = new int[2][4];
</pre>    
<pre>    public MyRectangle(int x1, int y1, int x2, int y2) {

        rectarray[0][0] = x1;
        rectarray[1][0] = y1; // first corner P1 (x1,y1)
</pre>        
<pre>        rectarray[0][1] = x2;

        rectarray[1][1] = y1; // second corner P2(x2=x3),(y2=y1)
</pre>        
<pre>        rectarray[0][2] = x2;

        rectarray[1][2] = y2; // opposite (third) corner P3(x3,y3)
</pre>         
<pre>        rectarray[0][3] = x1;

        rectarray[1][3] = y3; // fourth corner P4(x4=x1),(y4=y3)
</pre>        
<pre>    }

</pre>    
<pre>    public int[][] doRotate() {

</pre>        
<pre>        int i, j, k;

        double transform[][] = new double[2][2]; // the transform matrix
</pre>       
<pre>        transform[0][0] = 0.8660;                // rotation by 30 deg.

        transform[0][1] = -0.5;
        transform[1][0] = 0.5;
        transform[1][1] = 0.8660;
</pre>        
<pre>

        for (i=0; i<=1; i++) {
            for (j=0; j<=3; j++) {
                for (k=0; k<=1; k++) {
                    productmatrix[i][j] += transform[i][k]*rectarray[k][j];
                    productmatrix[i][j] = Math.round(productmatrix[i][j]);
                }
            }
        }
</pre>        
<pre>        return productmatrix;

</pre>        
<pre>    }

</pre>    
<pre>    public void displayrectarray() {

        int i, j;
        for (i=0; i<=1; i++) {
            for (j=0; j<=3; j++) {
                System.out.print(i + " ");
                System.out.print(j + " ");
                System.out.println(rectarray[i][j]);
            }
        }
    }
</pre>    
<pre>

    public void displayproductmatrix() {
        int i, j;
        for (i=0; i<=1; i++) {
            for (j=0; j<=3; j++) {
                System.out.print(i + " ");
                System.out.print(j + " ");
                System.out.println(productmatrix[i][j]);
            }
        }
</pre> 
<pre>    }

</pre>    
<pre>}

</pre>
<pre>/*

* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
</pre>



<pre>/** Main Method Class

*
* @author Azeem
*/
public class UseMyRectangle {
    public static void main(String args[]) {
</pre>        
<pre>        MyRectangle rectangle = new MyRectangle(100, 100, 200, 200);

</pre>        
<pre>        rectangle.displayrectarray();

</pre>        
<pre>        rectangle.doRotate();

</pre>        
<pre>        rectangle.displayproductmatrix();

    }
</pre>    
<pre>}
pzweilin
Offline
Joined: 2007-08-14
Points: 0

Hi !
You cannot make all your work without doing methods and perhaps a constructor...

Try with this code...

public class MyPanel extends JPanel {
// class member declaration
private int[][] rectangle;

public MyPanel() {
// invoke constructor of inherited class...
super();
// class member initialization in constructor..
// here, you can do what you want with rectangle member !
rectangle = new int[2][2];
rectangle[0][0] = 100;
}
}

Enjoy programming...

azeemofwalsall
Offline
Joined: 2012-08-17
Points: 0

Sorry, something went wrong with the code formatting during copy paste:

Here's the code again:

import static java.lang.System.out;

public class MyRectangle {

int x1, y1, x2, y2, x3, y3, x4, y4;

int[][] rectarray = new int[2][4];
int[][] productmatrix = new int[2][4];

public MyRectangle(int x1, int y1, int x2, int y2) {
rectarray[0][0] = x1;
rectarray[1][0] = y1; // first corner P1 (x1,y1)

rectarray[0][1] = x2;
rectarray[1][1] = y1; // second corner P2(x2=x3),(y2=y1)

rectarray[0][2] = x2;
rectarray[1][2] = y2; // opposite (third) corner P3(x3,y3)

rectarray[0][3] = x1;
rectarray[1][3] = y3; // fourth corner P4(x4=x1),(y4=y3)

}

public int[][] doRotate() {

int i, j, k;
double transform[][] = new double[2][2]; // the transform matrix

transform[0][0] = 0.8660; // rotation by 30 deg.
transform[0][1] = -0.5;
transform[1][0] = 0.5;
transform[1][1] = 0.8660;

for (i=0; i<=1; i++) {
for (j=0; j<=3; j++) {
for (k=0; k<=1; k++) {
productmatrix[i][j] += transform[i][k]*rectarray[k][j];
productmatrix[i][j] = Math.round(productmatrix[i][j]);
}
}
}

return productmatrix;

}

public void displayrectarray() {
int i, j;
for (i=0; i<=1; i++) {
for (j=0; j<=3; j++) {
System.out.print(i + " ");
System.out.print(j + " ");
System.out.println(rectarray[i][j]);
}
}
}

public void displayproductmatrix() {
int i, j;
for (i=0; i<=1; i++) {
for (j=0; j<=3; j++) {
System.out.print(i + " ");
System.out.print(j + " ");
System.out.println(productmatrix[i][j]);
}
}

}

}

And then the Main Class:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Azeem
*/
public class UseMyRectangle {
public static void main(String args[]) {

MyRectangle rectangle = new MyRectangle(100, 100, 200, 200);

rectangle.displayrectarray();

rectangle.doRotate();

rectangle.displayproductmatrix();
}

}