Lab: Gaining Experience with Arrays of Arrays
Instructions:
Answer as many of the following questions as you can during the lab period.
If you are unable to complete the assignment during the lab period it
is strongly recommended that you complete it on your own.
Getting Ready:
Before going any further, you should:
-
Make a directory for this lab.
-
Setup your development environment.
-
Download the following files:
to the appropriate directory/directories. (In most browsers, the
easiest way to do this is by right-clicking on each of the links
above.)
1. Processing Arrays of Arrays:
This part of the lab give you some experience with
processing (i.e., performing computations with) arrays
of arrays.
-
Add and implement a method with the following signature to the
DataUtilities
class.
public static int columnTotal(int column, int[][] data)
As the name implies, this method must find the total of the elements
in the given column
of data
.
What code did you add?
public static int columnTotal(int column, int[][] data)
{
int total;
total = 0;
for (int r = 0; r < data.length; r++)
{
total += data[r][column];
}
return total;
}
-
Create a driver that you can use to call this method. (No: There
is no hint for this question.)
-
Add and implement a method with the following signature to the
DataUtilities
class.
public static int rowTotal(int row, int[][] data)
As the name implies, this method must find the total of the elements
in the given row
of data
.
What code did you add?
public static int rowTotal(int row, int[][] data)
{
int total;
total = 0;
for (int c = 0; c < data[row].length; c++)
{
total += data[row][c];
}
return total;
}
-
Add code to your driver that calls this method. (Note: There is
no hint for this question.)
-
Add and implement a method with the following signature to the
DataUtilities
class.
public static boolean isRowConstant(int row, int[][] data)
As the name implies, this method must return true
if all of the elements of the given row
have the
same value and false
otherwise.
What code did you add?
public static boolean isRowConstant(int row, int[][] data)
{
boolean constant;
int c;
constant = true;
c = 1;
while (constant && (c < data[row].length))
{
if (data[row][c] != data[row][c-1])
{
constant = false;
}
c++;
}
return constant;
}
-
Add code to your driver that calls this method. (Note: There is
no hint for this question.)
2. Arrays of Arrays of Objects:
This part of the lab will give you some experience writing code that
creates and and processes arrays of arrays of objects.
-
Read and understand the
Fraction
class.
-
Add and implement a method with the following signature to the
DataUtilities
class.
public static Fraction[][] create(int rows, int columns)
It must create and return a "fraction table" in which the column
corresponds to the denominator of the fraction and the row
corresponds to the numerator of the fraction. (If the denominator
is 0 the element in the table must be null
.)
What code did you add?
public static Fraction[][] create(int rows, int columns)
{
Fraction[][] table;
table = new Fraction[rows][columns];
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < columns; c++)
{
if (c != 0)
{
table[r][c] = new Fraction(r, c);
}
}
}
return table;
}
-
Add code to your driver that calls this method. (Note: There is
no hint for this question.)
-
Add and implement a method with the following signature to the
DataUtilities
class.
public static void print(Fraction[][] table)
It must print the "fraction table" with a tab character after
each entry in the table. null
elements must be blank.
What code did you add?
public static void print(Fraction[][] table)
{
for (int r = 0; r < table.length; r++)
{
for (int c = 0; c < table[r].length; c++)
{
if (table[r][c] != null)
{
System.out.print(table[r][c].toString());
}
System.out.print("\t");
}
System.out.println();
}
}
-
Add code to your driver that calls this method. (Note: There is
no hint for this question.)
-
Add and implement a method with the following signature to the
DataUtilities
class.
public static Fraction[][] extract(Fraction[][] table,
int startingRow, int endingRow,
int startingCol, int endingCol)
It must extract a portion of a "fraction table" and return it.
That is, it must return a Fraction[][]
that contains
some of the rows and columns in table
. The starting
and ending rows and columns (which are inclusive) define the
portion of the table that must be extracted. What code did you
add?
public static Fraction[][] extract(Fraction[][] table,
int startingRow, int endingRow,
int startingCol, int endingCol)
{
Fraction[][] result;
int i, j;
result = new Fraction[endingRow - startingRow + 1][endingRow - startingRow + 1];
i = 0;
for (int r = startingRow; r <= endingRow; r++)
{
j = 0;
for (int c = startingCol; c <= endingCol; c++)
{
result[i][j] = table[r][c];
j++;
}
i++;
}
return result;
}
-
Add code to your driver that calls this method. (Note: There is
no hint for this question.)
-
Add a method to the
DataUtilties
class that returns true
if all of the elements on the main diagonal of a "table of fractions"
have the same value. (Note: There is no hint for this question.)
-
Add code to your driver that calls this method. (Note: There is
no hint for this question.)