Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement an application that will read data from an input file, interpret the d

ID: 3782150 • Letter: I

Question

Implement an application that will read data from an input file, interpret the data, and generate the output to the screen.

- The application will have two classes, Rectangle and Lab2ADriver.

- Name your Eclipse project, Lab2A.

Implement the Rectangle class with the following description.

Rectangle

Data fields

-numRows: int

-numCols: int

-filled: Boolean

Store the number of rows in the Rectangle

Store the number of columns in the Rectangle

Will define either a filled or unfilled rectangle

True = filled, False = unfilled

Methods

+Rectangle()

+Rectangle(int, int)

+Getter methods

+Setters methods

+toString(): String

Initialize rows, columns to 1 and filled to false

Input values will set the numRows & numCols

Create and return a String for output

For example when

numCols = 4, numRows = 3, filled = false

The method creates and returns the string

####
#   #
####

- Implement Lab2ADriver class with the following description. The Lab2ADriver class:

- Contains the main method.

- Contains an object array that will hold Rectangle objects.

- Reads in the text file “rectangleData.txt” that starts with the total number of lines in the file followed by data for constructing a Rectangle object.

- With the data read from the file, construct Rectangle object, and store it to the object array.

- Prints Rectangle objects to the screen accordingly.

For example

6                 <= Number of lines in an input file

6 3 filled

3 6 unfilled

4 4 filled

6 6 unfilled

9 4 filled

4 8 unfilled

rectangleData.txt

Output

###

###

###

###

###

###

######

#       #

######

####

####

####

####

######

#       #

#      #

#      #

#       #

######

####

####

####

####

####

####

####

####

####

########

#           #

#            #

########

Rectangle

Data fields

-numRows: int

-numCols: int

-filled: Boolean

Store the number of rows in the Rectangle

Store the number of columns in the Rectangle

Will define either a filled or unfilled rectangle

True = filled, False = unfilled

Methods

+Rectangle()

+Rectangle(int, int)

+Getter methods

+Setters methods

+toString(): String

Initialize rows, columns to 1 and filled to false

Input values will set the numRows & numCols

Create and return a String for output

For example when

numCols = 4, numRows = 3, filled = false

The method creates and returns the string

####
#   #
####

Explanation / Answer

Lab2ADriver.java


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Lab2ADriver {

   public static void main(String[] args) throws FileNotFoundException {
       Scanner scan = new Scanner(new File("D: ectangleData.txt"));
       Rectangle r[] = new Rectangle[scan.nextInt()];
       for(int i=0; i<r.length; i++){
           int rows = scan.nextInt();
           int cols = scan.nextInt();
           String filled = scan.next();
          
           r[i] = new Rectangle(rows, cols);
           if(filled.equals("filled")){
           r[i].setFilled(true);  
           }
           else{
               r[i].setFilled(false);  
           }
       }
       for(int i=0; i<r.length; i++){
       System.out.println(r[i].toString());
       System.out.println();
       }
   }

}

Rectangle.java


public class Rectangle {
   private int numRows;
   private int numCols;
   private boolean filled;
   public Rectangle(int rows, int cols){
       numCols = cols;
       numRows = rows;
      
   }
   public Rectangle(){
       numCols = 1;
       numRows = 1;
       filled = false;
   }
   public int getNumRows() {
       return numRows;
   }
   public void setNumRows(int numRows) {
       this.numRows = numRows;
   }
   public int getNumCols() {
       return numCols;
   }
   public void setNumCols(int numCols) {
       this.numCols = numCols;
   }
   public boolean isFilled() {
       return filled;
   }
   public void setFilled(boolean filled) {
       this.filled = filled;
   }
   public String toString(){
       String s = "numCols = "+numCols+", numRows = "+numRows+", filled = "+filled+" ";
       if(filled){
       for(int i=0; i<numRows ; i++){
           for(int j=0; j<numCols; j++){
               s = s + "# ";
           }
           s = s + " ";
       }
       }
       else{
           for(int i=0; i<numRows ; i++){
               for(int j=0; j<numCols; j++){
                   if(i==0 || i == numRows-1){
                   s = s + "# ";
                   }
                   else{
                       if(j==0 || j==numCols-1){
                       s = s + "# ";
                       }
                       else{
                           s = s + " ";
                       }
                   }
               }
               s = s + " ";
           }  
       }
       return s;
   }
}

Output:

numCols = 3, numRows = 6, filled = true
# # #
# # #
# # #
# # #
# # #
# # #


numCols = 6, numRows = 3, filled = false
# # # # # #
# #
# # # # # #


numCols = 4, numRows = 4, filled = true
# # # #
# # # #
# # # #
# # # #


numCols = 6, numRows = 6, filled = false
# # # # # #
# #
# #
# #
# #
# # # # # #


numCols = 4, numRows = 9, filled = true
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #


numCols = 8, numRows = 4, filled = false
# # # # # # # #
# #
# #
# # # # # # # #

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote