Objects As Return Values

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)

What you need to do for this exercise
  1. Implement the CreateATV method on the TelevisionHandler class:

    1. 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.

    2. 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

    3. 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)

    4. This method must create a new Television object, then get the user's input to set the brand, price, and size.

      1. You can get a string from the keyboard using string s = Console.ReadLine();

    5. This object must then be returned to whichever method is calling this one.

  2. There is a Objects_As_Return_Values.RunExercise method that you can use to test your work.

    1. 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.