Radford University ITEC

ITEC120-ibarland (incl. office hrs)infolectureslabshws

lab13: arrays

Nov.15 (Wed):
You can work on today's lab individually, or in pairs, as you like. It will not be checked off, but it's fair game for exam2.

These are the same exercises as last week, but using arrays, rather than lists. The lectures have some examples, of course.

  1. Interactively in BlueJ's code pad, Create a Double[] (“array of Double”) with three elements.
  2. Still in Code Pad, fill each location with some particular Double. Experiment with looking up values, and access the array's length. Can you make an array which contains identically the same Double object twice?
  3. Make a backup/archive copy of your hw05. For this lab, write a method static testTreasureArray(). This method will create three Treasures, and put them into an array of size three. Print out the array's toString to System.out, and note how useless it is. Try using java.util.Arrays.toString(), passing it your array.
  4. Using a for-loop, add up the weight of every Treasure in the list. (You can do this from within the method testTreasureArray, if you like.)
  5. Let's make a function which can print a nicer version of a Treasure-list. It should take in a Treasure[] as a parameter, and return a String: list only the Treasure's name, preceded by a number (starting with 1, not 0!)
    1) a chocolate egg
    2) a multi-colored pen
    3) a small nugget
    
    Note that this will have to be a static method, presumably inside your class Treasure.
    (This problem won't be re-used in a later homework though, since we won't used fixed-size arrays of Treasures. An array would be appropriate for a Explorer with exactly two pockets (or perhaps, with exactly 47 pockets, as in the safari jackets which are all the rage among explorers these days), but we won't actually use them in our homework. It'll be more appropriate to later have our Explorer keep a backpack full of Treasures — since a backpack doesn't contain a fixed number of items, it's more appropriate to represent a backpack using a list-of-Treasures, than using an array-of-Treasures.)

ITEC120-ibarland (incl. office hrs)infolectureslabshws


Nov.17 (Fri): Two-dimensional arrays

(We will have lab, but nothing will be checked off. Of course, you are responsible for knowing the material.)

The following code models a rectangular world with different types of terrain.

class World {
  private final static int WIDTH  = 7;
  private final static int HEIGHT = 5;
  char[][] terrain = new char[HEIGHT][WIDTH];   /* A 2-D array of characters. */
  
  public static final char MOUNTAIN_CHAR = 'M';
  public static final char WATER_CHAR    = '~';
  public static final char SWAMP_CHAR    = 'o';
  public static final char PLAINS_CHAR   = '.';
  public static final char FOREST_CHAR   = '^';


  /** Fill the array terrain[][] with random values.
   */
  public void fillAllRows() {
    for ( int r = 0;  r < terrain.length;  ++r ) {
      fillOneRow(r);
      }
    }

  /** Fill one row of the array terrain[]
   * @param rowNum which row to fill (0..{@value HEIGHT}).
   */
  private void fillOneRow( int rowNum ) {
    for ( int c = 0;  c < terrain[rowNum].length;  ++c ) {
      terrain[rowNum][c] = aRandomTerrain();
      }
    }

 /**
  * @return A random terrain feature (as a char).
  */
  private java.util.Random r = new java.util.Random();
  public char aRandomTerrain() {
    int prcnt = r.nextInt(100);
    if      (prcnt <  20) { return MOUNTAIN_CHAR; } 
    else if (prcnt <  40) { return WATER_CHAR;    }
    else if (prcnt <  60) { return PLAINS_CHAR;   }
    else if (prcnt <  80) { return SWAMP_CHAR;    }
    else if (prcnt < 100) { return FOREST_CHAR;   }
    else {
      System.err.println( "Whoops, fell through to end of aRandomTerrain." );
      return 'X';
      }
    }


  /**
   * @param rowNum which row to print, in [0,{@value HEIGHT}).
   * @return a string representing one row of the world's terrain.
   *   (A string of length {@value WIDTH}.)
   */
  String oneRowToString( int rowNum ) {
    String mapRow = "";
    for (int c = 0;  c < terrain[rowNum].length;  ++c ) {
      mapRow = mapRow + terrain[rowNum][c];
      }
    return mapRow;
    }

  /** @return a string representing the entier world's terrtain.
   *  (Will be {@value HEIGHT} rows, each of width {@value WIDTH}.)
   */
  public String toString() {
    String map = "";
    for (int rowNum = 0;  rowNum < terrain.length;  ++rowNum ) {
      map = map + oneRowToString( rowNum ) + "\n";
      }
    return map;
    }
    
  }
(You can also save-to-disk lab13-world.jar.)

ITEC120-ibarland (incl. office hrs)infolectureslabshws


©2006, Ian Barland, Radford University
Last modified 2006.Nov.16 (Thu)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme