c 1 introduction to programming and the c language

c 1 introduction to programming and the c language


DisciplinaFísica Básica I826 materiais10.228 seguidores
Pré-visualização50 páginas
are so that in the 
beginning the interest is a big part of the payment and the repayment is only a small part, and towards 
the end of the period, the situation is reversed. The relationship between the loans principal, payment 
and the interest rate can be expressed in the following formula:
rate
ratepaymentprincipal
periods
+
+\u2212
=
\u2212
1
)1(1
Finally, the outstanding immediately after the payment of the nth payment is calculated as:
rate
ratepaymentrateprincipalrestloan
n
n 1)1()1( \u2212+\u2212+=
If you look at the above class Loan there is a need for two versions: One for a serial loan and one for 
an annuity. When two classes have much in common, it is natural to think of a design with a common 
base class and two derived classes:
Loan
principal: double
rate: double
periods: int
Principal: double
Rate: double
Periods: int
Payment(n): double
Interest(n): double
Repayment(n): double
Outstanding(n):double
SerialLoan AnnuityLoan
Payment(n): double
Interest(n): double
Repayment(n): double
Outstanding(n):double
Payment(n): double
Interest(n): double
Repayment(n): double
Outstanding(n):double
The three classes can be implemented in the following manner:
public abstract class Loan
{
protected double principal;
protected double rate;
protected int periods;
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
118 
Abstract classes
public Loan(double principal, double rate, int periods)
{
this.principal = principal;
this.rate = rate;
this.periods = periods;
}
public double Principal
{
get { return principal; }
}
public double Rate
{
get { return rate; }
}
public int Periods
{
get { return periods; }
}
abstract public double Payment(int n);
abstract public double Interest(int n);
abstract public double Repayment(int n);
abstract public double Outstanding(int n);
}
public class SerialLoan : Loan
{
public SerialLoan(double principal, double rate, int periods) :
base(principal, rate, periods)
{
}
public override double Repayment(int n)
{
return principal / periods;
}
public override double Outstanding(int n)
{
return Repayment(0) * (periods - n);
}
public override double Interest(int n)
{
return Outstanding(n - 1) * rate;
}
public override double Payment(int n)
{
return Repayment(n) + Interest(n);
}
}
public class AnnuityLoan : Loan
{
public AnnuityLoan(double principal, double rate, int periods) :
base(principal, rate, periods)
{
}
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
119 
Abstract classes
public override double Payment(int n)
{
return principal * rate / (1 - Math.Pow(1 + rate, -periods));
}
public override double Outstanding(int n)
{
return principal * Math.Pow(1 + rate, n) - Payment(0) *
(Math.Pow(1 + rate, n) - 1) / rate;
}
public override double Interest(int n)
{
return Outstanding(n - 1) * rate;
}
public override double Repayment(int n)
{
return Payment(n) - Interest(n);
}
}
The program must print a plan for the loan\u2019s amortization, which is a table showing the status of the 
loan each payment:
public class Amortisation
{
private Loan loan;
By 2020, wind could provide one-tenth of our planet\u2019s 
electricity needs. Already today, SKF\u2019s innovative know-
how is crucial to running a large proportion of the 
world\u2019s wind turbines. 
Up to 25 % of the generating costs relate to mainte-
nance. These can be reduced dramatically thanks to our 
systems for on-line condition monitoring and automatic 
lubrication. We help make it more economical to create 
cleaner, cheaper energy out of thin air. 
By sharing our experience, expertise, and creativity, 
industries can boost performance beyond expectations. 
Therefore we need the best employees who can 
meet this challenge!
The Power of Knowledge Engineering
Brain power
Plug into The Power of Knowledge Engineering. 
Visit us at www.skf.com/knowledge
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
120 
Abstract classes
public Amortisation(Loan loan)
{
this.loan = loan;
}
public void Print()
{
Console.WriteLine("Principal: {0, 10:F}", loan.Principal);
Console.WriteLine("Rate of interest: {0, 10:F}", loan.Rate);
Console.WriteLine("Number of periods: {0, 10:D}\n", loan.Periods);
Console.WriteLine(
"Periods Payment Repayment Interest Outstanding");
for (int n = 1; n <= loan.Periods; ++n)
Console.WriteLine(&quot;{0, 7:D}{1, 15:F}{2, 15:F}{3, 15:F}{4, 15:F}&quot;, n,
loan.Payment(n), loan.Repayment(n), loan.Interest(n), loan.Outstanding(n));
}
}
Explanation
Note first that the class Loan is defined abstract. It must be because it has abstract methods. An abstract 
method is a method that is not defined \u2013 its code is not written. In this case there are four abstract 
methods. The reason that the methods are abstract is that how for example the payment is calculated 
depends on what kind of loan it is. The method can only be implemented when you know what kind 
of loan you are talking about. An abstract method is simply a prototype definition in which a method 
is defined by its name, return type and parameters, but its body is missing.
Loan is a type, and even though it is abstract, and you thus can\u2019t create objects of type Loan, the type 
can for example be used as a type of an argument to a method. Everybody who gets the informed that 
they are dealing with something of the type Loan knows that it is something that beyond the three 
properties have four methods, Payment(), Interest(), Repayment() and Outstanding(), and you can use 
the four abstract methods as if they existed.
The class SerialLoan inherits Loan, and thus it is up to class SerialLoan to implement the four abstract 
methods so that SerialLoan is a concrete class. The class SerialLoan can implement the abstract methods, 
because it has the needed knowledge \u2013 a knowledge not known in the class Loan. The implementations 
of the abstract methods are trivial, but note that a SerialLoan is specifically a Loan.
The class AnnuityLoan is also easy to implement, and it consists simply of writing the above formulas 
in C#. Here you should note the method Pow(), which is a static method of the Math class, which raises 
an argument in a power.
Finally, there is class Amortisation, which can print a plan for a loan\u2019s amortization. Here you should 
note that the class has an instance variable of type Loan, which is initialized in the constructor. So you 
can have a variable whose type is an abstract class, since concrete objects of types respectively SerialLoan 
and AnnuityLoan also is of the type Loan. Please note that the class Amortisation know nothing about 
the specific loan type, but nevertheless one can write method Print(), since all methods are defined for 
the abstract type of Loan. The class Amortisation will also support and work on new types of loans as 
long as they inherit the class Loan.
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
121 
Abstract classes
Test
If you test the above classes with the following program:
class Program
{
static void Main(string[] args)
{
Amortisation table1 = new Amortisation(new SerialLoan(10000, 0.02, 10));
Amortisation table2 = new Amortisation(new AnnuityLoan(10000, 0.02, 10));
table1.Print();
table2.Print();
}
}
you get the following result:
Comment
In the last examples, there typically have been a number of classes and in this case four classes in addition 
to the Main program. In all examples, the classes code have been in the same file, but as the examples 
become larger, there is not a viable option for the sake of clarity. Many choose to consistently