Instructor: Dan Shiovitz
Name: _____________________________________________________________
Mean around 23, which is good, since we've seen at least half this before.
[6 points total - 1 each] Is the statement below true or false? Circle one:
new
. null
. If you want the objects, you'll have to
new
them yourself.
initializer list
.
Pretty straightforward if you know the syntax:
char[] charArray = { 'f', 'e', 'd', 'c', 'b', 'a' };(Notice this way does not use
new
.)
Digits
of
unknown size has been instantiated and had values
filled in. Write a code fragment to print out the
elements of Digits
in reverse order
(i.e., printing out the last element first)
The important concept here is that all arrays have a
length
member, which is public
and
final
. The value that's in length
is
the actual length of the array, not the last usable index
in it.
for (int i = Digits.length - 1; i >= 0; i--) System.out.println(Digits[i]);
Write a class named Rabbits
that simulates a
population of rabbits. Full credit will require good use of
encapsulation. This class has the following methods:
class Rabbits { private int currentPop; private int maxPop; public Rabbits() { currentPop = 2; maxPop = 100; } public Rabbits(int newCurrent, int newMax) { currentPop = newCurrent; maxPop = newMax; } public int rabbitCount() { return currentPop; } public void multiply() { currentPop *= 2; if (currentPop > maxPop) currentPop = 0; } }
Write a code fragment that creates a Rabbits
object
(with 5 rabbits in the initial population, and a population limit of
N (which you can assume is an integer that has been declared
and initialized)) and then repeatedly increases the population
until it exceeds the limit. Each time the population increases,
print out the new total. When the population has gone over the
limit, end the loop and print out "The End".
Rabbits Warren = new Rabbits(5, N); while (Warren.rabbitCount() > 0) { Warren.multiply(); System.out.println("There are now " + Warren.rabbitCount() + " rabbits."); } System.out.println("The End.");