Exam 2 Topics
CS141: Introduction to Programming,
Fall 2011
General Comments and Recommendations
- The exam will cover Nested Loops in Chapter 4 (sec 6) and Chapters 5-6 of the text.
Note that the material is cumulative so you are still expected to remember the earlier material.
- The exam will be 1 hour long. It will be closed book. Electronic devices, of any kind, will
not be allowed.
- It is strongly recommended that students:
- Re-read the text
- Do the self-check exercises in each chapter (answers are in the text)
- Review the reading quizzes
- Review the programming code-completion and programming exercises.
- Be able to do the Nested-loop problems in exercise.
- Be able to do the Method problems in exercise.
- Be able to do the Array problems in exercise.
Code from Class
Here is the code that we created in class on Monday (Nov 14):
Chapter 4.6: Nested Loops
- You should be able to do these problems.
- Nested loops are often needed to work with 2D arrays.
Chapter 5: Methods
- What is a method?
- Why do we have methods?
- Organization, Modularization and Reusability, Efficiency
- Anatomy of a Method Declaration:
- method name
- parameters (type & name for each)
- return type
- body
- What does it mean "to call" a method.
- Example: Rolling dice - see in class problems.
- What is the scope of a variable?
- Recursion:
- What is a recursive method?
- What is the tyoical form: base case, and recursive case.
- Know how to write a recursive method, e.g. that
- prints out a list of numbers
- computer the sum 1 + 2 + ...+ n
- computes n! (i.e. n factorial)
Chapter 6: Arrays
- Declaring: int[] myNums; // creates only a reference!
- Initializing/Creating elements: // size is always fixed.
-
myNums = new int[10];
-
myNums = { 0,1,2,... };
- Setting the values:
- Individual elements:
myNums[3] = 255;
- Using a loop to set entire arrays:
-
for (int i=0; i<myNums.length; i++) myNums[i] = 10;
- using the fill method in the java.util.Arrays class:
Arrays.fill(myNums,10);
- Copying arrays: Beware!
-
int[] myNums2 = myNums; // does not make a copy!!
- use loop or Arrays.copyOf
- Enhanced loop:
for (int element: myNums) sum = sum + element;
// don't use to modify the array element values!
- Other: summing, searching, max/min, inserting elements, removing elements, sorting, etc
- Passing arrays as parameters in methods !!! Copy by reference vs Copy by value
- 2D Arrays
- Declaring
- Representation as an array of arrays
- In methods: parameters and return values.
- Higher dimensional arrays
Chapter 6: ArrayLists