Programming Exercises

   


1:

Implement a class called ArrayMath, containing the following methods to perform calculations on arrays:

  • A method called ArrayAverage that, as an argument, accepts an array of base type double (of any length) and returns the average of this array.

  • A method called ArraySum that accepts two arrays of base type int. The two arrays must have identical length. The method will add together each corresponding pair of array elements from the two arrays. Each sum is assigned to a corresponding element of a third array, which finally is returned to the caller.

  • A method called ArrayMax that finds and returns the maximum value in an array of base type int. The array argument can be of any length.

Write the code to test this class and its methods.

2:

Write the basic parts of a car game program. The program must include a Car class with the following members:

  • An instance variable called position of type int

  • A method with the header public void MoveForward(int distance) that adds distance to the position instance variable

  • A method with the header public void Reverse(int distance) that deducts distance from position

  • A method called GetPosition that simply returns the value of position to the caller

Furthermore, the program must contain a class called CarGame that (by using an array) contains 5 objects of type Car. It must be possible to move each car (forward and reverse) and to get the position of each of the cars by providing an array index of the corresponding car.

Write a small test program to ensure that the two classes function correctly.


   


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