
CPSC 212-302 Name: _____________________________
Test #1A February 9, 2005
Honor Pledge:
I pledge that I have neither given nor received unauthorized assistance on this test.
Signed: _______________________ Date: _____________
20 points. 50 minutes. Open Java API. Open cucs API. You may not visit any other web
page.
You are to develop a class called MatrixPatterns which has:
1. an instance variable which is a two-dimensional int array,
2. a one-argument constructor MatrixPatterns (String filename)
which instantiates and populates the array with values (only 0’s and 1’s) read
from the user-provided input file, and
3. public methods: int numRowOnes(), int numColOnes(), void
printMatrix()
which print the number of rows that are all ones, the number of columns that are
all ones, and all values of the array, as shown in the example below.
For example, assume that a sample input file called “inputfile” contains the following
data:
4 5
1 1 1 1 1
1 1 0 0 1
1 1 0 1 1
1 1 1 1 1
The first line contains the number of rows (4 in the sample file) and columns (5) of the
array. The next lines contain array int values. Note that there are 4 lines with 5 int
values per line. Note that an array value is either 0 or 1. Values are separated by spaces.
There are no blank lines. You may assume that the input is error free.
Then the following test driver:
public static void main(String args[]) {
MatrixPatterns mp = new MatrixPatterns("inputfile");
mp.printMatrix();
System.out.println(“Number of rows with all ones: “ + mp.numRowOnes());
System.out.println(“Number of columns with all ones: “ + mp.numColOnes());
} // end main