April 28th – Last lecture
We went over the elements that were tested in the ETS exam without looking at the specific questions. Some hints for answering multiple choice questions include:
· when asked which of these code segments do the same thing, trace the code carefully
· when given three code snippets or choices (i.e. I, II, and III) and asked which of the following are true? I only, II only, I and II, I and III, II and III do not stop as soon as you find one that is true.
· be sure to refer to the information sheet that accompanies the code to see whether there are explanations that are different from those you know.
· study the sort demos found at http://maven.smith.edu/~thiebaut/java/sort/demo.html
· review deMorgan’s law – professors care about this one
· when tracing code, don’t do it in your head; that leads to arithmetic errors, and off by one errors (going around a loop once too much or once too little).
· read the questions carefully and think about what is being asked.
a variable’s scope
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/scope.html
NOTE: (p. 161 our text) “Java automatically initializes any variables declared at the class level
i.e. class members, class instance variables, class attributes, class variables
access to members of a class
http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html
javadocs
http://java.sun.com/j2se/1.5.0/docs/api/index.html
As pointed out in lecture today, one of the problems associated with Lab 23 was the use of the variable names first and last.
last referred to the last (i.e. most
recent) element inserted not to the last element in the list.
first referred to the first element inserted, not to the first element in the list. This misunderstanding of the variable names led to a number of your errors and emphasizes the importance of choosing meaningful variable names (that can’t be misunderstood).
I made a mistake in the diagram for the following code snippet
public
class Quack
{
private Node first, last;
/**
* Construct a new (empty) Quack
*/
public
Quack()
{
first
= null;
last = null;
}
// end constructor
first and last are variables that point
to Nodes so it should have been what I had
at first (i.e. quack = new
Quack();) yields
Quack first
last
When a push
occurs, first and last will point to the created Node. For example: quack.push("Bob");
temp
Quack first
“Bob”
last
Followed by: quack.push("and");
temp
Quack first
“and” “Bob”
“
last
Followed by: quack.push("ted");
temp
Quack first
“ted” “and” “Bob”
“
last
and so forth...
if you come by tomorrow (Friday) afternoon or Monday afternoon, I’ll be glad to walk through the rest of the code for you.
If you get an exam question, you should re-draw the image for each step with the new part added... For example, here is the identical diagram shown in a clearer way.
Quack
first
“bob” “and” “ted”
last