home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
exam1-study-questions
exam1 study sheet
2007.Spring
Exam1 is scheduled for class on Feb.27 (Tue).
The exam will be in-class, on-paper, closed-computer.
It will consist of questions very much like these or questions, the
quiz1 study questions,
and Chapters 1,2 of the textbook.
Some of the questions may be directly from these sources.
The single best way to study for the exam is to sit down and
re-work homework and lab questions from scratch.
The exam will be similar to homework questions and quiz1.
An online help session (using WebCT chat room)
will be held Feb.26 (Mon) 20:30 until
there are no questions (or 22:00, whichever comes first).
You should know the syntax for
declaring classes,
writing methods (including constructors),
calling methods (including constructors),
creating instances (with new),
declaring+initializing fields,
declaring+initializing local variables,
and
the difference between fields and local variables.
You will need to know the following types:
int, double,
boolean,
String;
the keywords
void,
class,
return,
if-else,
this,
new,
and the javadoc keywords @return and @param.
(And, maybe others I forgot to list here.)
You will need to know about Java's arithmetic
(precedence of *,/
over +,-,
integer division (quotient) vs. floating-point division,
remainder (%),
and the potential of round-off error).
You will need to know
the logic operators
!, &&, ||.
-
Book 1.26-1.31
2.11-2.20,
2.29-2.32,
2.48-2.50.
Actually, all the problems in chapter 1 and 2 are good;
they just happen to require going into the specifics of the classes they talk about.
-
What is a predicate function?
-
What is a getter method? A setter method?
Make up a class for a field, and then write the exact code
for its getter and setter.
-
What are the only two places we'll assign directly to a field,
rather than use the setter?
-
What is the syntax of an assignment statement?
(Assigning to a local variable, or to a field)
-
Recall that in the EBay Fee problem from lab, calculating the final-value-fee
involved three different tiers: the interval [$0,$25),
[$25,$1000), and [$1000,∞).
What are good test cases, which test all the boundary conditions,
as well as pertinent non-boundary conditions?
-
What is the purpose of a constructor?
What keyword is used, to call a constructor?
Can objects ever be created, without that keyword being invoked? (Answer: No!)
-
You are watching a ballgame on TV.
What pieces of information make up the current state of the game?
Your favorite team just finished playing a game.
What pieces of information make up the current state of their season?
-
Last semester's exam1 study questions.
Because we are covering topics in a different order from last semester,
you won't be able to answer all of the questions on the study sheet!
(Including the very first two questions.)
Conversely, there are topics we have covered which aren't touched
on that study sheet
(most notably, writing constructors with arguments,
and much use of fields).
-
Last semester's exam1.
Everything but #1,#2,#11,#12 are
within your grasp, although some are a bit of a stretch.
Only after working through the old exam, check out
last semester's exam1 solution.
When giving a java expression or statement,
you should be able to type it into BlueJ's Code Panel without any error;
this means knowing where you need parentheses, where you need squirly-braces,
and where you need semicolons (though I'll give some slack on these).
I particularly recommend the problems which require actual code
or definitions.
Us vs. the book
There are a few slight differences in material,
between the Java code we're using in class,
and the textbook:
-
Ignore
“public”,
“private”.
(We'll get to those concepts once they have could conceivably
have some meaning to our programs.)
-
The book doesn't explicitly mention “this”,
when it calls methods or refers to fields
(relying on Java to fill in that word automatically).
While we're not requiring you to
use “this” in your programs,
we recommend it, and will use it in all our examples.
-
The book doesn't use getters and setters as consistently as we do.
-
(It drives me batty that in if statements,
they don't include a space between the if and
then following open-parenthesis.
That's okay, the way I place closing-curly-braces
undoubtedly drives the authors to distraction.)
-
We haven't yet introduced
“System.out.println”.
There is a pre-existing object named
System.out, which knows the method
println:
This method
it takes in a String, and
it returns nothing (void).
But, it has the ephemeral side-effect of changing pixels on
the “console window”.
For this class, until week 10 or so,
we will not use System.out.println.
The reason is that students sometimes confuse changing pixels on the
terminal window with returning a value. The two concepts are unrelated,
and returning a value is the important concept.
Okay, there is one situation in the lecture notes,
were we have slipped in a call to println:
In an if-else statement,
when we're the final default else branch,
and we think our code should never reach that line:
We ask the error-console to display a message:
System.err.println( "SomeFunction: we shouldn't reach this line; something's wrong!" );
In ITEC220, you'll learn about exceptions (the correct way to cause an error),
but in the mean time this is an appropriate place to call println.
And again, to emphasize:
the error message is not being returned -- it's just getting
printed on the error console!
- where the book uses the class ArrayList
(starting in Chapter 4), we will use LinkedList instead.
You can make that replacement, and everything will run just the same.1
New phrasing of old explanations
Finally:
Concepts we've talked about all semester,
but with slightly different phrasing, which might help.
- To “call a function” is to ask a question.
In object-oriented programming (Java),
we always walk up to a particular object and ask the question of them.
Examples: jo.pizzaArea(20), or Math.sqrt(16).
Don't confuse calling a function
with writing a function.
-
To “write a function” is to tell objects how to
answer the question, if it's ever asked of them.
It means solving the general case -- it's answering an infinite number of potential
questions all at once!
Example:
class PizzaServer {
/** Calculate the area of a pizza, given its diameter.
* @param diam The diameter of a pizza (in inches).
* @return The area of the pizza (in square inches).
*/
double pizzaArea( double diam ) {
return Math.PI * Math.pow( (diam/2), 2);
}
}
|
Writing a function
involves a parameter (here, “diam”).
It's like using x in algebra -- the parameter stands for
whatever particular value the question (“pizzaArea”)
is asked about.
- When calling a function,
you don't need to tell Java what type of argument you're giving it.
For example:
myTreasure.setDescription(
String "a gold nugget" ).
Java already knows (from the signature) what type the function wants,
and it will verify that you're providing the correct type.
- When writing the function,
don't try and mention some particular future input.
For example:
void setDescription( String
"a gold nugget");
instead use
void setDescription( String revisedDescription ).
The signature mentions only the parameter type-and-name.
Since you are writing the general case, it's a grievous misunderstanding to
think you know right now what specific String will be handed
to the function, when somebody calls it in the future!
Note that in solving one question,
we might ask other (sub)questions (like Math.pow).
Thus, writing a function can involve calling other functions.
- “Declaring a variable”
causes Java to create labeled box to hold a certain type.
Example: “Glarxzle g;”
causes java to to create a box which can hold a Glarxzle,
labeled g.
Note that declaring int n; or Glarxzle g;
does not put anything inside the box.
- When you “use a variable”, you just ask
Java to go up to the labeled box and use whatever is stored inside.
- When you “initialize a variable” or “assign to a
variable”, you tell Java to calculate the value on the right-hand-side of the
“gets” operator (=),
and stuff it into the box whose label is given on the left-hand-side.
Example: n = 7*8; causes Java to first evaluate the expression
on the right-hand-side (getting 56), and then goes to the box labeled n
and stuff 56 into it (obliterating whatever used to be in the box, if anything).
- To “declare a function” is to give its signature
()
Declaring a function is the same idea as declaring a variable:
you tell java what it's named, and what its (input and return) type is.2)
New phrasing of old explanations
Finally:
Concepts we've talked about all semester,
but with slightly different phrasing, which might help.
- To “call a function” is to ask a question.
In object-oriented programming (Java),
we always walk up to a particular object and ask the question of them.
Examples: jo.pizzaArea(20), or Math.sqrt(16).
Don't confuse calling a function
with writing a function.
-
To “write a function” is to tell objects how to
answer the question, if it's ever asked of them.
It means solving the general case -- it's answering an infinite number of potential
questions all at once!
Example:
class PizzaServer {
/** Calculate the area of a pizza, given its diameter.
* @param diam The diameter of a pizza (in inches).
* @return The area of the pizza (in square inches).
*/
double pizzaArea( double diam ) {
return Math.PI * Math.pow( (diam/2), 2);
}
}
|
Writing a function
involves a parameter (here, “diam”).
It's like using x in algebra -- the parameter stands for
whatever particular value the question (“pizzaArea”)
is asked about.
- When calling a function,
you don't need to tell Java what type of argument you're giving it.
For example:
myTreasure.setDescription(
String "a gold nugget" ).
Java already knows (from the signature) what type the function wants,
and it will verify that you're providing the correct type.
- When writing the function,
don't try and mention some particular future input.
For example:
void setDescription( String
"a gold nugget");
instead use
void setDescription( String revisedDescription ).
The signature mentions only the parameter type-and-name.
Since you are writing the general case, it's a grievous misunderstanding to
think you know right now what specific String will be handed
to the function, when somebody calls it in the future!
Note that in solving one question,
we might ask other (sub)questions (like Math.pow).
Thus, writing a function can involve calling other functions.
- “Declaring a variable”
causes Java to create labeled box to hold a certain type.
Example: “Glarxzle g;”
causes java to to create a box which can hold a Glarxzle,
labeled g.
Note that declaring int n; or Glarxzle g;
does not put anything inside the box.
- When you “use a variable”, you just ask
Java to go up to the labeled box and use whatever is stored inside.
- When you “initialize a variable” or “assign to a
variable”, you tell Java to calculate the value on the right-hand-side of the
“gets” operator (=),
and stuff it into the box whose label is given on the left-hand-side.
Example: n = 7*8; causes Java to first evaluate the expression
on the right-hand-side (getting 56), and then goes to the box labeled n
and stuff 56 into it (obliterating whatever used to be in the box, if anything).
- To “declare a function” is to give its signature
()
Declaring a function is the same idea as declaring a variable:
you tell java what it's named, and what its (input and return) type is.3)
1
It immediately raises the question:
if both these list classes have exactly the same methods as each other,
and always can be interchanged, then why do two such classes even exist?
We'll get to that near the end of the semester!
The brief answer:
depending on the situation, one class sometimes requires
less time or memory than the other.
For all the programs we'll write in this class, the performance will be the same.
We prefer avoiding the name “ArrayList”, because
students think it's an array, which it's not -- it's a list!
↩
2
Or to make the comparison more closely:
The function-name is just the label on a box,
and you have to specify the type of thing the box stores.
In Java, we don't have a way to declare a function-variable without
initializing it, and we can't ever get at the function itself without calling it.
(For instance, we can't pass Treasure.isBetterThan(Treasure)
into a generic sorting-function as an argument.)
Other languages (Lisp, Ruby) do allow functions
as “first class values”.
When we want to get the same effect in Java, we have to
use “the command pattern”.)
↩
3
Or to make the comparison more closely:
The function-name is just the label on a box,
and you have to specify the type of thing the box stores.
In Java, we don't have a way to declare a function-variable without
initializing it, and we can't ever get at the function itself without calling it.
(For instance, we can't pass Treasure.isBetterThan(Treasure)
into a generic sorting-function as an argument.)
Other languages (Lisp, Ruby) do allow functions
as “first class values”.
When we want to get the same effect in Java, we have to
use “the command pattern”.)
↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs