Array of objects

advertisement
Array of objects
The goal for this exercise is to continue exploring the concepts used in creating
an array of object references.
This exercise does NOT deal with arrays of simple/primitive values (such as an
array of integers).
Let’s say that we want to keep track of many Televisions. In order to do
this, we’ll use two things:
A) A class that contains information about each Television, and
B) An array, which keeps track of our collection of televisions
For this exercise, you should continue to work with the TelevisionHandler and
Television classes that were defined in the previous exercises.
For this exercise, we will continue to write code the manipulates an array of
object references.
What’s new about this exercise is that some of the object references may be
null.
What you need to do for this exercise:
1. Implement the PrintArrayOfPossiblyNullTVs method in the
TelevisionHandler class. The method needs to do all of the following steps:
a. The first thing the method should do is create an array of 10 references
to Television objects.
b. Next, write a for loop that loops over the array, and checks each space
in the array – if that slot is null, the program should print out a message
saying that “Slot X is null”, where X is the space number. If the first
element of the array is null, you will see “Slot 0 is null”. What do you
see when you run this code?
i. Include an easy to find comment explaining why you get the
results you do, when you use a ‘printing loop’ on the array
immediately after the array has been created.
c. Next, write code that allocates 4 Television objects, and stores those
objects into the array.Put one object into slot 0, another object into slot
1, another into slot 5, and the last into a slot 9.
i. For each TV, you should give it the brand “Brand X”, the size
should be 42, and the price should be 10 + the index in the
array that the Television is being assigned to.
d. Copy-and-paste the for loop that you wrote above, so that you can
figure out which slots in the array are null, and which aren’t. For slots
that are null, continue to print out the warning message. For any slots
that aren’t null, call the Print method on the object referred to by the
slot.
i. Note that you can check for non-null slots either by using the
else part of the existing if statement, or else by asking
if(array[slot] != null).
ii. Include an easy to find comment explaining why you get the
results you do, when you use a ‘printing loop’ on the array after
you put 4 objects into the the array.
e. Next, add a loop that goes through the array. Each time it finds an
array slot that is null, it should create a new Television, and store that
object into the empty slot (in the slot with null in it). When this loop
finishes running, every slot in the array will be occupied.
i. For each TV, you should give it the brand “Brand X”, the size
should be 42, and the price should be 10 + the index in the
array that the Television is being assigned to.
ii. Include an easy to find comment explaining why you get the
results you do, when you use a ‘printing loop’ on the array after
you’ve filled up the array with objects.
f. Lastly, copy-and-paste the for loop that you wrote above (in part b), so
that you can figure out which slots in the array are null, and which
aren’t. Since there should be no null slots, this means that this loop
should print out 10 separate TVs.
2. NOTE: You must implement each loop separately! Even if you could combine
the ‘print out the array that now contains 4 object references’ with the next
loop (the one that fills in all null slots with new objects), you must write those
two loops out separately. This will force you to repeatedly write out a loop
that deals with an array with null values, which is good exercise, and good
repetition!
3. OPTIONAL: Instead of just copy-and-pasting the ‘printing loop’ 3 times, create
a method on the TelevisionHandler class which will accept an array of
Televisions as a parameter. It will go through the array, and for each slot in
the array, if the array slot is empty (if it’s null), it will print out the message
specified above. If the array slot is non-null (if it’s occupied), it will call the
Print method on the object referred to by that slot.
a. This is not required, but is good practice.
4. The Array_Of_Possibly_Null_Objects. RunExercise method is provided
for you to test your implementation. This will call the
PrintArrayOfPossiblyNullTVs method in the TelevisionHandler class for
you (which is where your code should go).
Download