CS 126 Midterm Review Session
Overview:• For important information, see the webpage• Tips on getting started• Reviewing important topics• Question and Answer
Tips on Getting Started• Attend this session• Read over your notes a couple of times• Work through tutorial material (again)• Work through assignments (again)• Review in-class quizzes (solutions posted on newsgroup)• Try practice exams• See tutors about concepts you don’t understand or any
questions you may have
About This Session & These Slides• Not all topics covered in lecture appear in these slides• Of the topics covered here, not all aspects of the topics
covered in lecture and course notes appear here• Being comfortable with the contents of these slides is not
enough; you should consider studying from your course notes and the textbook as well
Preconditions and Postconditions
Preconditions state what must be true for the method to work correctly. Postconditions describe what is true after the method is called, including what is returned (assuming the preconditions were met).For the following method, give appropriate preconditions and postconditions.
public boolean puzzle(Object[] objArray, int firstIndex, int secondIndex){ // pre:
// post:
String s1 = (String)objArray[firstIndex]; String s2 = (String)objArray[secondIndex];
if(s1 == s2) { return true; } else { return false; }}
Preconditions and Postconditions - SOLUTION
For the following method, state the appropriate preconditions and postconditions.
public boolean puzzle(Object[] objArray, int firstIndex, int secondIndex){ // pre:
// post:
String s1 = (String)objArray[firstIndex]; String s2 = (String)objArray[secondIndex];
if(s1 == s2) { return true; } else { return false; }
}
objArray != nullobjArray contains String objects0 <= firstIndex <= objArray.length-10 <= secondIndex <= objArray.length-1
Returns true if objArray[firstIndex] references to the same object as objArray[secondIndex], false otherwise
Black-Box Testing
In Black-Box testing, we are concerned with boundary values and typical values.
Boundary values are:• Input values that make the precondition just barely
true• Special cases that might not occur often
Typical values are:• Normal (expected) values
Example
Describe a complete set of black-box test cases for the following method:
public double mix(double a, double b)// pre: -1 <= a <= 1, -1 <= b <= 1// post: returns -1 if a+b <= -1 1 if a+b >= 1 a+b otherwise
Black-Box Testing Example – SOLUTION
Boundary values:
a = -1, b = 0.5 (precondition)a = 1, b = 0.1 (precondition) a = 0.3, b = -1 (precondition)a = -0.65, b = 1 (precondition)a = -0.65, b = -0.35 (postcondition: == -1)a = 0.65, b = 0.35 (postcondition: == 1)
Typical values:
a = -0.8, b = -0.597 (postcondition: <= -1)a = 0.8, b = 0.597 (postcondition: >= 1)a = 0.6, b = 0.21 (postcondition: a+b)
White-Box Testing
In White-Box testing, we are concerned with making every line of code execute, as well as testing loops by iterating 0 (or minimum), 1, many, and maximum number of times (or as many of these as are applicable).
Consider the following method:
// pre: a is not null// post: returns true iff a contains at// least two equal objects consecutively
1. public boolean hasConsecDuplicates(Object[] a)2.{3. int i = 0;4. while(i < a.length - 1)5. {6. if(a[i].equals(a[i+1])7. {8. return true;9. }10. i++;11. }12. return false;13.}
Describe all distinct white-box test cases for this method.
White-Box Testing – SOLUTION
Test: Input:
Line 4: while loop – 0 times [ ]Line 4: while loop – 1 time [2 4]Line 4: while loop – many times [2 4 3 7 4 4 2 3 9]Line 4: while loop – max times [2 4 3 7 6 4 2 3 9]
Line 6: if statement – true [2 4 3 7 4 4 2 3 9]Line 6: if statement – false [2 4 3 7 4 8 2 3 9]
Line 8: return statement [2 4 3 7 4 4 2 3 9]Line 12: return statement [2 4 3 7 6 4 2 3 9]
ReferencesThings to remember:
• Primitives inside the box. Objects are always referenced (arrows).
• The = operator copies what is in the box, regardless of what it is (even if it is an arrow; it copies the arrow)
• When calling methods, parameters are copied as in assignment.
• The == works by comparing what’s in the box (either the primitive value or reference).
int a=0;int b=a;a=1;
String a= “hello”;String b = a;a= “bye”;
void increment(int a) {a=a+1;}
…int num = 3;
this.increment(num);
Recall Tutorial questions: tracing references. Questions and solutions are still up.
Recall the written assignment 2 question 3. Solutions are posted in the glass case out side MC 4065.
ListADTThings to remember:
• Know how to use the methods specified in the ADT and only the methods specified in the ADT.
• Know the different implementations (singly linked, circular, doubly linked, partially filled array)
Exact EfficiencyThings to remember:
• Terminating condition(s) in a for loop iterating through all elements such as i<a.length are done at most n+1 times
• The body of a for loop is done at most n times. Typically the worst case of a single loop is an expression close to n.
• Break statements change the minimum number of times a loop can occur.
• If statements and loops can change whether a line is executed at all.
• Sometimes best=worse because the code runs the exact same way every single time (signs of this include no break/return within the loop).
• The best case and worst case expressions must be defined for all possible values of n. If your answer for the best case only describes a condition where n=0, then your answer is wrong.
Exact Efficiency – Example
Let n = a.length
public void printEvens(int [] a){
System.out.println("Printing all evens...");
for (int i = 0; i < a.length; i++){
if (a[i] % 2 == 0)System.out.println(a[i]);
}
System.out.println("Done!");}
How many calls to “System.out.println” in the best case? In the worst case?
Exact Efficiency Example – Answers
Calls to “System.out.println” in the best case:2, when all the elements of a are odd
Calls to “System.out.println” in the worst case:n+2, when all the elements of a are even
Stacks & Queues Question
Implement the StackInterface operations push() and pop() using a queue to store the contents of the stack, and helper queues as necessary.
public class StackByQueue implements StackInterface{
QueueInterface q = DataFactory.makeQueue();
public void push(Object obj){
//your solution here}
public Object pop(){
//your solution here}
}
Stacks & Queues QuestionANSWERS
public void push(Object obj){
//store objects in q q.enqueue(obj);}
public Object pop(){
//create a helper queue QueueInterface tempQ = DataFactory.makeQueue();
//get last object in q Object temp = null;
while (!q.isEmpty()) {
temp = q.dequeue();
//if temp is not the last object if (!q.isEmpty()) { tempQ.enqueue(temp); }
}
//put everything back into q while (!tempQ.isEmpty())
{ q.enqueue(tempQ.dequeue());
}
//return object return temp;
}
Stack Question
You are given a stack and are asked to return a new stack, which is an exact copy of the old stack without any instances of the given object. The given stack must remain the same at the end of the method as when it was passed in.
public static StackInterface removeFromStack (StackInterface stack, Object toRemove)//Pre: stack is not null//Post: returns a new instance of a StackInterface //with all objects of stack except those equal to //toRemove.{
//Your code here}
Stack QuestionSOLUTION
public static StackInterface removeFromStack (StackInterface stack, Object toRemove)
//Pre: stack is not null, toRemove is not null//Post: returns a new instance of a stackInterface//with all objects of stack except those equal to//toRemove. {
//the stack to returnStackInterface ret = DataFactory.makeStack();//helper stacks
StackInterface helper = DataFactory.makeStack();
//go through stack adding the current item to//the helper stack
while (!stack.isEmpty()){
helper.push(stack.pop());}
//add all the elements to the two stacks//excluding the toRemove objects from retwhile (!helper.isEmpty()){
Object top = helper.pop();stack.push(top);if (!toRemove.equals(top)){
ret.push(top);}
}
return ret;
}
Linked Lists Question
An English word can be turned into Pig Latin by moving the first character to the end of the word and then appending “ay” to the end.
Example: COMPUTER --> OMPUTERCAY
In the following method, assume that head is a reference to a singly-linked list which represents an English word. Each Node stores a single character of the word as a String. The method will change the linked list in order to represent the original word as Pig Latin. Below is an example:
Before:
After:
Implement this method using linked list operations.
//pre: the list has at least two nodespublic void makePigLatin(Node head){
//your solution here}
Linked Lists QuestionANSWER
public void makePigLatin(Node head){
//find the last node in the listNode last = head;while (last.getNext() != null){
last = last.getNext();}
//move the current head node to the end and //reassign the head reference
last.setNext(head);head = head.getNext();
//update what the last node of the list islast = last.getNext();
//add on “A” and “Y”Node a = new Node(“A”);Node y = new Node(“Y”);last.setNext(a);a.setNext(y);
}