Due: Monday, October 29
Goal: Basic Array Handling
Listed below is a class. Your task in this drill is to complete the two methods.
The first method, firstLast6
, should return true if the array starts or ends with the
digit 6. Otherwise, the method should return false.
The second method, countNum6s
, should loop through the array and return the number
of 6's contained in the array.
The main method tests the firstLast6
and countNum6s
methods using some sample
arrays.
public class ArrayDrill
{
public static boolean firstLast6(int nums[])
{
// Complete the code here so this method
// returns true if nums starts or ends with a 6
}
public static int countNum6s(int nums[])
{
// Complete the code here to loop through the nums array
// and return the number of 6's in the array
}
public static void main(String[] args)
{
// Test the firstLast6 method with three cases
int[] nums1 = {1, 2, 6};
if (firstLast6(nums1) == true)
System.out.println("firstLast6: Correct result for {1, 2, 6}");
else
System.out.println("firstLast6: Incorrect result for {1, 2, 6}");
int[] nums2 = {6, 1, 2, 3};
if (firstLast6(nums2) == true)
System.out.println("firstLast6: Correct result for {6, 1, 2, 3}");
else
System.out.println("firstLast6: Incorrect result for {6, 1, 2, 3}");
int[] nums3 = {13, 6, 1, 6, 3};
if (firstLast6(nums3) == false)
System.out.println("firstLast6: Correct result for {13, 6, 1, 6, 3}");
else
System.out.println("firstLast6: Incorrect result for {13, 6, 1, 6, 3}");
// Test the countNum6s method
System.out.println();
if (countNum6s(nums1) == 1)
System.out.println("countNum6s: Correct result for {1, 2, 6}");
else
System.out.println("countNum6s: Incorrect result for {1, 2, 6} got " + countNum6s(nums1));
int[] nums4 = {1, 4, 7};
if (countNum6s(nums4) == 0)
System.out.println("countNum6s: Correct result for {1, 4, 7}");
else
System.out.println("countNum6s: Incorrect result for {1, 4, 7} got " + countNum6s(nums1));
if (countNum6s(nums3) == 2)
System.out.println("countNum6s: Correct result for {13, 6, 1, 6, 3}");
else
System.out.println("countNum6s: Incorrect result for {13, 6, 1, 6, 3} got " + countNum6s(nums1));
}
}
As usual, show working code to your instructor by the end of the day or email to kenrick@uaa.alaska.edu