These exercises belong to: http://de.slideshare.net/nicolayludwig/5-c-sharp-introductionobjectorientationpartii
- When to use Inheritance
- Polymorphism
- Abstraction
- Abstract Types
- Interfaces
- Sealed Types
- Static and dynamic Type of Objects
(5) c sharp introduction_object_orientation_part_ii_exercises
1. The following exercises need to be solved in the given order.
Exercises:
1. In the last series of exercises the class DynamicIntArray has been implemented; in
these tasks you also had to write some tests. Now we're going to systemize testing of
classes with so called unit tests.
a) Rewrite the unit tests for the class DynamicIntArray with the facilities integrated into
Visual Studio, a quick introduction can be found in “Anatomy of a Unit Test”.
b) You should be able to answer following questions afterwards:
• What is a unit test?
• Who creates unit tests?
• When are unit tests being created?
• Who executes the unit tests?
• When are unit tests being executed?
2. Let's revisit the class DynamicIntArray.
a) How can we implement a class "DynamicArray" that works with all sorts of types
(without generics!), e.g.: int, string, double[]? Hint: substitution principle.
b) Implement the class DynamicArray and test it with elements of type string. - Just
copy the former project with the DynamicIntArray (incl. unit tests) and modify the
code.
c) Redesign the unit tests as well.
3. Write another class "DynamicCarArray", which is able to store all kinds (types and
subtypes) of Cars.
a) Prove that is works with Buses and VintageCars stored in the same array at
different indexes.
b) Loop over an instance of that array containing types and subtypes of Car instances
and write the result of the method ToString() to the console res.
4. Implement the classes Date and Appointment. - They should be associated to each
other. - Which association did you choose? Document your idea in a clear manner.
5. Implement the types Shape, Triangle, Circle, Rectangle and Square in a C# project by
the usage of inheritance. Not every type needs to be implemented in its own class, if
helpful more types can be introduced. Respect following guidelines:
a) Stick to the SOLID principle.
b) Each type should reside in dedicated file.
c) All fields needs to be encapsulated.
d) One of the types needs to be an abstract class.
e) Following public methods should be implemented reasonably:
GetPosition()/SetPosition(), GetA()/SetA(), GetB()/SetB(), GetC()/SetC(),
GetD()/SetD() and GetRadius()/SetRadius().
f) The method ToString() should be overridden to generate a string representing the
data position, a, b, c, d and radius where reasonable.
g) You should be able to draw the type hierarchy on the whiteboard (e.g. with a UML
diagram).
h) Prove the functionality of that types with some unit tests.
2. 6. Learn about the interface IComparable (not IComparable(T)) in the .Net documentation
a) Implement the class “Car” and the inherited class “VintageCar”, the types can have
any fields and methods. The class VintageCar should implement the interfaces
IHistorical and IComparable. Esp. for the implementation of IComparable you need
to define how VintageCar instances, i.e. their fields, can be compared.
b) The method CompareTo() should only be able to compare instances of the type
VintageCar.
c) In which cases does the .Net framework require types to implement IComparable?
7. With the results of the last exercise show following aspects with a couple of unit tests
and belonging to comments:
a) Usage of IComparable.
b) Downcasting.
c) Static and dynamic type.
8. Read the chapters six and seven of the book "Visual C# 2010".
Remarks:
• The usage of the keywords var, dynamic, foreach and goto is not allowed!
• Avoid magic numbers and use constants where possible.
• The results of the programming exercises need to be runnable applications! All
programs have to be implemented as console programs.
• The programs need to be robust, i.e. they should cope with erroneous input from the
user.
• You should be able to describe your programs after implementation. Comments are
mandatory.
• In documentations as well as in comments, strings or user interfaces make correct use
of language (spelling and grammar)!
• Put your solutions into the TFS repository.
• Don't panic: In programming multiple solutions are possible.
• If you have problems use the Visual Studio help (F1), books and the internet primarily.
• Of course you can also ask colleagues; but it is of course always better, if you find a
solution yourself.
<<interface>>
IHistorical
+ Era() : Era
+ EstimatedPrize() : double