The goal for this exercise is to make sure that you can both pass objects to methods, and return objects from methods, as well as continuing to write programs that are composed of decoupled classes.
For this exercise, you should continue to work with the TelevisionHandler
and Television
classes that were defined in the previous exercises. Other than implementing the basic methods on the Television
class, it should not be necessary to complete those other exercises in order to complete this one.
What you need to do to prepare for this exercise:
Add a new method to the
class, named Television
HandlerPickMoreExpensiveTV
.
PickMoreExpensiveTV
is given (as parameters) 2 Television
objects,
figures out which one costs more (using the getter methods on the Television
class),
and returns that one. This method must be created by you, from scratch, if it hasn't been already.
If the two Television
s have the same price, then you must return the first parameter.
If the method is given one null parameter and one non-null parameter, then the method should return the non-null parameter. If both parameters are null, then the method should return null.
This public method should never crash, no matter what values are passed in as parameters.
Because the prior exercise, implementing the CreateATV
method, which creates
and returns a new
Television
object, some students get the impression that all methods that
return an object must create that object. This exercise is set up like this not only
to emphasize both passing objects into and back out of methods, but also to emphasize
that not all methods that return an object needs to instantiate (to create) that object.
You should fully understand how to make use of the above method in particular, and of passing objects into and returning an object from a method, in general.
There is a Objects_As_Parameters_And_Return_Value.RunExercise
method that you
can use to test your work.
You must have your Objects_As_Parameters_And_Return_Value.RunExercise
method
create two Television
objects that you’ll be comparing. Next, call the PickMoreExpensiveTV
method, saving that more expensive TV into a third local variable,
named moreExpensiveTV
. Finally, call the TelevisionHandler.PrintMyTV
method on each of the Television
objects, and then announce which Television
is more expensive, and then print that one.