CSE021 Lab12-2D Arrays and File Output Solved

30.00 $ 15.00 $

Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: . zip solution files instantly, after Payment


Rate this product

This week we will read more than one file as input, and also create an output file. More specifically, we will take in two matrices and multiply them. The result will then be written to a file. Most of the code and logic you need to finish this lab is presented in lecture this week, and also included in this lab assignment. Your job is to combine each portion to do the job correctly.


Before you get started, read chapters 5.7 and 5.9. Answer the assessment questions as you encounter them in the next section. The prompts for answering assessment questions are placed immediately following the material to which the listed questions relate.

Getting Started

After following the import instructions in the assignment page, you should have a Java project in Eclipse titled Lab 21_12. This PDF document is included in the project in the doc directory. The Java files you will use in this lab are in the src/matrix directory.

Part 1: Multiplying Matrices

The file format we will use has 2 types of information. The first is the dimensions of the array. The second is the actual values inside the array. In order to encode this information, we create a structured format that is easy to understand.


The first line will contain 2 numbers. The first is the # of rows and the second is the # of columns. Then each row of values will be printed in corresponding columns in a tab-delimited manner.



1   2   3
3   2   1
1   3   2


The 3×3 matrix shown above would be translated to the following file:


3      3

1      2     3

3      2     1

1      3     2


We will introduce is the new keyword null which means an ‘empty’ object to which a pointer can point. We can also check if a pointer is valid by checking it against the object, null.


Task 1: Analyze code

Analyze the following code and answer the assessment questions that follow:


1 public static int[][] proc(String filename) {


  • int[][] arr = null;
  • try {
  • Scanner sc = new Scanner(new FileReader(filename));
  • int row = sc.nextInt();
  • int column = sc.nextInt();
  • arr = new int[row][column];


  • for (int i = 0; i < row; i++) {
  • for (int j = 0; j < column; j++) {
  • arr[i][j] = sc.nextInt();
  • }
  • }
  • close();
  • } catch(NoSuchElementException e) {
  • out.println(e);
  • } catch(FileNotFoundException e) {
  • out.println(e);
  • }
  • return arr;
  • }


[Answer assessment questions 1a – 1g]


Task 2: Fill-in readMatrix

Fill in the code to read in the values from the file whose name is specified by the given string parameter inside the readMatrix method of MatrixMultiply.java [Hint: see Task 1].


Task 3: Analyze code

Analyze the following code sample and answer the assessment questions that follow:


String filename = “Result.txt”;

try {

FileWriter output = new FileWriter(filename);   String ostr = “”;   for (int i = 0; i < arr2D.length; i++) {    for (int j = 0; j < arr2D[0].length; j++) {      System.out.print(ostr = (arr2D[i][j] + “\t”));



System.out.println();    output.write(“\r\n”); // Carriage return



} catch(Exception e) {




[Answer assessment questions 2a – 2c]


Task 4: Fill-in writeMatrix

Fill in the code to write the values after multiplying the two matrices [Hint: See Task 3]. You will have 3 input combinations to test for this lab. Each output should be saved to a different file. They should be named Result1.txt, Result2.txt and Result3.txt.


Result1.txt is matrix1.txt and matrix2.txt multiplied.

Result2.txt is matrix2.txt and matrix3.txt multiplied.

Result3.txt is matrix2.txt and matrix4.txt multiplied.


You may do this by simply renaming the output file the program produces. Or you may modify filenames in code for different runs of the program. Other techniques are also possible.

Part 2: Fill in Survey

Please complete the survey in survey.txt provided in the project’s doc directory.


Part 3: (Assessment) Logic Check and Level of Understanding

Create a Word document or text file named Part3 that contains answers to the following:

  • For the proc method shown in Task 1, answer the following:
    1. What is the return type of the method, proc? What line in the code can you find this information?
    2. What is line 3 doing? (Answer in terms of variable name, type and initial value)
    3. Why do we need to use try in line 4?
    4. What line(s) read in the dimension of the matrix?
    5. What is line 8 doing?
    6. Why is there no while-loop in this method, and why we can use a for-loop?
    7. How many times does line 12 get executed? (Answer in terms of an expression involving # rows and # of columns)
  • Answer the following questions concerning the code given in Task 3:
    1. What does length mean?
    2. What does arr2D[0].length mean?
    3. What does arr2D[i][j] + “\t” create?
  • Lab12-ukkmkg.zip