Monthly Archives: October 2013

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

2 D Arrays in Java Assignments

Add the following Methods to a class called ArrayFun2D

public void print(int[][] arr)

Description: This method prints each element in the array. This will be a useful method when you are debugging your code and want to test it out to see if it works.

Method Call return value
print({{1,2} , {3,4}, { 5, 6 } } ) 1   2
3  4
5   6

public int sum(int[][] arr)

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

Method Call return value
sum({{1,2} , {3,4}, { 5, 6 } } ) 21 ( ie 1+2+3+4+5+6)

public String concat(String[][] strs)

Description: This method returns the concatenation of all the elements in the Array

Method Call return value
concat({{“a”,”xc” }, {“t”, “zoo”}, { “jjkj” } } ) “axctzzoojjkj”

public double mean( int[][] arr)

Description: This method returns the arithmetic mean of the array. Make sure you double check that you are correctly returning results that include decimal values .

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

public void printRow(int[][] arr, int row)

Description: This method prints all elements in rowThis can be (and should be) done with a single loop.

Method Call return value
printRow({{1,2} , {3,4}, { 5, 6 } } , 1 ) 3, 4
printRow( { 5 , 6} , {7, 12, 13 , 5 }, { 5, 6 ,0} } , 2 ) 5 , 6, 0

public void printCol(int[][] arr, int col)

Description: This method prints all elements in col

Method Call return value
printCol({{1,2} , {3, 4}, { 5, 6 } } , 1 ) 2
4
6
printCol( { 5 , 6} , {7, 12, 13 , 5 }, { 12, 6 ,0} } , 0 ) 5
7
12
printCol(    {12, 13 , 5 }, { 6, 2, 13 ,11}  , {1,2 } } , 2 ) 5
13

public int maxVal_inRow(int[][] arr, int row)

Description: This method returns the value of the largest number in the given row

Method Call return value
maxVal_inRow({{1,2} , {3,4}, { 5, 6 } } , 1 ) 4
maxVal_inRow ({   { 5 , 6} , {7, 12, 13 , 5 }, { 5, 6 ,0} } , 2 ) 6

public int maxVal_inCol(int[][] arr, int col)

Description: This method returns the value of the largest number in the given col

Method Call return value
maxVal_inCol({{1,2} , {3,4}, { 5, 6 } } , 0 ) 5
maxVal_inCol({{1,-2} , {3,-4}, { 5, -5 } } , 1 ) -2

public int[][] changeValues(int[][] table, int val)

Description:This method returns the array with all elements set toval

changeValues(table, 5)

Original Array

1 2
3 4
5 6
Returned Array

5 5
5 5
5 5

public void print_colMajor(int[][] arr)

Description: This method prints each element in the array – using column major order

Method Call return value
print({{1,2} , {3,4}, { 5, 6 } } ) 1   3   5
2  4    6

 

public int[][] removeVals(int[][] table, int val)

Description: This method returns a version of the 2-d array where val has been removed from all elements

removeValues(table, 5)

Original Array

{1 , 5}
{3, 4}
{5, 6}
Returned Array

{1}
{3, 4}
{6}

 

int[] twoD_to1D(int[][] arr)

Description: This method takes an input array that is in 2 dimensions  and  creates a 1 d array . Assume that the array is rectangular (not jagged).

Method Call return value
twoD_to1D ( {{1,2} , {3,4}, { 5, 6 } } ) { 1 , 2 ,  3 ,  4 , 5 ,  6 }

Matrices

int[][] add (int[][] matr1, int[][] matr2)

Description: This method returns the sum of matr1 and matr2

how to add matrices


 

** int[][] multiply(int[][] matr1, int[][] matr2)

Description: This method returns the product of matr1 and matr2

how to multiply matrices

**public int[][] invertValues(int[][] matr)

Description: This method returns the paramater with its values ‘inverted’

Original Array

1 2
3 4
5 6
Returned Array

6 5
4 3
2 1

Two Dimensinal Arrays in Java

Two Dimensional Arrays in Java

 

2-D array Demo Code

2 D Array Assignments

Array 3

Warming up

public void  printFactors( int n )

Description: This method prints all factors of n

Method Call return value/output
printFactors( 5)

1

5

printFactors( 6)

1

2

3

6

printFactors( 9 ) 1
3
9
printFactors( 15) 1
3
5
15

 

public int[] getFactors( int n )

Description: This method returns an array populated with the factors on n

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

 

 

public int[]  removeZeros(int [] nums)

Description: This method returns a version of the input –with all occurrences of 0 , zero, removed

Method Call return value/output
noZeros(  { 3 , 4, 0, 1} ) { 3, 4, 1}
noZeros(  { 0, 5 , 0, 0, 9, 0, 1 , 11} ) { 5,9,1,11}

 

 

** public int[]  doubleUp( int  [] nums, int val)

Description: This method retuns a version of the input –with all occurances of val  duplicated.

Method Call return value/output
doubleUp(  { 3 , 4, 0, 1}  , 4  ) { 3, 4, 0, 1}
doubleUp(  { 1, 3 , 1, 5 , 3, 3} ,  3 )    {1 , 3,  , 1, 5 , 3, 3, 3, }

 

 

 

Similar problems on codinbat

Pre4

Array Fun 2

Create a Class called ArrayFun that has the following methods Write the body for the methods described below.
public int[] digitsToArray(int nums)

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

Method Call return value/output
digitsToArray( 523 ) {5,2,3}
digitsToArray(1267 ) { 1 , 2 , 6 , 7}
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( {1, 7, 8 , 12,4, 3, 0, 4 , 1) 3
** 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 }) {21 , 5 , 5, 4,13 }
** 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:

if(overFlowAdditionOccurred)
throw new ArithmeticException(“Addition Overflow Error”);

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 )