5.6. Exercises

  1. Go back to your solution for exercise No. 3 at the end of Chapter 2. For all the classes you suggested, list the pairwise associations that you might envision occurring between them.

  2. If the class FeatureFilm were defined to have the following methods:

    public void Update(Actor a, string title);
    public void Update(Actor a, Actor b, string title);
    public void Update(string topic, string title);

    which of the following additional headers would be allowed by the compiler?

    public bool Update(string category, string theater);
    public bool Update(string title, Actor a);
    public void Update(Actor b, Actor a, string title);
    public void Update(Actor a, Actor b);

  3. Given the following simplistic code, which illustrates overloading, overriding, and straight inheritance of methods, how many different Fuel method signatures would each of the four classes recognize?

    class Vehicle
        {
          string name;
    
          public virtual void Fuel(string fuelType) {
            // details omitted...
          }
    
          public virtual bool Fuel(string fuelType, int amount) {
            // details omitted...
          }
        }
    
        class Automobile : Vehicle
        {
          public virtual void Fuel(string fuelType, string timeFueled) {
            // details omitted...
          }
    
          public override bool Fuel(string fuelType, int amount) {
            //...
          }
        }

    class Truck : Vehicle
        {
          public override void Fuel(string fuelType) {
            //...
          }
        }
    
        class SportsCar : Automobile
        {
          public override void Fuel(string fuelType) {
            //...
          }
    
          public override void Fuel(string fuelType, string timeFueled) {
            //...
          }
        }
    
        // Client code:
    
        Truck t = new Truck();
        SportsCar sc = new SportsCar();

  4. Given the following simplistic classes:

    class FarmAnimal
    {
      string name;
    
      public virtual string Name {
        get {
          return name;
        }
        set {
          name = value;
        }
      }
    
      public virtual void MakeSound() {
        Console.WriteLine(Name + " makes a sound...");
      }
           }
    
    class Cow : FarmAnimal
    {
      public override void MakeSound() {
        Console.WriteLine(Name + " goes Moooooo...");
      }
    }

    class Horse : FarmAnimal
    {
      public override string Name {
        set {
          base.Name = value + " [a Horse]";
        }
      }
    }

    what would be printed by the following client code?

    Cow c = new Cow();
    Horse h = new Horse();
    c.Name = "Elsie";
    h.Name = "Mr. Ed";
    c.MakeSound();
    h.MakeSound();

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset