CS 1713 Week 11:
Review and Exam
Objectives:
- Solidify concepts
- Finish case studies as needed
- Integrate material learned so far
Activities:
- More practice.
- Review for exam.
Exam 3 Review
Covers:
- Chapters 1 through 10 of textbook
- Laboratories 1 through 9
- Case studies 1 through 15
- Projects 1 and 2
Information Specific to Spring 2012 Section 2:
Exam 3 will last 75 minutes and consist of questions worth a total of 100 points.
Most likely, the exam will have the following types of problems:
- 15 points: linear and binary search for int, double, String, or Comparable.
May include either an array or an ArrayList.
- 15 points: selection or insertion sort for int, double, String, or Comparable.
May include either an array or an ArrayList.
- 10 points: questions about order of operations (how long does it take ...)
You much show how you got your answer.
These will not be multiple choice.
- 10 points: trace both the selection and insertion sorts.
- 10 points: string manipulation problem
- 10 points: an array or ArrayList of objects problem
- 10 points: trace a diagram
(like on Exam 2, but incluing an array of objects)
Exam Emphasis:
- Loops and Simple Arrays:
- Review this material from Exam 2
- Arrays of Objects:
- Create a class containing an array of objects
- Understand how to use arrays of objects to solve problems
- Manipulate arrays of objects
- Exceptions and File I/O
- Understand how to use try-catch
- Be able to read from a file given:
Scanner scan = new Scanner(new FileInputStream(filename));
- Be able to write to a file given:
PrintWriter out = new PrintWriter(new PrintWriter(filename),true);
- Understand that each of these needs to be in a try-catch
- Strings:
- Be able to write a code using the String class.
- Be able to use the following String methods:
compareTo, equals,
length, substring, indexOf, and charAt.
- Searching:
- Understand how the linear and binary searches work.
- Be able to code or trace the binary or linear search.
- Know the order of the binary and linear searches.
- Be able to search an array containing primitive types or String
objects.
- Sorting:
- Understand how the selection and insertion sorts work.
- Be able to code or trace the selection and insertion sorts.
- Know the order of the selection and insertion sorts.
- Be able to sort an array containing primitive types, String ,
or Comparable objects.
- Inheritance
- Understand how to extend a class and implement an interface
Comprehensive as far as programming.
Some types of problems:
- Write a method to do X.
- Write a code segment to do X.
- Design a class with the following properties....
- Instantiate objects of a specified class and use them to solve a problem.
- Trace the following code segment (emphasis on segments with loops and
arrays).
- Draw a picture of the variables and objects after the following code
is executed (emphasis on arrays and arrays of objects).
You can find a sample problem here
- Answer questions about scope and parameter passing.
- Use/implement the Comparable interface.
Example problem:
Suppose Square is a class that has been appropriately written.
It has a constructor with one double parameter, its side.
Draw an accurate schematic diagram of the program variables showing the
execution of the program.
double w = 2;
double x = 5;
double y = 7;
Square[] squares = new Square[3];
Square s;
s = new Square(w);
squares[0] = new Square(x);
squares[1] = s;
squares[2] = squares[0];
w = squares[2].getArea();
s = squares[2];
x = squares[1].getSide();
y = squares[0].getArea();
squares[2] = squares[1];
squares[2].setSide(9);
squares[1] = new Square(8);
y = squares[1].getArea();
x = squares[2].getSide();
Binary Search Review:
- For each iteration, divide the remaining part of the array into 2 parts
and eliminate one of these parts.
- It is fast because each part is about half of what is reminaing.
This makes the algorithm O(log n).
- It works because at each iteration at least one element is eliminated.
Otherwise the algorithm might not terminate.
- In fact, at least half of the items are eliminated on each iteration.
- This is the reason for the +1 and the -1 in
mid+1 and mid-1 of the algorithm.
- Without these fewer than half might be eliminated.
- Without these, if the remaining part of the array has size 3,
we would eliminate only 3/2 = 1 item.
- Without these, if the remaining part of the array has size 1,
only 1/2 = 0
items are eliminated and the algorithm might not terminate.
Selection Sort Review:
- For an array of size n, do the following n-1 times:
- Find a smallest value in the remaining (unsorted) part of the array.
- Put this item in the correct position using a swap.
- On the kth iteration, the first k elements
of the array are in the correct positions.
- The algorithm is O(n^{2}).
Insertion Sort Review:
- For an array of size n, do the following for k from 1
to n-1 (n-1 iterations):
- Sort the first k+1 elements of the array by moving the item
at position k into the correct place among the first k+1
elements of the array.
Assume that the first k elements are sorted. Note that this is always
true for k=1.
- Do this by moving the appropriate elements up one position and dropping
the item in the correct place.
- After the kth iteration, the first k+1 elements
of the array are sorted.
- The algorithm is O(n^{2}).
Some Simple Practice Problems:
- Assume that apple is an array of integers and banana
is an array of Strings. Write a Java code segment to accomplish each of
the following:
- Add 3 to each element in apple.
- Square each element of apple.
- Set the element in position 2 of banana to
"delicious"
- Find the largest value in apple.
- Find the position of the largest value in apple.
- Write the following public methods.
- public boolean match(int [] a, int [] b) returns
true if a and b are of the same length and
their elements match.
- public boolean match(String [] a, String [] b)
returns
true if a and b are of the same length and
their elements match.
- public int countValues(int [] a, int value) returns
the number of times value appears in the array a .
- public int linearSearch(String [] a, String name)
returns the position of an item that matches name in the array
a.
- Output the number of times each of the numbers 0 through 9 appear
in a stream of 1000 random numbers that range from 0 to 19.