2 years ago

#26470

test-img

E S

Java - Modify attributes of an object from a different class with protected methods

I am trying to swap cells in a board. I have the class Cell with attributes such as coordinate (position in the board). This class has the method setCoordinate as protected.

I also have the class Level, with attributes such as board (composed by many objects of the class Cell), and a method to swap pieces. It also has a method like getCell, to retrieve the cell object given a coordinate in the grid.

So, if I create a cell object such as:

Cell cell1 = (Cell) getCell(new Coordinate(2, 2));
Cell cell2 = (Cell) getCell(new Coordinate(1, 1));

When I swap pieces:

public void swapCells(Coordinate firstCoord, Coordinate secondCoord) {
      Cell temp = getCell(secondCoord);
      setCell(secondCoord, getCell(firstCoord));
      setCell(firstCoord, temp);}

I can place cell1 in cell2's spot, and cell2 in cell1's spot. However, the coordinates of each cell do not update according to the new position in the board.

I cannot use the method setCoordinate from class Cell because it is protected, and I cannot create a public method. I am not sure of the reason, but I am still learning and this is a requirement that is given.

How could I achieve a swap in which coordinates are updated too?

Here are some relevant and simplified parts of my code:

    public class Level {
   
    private Cell[][] board;

   
    public Level(String fileName) {
       
        parse(fileName);
    }

 
    private void parse(String fileName) {
        ... fills board...
    }

   

    public Cell getCell(Coordinate coord) {
        return board[coord.getRow()][coord.getColumn()];
        } 
   

    private void setCell(Coordinate coord, Cell cell) {
        
            board[coord.getRow()][coord.getColumn()] = cell;       
    }

    public void swapCells(Coordinate firstCoord, Coordinate secondCoord) {
        
            setCell(secondCoord,getCell(firstCoord));
            setCell(firstCoord,getCell(secondCoord)); 
        } 
    }
}


public class Cell {

    private Coordinate coordinate;

    public Cell(int row, int column) {
        setCoordinate(row,column);
    }

    public Coordinate getCoordinate() { return this.coordinate; }

    protected void setCoordinate(int row, int column) {
        this.coordinate = new Coordinate(row,column);
    }

}


public class Coordinate {
    private int row;
    private int column;

    public Coordinate(int row, int column) {
        setRow(row);
        setColumn(column);
    }

    public int getRow() { return this.row; };

    private void setRow(int row) {
        this.row = row;
    }

    public int getColumn() {return this.column; }

    private void setColumn(int column) {
        this.column = column;
    }
}

After looking into it more, it seems my problem is that when I do the swap, I assign a new piece to a cell in the board, instead of really swaping. The final board is correct, but I keep recreating pieces instead of swaping.

So, a bit simplified, to swap piece in (0,0) with piece in (1,1) I do:

board[0][0] = getCell(1,1);
board[1][1] = getCell(0,0);

But with this I am creating new pieces for selected positions.

What I have in the test is something like:

Cell cell1 = getCell(0,0);
Cell cell2 = getCell(1,1);

swapCells(cell1.getCoordinate(), cell2.getCoordinate());

assertEquals("(1,1)", cell1.getCoordinate().toString());

But the assertequals fails because they are on these objects cell1 and cell2, but I am not really using the objects, just creating new ones that look like them. Also, the swapCells method doesn't have Cell objects as arguments. I really can't see how to do this.

java

inheritance

private

protected

0 Answers

Your Answer

Accepted video resources