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

In this lab, you open a file and read input from that file in a prewritten...
In this lab, you open a file and read input from that file in a prewritten C++ program. The program should read and print the names of flowers and whether they are grown in shade or sun. The data is stored in the input file named flowers.dat. Instructions Ensure the source code file named Flowers.cpp is open in the code editor. Declare the variables you will need. Write the C++ statements that will open the input file flowers.dat for reading....
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of...
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 will read in from input file one line at a time until...
Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespaces, commas and periods....
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 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).
Program Language C++ How do I take lines of string from an input file, and implement...
Program Language C++ How do I take lines of string from an input file, and implement them into a stack using a double linked list? Example input, command.txt, and output file. Ex: Input.txt postfix: BAC-* prefix:+A*B/C-EF postfix:FE-C/B*A+ postfix:AB-C-D/ postfix:AB-CF*-D / E+ Command.txt printList printListBackwards Output.txt List: postfix:BAC-* prefix:+A*B/C-EF postfix:FE-C/B*A+ postfix:AB-C-D/ postfix:AB-CF*-D/E+ Reversed List: postfix:AB-CF*-D/E+ postfix:AB-C-D/ postfix:FE-C/B*A+ prefix:+A*B/C-EF postfix:BAC-*
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT