JDEP183H

Handout 3:  Why Object-Oriented? An Example

 

August 29, 2006

 

An Object-Oriented Programming Solution

 

Here we have an object-oriented solution to build a currency converter for different types of currencies: Japanese Yen, British Pounds, Mexican Pesos, and Euros.  Our strategy is first: Think of what objects we need!  We need a CurrencyConverter object!  As a result, we build that one first, as in CurrencyConverter.java.  Then we need to solve it.  How?  We build a client program (or an application, or a tester) to use these CurrencyConverter objects, as in CurrencyConverterApplication.java.

 

CurrencyConverter.java

 

 

class CurrencyConverter  {

 

      private double exchangeRate;

 

      public CurrencyConverter(double rate)  {

            exchangeRate = rate;

            }

 

      public double fromDollar (double dollar ) {

            return ( dollar * exchangeRate );

      }

 

      public void setExchangeRate ( double rate )  {

            exchangeRate  = rate;

      }

 

      public double toDollar (double foreignMoney) {

            return (foreignMoney / exchangeRate ) ;

      }

 

public double getExchangeRate ()  {

            return exchangeRate;

      }

}

 

 


CurrencyConverterApplication.java

 

class CurrencyConverterApplication {

 

      public static void main (String [ ] args)  {

            CurrencyConverter yenConverter, pesoConverter;

            CurrencyConverter poundConverter, euroConverter;

            double amountInYen, amountInPound, amountInPeso, amountInEuro;

           

            yenConverter = new CurrencyConverter (109.22);

            amountInYen = yenConverter.fromDollar(200);    

           

            poundConverter = new CurrencyConverter (0.559);

            amountInPound = poundConverter.fromDollar(300);

 

            pesoConverter = new CurrencyConverter (11.585);

            amountInPeso = pesoConverter.fromDollar(200);

 

            euroConverter = new CurrencyConverter (0.822);

            amountInEuro = euroConverter.fromDollar(200);

 

}

}

 

 

 

 


A Traditional Program Solution: Example 1

 

Without the object-oriented capability in traditional programming languages, one would have to do the following to solve the above problem.  With this setup, if you want to add another currency, then you will have to go in and change all the procedures/functions.  Further, if you need to change the formulae for computing the conversions, then you also need to change every if clause.

 

 

double YenExchangeRate;

double PoundExchangeRate;

double PesoExchangeRate;

double EuroExchangeRate;

int currencyType;  // to hold the type of currency, 1 for Yen,

                  // 2 for Pound, 3 for Peso, and 4 for Euro

double amountInYen, amountInPound, amountInPeso, amountInEuro;

 

double fromDollar( int currencyType, double dollar ) {

   if (currencyType == 1)

      return ( dollar * YenExchangeRate);

   else if (currencyType == 2)

      return ( dollar * PoundExchangeRate);

   else if (currencyType == 3)

      return ( dollar * PesoExchangeRate);

   else if (currencyType == 4)

      return ( dollar * EuroExchangeRate);

   }

 

double toDollar( int currencyType, double foreignMoney ) {

   if (currencyType == 1)

      return ( foreignMoney / YenExchangeRate);

   else if (currencyType == 2)

      return ( foreignMoney / PoundExchangeRate);

   else if (currencyType == 3)

      return ( foreignMoney / PesoExchangeRate);

   else if (currencyType == 4)

      return ( foreignMoney / EuroExchangeRate);

   }

 

YenExchangeRate = 109.22;

amountInYen = fromDollar(1, 200);  

           

PoundExchangeRate = 0.559;

amountInPound = fromDollar(2, 300);

 

PesoExchangeRate = 11.585;

amountInPeso = fromDollar(3, 200);

 

EuroExchangeRate = 0.822;

amountInEuro = fromDollar(4, 200);

 

 


A Traditional Program Solution: Example 2

 

Or, you could do the following!  With this setup, if you want to add another currency, then you will have to go in and change all the procedures/functions.  Further, if you need to change the formulae for computing the conversions, then you also need to change every if clause.  This is even more cumbersome

 

 

double YenExchangeRate;

double PoundExchangeRate;

double PesoExchangeRate;

double EuroExchangeRate;

int currencyType;  // to hold the type of currency, 1 for Yen,

                   // 2 for Pound, 3 for Peso, and 4 for Euro

double amountInYen, amountInPound, amountInPeso, amountInEuro;

 

double yenfromDollar( double dollar ) {

   return ( dollar * YenExchangeRate);

   }

double poundfromDollar( double dollar ) {

   return ( dollar * PoundExchangeRate);

   }

double pesofromDollar( double dollar ) {

   return ( dollar * PesoExchangeRate);

   }

double eurofromDollar ( double dollar )  {

   return ( dollar * EuroExchangeRate);

   }

 

double yentoDollar( double foreignMoney ) {

   return ( foreignMoney / YenExchangeRate);

   }

double poundtoDollar( double foreignMoney ) {

   return (foreignMoney / PoundExchangeRate);

   }

double pesotoDollar( double foreignMoney ) {

   return ( foreignMoney / PesoExchangeRate);

   }

double eurotoDollar( double foreignMoney ) {

   return (foreignMoney / EuroExchangeRate);

   }

     

YenExchangeRate = 109.22;

amountInYen = yenfromDollar(200);  

           

PoundExchangeRate = 0.559;

amountInPound = poundfromDollar(300);

 

PesoExchangeRate = 11.585;

amountInPeso = pesofromDollar(200);

 

EuroExchangeRate = 0.822;

amountInEuro = eurofromDollar(200);