Second part of the Arrays Test

By Mickey Arnold
Last updated 10 months ago
6 Questions
ARRAY TEST FREE RESPONSE - A
The following Numbers class will be used to analyze and retrieve sets of numbers.
public class Numbers
{
/** @param num is a positive non-decimal value
* Precondition : num >= 0
* @return true if the sum of the digit divisors of num is odd
@return false if the sum of the digit divisors of num is even
*/
public static boolean isGoofy(int num)
{
/* to be implemented in part(a) */
}

/* @param count is a positive non-decimal value
* Precondition : count > 0
* @return an array containing count Goofy numbers
*/
public static int[] getSomeGoofyNumbers(int count)
{
/* to be implemented in part(b) */
}
// There may be variables / fields, constructors, and methods that are not shown.
}

A. Write the Numbers method isGoofy(), as started below.  isGoofy() will receive an integer and determine if that integer is goofy or not goofy.

A goofy number is any number that has a sum of its digit divisors that is odd. The sum of the digit divisors must be greater than zero.

The number 12 has 2 digits – 1 and 2. The number 123 has 3 digits - 1,2, and 3.

The number 26 has 2 digits – 2 and 6. The number 26 is only divisible by 2.

The call isGoofy(12) would return true as 12 has a digit divisor sum of 3 – [1,2] which is odd.
The call isGoofy(15) would return false as 15 has a digit divisor sum of 6 – [1,5] which is even.
The call isGoofy(26) would return false as 26 has a digit divisor sum of 2 – [2] which is even. The call isGoofy(271) would return true as 271 has a digit divisor sum of 1 – [1] which is odd.
The call isGoofy(1234) would return true as 1234 has a digit divisor sum of 3 – [1,2] which is odd.


/** @param num is a positive non-decimal value
* Precondition : num >= 0
* @return true if the sum of the digits of num is odd
@return false if the sum of the digits of num is even
*/
public static boolean isGoofy(int num)
{
//code goes here
}


B. Write the Numbers method getSomeGoofyNumbers(), as started below.  getSomeGoofyNumbers() will receive a count of how many Goofy numbers to return.
The call getSomeGoofyNumbers(3) would return [1, 3, 5].
The call getSomeGoofyNumbers(15) would
return [1, 3, 5, 7, 9, 10, 12, 13, 14, 16, 17, 18, 19, 21, 25].

You must call the method from part a, assuming the method works as specified regardless of what you wrote. /* @param count is a positive non-decimal value
* Precondition : stop > 0
* @return an array containing count Goofy numbers
*/
public static int[] getSomeGoofyNumbers(int count)
{
//code goes here
}


ARRAY TEST FREE RESPONSE
The following Algorithmic class will be used to analyze the contents of an array.
public class Algorithmic
{

/** @param array is a list of positive integer values
* @param pos is the 1st location to process
* Precondition : array.length>=2 and pos>0 and pos<array.length
* @return the sum of positions pos and pos+1
*/
public static int getPairValue(int[] array, int pos)
{
/* to be implemented in part(a) */
}

/** @param num is a positive non-decimal value
* Precondition : num >= 0
* @return true if num is odd or false if num is even
*/
public static boolean isOdd(int num)
{
/* to be implemented in part(b) */
}

/** @param array is a list of positive integer values
* Precondition : array.length>0
* @return the count of all pairs that are odd
*/
public static int getOddPairCount(int[] array)
{
/* to be implemented in part(c) */
}

/** @param array is a list of positive integer values
* Precondition : array.length>0
* @return an array containing all of the pair sums that are odd
*/
public static int[] getOddPairArray(int[] array)
{
/* to be implemented in part(d) */
}
// There may be variables / fields, constructors, and methods that are //not shown.
}

Part A. Write the Algorithmic method getPairValue(), as started below.  getPairValue() will sum up two adjacent locations and return the sum. The call getPairValue(new int[]{2,2,3},1) would return 5.
The call getPairValue(new int[]{1,2,2,3},0) would return 3.
/** @param array is a list of positive integer values
* @param pos is the 1st location to process
* Precondition : array.length>=2 and pos>0 and pos<array.length-1
* @return the sum of positions pos and pos+1
*/
public static int getPairValue(int[] array, int pos)
{
//Code goes here
}

Part B. Write the Algorithmic method isOdd(), as started below.  isOdd() will receive an integer and determine if that integer is odd or even. The call isOdd(14) would return false.
The call isOdd(7) would return true.
/** @param num is a positive non-decimal value
* Precondition : num >= 0
* @return true if num is odd or false if num is even
*/
public static boolean isOdd(int num)
{
//code goes here
}

Part C. Write the Algorithmic method getOddPairCount(), as started below.  getOddPairCount() will receive an integer array and then determine how many pairs of integers inside the array sum to odd values.
The call getOddPairCount(new int[]{2,2,3}) would return 1.
The call getOddPairCount(new int[]{1,2,2,3}) would return 2.
You may call the methods from part a and part b, assuming the methods work as specified regardless of what you wrote. /** @param array is a list of positive integer values
* Precondition : array.length>0
* @return the count of all pairs that are odd
*/
public static int getOddPairCount(int[] array)
{
//code goes here.
}

Part D. Write the Algorithmic method getOddPairArray(), as started below.  getOddPairArray() will receive an integer array and then return a new array that contains all of the pair sums that are odd.
The call getOddPairArray(new int[]{2,2,3,4,5}) would return [5,7,9].
The call getOddPairArray(new int[]{1,2,2,3,6}) would return [3,5,9].
You may call the methods from part a, part b, and part c, assuming the methods work as specified regardless of what you wrote. /** @param array is a list of positive integer values
* Precondition : array.length>0
* @return an array containing all of the pair sums that are odd
*/
public static int[] getOddPairArray(int[] array)
{
//code goes here
}

Part A. Write the Algorithmic method getPairValue(), as started below.  getPairValue() will sum up two adjacent locations and return the sum. The call getPairValue(new int[]{2,2,3},1) would return 5.
The call getPairValue(new int[]{1,2,2,3},0) would return 3.
/** @param array is a list of positive integer values
* @param pos is the 1st location to process
* Precondition : array.length>=2 and pos>0 and pos<array.length-1
* @return the sum of positions pos and pos+1
*/
public static int getPairValue(int[] array, int pos)
{
//Code goes here//
}


Part B. Write the Algorithmic method isOdd(), as started below.  isOdd() will receive an integer and determine if that integer is odd or even. The call isOdd(14) would return false.
The call isOdd(7) would return true.
/** @param num is a positive non-decimal value
* Precondition : num >= 0
* @return true if num is odd or false if num is even
*/
public static boolean isOdd(int num)
{
//code goes here
}


Part C. Write the Algorithmic method getOddPairCount(), as started below.  getOddPairCount() will receive an integer array and then determine how many pairs of integers inside the array sum to odd values.
The call getOddPairCount(new int[]{2,2,3}) would return 1.
The call getOddPairCount(new int[]{1,2,2,3}) would return 2.
You may call the methods from part a and part b, assuming the methods work as specified regardless of what you wrote. /** @param array is a list of positive integer values
* Precondition : array.length>0
* @return the count of all pairs that are odd
*/
public static int getOddPairCount(int[] array)
{
//code goes here.
}


Part D. Write the Algorithmic method getOddPairArray(), as started below.  getOddPairArray() will receive an integer array and then return a new array that contains all of the pair sums that are odd.
The call getOddPairArray(new int[]{2,2,3,4,5}) would return [5,7,9].
The call getOddPairArray(new int[]{1,2,2,3,6}) would return [3,5,9].
You may call the methods from part a, part b, and part c, assuming the methods work as specified regardless of what you wrote. /** @param array is a list of positive integer values
* Precondition : array.length>0
* @return an array containing all of the pair sums that are odd
*/
public static int[] getOddPairArray(int[] array)
{
//code goes here
}