5.92

7 3/17: Loops and Big Bang

Practice with Loops

In class today we looked at ArrayList<T>s, for-each loops and for loops, and static methods. To practice using these:

Exercise 1. Define a class ArrayUtils as we did in class. Define a static method isSame(ArrayList<T> list1, ArrayList<T> list2, IComparator<T> comp) to check whether two lists have the same contents in the same order.

Exercise 2. Define a static method to reverse an ArrayList<T>. Implement this method two different ways: first using a for loop with indices, and second using a for-each loop.

Exercise 3. Define a static method to check if an ArrayList<T> is a palindrome: that is, its first element is the same as its last; its second element is the same as its next-to-last, and so on. Implement this method two different ways: again using both a for loop and a for-each loop. You may find your solution to reverse to be essential for one of these methods...

Practice with

Do problem 2 from this lab. Feel free to elaborate this game as much as you please, once you’ve completed the required parts. Also start thinking about what parts of this lab might be helpful to you in starting your final projects.