Array Fun 1 [Java]

Array Fun 1

 

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

public int[] swapFirstLast(int[] nums)

Description: This method returns a new version of the array with the first and last elements ofnums swapped.

Method Call return value/output
swapFirstLast( {3, 2 ,5 } ) {5,2,3}
swapFirstLast( {7, 6 ,9 ,12} ) { 12 , 6 , 9, 7}

public String[] swapFirstMiddle(String[] strs)

Description: This method returns a new version of the array with the first and middle elements ofstrs swapped.

Method Call return value/output
swapFirstMiddle( {“a”,”b”,”c” } ) { “b”, “a”, “c”}
swapFirstMiddle( { “zoo”,”foo”, “xoo”} ) { “foo”,”zoo”, “xoo”}

Loops


public void printEveryOther(int[] nums)

Description: This method prints every other element of nums on new lines .

Method Call return value/output
printEveryOther( {1 , 2 , 3 , 4 } ) 1
3
printEveryOther( {13 , 42 , 33 , 44 , 52} ) 13
33
52

public int sum(int[] nums )

Description: This method returns the sum of nums..

Method Call return value/output
sum( { 2 , 8 }) 10 ie 2 + 8
sum( { 2 , 8 , 4 } ) 14 ie (2 + 8 + 4 )

public int sumOdds(int[] nums )

Description: This method returns the sum of all the odd element of nums.

Method Call return value/output
sumOdds( { 5 , 2, 4 , 6 , 7 } ) 12 ( ie 5 + 7 )
sumOdds( { 13 , 3, 2, 6 , 7 } ) 23
public double mean(int[] nums )

Description: This method returns the mean of nums. Pay close attention to the second sample call and make sure you get “5.25

Method Call return value/output
mean( { 2 , 8 }) 5.0 ie (2+8)/2
mean( { 2 , 8 , 4 , 7 } ) 5.25 ie (2+8+4+7)/4

public double largest(double[] nums )

Description: This method returns the element of nums with the greatest value. Make sure that you test out the second call and get ‘-2‘.

Method Call return value/output
largest( { 2.6 , 8.2, 5.2}) 8.2
largest( { -2,-11, -4} ) -2

public 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)

*public int secondSmallest(int[] nums )

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

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