Mixing Arrays and Classes: A Class that contains an array of (possibly null) reference types

advertisement
Performing Calculations Using an Array (Of Possibly Null References)
The objective for this exercise is to provide you with an opportunity to pull
together the two major concepts that we've worked with recently: using arrays,
and using basic classes. For this exercise, you will be provided with a starter
class (a 'skeleton', if you will), and you should fill in those methods that are
marked with 'TODO'. You also need to fill in the code that's missing from main
that's marked 'TODO'.
What you need to do for this exercise:
1. Implement all the methods on the TVStorage class.
a. The constructor should do any initialization that needs to be done.
Each TVStorage class should be able to store exactly 5 Television
objects.
a. StoreTV should store the given Television object into the specified
array location.
a. If the location is invalid (negative, or too large), then this method
should do nothing.
b. Note that if the location is valid, then this method always store
the given tvToStore into the array, even if the tvToStore
parameter is null.
b. GetTV should return the Television object (including, possibly, the null
value) at the specified index, unless that index is invalid (negative, or
too large).
a. If the index is invalid, then it should return null
c. PrintAllTVs should call .Print on all non-null object references in the
array, and should print “Slot X is null” when it encounters a null array
slot (where X is replaced with the slot number).
2. You may want to run the sample code that has been provided to you, and
confirm that it produces the above, example output. You can do this by this
running the code in the RunExercise method of the
TVStorage_Demonstration class.
a. That code should produce the following output:
Slot 0 is null
Slot 1 is null
Slot 2 is null
Slot 3 is null
Slot 4 is null
Is t2 null? (It should be) True
Slot 0 is null
Slot 1 is null
Brand:Brand X
Price:1000.00
Size:42.00
Slot 3 is null
Brand:Brand Y
Price:2000.00
Size:52.00
Is t2 the same as t? (It should be) True
(Note that the real numbers must have exactly two decimal places in
the output – make sure you use Console.WriteLine(“…{x:0.00}”); in
order to ensure the proper output (where x = 0, or 1, or 2, as
appropriate))
Download