The goal for this exercise is to make sure that you can return an object from a method, and to continue using a program composed of decoupled classes.
What you need to do to prepare for this exercise:
For this exercise, you should continue to work with the TelevisionHandler
and Television
classes, as were detailed in prior exercises.
For this exercise, you will practice returning an object from a method. Since there’s only 1 object to return, we’ll
use a return
statement (we could also have done this using a ref
or out
parameter, too, but we’ll leave that alone for now)
Implement the CreateATV
method on the TelevisionHandler
class:
This method must create a new Television
object, then set the brand of the new
object to be “Brand X”, set the price to be 10.0, and set the size to be 42.0. This object
must then be returned to whichever method is calling this one.
Yes, this is a really contrived example. Yes, it would be much, much easier just to do this yourself directly, rather than calling this method. This all is being done so that you get some easy practice returning an object from a method; we’ll move on to more challenging stuff later
Create a method named CreateATVFromUserInput
on the TelevisionHandler
class. (Note that you will have to create this, from scratch, yourself – this is so you
can practice the 'sending' side of objects-as-return-values)
This method must create a new Television
object, then get the user's input to
set the brand, price, and size.
You can get a string from the keyboard using string s = Console.ReadLine();
This object must then be returned to whichever method is calling this one.
There is a Objects_As_Return_Values.RunExercise
method that you can use to test your
work.
You MUST write something that will call CreateATVFromUserInput
, store the result
into a local variable, and then print the Television
object that the method
returned. In other words, you need to practice the 'receiving' side of objects-as-return-values.