Exercises in this lecture   Go to the notes, in which this exercise belongs -- Keyboard shortcut: 'u'   Alphabetic index   Course home   

Exercise solution:
The interface ITaxable


First, we show the given classes Bus and House together with their superclasses:

using System;

public class FixedProperty{

  protected string location;
  protected bool inCity; 
  protected decimal estimatedValue;

  public FixedProperty(string location, bool inCity, decimal value){
    this.location = location;
    this.inCity = inCity;
    this.estimatedValue = value;
  }

  public FixedProperty(string location):
    this(location,true,1000000){
  }

  public string Location{
    get{
      return location;
    }
  }
}

public class Vehicle{

  protected int registrationNumber;
  protected double maxVelocity;
  protected decimal value;

  public Vehicle(int registrationNumber, double maxVelocity, 
                 decimal value){
    this.registrationNumber = registrationNumber;
    this.maxVelocity = maxVelocity;
    this.value = value;
  }

  public int RegistrationNumber{
    get{
      return registrationNumber;
    }
  }

}

public class Bus: Vehicle{

  protected int numberOfSeats;

  public Bus(int numberOfSeats, int regNumber, decimal value):
      base(regNumber, 80, value){
    this.numberOfSeats = numberOfSeats;
  }

  public int NumberOfSeats{
    get{
     return numberOfSeats;
    }
  } 

}

public class House: FixedProperty {

  protected double area;

  public House(string location, bool inCity, double area, 
               decimal value):
      base(location, inCity, value){
   this.area = area;
  }

  public double Area{
    get{
     return area;
    }
  } 
}

Here follows the the interface ITaxable together with the classes TaxableBus and TaxableHouse. Notice the way they the two classes implement the interface ITaxable. At the bottom, the class App serves as a client that demonstrates how taxable houses and taxable busses can be used together.

using System;

interface ITaxable{
  decimal TaxValue();
}

class TaxableBus: Bus, ITaxable {

  public TaxableBus(int numberOfSeats, int regNumber, decimal value):
    base(numberOfSeats, regNumber, value){
  }

  public decimal TaxValue(){
    return (value / 10) + 105M * numberOfSeats;
  }

}

class TaxableHouse: House, ITaxable {

  public TaxableHouse(string location, bool inCity, double area, decimal value):
    base(location, inCity, area, value){
  }

  public decimal TaxValue(){
    if (inCity)
      return (estimatedValue / 1000.0M) * 5M + 5M * (decimal)area;
    else
      return (estimatedValue / 1000.0M) * 3M;
  }
}


class App{

  public static void Main(){

     ITaxable[] tObjects = 
        new ITaxable[]{
           new TaxableHouse("Aalborg", true, 700, 500000M),
           new TaxableHouse("Skjern", true, 1800, 300000M),
           new TaxableBus(50, 12345, 300000M),
           new TaxableBus(10, 345678, 25000M)}; 

     foreach(ITaxable it in tObjects)
       Console.WriteLine("{0}", it.TaxValue());
  }
}