Programming Drill #13

Due: Wednesday, June 11
Goal:  Practice using an integrated debugger

For this drill you must use NetBeans (or some other IDE with a debugger).   First create a NetBeans project. 

The goal is to write a method that "left shifts" the input array by one -- so {6, 2, 5, 3} results in {2, 5, 3, 6}.  Examples include:

shiftLeft({6, 2, 5, 3}) results in the array {2, 5, 3, 6}
shiftLeft({1, 2}) results in the array {2, 1}
shiftLeft({1}) results in the array {1}	

Here is an attempted solution, which you can paste inside the class of your NetBeans project. 

public class ArrayDrill
{
    public void shiftLeft(int[] nums)
    {
      if (nums.length == 0)
      {
         return;
      }
      for (int i = 0; i < nums.length; i++)
      {
        nums[i] = nums[i+1];
      }
      nums[nums.length-1] = nums[0];
    }

    public void outputArray(int[] nums)
    {
	for (int i = 0; i < nums.length; i++)
	{
		System.out.print(nums[i] + " ");
	}
	System.out.println();
    }

    public static void main(String[] args)
    {
       // Test program
       int[] test1 = {6, 2, 5, 3};
       int[] test2 = {1, 2};
       int[] test3 = {1, 2, 3};

       ArrayDrill program = new ArrayDrill();

       System.out.println("Test 1");
       program.outputArray(test1);
       program.shiftLeft(test1);
       program.outputArray(test1);
       System.out.println();

       System.out.println("Test 2");
       program.outputArray(test2);
       program.shiftLeft(test2);
       program.outputArray(test2);
       System.out.println();

       System.out.println("Test 3");
       program.outputArray(test3);
       program.shiftLeft(test3);
       program.outputArray(test3);
       System.out.println();
   }
}
    

This program is supposed to shift left the three test arrays. However, it doesn't quite work.  There are two errors, one makes the program crash and the other makes it output the wrong value.  Use the debugger to find the errors and correct them.  You can start by setting a breakpoint and stepping through the program line by line, looking at variables as you go. Even if you can spot the error by just looking at the program, use the debugger to get some practice for how it works.