Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a simple program drawing rectanges
    primarykey
    data
    text
    <p>I am a beginner Java coder and I am working on a assignment for class. We have to make a rectangle class and a driver class. The point of the program is to draw rectangles with the '#' symbol based on user input of how many rows, columns, and if it is filled. So far I have everything but the filled or not filled logic. For example if I put 3 rows and 3 columns and it is filled it would look like this: </p> <pre><code>### ### ### </code></pre> <p>if it was the same thing but not filled it would look like this: </p> <pre><code>### # # ### </code></pre> <p>Please look at my rectangle class and driver class and see if you can help!</p> <pre><code>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 &lt; numRows;i++){ for(int a = 0; a &lt; 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 + "]"; } } </code></pre> <p>and my driver class: </p> <pre><code>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 &lt; 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&lt;rectangle&gt; box = new ArrayList&lt;rectangle&gt;(); // 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(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload