Inf1 OP : Lab Sheet Week 4 Q1 - Array Front
Overview

In this exercise, you will write a program ArrayFront9 containing a static method arrayFront9() with the following signature:

public static boolean arrayFront9(int[] nums)

Given an array of ints, this returns true if one of the first four elements in nums is a 9. The array length may be less than 4.

Here are some examples of evaluating the function on representative input arrays:

arrayFront9({1, 2, 9, 3, 4}) -> true
arrayFront9({1, 2, 3, 4, 9}) -> false
arrayFront9({1, 2, 3, 4, 5}) -> false

When you write a static function such as arrayFront9(), you should put it in the body of the class definition. You should not put it inside a main() method. In other words, your class definition will look roughly as follows:

public class ArrayFront9 {

        public static boolean arrayFront9(int[] nums) {
           // ADD CODE HERE
        }
}

However, you should always test your code, and to do this, you will need to be able call your method from inside the main() method. The code below extends the previous example to illustrate how you can take an int array from the command-line and pass it to the method arrayFront9().

public class ArrayFront9 {
    public static boolean arrayFront9(int[] nums) {
       // ADD CODE HERE
    }

    public static void main(String[] args) {
        int N = args.length;
        int[] nums = new int[N];
        for (int i = 0; i < N; i++) {
            nums[i] = Integer.parseInt(args[i]);
        }
        System.out.println(arrayFront9(nums));
    }
}

Example outputs are as follows:

: java ArrayFront9 1 2 3 4 9
false

: java ArrayFront9 1 2 9 3 4
true

: java ArrayFront9 1 9
true

An automated test has been created for this exercise: ArrayFront9Test.java.