Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 6 123 ... LastLast
Results 1 to 25 of 150

Thread: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

  1. #1
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Question Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    Ok, so I have a bunch of classes and I'd like to rotate an object which has an object shape, which is a 2d array and that's constructed in the parent class of the object class. I want to change the attribute (shape) in the method rotate() which calls the method multiplyarray that does the rotation by changing the shape. However, I realized that I don't have access to shape, or at least I don't know how to change it. I don't think the parent class has a public setShape method. Anyway here's the code:
    HTML Code:
      public static int [][] multiplyMatrix(int [][] m1)
      {
    
    int [][] m2 =
    {{0,0,0,1},
     {0,0,1,0},
     {0,1,0,0},
     {1,0,0,0},
    };
    
    
    int[][] result = new int[4][4];
    
    // multiply
    for (int i=0; i<4; i++)
      for (int j=0; j<4; j++)
        for (int k=0; k<4; k++)
        if (m1[i][k] * m2[k][j] > 0)
        {
            result[i][j] = 1;
        }
        else
        {
            result[i][j] = 0;
        }
    
    
    return result;
    }
    
    }
    
    the rotate method:
    
    synchronized void rotateClockwise() {
        currentPiece.shape = multiplyMatrix(shape);
    
        //gives me an error
    
        updateLocation();
    
    }
    the constructor (all three methods are in the same class):

    HTML Code:
        public Piece(int shape[][]) {
        super(shape);
        currentX = 7;
        currentY = 2;
        updateLocation();
    }
    this method is in another class and it contains the instance object whose attribute i want to modify:

    HTML Code:
        public void keyPressed(KeyEvent event) {
        int key = event.getKeyCode();
        switch (key) {
        case KeyEvent.VK_UP:  // up arrow
        case KeyEvent.VK_KP_UP:
            currentPiece.rotateCounterclockwise();
            break;
        case KeyEvent.VK_DOWN:  // down arrow
        case KeyEvent.VK_KP_DOWN:
            currentPiece.rotateClockwise();
            break;
        case KeyEvent.VK_LEFT:  // left arrow
        case KeyEvent.VK_KP_LEFT:
            currentPiece.moveLeft();
            break;
        case KeyEvent.VK_RIGHT:  // right arrow
        case KeyEvent.VK_KP_RIGHT:
            currentPiece.moveRight();
            break;
        case KeyEvent.VK_SPACE:  //  space bar
            currentPiece.drop();
        }
    }
    createPiece method (I want to access the shape attribute):

    HTML Code:
      public static Piece createPiece() {
        		int[][] s = SHAPES[(int) (Math.random()*SHAPES.length)];
        		switch ((int) (Math.random()*10)) {
        			case 0:
        			case 1:
        			case 2:
        			case 3:
        			default: return new Piece(s);
        		}
    I found out that super calls this constructor in Grid:

    HTML Code:
    public Grid(int[][] contents) {
        this.contents = contents;
        Dimension d = new Dimension(getColumns()*Tetris.SQUARE_SIZE,
                                    getRows()*Tetris.SQUARE_SIZE);
        setSize(d);
        setPreferredSize(d);
        setOpaque(false);
    }
    Now, I tried this:

    HTML Code:
    synchronized void rotateClockwise() {
    Grid.contents = multiplyMatrix(Grid.contents);
    updateLocation();
    
    }
    It gives me:

    non-static method getContents() cannot be referenced from a static context


    entire code can be found here (without certain modifications):

    CSE131 Lab 9: Tetris Game using Object-Oriented Design


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    non-static method getContents() cannot be referenced from a static context
    Non static methods are only callable using an instance of the class when calling from inside a static method.


    Why are you using static methods? Make all methods and variables non static (except main)
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    Can you tell me how to make my rotation method worki?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    Can you define what it is supposed to do in detail and give examples?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    Like I don't even know what variable to modify.

    --- Update ---

    Basically, I am trying to make a tetris game, and I am basically multiplying the shape object with another vector to make it rotate.

    Tetris Piece Rotation Algorithm - Stack Overflow

    However, I can't seem to modify the block as they fall down by rotating them with the push of a key. I can move them left and right, but I can't rotate them.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    I don't even know what variable to modify.
    Can you explain what the problem is?
    If there are error messages, copy the full text of the messages and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    I don't even know what I am doing. Don't assume I know what I am doing. I suspect the entire logic is wrong. I'll try to post the error as soon as I can.

    --- Update ---

    void static updateLocation() {
    setLocation(Tetris.SQUARE_SIZE*currentX,
    (int) (Tetris.SQUARE_SIZE*currentY));
    }

    I changed rotateClockwise to static and it gave me new errors. They weren't there before... Do you want me to post the entire source?

    error: <identifier> expected
    error: '(' expected
    error: invalid method declaration; return type required

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    What lines does each error message go with?


    You should not use static on anything except the main() method.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    Basically, there is an enum array object with 1 and 0 passed into the constructor for the class Piece randomly, and it calls super(shape), which creates a Grid object. The currentPiece, which I think is the variable I need to modify is created in the main class and I can't access it even though it's an object of type Piece, and since the object has a parameter shape, I can't pass the object currentPiece directly in my matrixMultiplication object. So I don't know what to do. I tried currentPiece.shape, but it didn't work. It's an instance of the object Piece, so it's supposed to work, but it doesn't...

    --- Update ---

    Quote Originally Posted by Norm View Post
    What lines does each error message go with?


    You should not use static on anything except the main() method.
    line 26, which is the first line of the three

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    Sorry, I have no idea why the compiler shows those errors. Can you use the javac command to compile the code and get some better error messages? The ones you posted are not very useful.
    Here is a sample of an error message from the javac command:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^

    Posting the error messages in code tags keeps the ^ below where the error was.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    full source code:

    public class Piece extends Grid {
    	int currentX;     // current X location on  the board
    	double currentY;  // current Y location on the board
     
     
    	public Piece(int shape[][]) {
    		super(shape);
    		currentX = 7;
    		currentY = 2;
    		updateLocation();
    	}
     
     
    	void updateSize() {
    		setSize(Tetris.SQUARE_SIZE*getColumns(),
    				Tetris.SQUARE_SIZE*getRows());
    	}
     
    	void static updateLocation() {
    		setLocation(Tetris.SQUARE_SIZE*currentX,
    					(int) (Tetris.SQUARE_SIZE*currentY));
    	}
     
    	synchronized void moveDown() {
     
    	}
     
    	synchronized void moveLeft() {
    		currentX--;
    		updateLocation();
    	}
     
    	synchronized void moveRight() {
    		currentX++;
    		updateLocation();
    	}
     
        synchronized static void rotateClockwise() {
        	Grid.contents = multiplyMatrix(Grid.contents);
    		updateLocation();
     
    	}
     
    	synchronized void rotateCounterclockwise() {
    	}
     
    	void fall() {
    		// replace the following by your code
    		Tetris.sleep(2000);
    	}
     
    	synchronized void drop() {
    		currentY++;
    		updateLocation();
    	}
     
      public static int [][] multiplyMatrix(int [][] m1)
      {
      		int [][] m2 =
    		{{0,0,0,1},
    		 {0,0,1,0},
    		 {0,1,0,0},
    		 {1,0,0,0},
    		};
     
     
        int[][] result = new int[4][4];
     
        // multiply
        for (int i=0; i<4; i++)
          for (int j=0; j<4; j++)
            for (int k=0; k<4; k++)
            if (m1[i][k] * m2[k][j] > 0)
            {
            	result[i][j] = 1;
            }
            else
            {
            	result[i][j] = 0;
            }
     
     
        return result;
      }
     
        }

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class Tetris extends JFrame implements KeyListener {
    	public static final int SQUARE_SIZE = 10; // 10 by 10 pixels
     
    	static Board board;
    	static Tetris game;
     
    	JPanel mainPanel;
    	public Piece currentPiece;
    	int score = 0;
    	JButton scoreButton;
     
    	public Tetris() {
    		super("Tetris");
    		game = this;
    		Container pane = getContentPane();
    		pane.setLayout(new BorderLayout());
     
    		scoreButton = new JButton("0");
    		scoreButton.setEnabled(false);
    		pane.add(scoreButton,BorderLayout.NORTH);
     
    		board = new Board();
    		mainPanel = new JPanel();
    		mainPanel.setLayout(null);
    		mainPanel.add(board);
    		mainPanel.setPreferredSize
    			(new Dimension(Board.COLUMNS*SQUARE_SIZE,
    						   Board.ROWS*SQUARE_SIZE));
    		pane.add(mainPanel);
     
    		addKeyListener(this);
    		addWindowListener(new WindowAdapter() {
    			public void windowClosing(WindowEvent we) {
    				System.exit(0);
    			}
    		});
    		pack();
    		show();
    		setResizable(false);
    	}
     
     
    	public void addToScore(int v) {
    		score += v;
    		scoreButton.setText(""+score);
    	}
     
    	public int getScore() {
    		return score;
    	}
     
    	static Board getBoard() {
    		return board;
    	}
     
    	static Tetris getGame() {
    		return game;
    	}
     
    	static void sleep(int milliseconds) {
    		try {
    			Thread.sleep(milliseconds);
    		} catch (InterruptedException ie) {
    		}
    	}
     
    	public static void main(String[] args) {
    		Tetris game = new Tetris();
    		while (true) {
    			game.dropPiece();
    		}
    	}
     
    	void dropPiece() {
    		currentPiece = PieceFactory.createPiece();
    		mainPanel.add(currentPiece);
    		currentPiece.repaint();
    		currentPiece.fall();
    		//mainPanel.remove(currentPiece);
    		board.repaint();
    		addToScore(1);
    	}
     
    	public void keyPressed(KeyEvent event) {
    		int key = event.getKeyCode();
    		switch (key) {
    		case KeyEvent.VK_UP:  // up arrow
    		case KeyEvent.VK_KP_UP:
    			currentPiece.rotateCounterclockwise();
    			break;
    		case KeyEvent.VK_DOWN:  // down arrow
    		case KeyEvent.VK_KP_DOWN:
    			currentPiece.rotateClockwise();
    			break;
    		case KeyEvent.VK_LEFT:  // left arrow
    		case KeyEvent.VK_KP_LEFT:
    			currentPiece.moveLeft();
    			break;
    		case KeyEvent.VK_RIGHT:  // right arrow
    		case KeyEvent.VK_KP_RIGHT:
    			currentPiece.moveRight();
    			break;
    		case KeyEvent.VK_SPACE:  //  space bar
    			currentPiece.drop();
    		}
    	}
     
    	public void keyReleased(KeyEvent arg0) {
    	}
     
    	public void keyTyped(KeyEvent arg0) {
    	}
     
     
    }

    import javax.swing.JComponent;
    import java.awt.*;
     
    public class Grid extends JComponent {
    	public int contents[][];  // each rows is an array of integers
     
    	public Grid(int[][] contents) {
    		this.contents = contents;
    		Dimension d = new Dimension(getColumns()*Tetris.SQUARE_SIZE,
    		                            getRows()*Tetris.SQUARE_SIZE);
    		setSize(d);
    		setPreferredSize(d);
    		setOpaque(false);
    	}
     
    	public int[][] getContents()
    	{
    		return contents;
    	}
     
    	public int getColumns() {
    		return contents[0].length;
    	}
     
    	public int getRows() {
    		return contents.length;
    	}
     
    	void paintSquare(int row, int col, Graphics g) {
    		if (contents[row][col] != 0)
    			g.fillRect(Tetris.SQUARE_SIZE*col+1,
    						Tetris.SQUARE_SIZE*row+1,
    						Tetris.SQUARE_SIZE-2,
    						Tetris.SQUARE_SIZE-2);
    	}
     
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		for (int row = 0; row < contents.length; row++) {
    			for (int col = 0; col < contents[row].length; col++) {
    					paintSquare(row,col,g);
    			}
    		}
    	}
    }

    public class PieceFactory {
     
    	public static final int[][] L1 =
    		{{1,1,0,0},
    		 {0,1,0,0},
    		 {0,1,0,0},
    		 {0,0,0,0},
    		};
     
    	public static final int[][] L2 =
    		{{0,1,0,0},
    		 {0,1,0,0},
    		 {1,1,0,0},
    		 {0,0,0,0},
    		};
     
    	public static final int[][] T =
    		{{0,1,0,0},
    		 {1,1,0,0},
    		 {0,1,0,0},
    		 {0,0,0,0},
    		};
     
    	public static final int[][] BOX =
    		{{1,1,0,0},
    		 {1,1,0,0},
    		 {0,0,0,0},
    		 {0,0,0,0},
    		};
     
    	public static final int[][] BAR =
    		{{1,1,1,1},
    		 {0,0,0,0},
    		 {0,0,0,0},
    		 {0,0,0,0},
    		};
     
    	public static final int[][] STEP1 =
    		{{1,0,0,0},
    		 {1,1,0,0},
    		 {0,1,0,0},
    		 {0,0,0,0},
    		};
     
    	public static final int[][] STEP2 =
    		{{0,1,0,0},
    		 {1,1,0,0},
    		 {1,0,0,0},
    		 {0,0,0,0},
    		};
     
    	public static final int[][][] SHAPES = {L1,L2,T,BOX,BAR,STEP1,STEP2};
     
    	public static Piece createPiece() {
    		int[][] s = SHAPES[(int) (Math.random()*SHAPES.length)];
    		switch ((int) (Math.random()*10)) {
    			case 0:
    			case 1:
    			case 2:
    			case 3:
    			default: return new Piece(s);
    		}
     
     
    	}
     
     
     
     
    }

    import java.awt.*;
     
    public class Board extends Grid {
    	public static final int COLUMNS = 16;
    	public static final int ROWS = 32;
    	public static final Color BLUE = new Color(0,0,128,40);
     
    	public Board() {
    		super(new int[ROWS][COLUMNS]);
    		setSize(COLUMNS*Tetris.SQUARE_SIZE,
    				ROWS*Tetris.SQUARE_SIZE);
    	}
     
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		g.setColor(BLUE);
    		paintStripes(g);
    	}
     
    	void paintStripes(Graphics g) {
    		for (int i = 0; i < COLUMNS; i += 4) {
    			g.fillRect(i*Tetris.SQUARE_SIZE,0,
    				Tetris.SQUARE_SIZE*2,Tetris.SQUARE_SIZE*ROWS);
    		}
    	}
     
     
    }


    --- Update ---

    me neither, but I think it's because my rotate object shouldn't be static, because the errors disappear when I remove static.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    the errors disappear when I remove static.
    Sounds good.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    But it doesn't work.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    it doesn't work.
    Please describe what " it doesn't work" means. Be more specific describing what the code is supposed to do.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    It's supposed to multiply matrix a with another matrix that will "rotate" matrix a and return it to another variable, which will be assigned to the shape of currentPiece. I don't know how to do the last part. I tried various things, but they don't work or sometimes don't even compile.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    Can you define what "multiply matrix a with another matrix" means and give an example of two matrices and the results when they are multiplied?

    assigned to the shape of currentPiece.
    Does that class have a setShape() method that takes a matrix as arg?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    I am trying to do a matrix multiplication with 1 and 0 as values.

    Binary Matrix Multiplication & Boolean Product

    It doesn't have a setShape() method, but the constructor calls super(shape) and it seem that the constructor in Grid does something with shape, so I don't think if that would make it work.

    I just added this in the Piece class:

    public void setShape(int shape[][])
    {
    this.shape = shape;
    }

    when I compile, I get this:
    error: cannot find symbol

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    cannot find symbol
    when you post error messages, you need to copy all of the message.
    What you posted does not give the symbol that is not found or the source line with the error. You need that information to solve the problem.

    I'm done for tonight. Back tomorrow.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    Quote Originally Posted by Norm View Post
    when you post error messages, you need to copy all of the message.
    What you posted does not give the symbol that is not found or the source line with the error. You need that information to solve the problem.

    I'm done for tonight. Back tomorrow.
    it's shape

    --- Update ---

    Ok, I need to do the following and I don't know how to do them:

    Change currentPiece and access the shape of the currentPiece, which is created in Tetris.java.

    Someone please help me.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    Where is the variable shape defined in the Tetris class?


    Also posted at http://forums.devshed.com/java-help-...ss-939937.html
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    Quote Originally Posted by Norm View Post
    Where is the variable shape defined in the Tetris class?


    Also posted at Changing the attribute of an instance object of a class that's declared in another pa - Dev Shed
    Shape is an attribute of Piece and is constructed in Grid as contents. Can you look at the source and tell me how to do it?

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    Can you make a smaller program that compiles, executes and shows the problem. There is too much code to go through.

    If you could get good error messages pasted here it would help. The error messages that have been posted have been nearly useless.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    How can you ask me to write a program that compiles and that shows the problem? If I could do that, I wouldn't even be asking for help. I told you the problem was that I can't change shape, which seems to be an attribute of Piece and that's constructed using super() and that is passed to the constructor in Grid as contents. Now, I would like to write a method in Piece that changes its instance currentPiece so I can rotate its shape, but I don't know how to do that. So my question is: what can I change in the program to get the access right needed to change shape in the instance currentObject.

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    change shape, which seems to be an attribute of Piece
    Can you explain the problem in terms of variable names and data types?

    Where is the variable shape defined? What data type is it?

    write a method in Piece that changes its instance currentPiece so I can rotate its shape
    Add a method to the Piece class called: rotateShape()
    Add code to that method that rotates its shape.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    Ok, shape is an int[][]

    the shape variable is passed in as an argument in the class PieceFactory (it is randomly generated through Math.Random which picks a shape enum object from a given list)

    the constructor in Piece calls super(shape), and that shape goes to the constructor in Grid.

    Now, I thought of doing the following and I would like to help me find a way to implement it or tell me there's a better way or whatever:

    Since the constructor in Grid seem to do something important, I think I need to create a new constructor that has currentX, currentY and shape as an argument. However, I need to access shape from currentPiece.shape (which is a Piece object initialized in Tetris.java), but I can't do that. Moreover, Tetris calls currentPiece.rotateCounterClockwise. Strangely, it's supposed to use currentPiece, but I don't know how exactly. Isn't it better to call currentPiece as an argument? Should I change that? Also, changing Grid.shape serves no purpose as currentPiece won't be affected. Now, the first thing I need to achieve is to be able to pass currentPiece.shape into the MatrixMultiplication method. Can you tell me how I might be able to do that?

Page 1 of 6 123 ... LastLast

Similar Threads

  1. Instance Variable does't work in extended class?
    By jean28 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 21st, 2013, 01:36 AM
  2. Replies: 3
    Last Post: June 17th, 2012, 06:22 PM
  3. calling a changing variable from another class
    By bondage in forum Collections and Generics
    Replies: 11
    Last Post: December 7th, 2011, 10:17 AM
  4. Replies: 7
    Last Post: July 21st, 2011, 02:29 PM
  5. Access and set variable in parent class through child
    By java_newbie in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 19th, 2011, 11:44 PM