ArrayList 1 Excercises

BlueJ users. Read this about how to use ArrayLists as parameters. Or consider using Eclipse if this is too much of a hassle.

ArrayList<Integer> factors(int num)

Description: This method returns an ArrayList populated with the factors of num

Method Call return value/output
factors(5) {1,5}
factors(10) {1,2,5,10}

int sum(ArrayList<Integer> nums)

Description: This method returns the sum of all elements in the list

Method Call return value/output
sum({1,5}) 6
sum( { 3,4, 2 }  ) 9

void printReverse(ArrayList<String> strs)

Description: This method prints all elements in reverse order

Method Call return value/output
printReverse({“a”,”b”}) “b”
“a”
printReverse( “xu”, “t”) “t”
“xu”

double mean(ArrayList<Integer> nums)

Description: This method returns the arithmetic mean of all elements in the list

Method Call return value/output
mean({1,5}) 3
mean( {3,4, 2 } ) 4.5

void printEvens(ArrayList<Integer> nums)

Description: This method prints out all even numbers

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

ArrayList<Double> everyOtherPi(ArrayList<Double> nums )

Description: This method returns a version of the input ArrayList with every other value changed to Math.PI

Do NOT make a new ArrayList in this method

Method Call return value/output
everyOtherPi ( {1,8,5,0, 7} ) {1, 3.1415 , 5 , 3.1415 , 7 , 3.1415 }
everyOtherPi({0,4,3,5} ) {0, 3.1415 ,  3 , 3.1415 }

boolean isIdentical(ArrayList<String> strs1 , ArrayList<String> strs2)

Description: This method returns true if each element in the two ArrayLists is the same

Method Call return value/output
isIdentical({“ab”, “a”} , { “x”, “a”} ) false
isIdentical({“ab”, “a”} , { “ab”, “a”} ) true
isIdentical({ “xy”} , { “XY”} ) false

int largest(ArrayList<Integer> nums)

Description: This method returns the largest element in nums

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

ArrayList<Integer>removeVal(ArrayList<Integer> nums, int val)

Description:This method returns an ArrayList with all instances of val removed

Do NOT make a new ArrayList in this method

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

 

 

TO do :

PrimesFactory