Tag Archives: loop

Array Fun 2 [2023]

Complete all the methods below. When you are done, copy and paste the    testmethods.v5.1  into your class and run it to see if you have any obvious errors.  The above code does not guarantee you a 100 but will help catch most of the common errors that students make.

Note: You may not use any external libraries (like Java.Arrays etc )or import any code. Everything can be done with just loops and variables.

int sumEveryN(int[] nums, int n)

Description:This method returns the sum of every  n  elements of nums ..

Method Call return value/output
sumEveryN( {1 , 2 , 3 , 4 }, 2 ) 4( ie 1 +3)
sumEveryN( {13 , 42, 15, 33 , 44 , 16 , 52} ,3) 98 ( ie 13 + 33+ 52)

String[] doubleArr(String[] strs)

Description: This method returns a new version of strs  in which each element now appears twice. This can be done with a for-each loop, which I believe is easier and more intuitive.

Method Call return value/output
doubleArr( {“a”,”b”,”c”} ) {“a”,”a”,”b”,”b”, “c” , “c”}
doubleArr( {“math”,”ware”,”house”,”.com” }) {“math”,”math”,”ware”,”ware”,”house”,”house”,”.com”, “.com”}

int  indexOf5(int[] nums )

Description:This returns the index of the first occurrence element 5  or -1 if 5 does not appear anywhere in the array.

Method Call return value/output
indexOf5( { 2 , 3 , 5 , 4 } ) 2
indexOf5( { 2 , 3 , 5 , 4, 5  } ) 2
indexOf5( { 2 , 3 ,7  , 4, 3,   } ) -1

More Sample calls and return vals

screenshot.41


int indexOf(int[] nums, int num)

Description: This method returns the index value of the first appearance of num  or -1 if num  is not an element of nums  .

Method Call return value/output
indexOf( {6,4 ,7,3, 4 }, 4) 1
indexOf( {6,4 7 ,3,2,7}, 7) 2
indexOf( {6,4 ,2,3}, 22) -1

 


int[] randos(int start, int end, int howMany)

Description:  This method returns an array of random numbers between [start,end] . Note make sure that each element in the new array attempts to make a new random int.  Use Math.random() , do not use any other mechanism for finding a random number. 

 randos_loop
 More example calls and returns :
randos2

double meanBetween(int[] nums, int min, int max)

Description:  This method returns the mean of nums ; however, this method only counts values within the range (min,max) as shown in the examples below:

screenshot.12
(Not inclusive, so do not count min or max )

int secondSmallest(int[] nums )

Description: This method returns the element of nums  with the second smallest value.

Note: You may not modify the input array. For instance, you may not put nums in order,  which would be bad because you were not asked to modify the array.

@precondition: nums.length >= 2

Note: You will lose credit if you use a constant to represent the smallest or second smallest number. See pseudocode 

Method Call return value/output
secondSmallest( { 2 , 18 , 22, 4 , 6 } ) 4
secondSmallest( { 3 , 7 , 15 , 1 ,101} ) 3

boolean isPalindromic(int[] nums)

Description:This method returns true if the elements of nums  are a palindrome. 

Method Call return value/output
isPalindromic(( { 5 , 2, 7 , 2 , 5} ) true
isPalindromic( { 5 , 2, 7 , 3 , 5} )) false
isPalindromic(( { 1 , 2, 1} ) true

Old versions:

Array Fun 1 

Array F un 2 (v 1)

 

Array Fun 3 (resizing included)

Array Fun 2

Java Assignment

Create a Class called ArrayFun2  that has the following methods Write the body for the methods described below.

 

Description: This method creates an array that is filled with all of the digits of nums.

Method Call return value/output
digitsToArray( 523 ) {3, 2, 5}
digitsToArray(1267 ) { 7 , 6 , 2,  1 }

public boolean allFactorsOfSum(int[] nums)

Description: This method returns true if each element ofnums is a factor of the sum of nums.

Method Call return value/output
allFactorsOfSum( {6,1,2,3} ) true (because all of the elements are factors of the sum which is12)
allFactorsOfSum( 1,4,7) false (because 7 is not a factor of the sum of this array which is 12)

public String[] doubleArr(String[] strs)

Description: This method returns a new version of strs with each element appearing twice.

Method Call return value/output
doubleArr( {“a”,”b”,”c”} ) {“a”,”a”,”b”,”b”, “c” , “c”}
doubleArr( {“math”,”ware”,”house”,”.com” }) {“math”,”math”,”ware”,”ware”,”house”,”house”,”.com”, “.com”}

public boolean isThere(int[] nums, int num)

Description: This method returns true if num is an element nums.

Method Call return value/output
isThere( {6,1,2,3}, 6) true
isThere( {6,1,2,3}, 5 ) false

public int indexOf(int[] nums, int num)

Description: This method returns the index value of the first appearance of num or -1 if num is not an element of nums .

Method Call return value/output
indexOf( {6,4 ,7,3, 4 }, 4) 1
indexOf( {6,4 7 ,3,2,7}, 7) 2
indexOf( {6,4 ,2,3}, 22) -1

public int lastIndexOf(int[] nums, int num)

Description: This method returns the index value of the last appearance of num or -1 if num is not an element of nums .

Method Call return value/output
lastIndexOf( {6, 4 ,7 ,3, 11, ,4}, 4) 5
lastIndexOf( {7, 6,4 , 7 ,3}, 7) 3
lastIndexOf( {6,4 ,2,3}, 22) -1

public boolean isIncreasing(double[] nums)

Description: This method returns true if each element in nums is greater than the element to its left.

Method Call return value/output
isIncreasing( {1,2,3,4 }) true
isIncreasing( {1,0 ,3,4 }) false
isIncreasing( {1,1, 2,3,4 }) false

public int largestSpan(int[] nums)

Description: This method returns the largest span of consecutive increasing numbers

Method Call return value/output
largestSpan( {4, 3, 1, 2, 3 , 1} ) 3
largestSpan( {4  , 3,  1 , 12 , 31 , 44 , 52 , 1} ) 5
largestSpan( {9, 7, 8 , 12,4, 3, 0, 4 , 1) 3
largestSpan( {1, 0, 4, 5 , 3,  2, 8 , 9, 10, 12, 4, 3,  1 }) 5

** int[] invert(int[] nums)

Description: This method ‘inverts’ an array by spliting the array in halves and ‘inverting’ each half. See sample calls to understand.

Method Call return value/output
invert( {5 , 21, 5, 13,4 }) {4, 13, 5, 21, 5  }
invert( { 1, 2, 3, 4, 5,6  })  { 6, 5, 4, 3, 2, 1}

** int[] shiftByN(int[] nums, int delta)

Description: This method returns the array with all digits shifted by delta . Any digits that are circulated off the end of the array should be returned to the other side. Note: delta could be positive or negative. Please keep in mind that Math.abs(delta) > nums.length could be possible 😉

Method Call return value/output
shiftByN( {5 , 21, 13,4 } 1) { 4 , 5 , 21, 13 }
shiftByN( {5 , 21, 13, 4 , 11} 2) { 4 , 11 , 5 , 21, 13}
shiftByN( {5 , 21, 13, 4 } -1) { 21, 13, 4 , 5}
shiftByN( {5 , 21, 13, 4 ,7 } -2) { 13, 4 ,7 , 5 , 21}
** int[] add(int[] num1, int[] num2, int base) throws ArithmeticException@precondition 1 < base < 11
@precondition num1.length = 5 and num2.length = 5
@postcondition: the returned array has 5 elements representing the sum or an ArithmeticException is thrown

Description: This method attempts to replicate addition. Consider both num1 and num2 represent the five digits of a number. Each element in the array stores one of the digits. For instance, the number 143 would be represented as {0,0,1,4,3 }. Numbers can be in any base between 2 and 10 inclusive , and the number 101102 in binary would appear in an arrays as : {1, 0, 1 , 1, 0 } . Let’s assume the numbers are positive.You should return an array representing the digits of the sum of num1 and num2. If the number of digits in the sum exceeds the maximum number of digits (5) , you should throw an arithmetic exception as shown in the code below:

Note: You may not use any kind of helper or utility methods that are built into pre-defined Java classes for converting numbers between bases. Only use techniques taught in this class.

Method Call return value/output
add( {0,0,0 ,4,2},{0,0,0,5,1}, 10) {0, 0, 0, 9, 3} ie (42 + 51 = 93)
add( {0, 0, 0 ,7,2},{0,0,0,5,1}, 10) {0, 0 , 1 , 2, 3} ie (72 + 51 = 123)
add( {0, 0 , 0 , 1 , 1}, { 0 , 0 , 0 , 1 }, 2) {0, 0 , 1 , 0 , 0} ie ( 112 + 12= 1002 )