Programming Exercises

   


1:

Expand the Bicycle class of Listing 14.2 to contain an instance variable called age that represents the age of a Bicycle. Write a property that allows users of the Bicycle class to set and get the age instance variable. Include code that prevents the age instance variable from being assigned a negative value or a value greater than 200. Equip the Bicycle class with an instance variable called numberOfAgeAccesses to represent the number of times the age instance variable has been accessed. Include a property to access numberOfAgeAccesses. Should this latter property contain both a set and a get statement block?

2:

Write a class called Rainfall containing a one-dimensional array with 12 elements representing monthly rainfall measurements. Include the following features:

  • Allow the users of the Rainfall objects to access the 12 array elements with an index of type int as if these objects themselves were arrays. Let the first month be identified with index 1 (not zero). Make the program check that all indexes provided are within the correct bounds. Have the program count the number of times a rainfall reading has been accessed. Store this number in an instance variable called numberOfRainAccesses.

  • Include a property called Average that calculates the average monthly rainfall.

  • Please keep this class for use in exercise three.

3:

Allow two Rainfall objects to be added together with the + symbol. This should allow you to write the following:

 totalRainfall = rainfallA + rainfallB; 

where all three objects are of type Rainfall. totalRainfall should now hold an array where the first element holds the result of adding the first element of the array in rainfallA together with the first element of the array in rainfallB; the second element holds the result of adding the second element of the array in rainfallA together with the second element of the array in rainfallB, and so on.

Allow two Rainfall objects to be compared with the < and > comparison operators. Let the average of the 12 elements in each of the two compared objects be the deciding factor. So, if rainfallA's average rainfall is greater than that of rainfallB, the following expression will be true:

 rainfallA > rainfallB 

and the next expression will be false:

 rainfallA < rainfallB 

Use the Average property that you have already written in exercise 2 to provide the necessary information for the comparison.

Please save the code for use in exercise 4.

4:

Write a class called RainfallQuarterly containing an array with four elements to store rainfall information on a quarterly basis. Write user-defined conversions between RainfallQuarterly and Rainfall going in both directions. A Rainfall object is converted to a RainfallQuartely object by adding together the first four array elements and assigning that value to the first array element of the RainfallQuarterly object, and so on. A RainfallQuartely object is converted to a Rainfall object by dividing the first array element by four and assigning this value to each of the first four elements in the Rainfall array, and so on. Should both conversions be explicit or just one of them, or perhaps both of them should be implicit? Why? Hint: In which conversion is data lost? In which conversion is it not lost?


   


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

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