The goal for this exercise is to make sure that you can pass an object to a method (as a parameter), and to start to explore the idea of decoupling / separation of concerns.
What you need to do to prepare for this exercise:
Let’s accomplish this exercise’s goal by working through a small (and somewhat contrived) example that none-the-less demonstrates some of the principles that you’ll be working towards in this lesson.
For all the methods that you implement (in this course (not just this exercise, but in this course, as you go forwards)), you should remember that since method is public, anyone, anywhere can call the method. Even people whom you never thought would call this method. Therefore, you need to make sure that all the parameters to this method are ‘safe’ before using them. In particular, you need to check that the any object references are valid, and not equal to the special null value. If any object reference parameters are null, you can simply return early.
This exercise assumes that you’ve already implemented the Television
class, as was detailed in a prior exercise.
What you need to do for this exercise:
Implement the PrintMyTV
method on the TelevisionHandler
class:
This method must print out the message "ABOUT TO PRINT TV" on it’s own line, then must call the Print
method on the Television
object, then must print the message "FINISHED PRINTING TV" on it’s own
line. An example of correct output might be:
ABOUT TO PRINT TV Brand:Sony Price:1000.17 Size:10.5 FINISHED PRINTING TV
If the parameter is null
, then nothing should be printed
Implement the PrintMyTVUsingGetters
method.
This method will be called by the tests, in order to verify that your method is able to correctly
handle the Television
objects.
This method will take the Television
object that was passed in as a parameter, and use the getter
methods on the object to extract all the data it needs, in order to print out a message that
looks similar to this:
The TV’s Brand is Sony, which is rad. The TV’s Price is 1000.17, which is really rad. The TV’s Size is 10.5, which is wicked rad.
You will need to implement the getter methods on the Television
class, if they haven't been implemented already
If the parameter is null, then nothing should be printed
There is a Objects_As_Parameters.RunExercise method that you can use to test your work.