Java Reference
In-Depth Information
the element at index position j . Explain the fundamental differences with
the former permute function.
Exercise 4.3 (Searching for words in dictionary)
Consider a dictionary of words stored in a plain array of strings: String
[] dictionary . Write a function static boolean isInDictionary
that takes as argument a given word stored in a String variable, and
report whether the word is already defined inside the dictionary or not.
Explain your choice for performing equality tests of words.
Exercise 4.4 (Cumulative sums: Sequential and recursive)
Write a function that takes a single array argument of elements of
type double , and returns its cumulative sum by iteratively adding the
elements altogether. Computing the cumulative sum of an array can also
be done recursively by using, for example, the following function proto-
type CumulativeSumRec(double array, int left, int right) .Im-
plement this function and test it using CumulativeSumRec(array, 0,
array.length-1);
Exercise 4.5 (Chasing bugs)
The following program when executed yields the following exception:
Exception in thread "main" java.lang.NullPointerException
at BugArrayDeclaration.main(BugArrayDeclaration.java:8)
Program 4.18 Bug in array declaration
class BugArrayDeclaration
{ public static void main( String
[ ]
t )
int [] array ;
int [] array2= null ;
array=array2 ;
array [0]=1;
}
}
Find the bug and correct the program so that it runs without any bug.
Exercise 4.6 (Sieve of Eratosthenes)
One wants to compute all prime integers falling within range [2 ,N ]for
a prescribed integer N
. The sieve of Eratosthenes algorithm uses a
boolean array to mark prime numbers, and proceeds as follows:
N
 
Search WWH ::




Custom Search