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 3 of 6 FirstFirst 12345 ... LastLast
Results 51 to 75 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. #51
    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
    I did not find the Tetris program complex.
    If that's the case, I find it weird that you couldn't help me the slightest bit and you kept asking those silly questions.

  2. #52
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 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

    Quote Originally Posted by wholegrain View Post
    If that's the case, I find it weird that you couldn't help me the slightest bit and you kept asking those silly questions.
    His questions look to be only ones that ask for clarification of just what your unclear problems might be and nothing more, and they don't appear "silly" to me, not in the least. The only thing silly I see in this entire thread is your last post above which adds nothing to the discussion other than suggesting its owner might have an over-developed sense of entitlement. My experience here has been that if you ask a well constructed, clear and specific question, you usually get a well constructed, clear and specific answer. Else we must ask for clarification, and keep asking until either the question becomes well defined or remains unanswerable.

  3. #53
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 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

    My objective has been to help you understand what the code was doing and where to look to find its problems.
    There are several basic concepts about what extending classes means that you don't understand.
    Don't expect me to tell you what coding changes to make to the code. I think that would be a waste of time because you need to understand what the code is doing and not just copy some code that could fix the problem.

    Right now it looks like the code has most of what you want and that there is a method that needs to be fixed to do the "rotation". I suggest that you copy that method into a small simple testing program and work on getting it to work. Pass it an array and print out the array after it changes it so you can visually verify that the method works like you want. when it is working, then it can be copied back to the big program.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #54
    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

    Well, it does seem that it's the matrixMultiplication method that's not working for some reason. If you wanted to help, you would have pointed out what was the problem.
    HTML Code:
        synchronized void rotateClockwise() {
     	//contents = multiplyMatrix(contents);
     	contents = matrix();
        updateLocation();
        }
    
      public static int [][] matrix()
      {
      	int [][] a =
      		{{1,1,1,1},
    		 {1,1,1,1},
    		 {1,1,1,1},
    		 {1,1,1,1},
    		};
    
      	return a;
      }
    This worked fine, but I guess the rotation matrix doesn't work for some reason doesn't even modify the matrix for some reason.

  5. #55
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 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

    pointed out what was the problem.
    I thought I have said several times that I don't know what you want that code to do? Explain what the algorithm is that the code is supposed to implement.
    Can you show an array before its passed to the method and what the array should be after the method work on it?

    the rotation matrix doesn't work
    Please explain what "doesn't work" means?
    Show the input and the output for the method and post what the output should be.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #56
    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 told you already. I want to make the shapes rotate.

    Can you just tell me how to detect collision?

  7. #57
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 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 want to make the shapes rotate.
    Thst sound like you have an image and want to change its orientation when it is drawn. But this code is not drawing images so I'm confused.
    I thought you wanted code to change the contents of a two dimensional array.

    how to detect collision
    What objects are colliding? How are the boundaries of the objects defined? The Shape class has some methods like contains and intersects that are useful in detecting collisions between two Shape objects.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #58
    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

    public class rotation
    {
     
      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;
      }
     
    	public static void printArray(int [][] array)
    	{
    		for(int row = 0; row < array.length; row++)
    		{
    			for(int col = 0; col < array[row].length; col++)
    			{
    				if (array[row][col] > 0)
    				{
     
    				System.out.printf("1");
    				}
    				else
    				System.out.printf("0");
    			}
    			System.out.printf("\n");
    		}
     
    	}
     
      public static int [][] transpose(int [][] m1)
      {
      	  int m = 4;
      	  int n = 4;
      	  int c = 0;
      	  int d = 0;
     
      	  int[][] transpose = new int [n][m];
     
     
      	  for ( c = 0 ; c < m ; c++ )
          {
    	      for ( d = 0 ; d < n ; d++ )
    	      {
    	         transpose[d][c] = m1[c][d];
    	      }
          }
     
          return transpose;
     
      }
     
     
    public static void main(String[] args)
    {
      		int [][] m1 =
    		{{0,0,1,1},
    		 {0,0,1,1},
    		 {0,0,0,0},
    		 {0,0,0,0},
    		};
     
    	int [][] transpose = multiplyMatrix(m1);
        transpose = transpose(transpose);
     
    	printArray(transpose);
     
     
     
     
     
    }
    }

    I did what you asked and got this

    input:

    {0,0,1,1},
    {0,0,1,1},
    {0,0,0,0},
    {0,0,0,0},

    output

    1 1 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0

    I calculated the transpose for input and then multiplied it with m2 to get the rotation.

    the algorithm that I was given seems to be flawed.

    --- Update ---

    Quote Originally Posted by Norm View Post
    Thst sound like you have an image and want to change its orientation when it is drawn. But this code is not drawing images so I'm confused.
    I thought you wanted code to change the contents of a two dimensional array.


    What objects are colliding? How are the boundaries of the objects defined? The Shape class has some methods like contains and intersects that are useful in detecting collisions between two Shape objects.
    Tetris block should collide. The boundaries are defined by 1 and 0. I don't know if the methods in the shape class can be applied here.

    Implementing Tetris: Collision Detection | Gamedevtuts+

    Basically, the site explains us how to do it, but I don't know if it can be applied to my program.

  9. #59
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 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

    algorithm that I was given seems to be flawed.
    I have no idea what the correct algorithm is. You will need to find it.

    Are you sure the algorithm is flawed or could it be your code is not following the algorithm?

    Tetris block should collide. The boundaries are defined by 1 and 0.
    Looks like the same kind of problem about "collisions". You need the correct algorithm to be able to detect a collision between Tetris blocks.

    What is a "Tetris block"? You have not used that term before.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #60
    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
    Thst sound like you have an image and want to change its orientation when it is drawn. But this code is not drawing images so I'm confused.
    I thought you wanted code to change the contents of a two dimensional array.
    Wait, are you saying I shouldn't change contents, but I should change shape? You told me we can't modify shape.

    --- Update ---

    Quote Originally Posted by Norm View Post
    I have no idea what the correct algorithm is. You will need to find it.

    Are you sure the algorithm is flawed or could it be your code is not following the algorithm?


    Looks like the same kind of problem about "collisions". You need the correct algorithm to be able to detect a collision between Tetris blocks.

    What is a "Tetris block"? You have not used that term before.
    Can't you look at my code and tell me what is wrong? The tetris block is shape.

    --- Update ---

    Strangely the transpose method gives me this:

    1000
    1000
    0000
    0000

    and not this:
    0000
    0000
    0011
    0011

    --- Update ---

    Ah never mind, the transpose method works, but not the multiplyMatrix method...

    it returns

    0000
    0000
    0000
    0000

    --- Update ---

    Ok it works now

    0000
    0000
    1100
    1100
    0000
    0000
    0011
    0011

    --- Update ---

    1110
    0010
    0000
    0000
    0111
    0100
    0000
    0000

  11. #61
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 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

    are you saying I shouldn't change contents, but I should change shape? You told me we can't modify shape.
    contents is a two dim int array in the Grid class. Its contents can be changed.
    I don't know where there is class variable named shape.
    Your question does not make sense because there is no shape variable to change.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #62
    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, please just tell me how to implement collision detection given the data structure and the class structure of my program.

    --- Update ---

    Quote Originally Posted by Norm View Post
    contents is a two dim int array in the Grid class. Its contents can be changed.
    I don't know where there is class variable named shape.
    Your question does not make sense because there is no shape variable to change.
    Do you think changing contents instead of shape will cause problem in the future? By shape I mean the attribute and not the variable.

  13. #63
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 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

    how to implement collision detection
    Define what "objects" are colliding? What data values make for a collision?

    With a physical shape like rectangles, a collision is when their sides touch.
    The position of their sides is determined by their x,y location and their width and height.


    changing contents instead of shape
    I do NOT see where there is a shape variable that can be changed.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #64
    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
    Define what "objects" are colliding? What data values make for a collision?

    With a physical shape like rectangles, a collision is when their sides touch.
    The position of their sides is determined by their x,y location and their width and height.

    I do NOT see where there is a shape variable that can be changed.
    I don't know. I have absolutely no idea how to implement it. I don't even have width and height as attributes.

    --- Update ---

    Quote Originally Posted by Norm View Post
    Define what "objects" are colliding? What data values make for a collision?

    I do NOT see where there is a shape variable that can be changed.
    Are you saying that it's ok to change contents?

  15. #65
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 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

    no idea how to implement it
    You always need to know what the code is supposed to do before you can implement it. I can't help you find the algorithm.

    Are you saying that it's ok to change contents?
    I don't know if or when the values in the contents array can be changed. It depends on what those values are used for. Look at the design for the program.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #66
    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
    You always need to know what the code is supposed to do before you can implement it. I can't help you find the algorithm.


    I don't know if or when the values in the contents array can be changed. It depends on what those values are used for. Look at the design for the program.
    Why are you even bothering to respond if all you can do is give those kind of answers. I am just wasting my time talking to you.

  17. #67
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 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 agree. Your questions about some algorithms for your game are not questions that anyone not coding your game can answer. Not many people would be interested in reading the doc for the game and figuring out what its rules are so they can write code for you.

    Good luck on your project.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #68
    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

    Usually, when someone responds to me. He usually points out what might be the problem instead of just asking questions that lead to nowhere and actually read the code I have given him.

    3 pages and not a single helpful answer. I never experienced this before.

    And how can you say the program is not complicated if you don't even understand it? If you understood it, you would easily be able to know how to go about doing certain things with the existing code. I didn't even ask for the algorithm.

  19. #69
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 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

    You would never explain what the problems were in terms of the code.
    Where is the shape?
    What does rotate the shape mean?
    What is a tetris block?
    What does a collision mean?

    I could make up stuff.
    A tetris block is a 2 dim array (the contents array defines a block)
    A collision is when more than 3 elements in one array are the same as the corresponding 3 elements in another array.

    Rotate an array would be when the rows and columns of a 2 dim array are swapped.

    And so on.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #70
    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'd like to mention that I didn't program the entire thing

    I am just trying to complete this lab project I found on google:

    CSE131 Lab 9: Tetris Game using Object-Oriented Design

    So you can't expect me to know more than you. I don't know where the shape is. Rotating the shape means rotate the 2d array representing the shape of a tetris block. I don't think I need to explain that. It should have been obvious. The other questions didn't need answers if you actually looked into the code. If you don't look at the code, I don't think it's possible to help, because I didn't program the entire thing.

  21. #71
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 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 have an understanding of how java programming works.
    I know nothing about Tetris and/or thousands of other java applications that exist.

    I do expect you to know more about the app (Tetris) than I do.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #72
    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

    Are you saying you never played Tetris? That's not even possible.

    --- Update ---

    So basically, I can't have a tetris block go through another tetris block. It must detect the collision and pile up. I thought about a way to implement this, but I need three different methods

    I am asked to do the following:

    Write a method in the Board class that takes a row and column index and a two-dimensional array as parameters, and checks to see that the non-zero entries of the given array correspond to empty positions on the grid, assuming that the given array's upper left were positioned at the given row and column. You'll use this to determine if a piece can move into a given position.

    So I have a Piece object and two variable currentX and currentY. I am assuming those will be passed into the method described above. currentX and currentY represent the top corner of the shapes. Now, how do I implement collision detection? I think we need to create an array of the same size as shape array that represent what's immediately below the shape and then do two for loops to see if there is a collision if the shape moves below.

    I am trying to find a simpler way though.

  23. #73
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 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

    Is the gird a large 2 dim array containing 0s and non-0s?
    The array to test with is smaller than the grid array and also contains 0s and non-0s.
    the array to test is to be aligned at the given row, column in the bigger array.
    test that each overlain element in the big array is 0 when the the overlaying smaller array's element is non-0

    Sounds like a nested loop using row and column indexes adjusted for each array.
    ThesSmaller array's 0,0 is the row,col value for the alignment point on the larger array.
    If the smaller array's value is not 0, then the larger array's value must be 0.
    If you don't understand my answer, don't ignore it, ask a question.

  24. The Following User Says Thank You to Norm For This Useful Post:

    wholegrain (February 17th, 2013)

  25. #74
    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, thanks. Board must be the array containing the previous pieces from what I could parse. But I have to write a method to save the previous pieces. I wrote this method, but I don't know if it would work.


        public boolean legalDown(int currentX, double currentY, int[][] board)
        {
           int x = currentX;
           int y = currentY-1;
     
           for (int row = 0; row < 4; row++)
           {
               for(int col = 0; col < 4; col++)
               {
                   if (shape[row][col] == 0 && board[x+col][y]row] == 1)
                   {
                       return true;
                   }
                   else
                   {
                       return false;
                   }
               }
           }
     
     
     
        }

    Ok, I came up with this, but I don't know if I can access shape directly like this. If I don't pass shape as a parameter to the method. What would happen? Do I need to call it like this: currentPiece.legalDown? I am not sure. Also, I don't know if it would work. Also, there is no method to save the 1 and 0 of the previous pieces right now, right? I have to write it in Board, right?

    --- Update ---

    HTML Code:
    public class collisiontest
    {
    
    	public static boolean legalDown(int currentX, int currentY, int[][] board, int[][] shape)
    	{
    	   int x = currentX;
    	   int y = currentY-1;
    
    	   for (int row = 0; row < 4; row++)
    	   {
    	   	for(int col = 0; col < 4; col++)
    	   	{
    
    	   		if (shape[row][col] == 0 && (board[x+col][y+row] == 0 || board[x+col][y+row] == 1))
    	   		{
    	   			return true;
    	   		}
    	   		else
    	   		{
    	   			if (shape[row][col] == 1 && board[x+col][y+row] == 0)
    	   			{
    	   				return true;
    	   			}
    	   			else
    	   			{
    	   				return false;
    	   			}
    	   		}
    	   	}
    	   }
    
    
        return true;
    	}
    
    
    public static void main(String[] args)
    {
      		int [][] m1 =
    		{{1,1,0,0},
    		 {1,1,0,0},
    		 {0,0,0,0},
    		 {0,0,0,0},
    		};
    
    
    		int[][] board =
    		{{0,0,0,0},
    		 {1,1,1,1},
    		 {1,1,1,1},
    		 {1,1,1,1},
    		 {1,1,1,1},
    		 {1,1,1,1},
    		 {1,1,1,1},
    		};
    
    		int[][] board2 =
    		{{0,0,0,0},
    		 {0,0,0,0},
    		 {0,0,0,0},
    		 {0,0,0,0},
    		 {0,0,0,0},
    		 {0,0,0,0},
    		 {1,1,0,0},
    		};
    
    int currentX = 0;
    int currentY = 4;
    
    
    		System.out.println(legalDown(currentX, currentY, board, m1));
    
    
    
    
    
    
    
    }
    }
    Ok, I am trying to test the method, but it doesn't work it always returns true. I am trying to figure out why.

  26. #75
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 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 posted method returns true or false on the first elements it looks at. Shouldn't it look at all the elements before making the decision for one of the values to be returned? The other value could be returned at the first failure.


    a method to save the previous pieces.
    I don't know the definition of a piece
    or what a previous piece is
    or what saving a piece means.

    do I need to call it like this: currentPiece.legalDown?
    If legalDown() is a method in the Piece class (or the class it extends)
    and shape is a variable in the Piece class (or the class it extends)
    then that is how you could call the legalDown() method.


    Can you explain what the method: legalDown() is supposed to do?
    What are its args?
    When does it return true or false?
    If you don't understand my answer, don't ignore it, ask a question.

Page 3 of 6 FirstFirst 12345 ... 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