CS 231: Lab #2

The goal of this lab period is to get you started on the current assignment.

Documentation for Java 1.5 is located at: Java 1.5 SE API.

Documentation for Java 1.6 is located at: Java 1.6 SE API

If using a terminal is new to you, or you need a refresher, please check out the Terminal tutorial.


  1. Create a working directory for today's lab. Then start a Java file called Lab2Test.java.
  2. Create a main function in the Lab2Test class. The args parameter 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 try running your Lab2Test with some additional strings on the command line.
  3. 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 boobo. Given a string s, you can make the conversion using the Integer method parseInt().

    Integer.parseInt( s )

  4. Create a 2D array of type String. I'll call it ranger.

    String 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 references and column references separately.

    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 it a random character.

  5. Print out the contents of the 2D array as a 2D grid using a nested for loop. You can do this by using the System.out.print for all the elements of a row and then putting a System.out.println between rows.

Once you are comfortable with the above tasks, go on to the assignment.