Programming Exercises

   


Exercises 2 5 each build on the code written in the previous exercise, so please keep the code from each exercise handy for the next exercise. As you progress through the questions, you should write a Main method containing code to demonstrate the classes you write.

1:

Write a class called Robot with the following three instance variables: name (of type string), age (of type ushort), and isOn (of type bool). Make the program initialize these three instance variables to "unknown", 0, and false, respectively (without using constructors). Include accessor and mutato methods to assign and retrieve the values of these instance variables.

Write a Main method that tests the Robot class.

2:

Allow the users of the Robot class to set the initial values of the three instance variables when a Robot object is created. To this end, declare one constructor with three formal parameters and one default constructor.

3:

Include a member variable called robotsCreated that keeps track of the number of Robot objects created. Make the constructors update robotsCreated so this variable is always up-to-date. Implement the following logic in the default constructor: If robotsCreated is less than five when this constructor is called, set isOn to true; otherwise, set it to false.

4:

The Robot is able to perform a few simple calculations. For example, it can find the average of three int numbers. This ability is provided through a method with the following header:

 public int Average (int x, int y, int z) 

This method does not itself perform the average calculation but uses a static method named Average contained inside a class called RobotMath. The RobotMath class only contains static methods and static variables and should never be instantiated. Ensure that RobotMath is never instantiated.


   


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