创建绘制矩形的简单程序

时间:2013-09-02 21:27:44

标签: java arrays object draw

我是一名初学者Java编码员,我正在为课堂作业做准备。我们必须创建一个矩形类和一个驱动程序类。程序的要点是根据用户输入的多少行,列以及是否填充来绘制带有“#”符号的矩形。到目前为止,除了填充或未填充的逻辑外,我还有其他一切例如,如果我放入3行3列并填充它,它将如下所示:

###
###
###

如果它是相同的但没有填充它将看起来像这样:

###
# #
###

请查看我的矩形类和驱动程序类,看看你是否可以提供帮助!

public class rectangle {

    private int numRows;        // The number of rows
    private int numCols;        // The number of columns
    private boolean filled;     // The boolean to determine if the rectangle is filled or unfilled


    // initialize variables
    public rectangle() { 
        numRows = 1;
        numCols = 1;
        filled = false;
    }

    /**
     * @param rows
     * @param cols
     */
    public rectangle(int numRows,int numCols){
        setRows(numRows);
        setCols(numCols);
        setFilled(filled);
    }

    // getters and setters
    public int getRows() {
        return numRows;
    }

    public void setRows(int numRows) {
        this.numRows = numRows;
    }

    public int getCols() {
        return numCols;
    }

    public void setCols(int numCols) {
        this.numCols = numCols;
    }

    public boolean getFilled() {
        return filled;
    }

    public void setFilled(boolean filled) {
        this.filled = filled;
    }

    // Create and return string for output
    @Override
    public String toString() {
        if(filled = true){
        for(int i = 0; i < numRows;i++){
            for(int a = 0; a < numCols;a++){
            System.out.print("#");
        }
            System.out.println();
        }
        System.out.println();
        }

        if(filled = false){
            // need this part
            }   


        return "rectangle [numRows=" + numRows + ", numCols=" + numCols
                + ", filled=" + filled + "]";
    }
}

和我的司机班:

public class Lab1ADriver {

    /**
     * @param args
     * @throws FileNotFoundException 
     */
    public static void main(String[] args) throws FileNotFoundException {

        // create scanner
         Scanner kb = new Scanner(System.in);

        // create printwriter
        PrintWriter output = new PrintWriter("rectangleData.txt");

        // Ask user for input
        System.out.print("How many rectangles will you be using?");
        int numRec = kb.nextInt();
        output.println(numRec);

        // write to file
        for(int i=0; i < numRec; i++){
            System.out.print("How many rows are in the rectangle?");
            int numRows = kb.nextInt();
            System.out.print("How many columns are in the rectangle?");
            int numCols = kb.nextInt(); 
            System.out.println("Is rectangle filled? (y/n)");
            String filled = kb.next();

            // write vars to text file      
            output.println(numRows + " " + numCols + " " + filled);
        }
        // close scanner and pw
        kb.close();
        output.close();

        // array list to hold rectangle objects
        ArrayList<rectangle> box = new ArrayList<rectangle>();

        // open file for reading
        File inFile = new File("rectangleData.txt");
        Scanner recFile = new Scanner(inFile);

        // read data and add rectangle objects to array list
        recFile.nextLine();
        while(recFile.hasNext()){
            rectangle a = new rectangle();
            // read next line
            String recData = recFile.nextLine();

            // extract number of rows 
            String rowData = recData.substring(0, 1);
            rowData = rowData.trim();
            int numRows = Integer.parseInt(rowData);

            // extract columns
            String colData = recData.substring(1, 3);
            colData = colData.trim();
            int numCols = Integer.parseInt(colData);

            // extract boolean filled
            String fillData = recData.substring(3, 5);
            fillData = fillData.trim();
            char f = fillData.charAt(0);
            if (f == 'y'){
                a.setFilled(true);
            }
            else {
                a.setFilled(false) ;
            }


            a.setRows(numRows);
            a.setCols(numCols);     
            // add rectangle object to arraylist
            box.add(a);     
        }   
        recFile.close();    
        box.toString();
    }
}

1 个答案:

答案 0 :(得分:0)

您可以创建一个函数,将您绘制的实际行和列作为输入,如果您在边界,则返回true,否则返回false。

如果该函数返回true,如果函数返回false,则绘制一个'#'。

显然,只有当你处于未填充状态时才使用该功能。

// Create and return string for output
@Override
public String toString() {
    for(int i = 0; i < numRows;i++){
        for(int a = 0; a < numCols;a++)
        {
            if (amIAtTheBorder(i,a) || filled)
            {
                System.out.print("#");
            }
        }
        System.out.println();
    }
    System.out.println();

    ...
}