8.1 Parameter variance
First, be sure you understand the co-variance problem stated above. Why is it problematic to execute aref.Op(sref)in the class Client?
The parameter variance problem, and the distinction between covariance and contravariance, is not really a topic in C#. The program with the classes A/B/S/T on the previous page compiles and runs without problems. Explain why!
8.2 A specialization of Stack
It is noteworthy that the abstract Stack is programmed without any instance variables (that is, without any data representation of the stack). Notice also that we have been able to program a single non-abstract method ToggleTop, which uses the abstract methods Top, Pop, and Push.
Make a non-abstract specialization of Stack, and decide on a reasonable data representation of the stack.
In this exercise it is OK to ignore exception/error handling. You can, for instance, assume that the capacity of the stack is unlimited; That popping an empty stack an empty stack does nothing; And that the top of an empty stack returns the string "Not Possible". In a later lecture we will revisit this exercise in order to introduce exception handling. Exception handling is relevant when we work on full or empty stacks.
Write a client of your stack class, and demonstrate the use of the inherited method ToggleTop. If you want, you can also adapt my stack client class .
8.3 Course and Project classes
In the earlier exercise about courses and projects (found in the lecture about classes) we programmed the classes BooleanCourse, GradedCourse, and Project. Revise and reorganize your solution (or the model solution) such that BooleanCourse and GradedCourse have a common abstract superclass called Course.
Be sure to implement the method Passed as an abstract method in class Course.
In the Main method (of the client class of Course and Project) you should demonstrate that both boolean courses and graded courses can be referred to by variables of static type Course.
8.4 The interface ITaxable
For the purpose of this exercise you are given a couple of very simple classes called Bus and House. Class Bus specializes the class Vehicle. Class House specializes the class FixedProperty. All the classes are here.
First in this exercise, program an interface ITaxable with a parameterless operation TaxValue. The operation should return a decimal number.
Next, program variations of class House and class Bus which implement the interface ITaxable. Feel free to invent the concrete taxation of houses and busses. Notice that both class House and Bus have a superclass, namely FixedProperty and Vehicle, respectively. Therefore it is essential that taxation is introduced via an interface.
Demonstrate that taxable house objects and taxable bus objects can be used together as objects of type ITaxable.
8.5 An abstract GameObject class
Restructure this program such that class Die and class Card both inherit an abstract class GameObject. You should write the class GameObject.
The client program should survive this restructuring. (You may, however, need to change the name of the type IGameObject to GameObject). Compile and run the given client program with your classes.
8.6 Comparable Dice
In this exercise we will arrange that two dice can be compared to each other. The result of die1.CompareTo(die2) is an integer. If the integer is negative, die1 is considered less than die2; If zero, die1 is considered equal to die2; And if positive, die1 is considered greater than die2. When two dice can be compared to each other, it is possible sort an array of dice with the standard Sort method in C#.
Program a version of class Die which implements the interface System.IComparable.
Consult the documentation of the (overloaded) static method System.Array.Sort and locate the Sort method which relies on IComparable elements.
Make an array of dice and sort them by use of the Sort method.
Generated: Monday February 7, 2011, 12:18:08