Question

In: Computer Science

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

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

###

###

###

###

###

###

######

#       #

######

####

####

####

####

######

#       #

#      #

#      #

#       #

######

####

####

####

####

####

####

####

####

####

########

#           #

#            #

########

Solutions

Expert Solution

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:\\rectangleData.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+"\n";
       if(filled){
       for(int i=0; i<numRows ; i++){
           for(int j=0; j<numCols; j++){
               s = s + "# ";
           }
           s = s + "\n";
       }
       }
       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 + "\n";
           }  
       }
       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
# # # # # # # #
# #
# #
# # # # # # # #


Related Solutions

Implement in Python a script that does the following: 1) reads input from a supposed file...
Implement in Python a script that does the following: 1) reads input from a supposed file called firstnames_2.txt. 2) processes the input and writes and saves the output to a file. NOTE: Please make sure that the names are written in the outfile with one name on each line no comma ( , ) after the name in the output
Write a program that takes two sets ’A’ and ’B’ as input read from the file...
Write a program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the file...
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file...
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file prog2 input.txt. The first line of the file corresponds to the set ’A’, the second line is the set ’B’, and the third line is the set ’C’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The...
1.   Bubble Sort Implement a bubble sort program that will read from a file “pp2.txt” from...
1.   Bubble Sort Implement a bubble sort program that will read from a file “pp2.txt” from the current directory a list of intergers (10 numbers to be exact), and the sort them, and print them to the screen. You can use redirection to read data from a given file through standard input, as opposed to reading the data from the file with the read API (similar to Lab #1). You can assume the input data will only have 10 numbers...
Write a program that creates a concordance. There will be two ways to create a concordance. The first requires a document to be read from an input file, and the concordance data is written to an output file.
Concepts tested by this program            Hash Table,            Link List,hash code, buckets/chaining,exception handling, read/write files (FileChooser)A concordance lists every word that occurs in a document in alphabetical order, and for each word it gives the line number of every line in the document where the word occurs.Write a program that creates a concordance. There will be two ways to create a concordance. The first requires a document to be read from an input file, and the concordance data is written to...
Implement in a computer the curve fitting. Generate appropriate data for your implementation (e.g., generate data...
Implement in a computer the curve fitting. Generate appropriate data for your implementation (e.g., generate data from a polynomial function and add noise with variance σ2). Show the mean square error of the estimator in your implementation. What can you say about under/over fitting (that is, when the degree of your approximator is too small or too large for your data).
Design and implement a C++ program read in a whole line of characters as the input...
Design and implement a C++ program read in a whole line of characters as the input string; count and display how many times how frequently (among the letters) each (case insensitive) letter appears in the above mentioned input string; Sample program execution: An example of executing such a program is shown below. Note that the user input is in italic font. Please enter a line of characters: This is a really long line of characters! There are 41 characters in...
Write a modularized, menu-driven program to read a file with unknown number of records. Input file...
Write a modularized, menu-driven program to read a file with unknown number of records. Input file has unknown number of records of inventory items, but no more than 100; one record per line in the following order: item ID, item name (one word), quantity on hand , and a price All fields in the input file are separated by a tab (‘\t’) or a blank ( up to you) No error checking of the data required Create a menu which...
The file HW_05.xlsx contains data from a survey of 105 randomly selected households. a. Interpret the...
The file HW_05.xlsx contains data from a survey of 105 randomly selected households. a. Interpret the ANOVA table for this model. In particular, does this set of independent variables provide at least some power in explaining the variation in the dependent variable? Report the F ratio statistics and p- value for this hypothesis test. b. Interpret coefficients of independent variables in the model. c. Using the regression output, determine which of the independent variables should be excluded from the regression...
Write a Fortran program that is able to read in the data file. The file has...
Write a Fortran program that is able to read in the data file. The file has lines with the structure: 19990122 88888 30.5 Where: i) the first is an 8 digit code with the date: yyyymmdd (yyyy is the year, mm is the month, and dd is the day) ii) the second is the five digit odometer reading of a car iii) the third is the amount of fuel put into the car on that date to fill the tank...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT