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 2 of 6 FirstFirst 1234 ... LastLast
Results 26 to 50 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. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 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 need to access shape from currentPiece.shape
    Where is the shape variable defined?

    pass currentPiece.shape into the MatrixMultiplication method.
    Again where is the variable shape defined?

    		case KeyEvent.VK_DOWN:  // down arrow
    		case KeyEvent.VK_KP_DOWN:
    			currentPiece.rotateClockwise();
    Here the code calls a rotate method in the currentPiece object.
    If you don't understand my answer, don't ignore it, ask a question.

  2. #27
    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 defined in Grid, but the variable name is different in Grid.

    public class Grid extends JComponent {
    	public int contents[][];  // contents = shape


    It's defined as shape. However, setting a getter method in Grid won't help as currentPiece is a Piece object...

    Can you take a look at the code and tell me how I think I should implement the rotation method based on the data structure and the class structure?

    --- Update ---

    Quote Originally Posted by Norm View Post


    Here the code calls a rotate method in the currentPiece object.
    Yeah, I have a question on that. Since it doesn't pass currentPiece as an argument. How do they expect us to modify currentPiece with rotateClockWise()?

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 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

    defined in Grid, but the variable name is different in Grid.
    The name of the variable can not change from a base class to an extending class. If the variable names are different, then the variables are different.

    It's defined as shape
    Can you copy the code that defines a class variable named shape? I'm having a hard time finding it.

    setting a getter method in Grid won't help as currentPiece is a Piece object...
    What class does the Piece class extend? Do you understand what it means when a class extends another class?

    implement the rotation method
    Define what you mean by "rotation". Post an example array before it is rotated and after it is rotated and explain the steps that were taken to do the rotation.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #29
    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
    The name of the variable can not change from a base class to an extending class. If the variable names are different, then the variables are different.


    Can you copy the code that defines a class variable named shape? I'm having a hard time finding it.


    What class does the Piece class extend? Do you understand what it means when a class extends another class?


    Define what you mean by "rotation". Post an example array before it is rotated and after it is rotated and explain the steps that were taken to do the rotation.
    Piece calls super(shape). Piece extends Grid, which has a constructor that takes shape and turn it into contents.

    Shape isn't declared anywhere. The class variable shape doesn't exist, because Piece inherits contents (or shape, it's under a different name)

    matrixMultiplication is supposed to do:

    1 0 0
    1 0 0
    1 1 0

    X

    0 0 1
    0 1 0
    1 0 0

    =

    0 0 1
    0 0 1
    0 1 1

  5. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 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 are the steps for the rotation? Can you list the steps for doing the "rotation"?
    Your post looks like it multiplication: A x B = C Two operands and an operator generates a product
    How does that relate to "rotation"? I was expecting two arrays: one before and one after the rotation.

    constructor that takes shape and turn it into contents.
    From what you have said, the variable contents in the Grid class seems to be the "shape" you are talking about.

    Does the rotateClockwise() method in the Piece class do what you want? Or at least work on the data that you want to change?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #31
    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
    What are the steps for the rotation? Can you list the steps for doing the "rotation"?
    Your post looks like it multiplication: A x B = C Two operands and an operator generates a product
    How does that relate to "rotation"? I was expecting two arrays: one before and one after the rotation.


    From what you have said, the variable contents in the Grid class seems to be the "shape" you are talking about.

    Does the rotateClockwise() method in the Piece class do what you want? Or at least work on the data that you want to change?
    It just multiply the 2d array with a predefined 2d array to get the product of their matrices and instead of a value between 0 and max int value we get either 1 or 2. I posted the code already.

  7. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 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

    Does the rotateClockwise() method in the Piece class do what you want? Or at least work on the data that you want to change?

    I posted the code already.
    Does that mean you have the code that does the rotation?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #33
    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 didn't implement the rotateClockwise method, because I can't access any of the variables as I said.

    I only have the code that does matrix multiplication. I can't use it, because I can't modify the shape attribute of currentPiece. Did you look at the codes?

  9. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 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 didn't implement the rotateClockwise method, because I can't access any of the variables as I said.
    There is a rotateClockwise() method in the Piece class. What is wrong with that?
    Be more specific than "any of the variables". Where are you trying to call that method? What variables do you need access to?


    I can't modify the shape attribute of currentPiece.
    Please use variable names and class names when talking about your problem. There is no variable: shape. The variable currentPiece is a Piece object.
    Why can't you modify the code?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #35
    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

    there is no variable shape, but the constructor pass the argument shape to the constructor of Grid.

    the method rotateClockwise doesn't work, because I implemented it and it didn't work because I didn't have access to it.

    Can you take a look at the code and tell me how I can change the form of the blocks?

  11. #36
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 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 method rotateClockwise doesn't work
    Please explain what "doesn't work" means?
    What needs to be changed to make it work?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #37
    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 needs to be able to change the form of the currentShape.

    ex:

    0 1 1
    0 0 1
    0 0 1

    It's a tetris game.

    (currentShape contains an attribute that stores an int[][] type that supposed to represent a shape. The shapes are inside an enum inside Piecefactory

    Did you try running the code?

  13. #38
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 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 needs to be able to change the form of the currentShape.
    That's a very vague answer that skips or ignores the questions I asked.
    Are you saying that the rotateClockwise() method in the code needs to be rewritten to generate the correct results?
    It currently executes but the output is wrong.

    Sorry, I know nothing about a tetrix game.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #39
    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 can't output anything because it can't get the shape of the current object.

    I didn't skip your question I already told you there was a shape attribute that was constructed by the parent class of Piece.

    	public Piece(int shape[][]) {
    		super(shape);
    		currentX = 7;
    		currentY = 2;
    		updateLocation();
    	}

    Did you even look at the code?

  15. #40
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 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 can't output anything because it can't get the shape of the current object.
    Please explain what "it" is?
    By "shape" do you mean the contents variable defined in the Grid class?
    Do you know what it means when a class extends another class?

    Did you even look at the code?
    Yes, that was why I asked where the class variable shape was. I can not see it in the code.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #41
    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

    the variable is not there, but it's an attribute. the method matrixMultiplication and the method rotateCounterClockwise can't get the shape.

    --- Update ---

     
    	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);
    	}

    Can you explain to me what this does?

  17. #42
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 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

    but it's an attribute
    Sorry, I have no idea how to code with attributes.

    rotateCounterClockwise can't get the shape.
    Are you talking about accessing the Grid class variable: contents?
    What problem(s) are you having accessing the contents variable from the rotateCounterClockwise() method?
    "can't get" is not an error message from the java compiler. If you get compiler errors, copy the full text of the error messages and paste them here.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #43
    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 Grid(int[][] contents) {
    		this.contents = contents;
    		Dimension d = new Dimension(getColumns()*Tetris.SQUARE_SIZE,
    		                            getRows()*Tetris.SQUARE_SIZE);
    		setSize(d);
    		setPreferredSize(d);
    		setOpaque(false);
    	}

    Ok then just explain me what this does at the very least.

  19. #44
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 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 looks like a constructor.
    Which line(s) are you having problems with?
    I don't see anything unusual.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #45
    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 know it's a constructor, but I would like to know what it does exactly. It seems to be printing something, but I don't know if it's printing an individual shape or something else.

  21. #46
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 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

    if it's printing an individual shape or something else.
    Where do you see it printing anything? I don't see it.
    Which statement are you talking about?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Nevermind,

    what do these do?

    		Dimension d = new Dimension(getColumns()*Tetris.SQUARE_SIZE,
    		                            getRows()*Tetris.SQUARE_SIZE);
    		setSize(d);
    		setPreferredSize(d);
    		setOpaque(false);

    the last 3 seems to change some attributes, but they weren't declared in the class.

  23. #48
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 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 last three are methods in the JComponent class. Read the API doc for that class to get descriptions of what those methods do. The Grid class extends the JComponent class, so these methods can be called in the Grid class as local methods.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #49
    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, one last question. Are you a programmer and did you work in the industry? And were you involved in complex projects, and do you find a Tetris program to be complex?

  25. #50
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 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 did not find the Tetris program complex.
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 6 FirstFirst 1234 ... 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