Java Introductory Lab

Part 1: Compiling and running a program

1. Create the program Prog1.java (capitalization is important) in your favorite text editor, and enter in the following program.
class Prog1 {

  /* This space intentionally left blank.
     Part 2 of this lab should be inserted
     here when you get there. */

  public static void main(String args[]) {

    // Create an array of numbers
    int arraySize = 10;
    int[] numArray = new int[arraySize];

    // Fill the array with random integers
    int i;
    for (i=0; i < arraySize; i++)
      numArray[i] = (int)(Math.random()*50);

    // Display the integers to the screen
    for (i=0; i < arraySize; i++) {
      System.out.print(numArray[i]);
      System.out.print(" ");
    }
    System.out.println();

  }
}

2. Compile the program by typing "javac Prog1.java" at a command prompt. Debug if you have any compiler errors.

3. Type "ls" to look at your files. You should see Prog1.java, which is the source code. You should also see Prog1.class, which is the bytecode produced by the compiler.

4. Run your program by typing "java Prog1" (do not include the .class at the end of it). This will run your program. What does it do?

5. What similarities and differences do you see in the program with C++? In particular, Java uses garbage collection. What extra statement would be needed in this program if Java did not implement garbage collection?
 

Part 2: Adding class methods to the class Prog1

1. The following code adds a method (function associated with a class) to your class Prog1. Add the following code in the spot indicated by the comments in the above program.
private static int addup(int numbers[]) {
  int i, total=0;
  for (i=0; i < numbers.length; i++)
    total += numbers[i];
  return total;
}

Notice the expression "numbers.length". What does this do? How is this different from C++? What does this suggest that a Java array really is?

2. Add code to the main function to call addup and display the answer to the screen.

3. Add another method to the class to sort the numbers in the array. Add code to main to display the sorted array to the screen.
 

Part 3: Creating your own instantiable classes

1. In your text editor, start a new program called Prog2.java. Insert the following code:
class NumberList {
  private int[] numArray;
  private int arraySize;

  public NumberList() {
    arraySize = 10;
    numArray = new int[arraySize];
    for (int i=0; i < arraySize; i++)
      numArray[i] = (int)(Math.random()*50);
  }

  public void displayNumbers() {
    for (int i=0; i < arraySize; i++) {
      System.out.print(numArray[i]);
      System.out.print(" ");
    }
    System.out.println();
  }
}
 
class Prog2 {
  public static void main(String args[]) {
NumberList list1 = new NumberList();
NumberList list2 = new NumberList();
    NumberList list3 = new NumberList();

    list1.displayNumbers();
    list2.displayNumbers();
    list3.displayNumbers();
  }
}

2. NumberList() is a constructor for the class NumberList. Java has no destructors. Why?

3. Note that list1 is a dynamically created NumberList, but a "." and not a "->" is used to access the method. What are the benefits of this syntax change from C++?

4. Add a public method to the class NumberList to sort the numbers. Add code to main to display the sorted numbers when you run your program.

5. Add a second constructor to NumberList which allows the arraySize to be set.

6. Change main so that instead of three different variables (list1, list2, list3), an array of NumberLists is created and used.

7. Intentionally break your program so that you overstep the bounds of the array, i.e. you try to access an array element that's higher than one you declared. How does Java respond? What would C++ have done?

8. Add a static variable (class variable) to the class NumberList which keeps track of how many NumberList objects have been instantiated. Update it appropriately in the constructor. Provide a static method (class method) to print out the value of this variable.