java constructors and clearboard method which clears board except the positions occupied?

ぐ巨炮叔叔 提交于 2020-01-05 07:17:27

问题


Ok I really need help with all of this, I have a class named Board.java The board is represented as a two-dimensional array of char’s. A turtle can leave a trail by writing a character into each position on the board that it passes through. There will be two constructors for the board. The default constructor takes no arguments and will create a board with 10 rows and 25 columns. Set each element in the board to be a blank space. The second constructor will take two integers that specify the number of rows and the number of columns, respectively. If the number of rows or columns specified is below 1, set the value to 1. If the number of rows or columns specified is greater than 80, set the value to 80. Set each element in the board to be a blank space. The class will need a clearBoard method. This will put a blank space in every position, except those positions occupied by turtles. Turtles mark their positions using the characters '0', '1', '2', ... '9'.

I have the following completed but I am not sure my constructors are right, and I don't know how to start the clearBoard method. Help please!!

import java.util.Arrays;

public class Board {

    private char [][] theBoard;

     public Board() { // This will not take any arguments 


        theBoard = new char [10][25]; //10 rows and 25 columns
        for (int row = 0; row < theBoard.length; row++ ) {
             for (int col = 0; col < theBoard[row].length; col++ )
                 theBoard [row][col] = ' ';
    System.out.println();
        }
   }

   public Board (int [][] theBoardArray) { 
        char [][] theBoard = new char [theBoardArray.length] [theBoardArray[0].length];
        for (int row = 0; row < theBoard.length; row++ ) {
                if (row <1)
                    row = 1;
                else if (row >80)
                    row =80;
             for (int col = 0; col < theBoard[row].length; col++ ){
                if (col <1)
                    col = 1;
                else if (col >80)
                    col =80;
                 theBoard [row][col] = ' ';
            }
    System.out.println();
        }
    }

回答1:


Fixed some parts, cleaned up a bit and added the rest you want. Explanations are in code comments.

public class Board {

    private char [][] theBoard;

    public Board() { // This will not take any arguments
        this(10, 25); // calls the other constructor
        // avoid duplicate code, where possible
    }

    // takes number of rows and collumns
    public Board (int rows, int cols) {
        // fix illegal row and column numbers
        rows = fixNumber(rows);
        cols = fixNumber(cols);
        // create the board and fill it with ' '
        theBoard = new char [rows][cols];
        for (int row = 0; row < theBoard.length; row++) {
            for (int col = 0; col < theBoard[row].length; col++)
                theBoard[row][col] = ' ';
        }
    }

    private int fixNumber(int number) {
        if (number < 1) {
            return 1;
        } else if (number > 80) {
            return 80;
        } else {
            return number;
        }
    }

    // almost like constructor, just does not create a new char[][] and
    // only puts ' ' into fields not containing any of '0' - '9'
    public void clearBoard() {
        for (int row = 0; row < theBoard.length; row++ ) {
            for (int col = 0; col < theBoard[row].length; col++) {
                if (theBoard[row][col] < '0' || theBoard[row][col] > '9') {
                    theBoard[row][col] = ' ';   
                }
            }
        }
    }
}



回答2:


First off, you should not set an int to be ' '. ' ' is a char, ints are numbers. This code will result in the int being set to the int 32, the ascii index for a space. I suppose if you want to account for a space being 32, this will work, but is odd.

The second constructor is not quite right. You are bringing in an int array, but you said you would be taking two ints. The "if else" portion of setting the rows and columns to be inside the values is good, but not the way you are getting to them.

Try passing in two ints, rows and columns, and then checking to be sure they are within the acceptable limits and changing them if they are not, then creating the int array.



来源:https://stackoverflow.com/questions/13734233/java-constructors-and-clearboard-method-which-clears-board-except-the-positions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!