Questions

  1. What is the keyword used to call the constructor from the base class?
  • this.
  • the name of the base class.
  • base
  • over
  • inherits

Problems

  1. Consider the diagram representing the “Room”, “ClassRoom”, “Office” classes and their relations.
A UML diagram for the Room ⇽ ClassRoom class (text version)

Suppose you are given an implementation of the Room class, such that

    Room test = new Room("UH", 243);
    Console.WriteLine(test);

displays

UH 243
  1. Write an implementation of the ClassRoom class. Your ToString method should display the room’s building and number, in addition to whether it has AV set-up.

    Solution

    class ClassRoom : Room
    {
      private bool av;
     
      public ClassRoom(string bP, int nP, bool aP)
        : base(bP, nP)
      {
        av = aP;
      }
     
      public override string ToString()
      {
        return base.ToString() + "av? " + av;
      }
    }
  2. Write a SameBuilding static method to be placed inside the Room class such that

    Office test1 = new Office("UH", 127, "706 737 1566");
    ClassRoom test2 = new ClassRoom("UH", 243, true);
    Office test3 = new Office("AH", 122, "706 729 2416");
    Console.WriteLine(Room.SameBuilding(test1, test2));
    Console.WriteLine(Room.SameBuilding(test2, test3));

    Would display “true” and “false”.

    Solution

      public static bool SameBuilding(Room a, Room b)
      {
        return a.building == b.building;
      }
  3. Consider the diagram representing the “Article”, “Book” classes and their relations.

    A UML diagram for the Article ⇽ Book class (text version)
    1. Write a (partial) implementation of the Article abstract class:

      1. Write an implementation for the price attribute: you can either use a getter and a setter (as pictured in the UML diagram), or a property. However, in both cases, setting the price to a negative value should result in an ArgumentOutOfRangeException exception being thrown.
      2. Write an abstract ShippingCosts() method.

      Solution

      using System;
       
      abstract class Article
      {
        public string Id { get; set; }
        private decimal price;
       
        public void SetPrice(decimal priceP)
        {
          if (priceP < 0)
          {
            throw new ArgumentOutOfRangeException();
          }
          else
          {
            price = priceP;
          }
        }
       
        public decimal GetPrice()
        {
          return price;
        }
       
        public Article(string idP, decimal priceP)
        {
          Id = idP;
          SetPrice(priceP);
        }
       
        public abstract decimal ShippingCosts();
      }
    2. Now, assume given a complete implementation of the Article abstract class. Write a complete implementation of the Book class (header included), containing:

      1. An implementation of the Title property using auto-properties.
      2. A Book constructor that passes the idP and priceP arguments to the Article constructor. The titleP argument should be assigned to the Title property.
      3. A ShippingCosts() method that returns either 5.0, or 10% of the Book’s price, whichever is smallest.

      Solution

      class Book : Article
      {
        public string Title { get; set; }
       
        public Book(string idP, decimal priceP, string titleP)
          : base(idP, priceP)
        {
          Title = titleP;
        }
       
        public override decimal ShippingCosts()
        {
          decimal tenp = .1M * GetPrice();
          if (tenp > 5M)
          {
            tenp = 5M;
          }
          return tenp;
        }
      }
    3. Write statements that, if placed in a Main method, would

      1. Create a Book with Id “AAA001”, price $12.5, titled “What it’s like to be a bird”.
      2. Display (nicely) its shipping costs.
      3. Display its Id (as retrieved from the object).

      Solution

      using System;
       
      class Program
      {
        public static void Main()
        {
          Book test = new Book(
            "AAA001",
            12.5M,
            "What it's like to be a bird."
          );
          Console.WriteLine($"{test.ShippingCosts():C}");
          Console.WriteLine(test.Id);
        }
      }

      (Download this code){.download-button}