home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
lect09a
while
Misc review questions:
-
the keyword “static” means
-
How is a constructor different from other methods?
-
In a static method, can you use this?
-
In a non-static method, can you use this?
- Suppose we had
class Robot {
String color; // a regular field
static String defaultForNewRobots; // a static field
// other code elided…
}
|
Suppose 100 Robot objects have been created.
How many color fields exist?
How many mostCommonColor fields exist?
How do your answers change, if no Robot objects have been created?
-
To declare a named-constant, use the adjectives .
By convention, named-constants are written using
characters.
-
Suppose we have a Robot r7;
-
Static, or non?:
A function which sets the is-transformed status of a robot.
-
How would we call its method setIsTransformed, passing it true?
(The method sets the status of whether-or-not the robot is transformed.)
-
How would you write the signature for that method?
-
Complete the method.
We ended last time with
a simple while-loop:
while (stu.creditHours < 120) {
stu.takeCourseInteractive();
}
|
What is the fewest number of times this loop might execute the body?
What is the most?
(lect09a-soln.html)
Change this:
-
Repeatedly get an A in a 3-hour course, until the gpa is ≥ 3.0.
Try it on some different students.
What is the most number of times this loop might run?
What is the smallest number of times this loop might run?
-
Update the code, to print out (for fun) how many 3-hour A's were taken
before the gpa reached the desired point?
(I can think of three different ways1. We'll put some on the board.)
-
A variable which keeps track of how many times you've been through
the loop is called an index variable.
-
We can use the index variable during our loop as well:
Suppose the student cycles through getting A's, B's, and C's:
How can we have this happen?
Hint: %.
-
Write a loop which prints the numbers from 0...up to (but not including)
n.
The
code we arrived at..
1
store the before-fields, and compare to after-fields;
update a variable/counter;
Closed form solution.
Of course, do not make a new field, though!
↩
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs