Advanced Programming - CS239

Department of Computer Science

LAB 18: EXPERIMENTING WITH ARRAYS OF ARRAYS

Getting Ready: Before going any further you should:

1. Make a directory on your N: drive for this lab.

2. Setup your development environment.

3. Copy the file named FieldFormat.java

4. Copy the file named TableWriter.java

5. Copy the file named Driver.java

 

Part I: This part of the lab will help you understand arrays of arrays.

  1. In the printTable() method, does the variable i correspond to the first index or the second index in the 2-dimensional array table?

 The first

  1. Compile and execute Driver. What is printed?

ÏÏÏ
ÏÏ«Ï ----jGRASP exec: java Driver
ÏϧÏ
ÏϧϠ     DJIA 10,487.11    +16.52    +0.16%
ÏϧϠ   NASDAQ  2,060.94     +3.52    +0.18%
ÏϧϠ  S&P 500  1,142.76     +0.95    +0.08%
ÏϧÏ
ÏÏ©Ï ----jGRASP: operation complete.
¼¼ÏÏ

 

  1. Why do all of the columns in the table line up when they are printed?

Because we have set the field width to 10

 

  1. Is the following declaration valid?

 

String[][] table = {

{"DJIA","10,487.11","+16.52","+0.16%"},

{"NASDAQ","2,060.94","+3.52","+0.18%"},

{"S&P 500","1,142.76","+0.95","+0.08%"},

{"10-Yr TR.","4.20%","+0.06"}

};

 

yes

 

  1. Change the declaration of table in Driver to the above. Compile Driver. Does the fact that it compiles change your answer to the question above?

No because I said it was valid

 

 

 

  1. Execute Driver. What error is generated and why?

Ï «Ï ----jGRASP exec: java Driver
ÏϧÏ
ÏϧϠ     DJIA 10,487.11    +16.52    +0.16%
ÏϧϠ   NASDAQ  2,060.94     +3.52    +0.18%
ÏϧϠ  S&P 500  1,142.76     +0.95    +0.08%
ÏϧÏException in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
ÏϧÏat TableWriter.printTable(TableWriter.java:41)
ÏϧÏat Driver.main(Driver.java:35)
ÏϧÏ
ÏÏ§Ï ----jGRASP wedge2: exit code for process is 1.
ÏÏ©Ï ----jGRASP: operation complete.
¼¼ÏÏThe exception arises because the last row only has 3 elements  so

        When table [i][j] refers to  table[3][3] since there is no 4th element int the 4th row, there is an array out of bounds exception

 

Part II: This part of the lab will help you get some experience writing code that uses arrays of arrays.

  1. Fix the printTable() method in the TableWriter class so that it no longer has the problem identified above. What did you do?

Changed table[0] to table[i] so that it would look at the length of each row and only try to print the number of elements in that row.

for (j=0; j <table[0].length; j++)
for (j=0; j <table[i].length; j++)

 

 

 

2. Write a method public void printTableWithHeaders(String[][] table) that prints row and column headers (containing the index) along with the table. For example:

       0    1   2

0 DJIA 678 Up

1 AMX 1021 Down

 


 

public void printTableWithHeaders(String[][] table)
{
FieldFormat formatter;
int i, j;
formatter =
new FieldFormat();
formatter.setFieldWidth(fieldWidth);
     
// print header row (column indicators)
out.print ("");
for (i = 0; i < table.length; i++)
{ 
   out.print(formatter.fitStringToField(i + " "));
}
out.println();
 
for (i=0; i < table.length; i++)
{
   out.print(i);
//row indicators
   for (j=0; j <table[i].length; j++)
   {
 
     if (table[i][j] == null)
       {
          out.print(formatter.fitStringToField(" "));
       }  // end if
       else
      {
          out.print( formatter.fitStringToField(table[i][j]));
      } // end else
    }// end for j
    out.println();
 } 
// end for i
} // end printTable with headers

 

Output

 Ï«Ï ----jGRASP exec: java Driver
ÏϧÏ
ÏϧϠ       0         1         2         3
ÏϧÏ0      DJIA 10,487.11    +16.52    +0.16%
ÏϧÏ1    NASDAQ  2,060.94     +3.52    +0.18%
ÏϧÏ2   S&P 500  1,142.76     +0.95    +0.08%
ÏϧÏ3 10-Yr TR.     4.20%     +0.06
ÏϧÏ
ÏÏ©Ï ----jGRASP: operation complete. ÏÏÏ


 

 

3. Write a method public void writeTable(String[][] table) that prints each element in the table preceeded by its element reference (i.e., row,column) and a ":". It should print a '\t' between elements and should not print the elements in a fixed-width field (i.e., it should not use a FieldFormat object). For example:

0,0:DJIA 0,1:678 0,2:Up

1,0:AMX 1,1:1021 1,2:Down

 


/**
*  Prints each element in the table preceeded by its element

*  reference(i.e., row,column) and a ":" followed by a \t
*
*   @param table
*/

public void writeTable(String[][] table)
{
     
int i,j;
     
for (i=0; i < table.length; i++)
      {
        
for (j=0; j <table[i].length; j++)  
         {
            System.out.print

                 (i + "," + j +":"+table[i][j]+ ",\t ");
         }
// end for j
         System.out.println();
      }
// end for i
}// end writeTable

Output

ÏÏ«Ï ----jGRASP exec: java Driver
ÏϧÏ
ÏϧÏ0,0:DJIA,_ 0,1:10,487.11,_ 0,2:+16.52,_ 0,3:+0.16%,_
ÏϧÏ1,0:NASDAQ,_ 1,1:2,060.94,_ 1,2:+3.52,_ 1,3:+0.18%,_
ÏϧÏ2,0:S&P 500,_ 2,1:1,142.76,_ 2,2:+0.95,_ 2,3:+0.08%,_
ÏϧÏ3,0:10-Yr TR.,_ 3,1:4.20%,_ 3,2:+0.06,_
ÏϧÏ
ÏÏ©Ï ----jGRASP: operation complete.