RU beehive logo ITEC dept promo banner
ITEC 120
2010fall
ibarland

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs

lect13c
2-D arrays

Review solution: Hideout#isInRevolt, and how it's a straightforward loop once we write willBattleAnyOf (which is also a straightforward loop).

How 2-D arrays are represented. Processing an entire array, by processing one row at a time.

Compare: How to represent objects in Super Mario Galaxy?
What classes involved? What fields? What methods?
Note that they have an 'angry' mode, which eventually stops. How is that implemented?

hw10—gRUe: what classes involved? What fields? What methods?

2-D arrays: concepts

We can have arrays of any type, in Java. We've seen, array-of-double (double[]) array-of-string (String[]), and array-of-Robot (Robot[]), for example. But we might also have array-of-array-of-double (that is, double[][]). We call this a two-dimensional array.

The gives an example 2-D arrays to show a tic-tac-toe board in §7.6.

Methods on 2-D arrays

Group Task Write a method which takes any 2-D array of doubles, and calculates the average value (that is, it adds them all up, and divides by the length×width).
To add up all the values in the array, you can either use a helper method ( something like double addOneRow( double[][] nums, int rowNum)), or you can use a nested loop.

example code

Your Task Write a method which takes any 2-D array of ints, and finds the maximum value.
You can either use a helper method (double findMaxInRow( double[][] nums, int rowNum)), or you can use a nested loop.

Your Task Write a method void moveEachToward(double[][] nums, double target) which takes any 2-D array of ints, and replaces each number n with2 (n+target)/2. That is, moveEachHalfwayTo(0) ends up halving each item in the array, and moveEachHalfwayTo(256) makes each item “twice as close” to 256.
You can either use a helper method (something like moveEachInRowToward( double[][] nums, int target, int rowNum)), or you can use a nested loop.


1 Actually, BlueJ let's you type this in its interactive call-a-method dialog box.      

2 Hey, for once integer division is what we want, since we're storing the result back into an int. Note that any number in [0,256), when average with 256, is still in [0,256). That's because (255+256)/2 is 255, by integer division. This will be important below.      

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs


©2010, Ian Barland, Radford University
Last modified 2010.Dec.07 (Tue)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme