Second part of the Arrays Test
By Mickey Arnold
starstarstarstarstar
Last updated 10 months ago
6 Questions
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
}