Programming Exercises

   


Exercises 2 4 build on the code of each previous exercise.

1:

Write four classes called ElectronicDevice, Radio, Computer, and MobilePhone. Let ElectronicDevice be the base class for the other three classes and include the following three class members in its definition:

  • A private instance variable called brandName (of type string)

  • A public property called BrandName to access brandName

  • A private instance variable called isOn (of type bool)

Also include two methods called SwitchOn (which must write "On" and set isOn to true) and SwitchOff (which must write "Off" and set isOn to false). The three subclasses remain empty for now.

Write the appropriate code to test that even though the three subclasses have empty definitions, you can utilize their inherited class members.

2:

Override the SwitchOn and SwitchOff methods in each of the subclasses so that they apart from the actions they perform in the ElectronicDevice also write the name of the device that is being switched on or off. For example when you call the SwitchOn method for Radio you should see the following output on the screen

 On 

Radio and isOn should still be assigned the value true. (Hint: Use the base access to achieve this functionality.)

3:

Equip the ElectronicDevice class with two constructors:

  • A default constructor that sets brandName to "unknown" and isOn to false.

  • A constructor that takes one argument of type string that is used to initialize brandName while isOn also is set to false.

Add the following instance variables to the program:

  • Add currentFrequency of type double to Radio.

  • Add internalMemory of type int to Computer.

  • Add lastNumberDialed of type uint to MobilePhone.

Add the following two constructors to each of the Radio, Computer and MobilePhone classes:

  • A default constructor that not only initializes the instance values defined in the particular class but also the instance variables inherited (brandName and isOn) from ElectronicDevice.

  • A constructor that takes two arguments one to set the value defined for the class (either currentFrequency, internalMemory, or lastNumberDialed) and one to set the value of brandName. (Hint: Call the appropriate constructor initializers to implement this.)

4:

Write a class called LaptopComputer that is derived from the Computer class. Define the instance variable maxBatteriLife of type uint for this class and provide two constructors:

  • One default constructor that assigns appropriate values to maxBatteriLife as well as the other class members LaptopComputer inherits.

  • One constructor with which you can pass initial values to maxBatteriLife as well as the class members LaptopComputer inherits from its descendants.


   


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net