There is a more recent offering of this course. This is an archive of the
course which was offered in Fall 2017.
SE1011
Class Exercises
Week 10 Lecture 1 Class Exercises
Complete as many as you can. You may work with a partner.
1. Implement a class method called `add()` that accepts an `ArrayList` of `String`s and one additional `String` and returns a new `ArrayList` that contains all of the elements in the original `ArrayList` and the new element added at the back of the `ArrayList`. Here is how it would be used:
```
ArrayList words = new ArrayList<>();
words.add("one");
words.add("two");
words.add("three");
words = add(words, "four");
```
2. Draw the memory diagram that illustrates what happens in memory with the code in the previous question is run.
3. Implement a class method called `add()` that accepts an array of `String`s and one additional `String` and returns a new array that contains all of the elements in the original array and the new element added at the back of the array. Here is how it would be used:
```
String[] words = {"one", "two", "three"};
words = add(words, "four");
```
4. Draw the memory diagram that illustrates what happens in memory with the code in the previous question is run.
5. Write a class method, `calculateAverage()` that accepts an `ArrayList` and returns the average of all the values in the list.
6. Write a class method, `raiseToPower()` that accepts an `ArrayList` (`bases`) and a `double` (`exponent`) and returns an `ArrayList` that contains all of the values in `bases` raised to the `exponent` power.
7. Draw the memory diagram that illustrates what happens in memory with the code in the previous question is run.
8. Write a class method, `raiseToPowerInPlace()` that accepts an `ArrayList` (`bases`) and a `double` (`exponent`) and returns nothing. The method must change the values stored in `bases` such that they are raised to the `exponent` power.
9. Draw the memory diagram that illustrates what happens in memory with the code in the previous question is run.
10. Write a class method, `reverse()` that accepts an `ArrayList` and returns an `ArrayList` that contains all of the elements in the original list in reverse order.
11. Optional: Write a class method, `reverse()` that accepts an `ArrayList` and returns an `ArrayList` that contains all of the elements in the original list in reverse order. [Note: this is a bit tricky since you don't know what type of values are stored in the lists, but see if you can figure it out]