ICS 141. Programming with Objects. Lab 4.
EXTRA CREDIT 15 Points
Due No Later than Feb. 10
Your Name:
_____________________________________________________________
Points: 15
Points earned:
Goals: To
learn multi-dimensional arrays.
Part I: Understanding the Problem.
Suppose we have the following
table that gives the distances between a collection of cities:
|
|
|
|
|
|
|
|
|
0 |
280 |
150 |
64 |
68 |
|
|
280 |
0 |
380 |
344 |
230 |
|
|
150 |
380 |
0 |
140 |
210 |
|
|
64 |
344 |
140 |
0 |
120 |
|
|
68 |
230 |
210 |
120 |
0 |
The problem is to create an
array that stores the above information and then write some interesting code
using the array. We need multi-dimensional arrays to do this.
Part II: Array Declarations and Make the Code to Work:
Below, you are given the code
that almost completely declares the array and checks whether the information in
the above table is symmetrical. A table
is symmetrical if the content of a cell with index (i, j) is equal to the
content of a cell with index (j, i).
1. Your first task is to
complete the code. Determine what is missing and fix the code. Do it now. In the code given below, there are some
syntax errors and you should fix them first.
//
Author: Kuodi Jian
//
Date:
// Description: Check 2D array for symmetrical
property. Find maximum
// distance between any two
cities.
public class Lab3 {
//
Declare, instantiate and initialize an array
private
static distances = {{0, 280, 150, 64, 68},
{280,
0, 380, 344,230},
{150,
380, 0, 140, 210},
{64,
344, 140, 0, 120},
{68,
230, 210, 120, 0}};
//
Compare each column with its corresponding row to check for
// the
symmetrical property
public static boolean
isSymmetrical(anArray) {
for
(int row = 0; row < anArray.length; row++) {
for (int column = 0; column <
anArray[row].length; column++) {
if
(anArray[row][column] != anArray[column][row])
return
false;
}
}
return
true;
}
// From
main(), invoke the symmetry checking method
public
static void main(String[] s) {
for
(int row = 0; row < distances.length; row++) {
for (int column = 0; column <
distances[row].length; column++) {
System.out.print(distances[row][column]+
" ");
}
System.out.println();
}
if
(isSymmetrical(distances)) {
System.out.println("Data
is symmetric.");
}
else
{
System.out.println("Data
is asymmetric.");
}
}
}
Part III: Writing More Code Using Multi-Dimensional
Arrays.
Here is pseudo code that prints for each city the maximum distance from that city to any other city. Complete the code. The expected output is
280
380
380
344
230
public
static void printLargestDistance(int[][] distances) {
for (each city from 0 to number of
cities) {
let max be the distance from city
to the 0th city;
for (each other city from 1 to
number of cities) {
if (the distance from city
to other > max) {
max = distance from
city to other;
}
}
System.out.println(max);
}
}
After finishing the
above method, you need to add one line of code “printLargestDistance(distances)” at the bottom of the main method.
Capture the output window,
paste it into a MS Word page, print it out and staple it with this document.
Hand to your instructor the
following:
1)
Your screen
captured output.
2)
A diskette with
your program on it