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?

 

  1. Compile and execute Driver. What is printed?

 

 

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

 

 

  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"}

};

 

 

 

  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?

 

 

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

 

 

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?

 

 

 

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

 

 

Attach your method to your submission

 

 

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

 

Attach your method to your submission.