|
Second Summer 2002 |
Questions:
a with the values:
1 2 3 4 5 6 7 8 9 10
int[] a = new int[10]; for (int i = 0; i < a.length; i++) a[i] = (i + 1);
0 2 4 6 8 10 12 14 16 18
int[] a = new int[10]; for (int i = 0; i < a.length; i++) a[i] = 2 * i;
1 4 9 16 25 36 49 64 81 100
int[] a = new int[10]; for (int i = 0; i < a.length; i++) a[i] = i * i;
0 0 0 0 0 0 0 0 0 0
int[] a = new int[10];
1 4 9 16 9 7 4 9 11
int[] a = { 1, 4, 9, 16, 9, 7, 4, 9, 11};
a with ten random numbers between 1 and 100. Write code
for two nested loops that fill a with ten different random numbers between 1 and 100.
for (int i = 0; i < a.length; i++) a[i] = 1 + gen.nextInt(100);
for (int i = 0; i < a.length; i++) { int number = 1 + gen.nextInt(100); int count = 0; for (int j = 0; j < i; j++) { if (a[j] == number) { count += 1; } } if (count > 0) i -= 1; // ...why? else a[i] = number; }
int min = a[0];
int max = a[0];
for (int i = 0; i < a.length; i++) {
if (min > a[i]) min = a[i];
if (max < a[i]) max = a[i];
}
int[] v = new int[10]; for (int i = 1; i <= 10; i++) v[i] = i * i;
v[10] will produce an array out of bounds exception.
Explain two ways of fixing the error.
Change the limits to 0 and 9
or change the body of the for loop to index for (int i = 0; i <= 9; i++) v[i] = (i + 1) * (i + 1);
v with a value
of i - 1
for (int i = 1; i <= 10; i++) v[i - 1] = i * i;
An integer-valued expression that indicates position in a sequence
(starting from 0). The bounds are 0 and the length of the
array minus one.
Computing and reporting the average, finding
min, max, counting, searching, some versions of append,
insert and remove (those that work with full
arrays, not with companion variables).
Sorting an array. Also those versions of remove
and insert that work with a companion variable.
Generating an array of random values. Those versions of
insert and remove that work with full arrays.
Just describe each method. Don't implement the methods.
Product[] parameters that change an array
of products in each of the two ways just described.
Think of an array as being a book shelf. The books are the
actual products that the shelf is storing. One can do these two things:
Parallel arrays are arrays of features such that
the collection of all values that appear in the locations with the same
index (
Avoid them by defining an entity class (with those features) and creating
an array of such entities. Helps keep the features in sync.
i) in the arrays all refer to one and the same entity.
Catalog that stores a collection of products. What public
methods should you support? What advantages and disadvantages does a Catalog class
have over a Product[] array?
The products will be available by name. The advantage would be that
the names could be meaningful, but you'd have to either know it or implement a method
that works as a table of contents. Another disadvantage is that you can't add a new
product at run time, you'd have to change the source code and recompile. In this
respect a
Arrays will give us more uniformity and the ability to scan all the elements
through a loop, sort the products in the catalog, add some more at run time if
we want to, the disadvantage is that the index may be somewhat meaningless. Hashtable would work much better.
v is a sorted vector of products. Describe how a
new product can be inserted in its proper position so that the resulting vector stays sorted.
Start from the beginning and search for an element that has the same value. You will be going either up or down. Insert where you find the value or when you know that you want be able to find the value (the value is below or above the one that you're looking, depending on how the vector is sorted -- ascending or descending).
The arrays should have the same length. Compare the
elements one by one and stop with false if there is a mismatch.
Otherwise the answer is: true
Arrays should be the same length. Use a loop to scan all elements in one array and set the elements with the same index in the other array have the values you find in the first.
There's more than one way of answering this question: you could essentially get a new array of the same size (filled with zero by default) and change the reference to it. If you're doing this in a method it won't work so you need to change the elements to 0 one by one.
If you have the actual reference just point it to a new array of length 0. Otherwise return such an array and ask the caller to do that (agree on a protocol).
Yes.
Yes, of type int.
They can.
They cannot.
They should.
They may not.
Sometimes, in certain circumstances.
No. How could you declare that.
That's why the Vector treats them
all as Objects.
It can.
Not directly, not really.
Indeed.
It can (just add elements to it)
m[i].deposit(100);