The goal of this lab period is to give you practice using 2D arrays.
- Make a new folder for this week's project.
- Start a Java file called
Grid.javaand make a public class called Grid.
Create a main function in the Grid class. The
argsparameter of the main function contains strings from the command line. Use a for loop to print out all of the elements of the args array. Then run your Grid class a few different times with additional strings on the command line. For example, try the following.
$ java Grid 42 96 81 hut hut
Declare two int variables in the main function, I'll call them yogi and booboo. Convert arguments 0 and 1 of the command line strings into ints and assign them to yogi and booboo. Given a string s, you can make the conversion using the Integer method
Create a 2D array of type String. I'll call it ranger.
Start by allocating the 2D array in a single new statement, giving it yogi and booboo as the row and column sizes. Then try the alternative of allocating the row size and column size separately. Is there any value in the second method?
Allocating a 2D grid of String references does not create any String objects, however. To do that, you have to go through every location in the grid and create the String object for it. Do that now and give each location in the grid a random character.
- Print out the contents of the 2D array as a 2D grid using a nested for loop. Make it look nice. As part of the print process, print out the number of rows and columns in the grid, querying the array for its length.
Once you have completed the lab, go on to the project.