📄 the oo design process3.mht
字号:
however, so it indeed belongs in the problem statement.</P>
<P>It's essential to keep the problem statement focused on the =
problem at=20
hand. We don't want to solve the general problem of "banking" in a =
piece=20
of kid's software. On the other hand, we don't want to leave out =
an=20
essential detail either. If the average reader (who is assumed to =
be a=20
domain expert) needs some piece of information to make sense of =
the=20
problem statement, then it belongs. If the information is an =
essential=20
part of the domain, we wouldn't add it. In the current case, if I =
really=20
was writing a turn-key banking system, I certainly wouldn't bother =
to=20
define "effective interest" because that's something that I'd =
expect my=20
domain expert to know. (By the same token, the designers must know =
enough=20
about the problem domain to understand these basic terms as well. =
You=20
can't design an accounting application unless you know something =
about=20
accounting -- at least at the level of an intelligent layman.)</P>
<P>So, I dug out a finance-for-the-complete-idiot book, looked up=20
<I>nominal interest</I>, and modified the definition of =
<I>interest</I> as=20
follows:</P><!-- Sidebar -->
<TABLE align=3Dcenter border=3D1 cellPadding=3D3 class=3Dsidebar =
width=3D"80%">
<TBODY>
<TR>
<TD>
<P>
<TABLE border=3D0 cellPadding=3D4 cellSpacing=3D0>
<TBODY>
<TR>
<TD vAlign=3Dbaseline><B>Interest</B></TD>
<TD vAlign=3Dbaseline>A credit given as a reward for =
keeping=20
your money in the bank. Interest is added to the =
balance=20
daily.=20
<P>The <I>Bank of Allen</I> has to work like a real =
bank, with=20
interest computed on a monthly basis. Nonetheless, the =
earned=20
interest has to be posted to the passbook every day. =
The=20
problem is that daily compounding and monthly =
compounding=20
yield different results.</P>
<P>For example, if you start with $100 in the bank, =
and have a=20
10% interest rate, compounded once every 10 days, =
you'll end=20
up with $110.00 in the account at the end of the ten =
days.</P>
<P>On the other hand, if you divide to create a daily =
interest=20
of 1%/day and compound daily, you'll end up with a =
larger=20
number:=20
<TABLE border=3D0 cellPadding=3D4>
<TBODY>
<TR>
<TD vAlign=3Dbaseline>Day 0</TD>
<TD vAlign=3Dbaseline>Principal</TD>
<TD vAlign=3Dbaseline>$100.00</TD></TR>
<TR>
<TD vAlign=3Dbaseline></TD>
<TD vAlign=3Dbaseline>Interest</TD>
<TD vAlign=3Dbaseline>$1.00</TD></TR>
<TR>
<TD vAlign=3Dbaseline>Day 1</TD>
<TD vAlign=3Dbaseline>Principal + interest</TD>
<TD vAlign=3Dbaseline>$101.00</TD></TR>
<TR>
<TD vAlign=3Dbaseline></TD>
<TD vAlign=3Dbaseline>Interest</TD>
<TD vAlign=3Dbaseline>$1.01</TD></TR>
<TR>
<TD vAlign=3Dbaseline>Day 2</TD>
<TD vAlign=3Dbaseline>Principal + interest</TD>
<TD vAlign=3Dbaseline>$102.01</TD></TR>
<TR>
<TD vAlign=3Dbaseline></TD>
<TD vAlign=3Dbaseline>Interest</TD>
<TD vAlign=3Dbaseline>$1.02</TD></TR>
<TR>
<TD vAlign=3Dbaseline>Day 3</TD>
<TD vAlign=3Dbaseline>Principal + interest</TD>
<TD vAlign=3Dbaseline>$103.03</TD></TR>
<TR>
<TD vAlign=3Dbaseline></TD>
<TD vAlign=3Dbaseline>Interest</TD>
<TD vAlign=3Dbaseline>$1.03</TD></TR>
<TR>
<TD vAlign=3Dbaseline>Day 4</TD>
<TD vAlign=3Dbaseline>Principal + interest</TD>
<TD vAlign=3Dbaseline>$104.06</TD></TR>
<TR>
<TD vAlign=3Dbaseline></TD>
<TD vAlign=3Dbaseline>Interest</TD>
<TD vAlign=3Dbaseline>$1.04</TD></TR>
<TR>
<TD vAlign=3Dbaseline>Day 5</TD>
<TD vAlign=3Dbaseline>Principal + interest</TD>
<TD vAlign=3Dbaseline>$105.10</TD></TR>
<TR>
<TD vAlign=3Dbaseline></TD>
<TD vAlign=3Dbaseline>Interest</TD>
<TD vAlign=3Dbaseline>$1.05</TD></TR>
<TR>
<TD vAlign=3Dbaseline>Day 6</TD>
<TD vAlign=3Dbaseline>Principal + interest</TD>
<TD vAlign=3Dbaseline>$106.15</TD></TR>
<TR>
<TD vAlign=3Dbaseline></TD>
<TD vAlign=3Dbaseline>Interest</TD>
<TD vAlign=3Dbaseline>$1.06</TD></TR>
<TR>
<TD vAlign=3Dbaseline>Day 7</TD>
<TD vAlign=3Dbaseline>Principal + interest</TD>
<TD vAlign=3Dbaseline>$107.21</TD></TR>
<TR>
<TD vAlign=3Dbaseline></TD>
<TD vAlign=3Dbaseline>Interest</TD>
<TD vAlign=3Dbaseline>$1.07</TD></TR>
<TR>
<TD vAlign=3Dbaseline>Day 8</TD>
<TD vAlign=3Dbaseline>Principal + interest</TD>
<TD vAlign=3Dbaseline>$108.28</TD></TR>
<TR>
<TD vAlign=3Dbaseline></TD>
<TD vAlign=3Dbaseline>Interest</TD>
<TD vAlign=3Dbaseline>$1.08</TD></TR>
<TR>
<TD vAlign=3Dbaseline>Day 9</TD>
<TD vAlign=3Dbaseline>Principal + interest</TD>
<TD vAlign=3Dbaseline>$109.36</TD></TR>
<TR>
<TD vAlign=3Dbaseline></TD>
<TD vAlign=3Dbaseline>Interest</TD>
<TD vAlign=3Dbaseline>$1.09</TD></TR>
<TR>
<TD vAlign=3Dbaseline>Day 10</TD>
<TD vAlign=3Dbaseline>Principal + interest</TD>
<TD =
vAlign=3Dbaseline>$110.45</TD></TR></TBODY></TABLE>
<P>You need to use two interest rates to solve this =
problem:=20
one that the kid knows about and another that the bank =
uses=20
internally.=20
<DL>
<DT><B>Effective interest rate</B>=20
<DD>The actual interest rate, including the effect =
of=20
compounding. Given a nominal monthly rate of 5%, the =
effective interest rate when compounded daily is=20
5.12%. =20
<DT><B>Nominal interest rate</B>=20
<DD>The stated interest rate, excluding the effect =
of=20
compounding. </DD></DL>
<P><B>What's the <I>effective</I> rate, compounded =
daily, that=20
would achieve a <I>nominal</I> monthly rate of 5%?</B> =
<TABLE border=3D0 cellPadding=3D0 class=3Dcode-sample>
<TBODY>
<TR>
<TD><PRE>N =3D Nominal rate expressed as a decimal =
(.05)
P =3D Number of periods (30 days)
E =3D Effective rate expressed as a decimal
Formula:
E =3D (1 + N/P)<SUP>P</SUP> -1 =3D .0512
An interest rate of 5%, compounded daily, yields the same balance=20
at the end of 30 days as 5.12% compounded =
monthly.</PRE></TD></TR></TBODY></TABLE></P>
<P><B>What <I>nominal</I> rate do I use if I'm =
compounding=20
daily, but want an <I>effective</I> interest rate of=20
5%/month?</B>=20
<TABLE border=3D0 cellPadding=3D0 class=3Dcode-sample>
<TBODY>
<TR>
<TD><PRE>N =3D Nominal rate expressed as a decimal =
P =3D Number of periods (30 days)
E =3D Effective rate expressed as a decimal (.05)
Formula:
N =3D P * (E + 1)<SUP>1/P</SUP> -1 =3D .0488
An interest rate of 4.88%, compounded daily, yields the same balance=20
at the end of 30 days as 5% compounded =
monthly.</PRE></TD></TR></TBODY></TABLE>
<P>The effective and nominal rates are the same if you =
compound only once in a period. That is, if you =
compute=20
interest once a month, the effective and nominal =
monthly rates=20
are the same.</P>
<P>To keep things easy to explain, the bank publishes =
the=20
effective rate: "Your monthly interest rate is 10%. =
This means=20
that if you keep $10 in the bank for a month, the bank =
will=20
pay you a dollar."</P>
<P>Since interest is compounded daily, the nominal =
rate must=20
be used for daily computation (so the effective rate =
at the=20
end of the month comes out right). For example, given =
a 30-day=20
month, and a published (effective) monthly rate of 5%, =
the=20
(nominal) daily rate used in compounding is 4.88%. If =
you=20
start out with $10 in the account and apply the =
following=20
algorithm, you'll end up with a balance of $10.50:=20
<TABLE border=3D0 cellPadding=3D0 class=3Dcode-sample>
<TBODY>
<TR>
<TD><PRE> double balance =3D 10.00;
int days_in_month =3D 30;
double effective_rate =3D ->05;
double nominal_rate =3D days_in_month *
( Math.pow(effective_rate+1,
1.0/days_in_month)
-1);
for ( int i =3D 0; i </ days_in_month; ++i )
balance =3D balance + balance *
(nominal_rate/days_in_month));=20
</PRE></TD></TR></TBODY></TABLE></P></TD></TR></TBODY></TABLE><!-- End =
sidebar --></P></TD></TR></TBODY></TABLE>
<P>Now things are starting to look more reasonable, although it =
turns out=20
that we're still not done. That's the topic of next month's =
column.</P><A=20
name=3Dresources>
<P><STRONG class=3Dsubhead>Resources </STRONG>
<UL>
<LI>Read last month's OO Design Process column, "<A=20
=
href=3D"http://www-106.ibm.com/developerworks/library/oo-design2/index.ht=
ml?dwzone=3Dcomponents">Beginning=20
to design software</A>."=20
<LI>Visit the <A=20
=
href=3D"http://www.winchestermysteryhouse.com/index.html">Winchester=20
Mystery House</A> to see the results of haphazard design. =
</LI></UL>
<P></P><A name=3Dauthor>
<P><STRONG class=3Dsubhead>About the author</STRONG> <BR>Allen =
Holub has=20
been working in the computer industry since 1979. He is widely =
published=20
in magazines (<I>Dr. Dobb's Journal</I>, <I>Programmers =
Journal</I>,=20
<I>Byte</I>, <I>MSJ</I>, among others) and is a Contributing =
Editor to the=20
online magazine <A =
href=3D"http://www.javaworld.com/">JavaWorld</A>. He has=20
eight books to his credit, the latest of which (<A=20
=
href=3D"http://www.amazon.com/exec/obidos/ASIN/1893115100/alleiholuasso">=
<I>Taming=20
Java Threads</I></A>) covers the traps and pitfalls of Java =
threading.=20
He's been designing and building object-oriented software for =
longer than=20
he cares to remember. After eight years as a C++ programmer, Allen =
abandoned C++ for Java programming in early 1996. He's been =
teaching=20
programming (first C, then C++ and MFC, now OO-Design and Java=20
programming) both on his own and for the University of California =
Berkeley=20
Extension since 1982. Allen offers both public classes and =
in-house=20
training in Java technology and object-oriented design topics. He =
also=20
does object-oriented design consulting and contract Java =
programming.=20
Visit Allen's Web site at <A=20
href=3D"http://www.holub.com/">http://www.holub.com/</A>.</P>
<P>The "Bank of Allen" is a trademark of Allen I. Holub. This =
program, and=20
the design thereof, is (c) 2000, Allen I. Holub. All rights =
reserved.</P><!-- End paper --><BR></TD>
<TD vAlign=3Dtop width=3D8><!-- gutter--><IMG height=3D1=20
src=3D"http://www-106.ibm.com/developerworks/images/c.gif" =
width=3D8> </TD><!-- Formatter, when paper is in folder, change =
above to =0A=
../../images/c.gif --></TR><!-- gutter -->
<TR height=3D8 vAlign=3Dtop>
<TD vAlign=3Dtop width=3D8><!-- gutter --></TD>
<TD vAlign=3Dtop width=3D8><!-- gutter --></TD>
<TD vAlign=3Dtop width=3D8><!-- gutter --></TD></TR>
<TR height=3D8 vAlign=3Dtop>
<TD vAlign=3Dtop width=3D8><!-- gutter --></TD>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -