<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Opevans</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Opevans"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Opevans"/>
	<updated>2026-07-01T18:03:04Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56501</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a or</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56501"/>
		<updated>2011-12-03T03:43:07Z</updated>

		<summary type="html">&lt;p&gt;Opevans: /* Currency Conversion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Wiki Chapter: CSC/ECE 517 Fall 2011/ch7 7a or&lt;br /&gt;
&lt;br /&gt;
''7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
Money is very important to people worldwide and is something that often must be represented in computer programs.  This raises the question of how money should be represented, which can vary from program to program.  There is much to be considered such as string formatting, the way money is used, and the variety of different currencies in the world.  In Chapter 6 of Skrien's ''Object-Oriented Design Using Java'', he suggests an implementation that represents money as a class, but there are other approaches that can be taken, each with their respective advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Properties of Money==&lt;br /&gt;
Money has certain properties and uses that must be considered in implementation.&lt;br /&gt;
&lt;br /&gt;
===Responsibilities/Using Money===&lt;br /&gt;
The following are ways in which a representation of money should be able to be used: &amp;lt;ref name=&amp;quot;lec20&amp;quot;&amp;gt;http://courses.ncsu.edu/csc517/common/lectures/notes/lec20.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
*To represent a positive or negative amount of money.&lt;br /&gt;
*Possibly representing multiple components of a currency - i.e. dollars and cents.&lt;br /&gt;
*Possibly representing amounts of multiple currencies - i.e. a mix of US Dollars, euros, and yen.'&lt;br /&gt;
*To have a string formatting potentially including currency symbols like the dollar sign ($) and/or commas and/or decimal points.&lt;br /&gt;
*Addition, subtraction, multiplication, and division.&lt;br /&gt;
The degree to which of the above must be met will depend on the depth of the implementing project.&lt;br /&gt;
&lt;br /&gt;
===Different Currencies===&lt;br /&gt;
Some applications may only handle only one currency such as the US Dollar, but more generally applied applications, such as banking, must be able to handle different kinds of currencies.&lt;br /&gt;
&lt;br /&gt;
There are currently over 180 currencies in use around the world.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/List_of_circulating_currencies&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Implementation must be aware of how to specifically represent each currency used.  Some currencies use decimal points, such as how the US has 100 cents to a dollar, while others have no decimal, such as yen.  Each currency also has a different sign symbol used such as $, €, or ¥.  Different currencies also have different rules about how rounding values is handled.&lt;br /&gt;
&lt;br /&gt;
Special consideration must be taken when using different currencies in conjunction with each other.  Foreign exchange rates can be used to convert between different currencies, but the exchange rate often change, so attention must be paid to that.  In some cases, it is also important to keep track of how much money the user has of more than one currency at a time, so that must be handled somehow. &amp;lt;ref&amp;gt;http://www.xencraft.com/resources/multi-currency.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Immutable vs. Mutable===&lt;br /&gt;
Money values can be either mutable, meaning changeable, or immutable, meaning unchanging.  Mutable money values are useful in that a new value does not need to be created for every change.  However, mutable values cause problems for use in hash maps or with concurrency.  The issue with concurrency is a read and write issue in a system with multiple users.  With changing values, money not be what the user expects and lead to unpredictable behavior.  Skrien feels this is a significant enough disadvantage to prefer an immutable representation for money.&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Possible Representations==&lt;br /&gt;
There are several possible ways to represent money in code.&lt;br /&gt;
&lt;br /&gt;
===Floating Point Number===&lt;br /&gt;
Floating point numbers seem like an appealing option since they already include a fractional component, which lends itself straightforward to currencies like the US dollar which also have a fractional component.  However, floating point numbers come with a drawback due to the way floats are formatted.  Since floats are based on binary representation rather than a decimal representation, a value that would be exact in decimal notation can only be stored approximately in a floats point number.  For instance, 9.48 is stored as 9.479999542236328125.  Performing calculations with these approximated values can lead to inaccurate results. &amp;lt;ref name=&amp;quot;dobbs&amp;quot;&amp;gt;http://drdobbs.com/java/184405653&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another deficiency of floating point numbers is that they lose precision as the number gets larger. This is less of a problem when dealing with small amounts of currency, but when dealing with large amounts, on the order of the United States federal debt, the number of significant figures to the right of the decimal point decreases, again leading to possibly inaccurate results.&amp;lt;ref&amp;gt;Skrien, Dale. ''Object-Oriented Design Using Java''. McGraw Hill, 2009, p. 174.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Two Integers===&lt;br /&gt;
For currencies with a fractional component, using two integers to represent one amount of money is an option.  One integer to represent the main component and one to represent the fractional part.  For US currency, this would mean an integer for dollars and an integer for cents.  This representation makes displaying monetary values simple, as each component is already split out and accurate.  Also, by using integers, calculations with money work out as they should, avoiding the loss of accuracy floating points have.  However, code will need to be implemented to handle when the fraction component carries over.  In the case of dollars and cents, if the cents exceed 100, the cents value should be decreased by 100 and the dollars increased by 1.  This handling of carries adds overhead to calculations.  Some drawbacks of this implementation are the added overhead and storage required&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref&amp;gt;http://www.infoplease.com/spot/stockdecimal1.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===One Integer, Implied Decimal Point===&lt;br /&gt;
One integer can be used to represent money, even including the fractional component. The location of the decimal place is simply implied based on the currency.  For instance, a single integer representation of $12.34 would be 1234. When dealing with US currency, this exploits the fact that the fundamental unit of currency in the US is actually the cent, not the dollar &amp;lt;ref name=&amp;quot;dobbs&amp;quot;/&amp;gt;. A specialized print method would have to be constructed to display the monetary amount properly to the user, but all calculations work out properly.  This approach has the same advantage that using two integers of maintaining accuracy, but does not have the added overhead or storage.  In ''Object-Oriented Design Using Java'', Skrien considers this to be the best use of the primitive types for representing money.&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;. This method shares the same deficiency of two integers in that it cannot represent fractions of a fundamental unit of money.&lt;br /&gt;
&lt;br /&gt;
===BigDecimal===&lt;br /&gt;
Java offers a [http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html BigDecimal] class, which many people use for representing money.  BigDecimal is capable of storing any size number since it stores each digit as an entry in an array, which grows as necessary.  It also allows for specification of both the rounding method and the scale, the number of digits past the decimal place, for proper monetary calculations. &amp;lt;ref&amp;gt;http://www.javaranch.com/journal/2003/07/MoneyInJava.html&amp;lt;/ref&amp;gt;  On the downside, this specification for rounding and scale is required, which adds code to the implementation&amp;lt;ref name=&amp;quot;dobbs&amp;quot; /&amp;gt;. BigDecimal values are also immutable, which avoids read/write and write/write conflicts in applications. &amp;lt;ref&amp;gt;http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial&amp;lt;/ref&amp;gt;  While there are a lot of nice things about BigDecimal, there are some more issues with BigDecimal which can cause problems in representing money.  BigDecimal cannot represent numbers with infinite digits such as 1/9 (.11111...) and throws an ArithmeticException on any calculation that would produce such number.  To deal with this, hacks such as the following must be implemented:&lt;br /&gt;
 BigDecimal bd1 = BigDecimal(1);&lt;br /&gt;
 BigDecimal bd2 = BigDecimal(9);&lt;br /&gt;
 try {&lt;br /&gt;
     return bd1.divide(bd2);&lt;br /&gt;
 } catch(ArithmaticException ae) {&lt;br /&gt;
     return new BigDecimal(bd1.doubleValue() / bd2.doubleValue());&lt;br /&gt;
 }&lt;br /&gt;
Also, since there isn't a BigDecimal equivalent for SQL databases, so storing BigDecimal values either requires conversion to a different format, which can affect accuracy and/or precision, or the value must be stored as a BLOB, which is cumbersome and inefficient.  In general use, calculations with BigDecimal are significantly slower than with integers, and also take up more storage space. &amp;lt;ref&amp;gt;http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Class===&lt;br /&gt;
In ''Object-Oriented Design Using Java'', recommends using a class to represent money, rather than just using a long integer alone.  He gives two reasons for using a class.  The first is encapsulation - by encapsulating money into a class, all the operations specific to working with money can be implemented to work as desired in all cases, which also avoids code duplication.  The second reason is that, in compiled languages like Java, the compiler is better able to detect coding errors, such as adding money to something that isn't money. &amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt; Implementing Money as a class is a generally good idea, but there are multiple ways to go about it.&lt;br /&gt;
&lt;br /&gt;
====Abstract Class====&lt;br /&gt;
If multiple kinds of currencies are needed to be represented, one implementation is to create an abstract Money class and have each possible currency be a subclass of the abstract class.  Since there are over 180 different currencies worldwide, it can be a challenge to implement subclasses for each one required.&lt;br /&gt;
&lt;br /&gt;
The following is the abstract class proposed by Skrien in Object-Oriented Design Using Java:&lt;br /&gt;
 public abstract class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public long getAmount()&lt;br /&gt;
   public String toString()&lt;br /&gt;
   public int compareTo(Money m)&lt;br /&gt;
   public boolean equals(Object o)&lt;br /&gt;
   public int hashCode()&lt;br /&gt;
   public Money plus(Money)&lt;br /&gt;
   public Money minus(Money)&lt;br /&gt;
   public Money times(double factor)&lt;br /&gt;
   public Money dividedBy(double divisor)&lt;br /&gt;
   public Money negate()&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;&amp;gt;Skrien, Dale. Object-Oriented Design Using Java. McGraw Hill, 2009, p. 179-183.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Single Class====&lt;br /&gt;
Rather than implementing a variety of subclasses for each currency, a single class can be used to represent money, with currency specified within.  This is a good idea, since most modern currencies are decimal and as such can be calculated and treated in the same way. The only differences are in decimal locations and the symbols used to present them.  Java has a [http://docs.oracle.com/javase/6/docs/api/java/util/Currency.html Currency class] which holds the information for a given currency.&lt;br /&gt;
&lt;br /&gt;
This method is insufficient for representing a non-decimal currency, such as pre-1971 British currency (240 pence per pound sterling.) &lt;br /&gt;
The following is the Single Class implementation given by Skrien in ''Object-Oriented Design Using Java'':&lt;br /&gt;
&lt;br /&gt;
 public class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   private long amount;&lt;br /&gt;
   private Currency currency;&lt;br /&gt;
   public Money(long amount, Currency currency) {&lt;br /&gt;
     this.amount = amount;&lt;br /&gt;
     this.currency = currency;&lt;br /&gt;
   }&lt;br /&gt;
   public long getAmount() { return amount; }&lt;br /&gt;
   public Currency getCurrency() { &lt;br /&gt;
     return currency; }&lt;br /&gt;
   public String toString() { ...above... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Mixed Money====&lt;br /&gt;
When dealing with multiple currencies it might be necessary to keep track of money consisting of multiple currencies.  Simply converting into one common currency is not a good approach, since exchange rates vary over time, so the amount of each currency should be kept track of.  Skrien suggests using a MixedMoney class, which acts as a sort of wallet holding amounts of different currencies.&lt;br /&gt;
&lt;br /&gt;
The following is Skrien's implementation of MixedMoney:&lt;br /&gt;
 public class MixedMoney implements Money {&lt;br /&gt;
   public MixedMoney() { ... }&lt;br /&gt;
   public String toString() { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
   public Collection getCurrencies() { ... }&lt;br /&gt;
   public long getAmount(Currency currency) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since it uses a lot of the same methods as the regular Money class, Skrien renamed Money to SimpleMoney and created a common Money interface that is as follows:&lt;br /&gt;
&lt;br /&gt;
 public interface Money extends Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public int compareTo(Money o);&lt;br /&gt;
   public Money plus(Money money);&lt;br /&gt;
   public Money minus(Money money);&lt;br /&gt;
   public Money times(double factor);&lt;br /&gt;
   public Money dividedBy(double divisor);&lt;br /&gt;
   public Money negate();&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The money within MixedMoney is suggested to be represented by either a hash map, a collection, or a tree structure.  The hash map implementation is rejected for storage and calculation time issues.  The tree structure is deemed the best approach as it is elegant and avoids checking classes.&lt;br /&gt;
&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Currency Conversion==&lt;br /&gt;
When dealing with multiple currencies, it is often necessary to convert from one to another.&lt;br /&gt;
Therefore some means for doing so must be implemented in any program that contains a representation of money. Skrien's implementation uses a MoneyConverter class that manages exchange rates and delegates the actual conversion and creation of a new money object denominated in the target currency to the Money class.&lt;br /&gt;
&lt;br /&gt;
Skrien's implementation is as below:&amp;lt;ref name=skrien179/&amp;gt;&lt;br /&gt;
 Public class MoneyConverter&lt;br /&gt;
 {&lt;br /&gt;
  public double getRate (Currency from, Currency to)&lt;br /&gt;
  public void setRate (Currency from, Currency to, double rate)&lt;br /&gt;
  public Money convertTo(Money money, Currency to) {&lt;br /&gt;
   return money.convertTo(to, this);&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
==Money Implementations in Practice==&lt;br /&gt;
In practice, many decide similarly to Skrien to encapsulate Money as a class.  The choice of underlying representation does vary, though, between types like int, double, or BigDecimal.  I did not find any sources that used floats or two integers, which Skrien rejected. &amp;lt;ref name=&amp;quot;dobbs&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;Representing Money&amp;quot;&amp;gt;http://www.javapractices.com/topic/TopicAction.do?Id=13&amp;lt;/ref&amp;gt; &amp;lt;ref name=&amp;quot;C++&amp;quot;&amp;gt;http://www.di-mare.com/adolfo/p/money.htm&amp;lt;/ref&amp;gt;  I found one source that addressed the issue of mixed money, which implemented a MoneyBag class that holds an array of Money. &amp;lt;ref name=&amp;quot;testing&amp;quot;&amp;gt;http://junit.sourceforge.net/doc/testinfected/testing.htm&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are also existing projects which seek to provide programmers with a library of classes to use for Money such as [http://timeandmoney.sourceforge.net/ Time and Money] and [http://joda-money.sourceforge.net/ Joda-Money].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56500</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a or</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56500"/>
		<updated>2011-12-03T03:29:06Z</updated>

		<summary type="html">&lt;p&gt;Opevans: /* Two Integers */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Wiki Chapter: CSC/ECE 517 Fall 2011/ch7 7a or&lt;br /&gt;
&lt;br /&gt;
''7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
Money is very important to people worldwide and is something that often must be represented in computer programs.  This raises the question of how money should be represented, which can vary from program to program.  There is much to be considered such as string formatting, the way money is used, and the variety of different currencies in the world.  In Chapter 6 of Skrien's ''Object-Oriented Design Using Java'', he suggests an implementation that represents money as a class, but there are other approaches that can be taken, each with their respective advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Properties of Money==&lt;br /&gt;
Money has certain properties and uses that must be considered in implementation.&lt;br /&gt;
&lt;br /&gt;
===Responsibilities/Using Money===&lt;br /&gt;
The following are ways in which a representation of money should be able to be used: &amp;lt;ref name=&amp;quot;lec20&amp;quot;&amp;gt;http://courses.ncsu.edu/csc517/common/lectures/notes/lec20.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
*To represent a positive or negative amount of money.&lt;br /&gt;
*Possibly representing multiple components of a currency - i.e. dollars and cents.&lt;br /&gt;
*Possibly representing amounts of multiple currencies - i.e. a mix of US Dollars, euros, and yen.'&lt;br /&gt;
*To have a string formatting potentially including currency symbols like the dollar sign ($) and/or commas and/or decimal points.&lt;br /&gt;
*Addition, subtraction, multiplication, and division.&lt;br /&gt;
The degree to which of the above must be met will depend on the depth of the implementing project.&lt;br /&gt;
&lt;br /&gt;
===Different Currencies===&lt;br /&gt;
Some applications may only handle only one currency such as the US Dollar, but more generally applied applications, such as banking, must be able to handle different kinds of currencies.&lt;br /&gt;
&lt;br /&gt;
There are currently over 180 currencies in use around the world.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/List_of_circulating_currencies&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Implementation must be aware of how to specifically represent each currency used.  Some currencies use decimal points, such as how the US has 100 cents to a dollar, while others have no decimal, such as yen.  Each currency also has a different sign symbol used such as $, €, or ¥.  Different currencies also have different rules about how rounding values is handled.&lt;br /&gt;
&lt;br /&gt;
Special consideration must be taken when using different currencies in conjunction with each other.  Foreign exchange rates can be used to convert between different currencies, but the exchange rate often change, so attention must be paid to that.  In some cases, it is also important to keep track of how much money the user has of more than one currency at a time, so that must be handled somehow. &amp;lt;ref&amp;gt;http://www.xencraft.com/resources/multi-currency.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Immutable vs. Mutable===&lt;br /&gt;
Money values can be either mutable, meaning changeable, or immutable, meaning unchanging.  Mutable money values are useful in that a new value does not need to be created for every change.  However, mutable values cause problems for use in hash maps or with concurrency.  The issue with concurrency is a read and write issue in a system with multiple users.  With changing values, money not be what the user expects and lead to unpredictable behavior.  Skrien feels this is a significant enough disadvantage to prefer an immutable representation for money.&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Possible Representations==&lt;br /&gt;
There are several possible ways to represent money in code.&lt;br /&gt;
&lt;br /&gt;
===Floating Point Number===&lt;br /&gt;
Floating point numbers seem like an appealing option since they already include a fractional component, which lends itself straightforward to currencies like the US dollar which also have a fractional component.  However, floating point numbers come with a drawback due to the way floats are formatted.  Since floats are based on binary representation rather than a decimal representation, a value that would be exact in decimal notation can only be stored approximately in a floats point number.  For instance, 9.48 is stored as 9.479999542236328125.  Performing calculations with these approximated values can lead to inaccurate results. &amp;lt;ref name=&amp;quot;dobbs&amp;quot;&amp;gt;http://drdobbs.com/java/184405653&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another deficiency of floating point numbers is that they lose precision as the number gets larger. This is less of a problem when dealing with small amounts of currency, but when dealing with large amounts, on the order of the United States federal debt, the number of significant figures to the right of the decimal point decreases, again leading to possibly inaccurate results.&amp;lt;ref&amp;gt;Skrien, Dale. ''Object-Oriented Design Using Java''. McGraw Hill, 2009, p. 174.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Two Integers===&lt;br /&gt;
For currencies with a fractional component, using two integers to represent one amount of money is an option.  One integer to represent the main component and one to represent the fractional part.  For US currency, this would mean an integer for dollars and an integer for cents.  This representation makes displaying monetary values simple, as each component is already split out and accurate.  Also, by using integers, calculations with money work out as they should, avoiding the loss of accuracy floating points have.  However, code will need to be implemented to handle when the fraction component carries over.  In the case of dollars and cents, if the cents exceed 100, the cents value should be decreased by 100 and the dollars increased by 1.  This handling of carries adds overhead to calculations.  Some drawbacks of this implementation are the added overhead and storage required&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref&amp;gt;http://www.infoplease.com/spot/stockdecimal1.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===One Integer, Implied Decimal Point===&lt;br /&gt;
One integer can be used to represent money, even including the fractional component. The location of the decimal place is simply implied based on the currency.  For instance, a single integer representation of $12.34 would be 1234. When dealing with US currency, this exploits the fact that the fundamental unit of currency in the US is actually the cent, not the dollar &amp;lt;ref name=&amp;quot;dobbs&amp;quot;/&amp;gt;. A specialized print method would have to be constructed to display the monetary amount properly to the user, but all calculations work out properly.  This approach has the same advantage that using two integers of maintaining accuracy, but does not have the added overhead or storage.  In ''Object-Oriented Design Using Java'', Skrien considers this to be the best use of the primitive types for representing money.&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;. This method shares the same deficiency of two integers in that it cannot represent fractions of a fundamental unit of money.&lt;br /&gt;
&lt;br /&gt;
===BigDecimal===&lt;br /&gt;
Java offers a [http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html BigDecimal] class, which many people use for representing money.  BigDecimal is capable of storing any size number since it stores each digit as an entry in an array, which grows as necessary.  It also allows for specification of both the rounding method and the scale, the number of digits past the decimal place, for proper monetary calculations. &amp;lt;ref&amp;gt;http://www.javaranch.com/journal/2003/07/MoneyInJava.html&amp;lt;/ref&amp;gt;  On the downside, this specification for rounding and scale is required, which adds code to the implementation&amp;lt;ref name=&amp;quot;dobbs&amp;quot; /&amp;gt;. BigDecimal values are also immutable, which avoids read/write and write/write conflicts in applications. &amp;lt;ref&amp;gt;http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial&amp;lt;/ref&amp;gt;  While there are a lot of nice things about BigDecimal, there are some more issues with BigDecimal which can cause problems in representing money.  BigDecimal cannot represent numbers with infinite digits such as 1/9 (.11111...) and throws an ArithmeticException on any calculation that would produce such number.  To deal with this, hacks such as the following must be implemented:&lt;br /&gt;
 BigDecimal bd1 = BigDecimal(1);&lt;br /&gt;
 BigDecimal bd2 = BigDecimal(9);&lt;br /&gt;
 try {&lt;br /&gt;
     return bd1.divide(bd2);&lt;br /&gt;
 } catch(ArithmaticException ae) {&lt;br /&gt;
     return new BigDecimal(bd1.doubleValue() / bd2.doubleValue());&lt;br /&gt;
 }&lt;br /&gt;
Also, since there isn't a BigDecimal equivalent for SQL databases, so storing BigDecimal values either requires conversion to a different format, which can affect accuracy and/or precision, or the value must be stored as a BLOB, which is cumbersome and inefficient.  In general use, calculations with BigDecimal are significantly slower than with integers, and also take up more storage space. &amp;lt;ref&amp;gt;http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Class===&lt;br /&gt;
In ''Object-Oriented Design Using Java'', recommends using a class to represent money, rather than just using a long integer alone.  He gives two reasons for using a class.  The first is encapsulation - by encapsulating money into a class, all the operations specific to working with money can be implemented to work as desired in all cases, which also avoids code duplication.  The second reason is that, in compiled languages like Java, the compiler is better able to detect coding errors, such as adding money to something that isn't money. &amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt; Implementing Money as a class is a generally good idea, but there are multiple ways to go about it.&lt;br /&gt;
&lt;br /&gt;
====Abstract Class====&lt;br /&gt;
If multiple kinds of currencies are needed to be represented, one implementation is to create an abstract Money class and have each possible currency be a subclass of the abstract class.  Since there are over 180 different currencies worldwide, it can be a challenge to implement subclasses for each one required.&lt;br /&gt;
&lt;br /&gt;
The following is the abstract class proposed by Skrien in Object-Oriented Design Using Java:&lt;br /&gt;
 public abstract class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public long getAmount()&lt;br /&gt;
   public String toString()&lt;br /&gt;
   public int compareTo(Money m)&lt;br /&gt;
   public boolean equals(Object o)&lt;br /&gt;
   public int hashCode()&lt;br /&gt;
   public Money plus(Money)&lt;br /&gt;
   public Money minus(Money)&lt;br /&gt;
   public Money times(double factor)&lt;br /&gt;
   public Money dividedBy(double divisor)&lt;br /&gt;
   public Money negate()&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;&amp;gt;Skrien, Dale. Object-Oriented Design Using Java. McGraw Hill, 2009, p. 179-183.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Single Class====&lt;br /&gt;
Rather than implementing a variety of subclasses for each currency, a single class can be used to represent money, with currency specified within.  This is a good idea, since most modern currencies are decimal and as such can be calculated and treated in the same way. The only differences are in decimal locations and the symbols used to present them.  Java has a [http://docs.oracle.com/javase/6/docs/api/java/util/Currency.html Currency class] which holds the information for a given currency.&lt;br /&gt;
&lt;br /&gt;
This method is insufficient for representing a non-decimal currency, such as pre-1971 British currency (240 pence per pound sterling.) &lt;br /&gt;
The following is the Single Class implementation given by Skrien in ''Object-Oriented Design Using Java'':&lt;br /&gt;
&lt;br /&gt;
 public class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   private long amount;&lt;br /&gt;
   private Currency currency;&lt;br /&gt;
   public Money(long amount, Currency currency) {&lt;br /&gt;
     this.amount = amount;&lt;br /&gt;
     this.currency = currency;&lt;br /&gt;
   }&lt;br /&gt;
   public long getAmount() { return amount; }&lt;br /&gt;
   public Currency getCurrency() { &lt;br /&gt;
     return currency; }&lt;br /&gt;
   public String toString() { ...above... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Mixed Money====&lt;br /&gt;
When dealing with multiple currencies it might be necessary to keep track of money consisting of multiple currencies.  Simply converting into one common currency is not a good approach, since exchange rates vary over time, so the amount of each currency should be kept track of.  Skrien suggests using a MixedMoney class, which acts as a sort of wallet holding amounts of different currencies.&lt;br /&gt;
&lt;br /&gt;
The following is Skrien's implementation of MixedMoney:&lt;br /&gt;
 public class MixedMoney implements Money {&lt;br /&gt;
   public MixedMoney() { ... }&lt;br /&gt;
   public String toString() { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
   public Collection getCurrencies() { ... }&lt;br /&gt;
   public long getAmount(Currency currency) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since it uses a lot of the same methods as the regular Money class, Skrien renamed Money to SimpleMoney and created a common Money interface that is as follows:&lt;br /&gt;
&lt;br /&gt;
 public interface Money extends Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public int compareTo(Money o);&lt;br /&gt;
   public Money plus(Money money);&lt;br /&gt;
   public Money minus(Money money);&lt;br /&gt;
   public Money times(double factor);&lt;br /&gt;
   public Money dividedBy(double divisor);&lt;br /&gt;
   public Money negate();&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The money within MixedMoney is suggested to be represented by either a hash map, a collection, or a tree structure.  The hash map implementation is rejected for storage and calculation time issues.  The tree structure is deemed the best approach as it is elegant and avoids checking classes.&lt;br /&gt;
&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Currency Conversion==&lt;br /&gt;
When dealing with multiple currencies, it is often necessary to convert from one to another.&lt;br /&gt;
Therefore some means for doing so must be implemented in any program that contains a representation of money. The best way to implement this is to create a member function of the money class that multiplies the amount by a conversion factor and returns a new money object denominated in the target currency.&amp;lt;ref name=skrien179/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Money Implementations in Practice==&lt;br /&gt;
In practice, many decide similarly to Skrien to encapsulate Money as a class.  The choice of underlying representation does vary, though, between types like int, double, or BigDecimal.  I did not find any sources that used floats or two integers, which Skrien rejected. &amp;lt;ref name=&amp;quot;dobbs&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;Representing Money&amp;quot;&amp;gt;http://www.javapractices.com/topic/TopicAction.do?Id=13&amp;lt;/ref&amp;gt; &amp;lt;ref name=&amp;quot;C++&amp;quot;&amp;gt;http://www.di-mare.com/adolfo/p/money.htm&amp;lt;/ref&amp;gt;  I found one source that addressed the issue of mixed money, which implemented a MoneyBag class that holds an array of Money. &amp;lt;ref name=&amp;quot;testing&amp;quot;&amp;gt;http://junit.sourceforge.net/doc/testinfected/testing.htm&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are also existing projects which seek to provide programmers with a library of classes to use for Money such as [http://timeandmoney.sourceforge.net/ Time and Money] and [http://joda-money.sourceforge.net/ Joda-Money].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56499</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a or</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56499"/>
		<updated>2011-12-03T03:27:01Z</updated>

		<summary type="html">&lt;p&gt;Opevans: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Wiki Chapter: CSC/ECE 517 Fall 2011/ch7 7a or&lt;br /&gt;
&lt;br /&gt;
''7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
Money is very important to people worldwide and is something that often must be represented in computer programs.  This raises the question of how money should be represented, which can vary from program to program.  There is much to be considered such as string formatting, the way money is used, and the variety of different currencies in the world.  In Chapter 6 of Skrien's ''Object-Oriented Design Using Java'', he suggests an implementation that represents money as a class, but there are other approaches that can be taken, each with their respective advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Properties of Money==&lt;br /&gt;
Money has certain properties and uses that must be considered in implementation.&lt;br /&gt;
&lt;br /&gt;
===Responsibilities/Using Money===&lt;br /&gt;
The following are ways in which a representation of money should be able to be used: &amp;lt;ref name=&amp;quot;lec20&amp;quot;&amp;gt;http://courses.ncsu.edu/csc517/common/lectures/notes/lec20.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
*To represent a positive or negative amount of money.&lt;br /&gt;
*Possibly representing multiple components of a currency - i.e. dollars and cents.&lt;br /&gt;
*Possibly representing amounts of multiple currencies - i.e. a mix of US Dollars, euros, and yen.'&lt;br /&gt;
*To have a string formatting potentially including currency symbols like the dollar sign ($) and/or commas and/or decimal points.&lt;br /&gt;
*Addition, subtraction, multiplication, and division.&lt;br /&gt;
The degree to which of the above must be met will depend on the depth of the implementing project.&lt;br /&gt;
&lt;br /&gt;
===Different Currencies===&lt;br /&gt;
Some applications may only handle only one currency such as the US Dollar, but more generally applied applications, such as banking, must be able to handle different kinds of currencies.&lt;br /&gt;
&lt;br /&gt;
There are currently over 180 currencies in use around the world.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/List_of_circulating_currencies&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Implementation must be aware of how to specifically represent each currency used.  Some currencies use decimal points, such as how the US has 100 cents to a dollar, while others have no decimal, such as yen.  Each currency also has a different sign symbol used such as $, €, or ¥.  Different currencies also have different rules about how rounding values is handled.&lt;br /&gt;
&lt;br /&gt;
Special consideration must be taken when using different currencies in conjunction with each other.  Foreign exchange rates can be used to convert between different currencies, but the exchange rate often change, so attention must be paid to that.  In some cases, it is also important to keep track of how much money the user has of more than one currency at a time, so that must be handled somehow. &amp;lt;ref&amp;gt;http://www.xencraft.com/resources/multi-currency.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Immutable vs. Mutable===&lt;br /&gt;
Money values can be either mutable, meaning changeable, or immutable, meaning unchanging.  Mutable money values are useful in that a new value does not need to be created for every change.  However, mutable values cause problems for use in hash maps or with concurrency.  The issue with concurrency is a read and write issue in a system with multiple users.  With changing values, money not be what the user expects and lead to unpredictable behavior.  Skrien feels this is a significant enough disadvantage to prefer an immutable representation for money.&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Possible Representations==&lt;br /&gt;
There are several possible ways to represent money in code.&lt;br /&gt;
&lt;br /&gt;
===Floating Point Number===&lt;br /&gt;
Floating point numbers seem like an appealing option since they already include a fractional component, which lends itself straightforward to currencies like the US dollar which also have a fractional component.  However, floating point numbers come with a drawback due to the way floats are formatted.  Since floats are based on binary representation rather than a decimal representation, a value that would be exact in decimal notation can only be stored approximately in a floats point number.  For instance, 9.48 is stored as 9.479999542236328125.  Performing calculations with these approximated values can lead to inaccurate results. &amp;lt;ref name=&amp;quot;dobbs&amp;quot;&amp;gt;http://drdobbs.com/java/184405653&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another deficiency of floating point numbers is that they lose precision as the number gets larger. This is less of a problem when dealing with small amounts of currency, but when dealing with large amounts, on the order of the United States federal debt, the number of significant figures to the right of the decimal point decreases, again leading to possibly inaccurate results.&amp;lt;ref&amp;gt;Skrien, Dale. ''Object-Oriented Design Using Java''. McGraw Hill, 2009, p. 174.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Two Integers===&lt;br /&gt;
For currencies with a fractional component, using two integers to represent one amount of money is an option.  One integer to represent the main component and one to represent the fractional part.  For US currency, this would mean an integer for dollars and an integer for cents.  This representation makes displaying monetary values simple, as each component is already split out and accurate.  Also, by using integers, calculations with money work out as they should, avoiding the loss of accuracy floating points have.  However, code will need to be implemented to handle when the fraction component carries over.  In the case of dollars and cents, if the cents exceed 100, the cents value should be decreased by 100 and the dollars increased by 1.  This handling of carries adds overhead to calculations.  Some drawbacks of this implementation are the added overhead and storage required&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currencty. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref&amp;gt;http://www.infoplease.com/spot/stockdecimal1.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===One Integer, Implied Decimal Point===&lt;br /&gt;
One integer can be used to represent money, even including the fractional component. The location of the decimal place is simply implied based on the currency.  For instance, a single integer representation of $12.34 would be 1234. When dealing with US currency, this exploits the fact that the fundamental unit of currency in the US is actually the cent, not the dollar &amp;lt;ref name=&amp;quot;dobbs&amp;quot;/&amp;gt;. A specialized print method would have to be constructed to display the monetary amount properly to the user, but all calculations work out properly.  This approach has the same advantage that using two integers of maintaining accuracy, but does not have the added overhead or storage.  In ''Object-Oriented Design Using Java'', Skrien considers this to be the best use of the primitive types for representing money.&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;. This method shares the same deficiency of two integers in that it cannot represent fractions of a fundamental unit of money.&lt;br /&gt;
&lt;br /&gt;
===BigDecimal===&lt;br /&gt;
Java offers a [http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html BigDecimal] class, which many people use for representing money.  BigDecimal is capable of storing any size number since it stores each digit as an entry in an array, which grows as necessary.  It also allows for specification of both the rounding method and the scale, the number of digits past the decimal place, for proper monetary calculations. &amp;lt;ref&amp;gt;http://www.javaranch.com/journal/2003/07/MoneyInJava.html&amp;lt;/ref&amp;gt;  On the downside, this specification for rounding and scale is required, which adds code to the implementation&amp;lt;ref name=&amp;quot;dobbs&amp;quot; /&amp;gt;. BigDecimal values are also immutable, which avoids read/write and write/write conflicts in applications. &amp;lt;ref&amp;gt;http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial&amp;lt;/ref&amp;gt;  While there are a lot of nice things about BigDecimal, there are some more issues with BigDecimal which can cause problems in representing money.  BigDecimal cannot represent numbers with infinite digits such as 1/9 (.11111...) and throws an ArithmeticException on any calculation that would produce such number.  To deal with this, hacks such as the following must be implemented:&lt;br /&gt;
 BigDecimal bd1 = BigDecimal(1);&lt;br /&gt;
 BigDecimal bd2 = BigDecimal(9);&lt;br /&gt;
 try {&lt;br /&gt;
     return bd1.divide(bd2);&lt;br /&gt;
 } catch(ArithmaticException ae) {&lt;br /&gt;
     return new BigDecimal(bd1.doubleValue() / bd2.doubleValue());&lt;br /&gt;
 }&lt;br /&gt;
Also, since there isn't a BigDecimal equivalent for SQL databases, so storing BigDecimal values either requires conversion to a different format, which can affect accuracy and/or precision, or the value must be stored as a BLOB, which is cumbersome and inefficient.  In general use, calculations with BigDecimal are significantly slower than with integers, and also take up more storage space. &amp;lt;ref&amp;gt;http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Class===&lt;br /&gt;
In ''Object-Oriented Design Using Java'', recommends using a class to represent money, rather than just using a long integer alone.  He gives two reasons for using a class.  The first is encapsulation - by encapsulating money into a class, all the operations specific to working with money can be implemented to work as desired in all cases, which also avoids code duplication.  The second reason is that, in compiled languages like Java, the compiler is better able to detect coding errors, such as adding money to something that isn't money. &amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt; Implementing Money as a class is a generally good idea, but there are multiple ways to go about it.&lt;br /&gt;
&lt;br /&gt;
====Abstract Class====&lt;br /&gt;
If multiple kinds of currencies are needed to be represented, one implementation is to create an abstract Money class and have each possible currency be a subclass of the abstract class.  Since there are over 180 different currencies worldwide, it can be a challenge to implement subclasses for each one required.&lt;br /&gt;
&lt;br /&gt;
The following is the abstract class proposed by Skrien in Object-Oriented Design Using Java:&lt;br /&gt;
 public abstract class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public long getAmount()&lt;br /&gt;
   public String toString()&lt;br /&gt;
   public int compareTo(Money m)&lt;br /&gt;
   public boolean equals(Object o)&lt;br /&gt;
   public int hashCode()&lt;br /&gt;
   public Money plus(Money)&lt;br /&gt;
   public Money minus(Money)&lt;br /&gt;
   public Money times(double factor)&lt;br /&gt;
   public Money dividedBy(double divisor)&lt;br /&gt;
   public Money negate()&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;&amp;gt;Skrien, Dale. Object-Oriented Design Using Java. McGraw Hill, 2009, p. 179-183.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Single Class====&lt;br /&gt;
Rather than implementing a variety of subclasses for each currency, a single class can be used to represent money, with currency specified within.  This is a good idea, since most modern currencies are decimal and as such can be calculated and treated in the same way. The only differences are in decimal locations and the symbols used to present them.  Java has a [http://docs.oracle.com/javase/6/docs/api/java/util/Currency.html Currency class] which holds the information for a given currency.&lt;br /&gt;
&lt;br /&gt;
This method is insufficient for representing a non-decimal currency, such as pre-1971 British currency (240 pence per pound sterling.) &lt;br /&gt;
The following is the Single Class implementation given by Skrien in ''Object-Oriented Design Using Java'':&lt;br /&gt;
&lt;br /&gt;
 public class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   private long amount;&lt;br /&gt;
   private Currency currency;&lt;br /&gt;
   public Money(long amount, Currency currency) {&lt;br /&gt;
     this.amount = amount;&lt;br /&gt;
     this.currency = currency;&lt;br /&gt;
   }&lt;br /&gt;
   public long getAmount() { return amount; }&lt;br /&gt;
   public Currency getCurrency() { &lt;br /&gt;
     return currency; }&lt;br /&gt;
   public String toString() { ...above... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Mixed Money====&lt;br /&gt;
When dealing with multiple currencies it might be necessary to keep track of money consisting of multiple currencies.  Simply converting into one common currency is not a good approach, since exchange rates vary over time, so the amount of each currency should be kept track of.  Skrien suggests using a MixedMoney class, which acts as a sort of wallet holding amounts of different currencies.&lt;br /&gt;
&lt;br /&gt;
The following is Skrien's implementation of MixedMoney:&lt;br /&gt;
 public class MixedMoney implements Money {&lt;br /&gt;
   public MixedMoney() { ... }&lt;br /&gt;
   public String toString() { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
   public Collection getCurrencies() { ... }&lt;br /&gt;
   public long getAmount(Currency currency) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since it uses a lot of the same methods as the regular Money class, Skrien renamed Money to SimpleMoney and created a common Money interface that is as follows:&lt;br /&gt;
&lt;br /&gt;
 public interface Money extends Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public int compareTo(Money o);&lt;br /&gt;
   public Money plus(Money money);&lt;br /&gt;
   public Money minus(Money money);&lt;br /&gt;
   public Money times(double factor);&lt;br /&gt;
   public Money dividedBy(double divisor);&lt;br /&gt;
   public Money negate();&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The money within MixedMoney is suggested to be represented by either a hash map, a collection, or a tree structure.  The hash map implementation is rejected for storage and calculation time issues.  The tree structure is deemed the best approach as it is elegant and avoids checking classes.&lt;br /&gt;
&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Currency Conversion==&lt;br /&gt;
When dealing with multiple currencies, it is often necessary to convert from one to another.&lt;br /&gt;
Therefore some means for doing so must be implemented in any program that contains a representation of money. The best way to implement this is to create a member function of the money class that multiplies the amount by a conversion factor and returns a new money object denominated in the target currency.&amp;lt;ref name=skrien179/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Money Implementations in Practice==&lt;br /&gt;
In practice, many decide similarly to Skrien to encapsulate Money as a class.  The choice of underlying representation does vary, though, between types like int, double, or BigDecimal.  I did not find any sources that used floats or two integers, which Skrien rejected. &amp;lt;ref name=&amp;quot;dobbs&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;Representing Money&amp;quot;&amp;gt;http://www.javapractices.com/topic/TopicAction.do?Id=13&amp;lt;/ref&amp;gt; &amp;lt;ref name=&amp;quot;C++&amp;quot;&amp;gt;http://www.di-mare.com/adolfo/p/money.htm&amp;lt;/ref&amp;gt;  I found one source that addressed the issue of mixed money, which implemented a MoneyBag class that holds an array of Money. &amp;lt;ref name=&amp;quot;testing&amp;quot;&amp;gt;http://junit.sourceforge.net/doc/testinfected/testing.htm&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are also existing projects which seek to provide programmers with a library of classes to use for Money such as [http://timeandmoney.sourceforge.net/ Time and Money] and [http://joda-money.sourceforge.net/ Joda-Money].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56498</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a or</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56498"/>
		<updated>2011-12-03T03:25:44Z</updated>

		<summary type="html">&lt;p&gt;Opevans: /* Currency Conversion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Wiki Chapter: CSC/ECE 517 Fall 2011/ch7 7a or&lt;br /&gt;
&lt;br /&gt;
''7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
Money is very important to people worldwide and is something that often must be represented in computer programs.  This raises the question of how money should be represented, which can vary from program to program.  There is much to be considered such as string formatting, the way money is used, and the variety of different currencies in the world.  In Chapter 6 of Skrien's ''Object-Oriented Design Using Java'', he suggests an implementation that represents money as a class, but there are other approaches that can be taken, each with their respective advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Properties of Money==&lt;br /&gt;
Money has certain properties and uses that must be considered in implementation.&lt;br /&gt;
&lt;br /&gt;
===Responsibilities/Using Money===&lt;br /&gt;
The following are ways in which a representation of money should be able to be used: &amp;lt;ref name=&amp;quot;lec20&amp;quot;&amp;gt;http://courses.ncsu.edu/csc517/common/lectures/notes/lec20.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
*To represent a positive or negative amount of money.&lt;br /&gt;
*Possibly representing multiple components of a currency - i.e. dollars and cents.&lt;br /&gt;
*Possibly representing amounts of multiple currencies - i.e. a mix of US Dollars, euros, and yen.'&lt;br /&gt;
*To have a string formatting potentially including currency symbols like the dollar sign ($) and/or commas and/or decimal points.&lt;br /&gt;
*Addition, subtraction, multiplication, and division.&lt;br /&gt;
The degree to which of the above must be met will depend on the depth of the implementing project.&lt;br /&gt;
&lt;br /&gt;
===Different Currencies===&lt;br /&gt;
Some applications may only handle only one currency such as the US Dollar, but more generally applied applications, such as banking, must be able to handle different kinds of currencies.&lt;br /&gt;
&lt;br /&gt;
There are currently over 180 currencies in use around the world.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/List_of_circulating_currencies&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Implementation must be aware of how to specifically represent each currency used.  Some currencies use decimal points, such as how the US has 100 cents to a dollar, while others have no decimal, such as yen.  Each currency also has a different sign symbol used such as $, €, or ¥.  Different currencies also have different rules about how rounding values is handled.&lt;br /&gt;
&lt;br /&gt;
Special consideration must be taken when using different currencies in conjunction with each other.  Foreign exchange rates can be used to convert between different currencies, but the exchange rate often change, so attention must be paid to that.  In some cases, it is also important to keep track of how much money the user has of more than one currency at a time, so that must be handled somehow. &amp;lt;ref&amp;gt;http://www.xencraft.com/resources/multi-currency.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Immutable vs. Mutable===&lt;br /&gt;
Money values can be either mutable, meaning changeable, or immutable, meaning unchanging.  Mutable money values are useful in that a new value does not need to be created for every change.  However, mutable values cause problems for use in hash maps or with concurrency.  The issue with concurrency is a read and write issue in a system with multiple users.  With changing values, money not be what the user expects and lead to unpredictable behavior.  Skrien feels this is a significant enough disadvantage to prefer an immutable representation for money.&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Possible Representations==&lt;br /&gt;
There are several possible ways to represent money in code.&lt;br /&gt;
&lt;br /&gt;
===Floating Point Number===&lt;br /&gt;
Floating point numbers seem like an appealing option since they already include a fractional component, which lends itself straightforward to currencies like the US dollar which also have a fractional component.  However, floating point numbers come with a drawback due to the way floats are formatted.  Since floats are based on binary representation rather than a decimal representation, a value that would be exact in decimal notation can only be stored approximately in a floats point number.  For instance, 9.48 is stored as 9.479999542236328125.  Performing calculations with these approximated values can lead to inaccurate results. &amp;lt;ref name=&amp;quot;dobbs&amp;quot;&amp;gt;http://drdobbs.com/java/184405653&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another deficiency of floating point numbers is that they lose precision as the number gets larger. This is less of a problem when dealing with small amounts of currency, but when dealing with large amounts, on the order of the United States federal debt, the number of significant figures to the right of the decimal point decreases, again leading to possibly inaccurate results.&amp;lt;ref&amp;gt;Skrien, Dale. ''Object-Oriented Design Using Java''. McGraw Hill, 2009, p. 174.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Two Integers===&lt;br /&gt;
For currencies with a fractional component, using two integers to represent one amount of money is an option.  One integer to represent the main component and one to represent the fractional part.  For US currency, this would mean an integer for dollars and an integer for cents.  This representation makes displaying monetary values simple, as each component is already split out and accurate.  Also, by using integers, calculations with money work out as they should, avoiding the loss of accuracy floating points have.  However, code will need to be implemented to handle when the fraction component carries over.  In the case of dollars and cents, if the cents exceed 100, the cents value should be decreased by 100 and the dollars increased by 1.  This handling of carries adds overhead to calculations.  Some drawbacks of this implementation are the added overhead and storage required&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currencty. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref&amp;gt;http://www.infoplease.com/spot/stockdecimal1.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===One Integer, Implied Decimal Point===&lt;br /&gt;
One integer can be used to represent money, even including the fractional component. The location of the decimal place is simply implied based on the currency.  For instance, a single integer representation of $12.34 would be 1234. When dealing with US currency, this exploits the fact that the fundamental unit of currency in the US is actually the cent, not the dollar &amp;lt;ref name=&amp;quot;dobbs&amp;quot;/&amp;gt;. A specialized print method would have to be constructed to display the monetary amount properly to the user, but all calculations work out properly.  This approach has the same advantage that using two integers of maintaining accuracy, but does not have the added overhead or storage.  In ''Object-Oriented Design Using Java'', Skrien considers this to be the best use of the primitive types for representing money.&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;. This method shares the same deficiency of two integers in that it cannot represent fractions of a fundamental unit of money.&lt;br /&gt;
&lt;br /&gt;
===BigDecimal===&lt;br /&gt;
Java offers a [http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html BigDecimal] class, which many people use for representing money.  BigDecimal is capable of storing any size number since it stores each digit as an entry in an array, which grows as necessary.  It also allows for specification of both the rounding method and the scale, the number of digits past the decimal place, for proper monetary calculations. &amp;lt;ref&amp;gt;http://www.javaranch.com/journal/2003/07/MoneyInJava.html&amp;lt;/ref&amp;gt;  On the downside, this specification for rounding and scale is required, which adds code to the implementation&amp;lt;ref name=&amp;quot;dobbs&amp;quot; /&amp;gt;. BigDecimal values are also immutable, which avoids read/write and write/write conflicts in applications. &amp;lt;ref&amp;gt;http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial&amp;lt;/ref&amp;gt;  While there are a lot of nice things about BigDecimal, there are some more issues with BigDecimal which can cause problems in representing money.  BigDecimal cannot represent numbers with infinite digits such as 1/9 (.11111...) and throws an ArithmeticException on any calculation that would produce such number.  To deal with this, hacks such as the following must be implemented:&lt;br /&gt;
 BigDecimal bd1 = BigDecimal(1);&lt;br /&gt;
 BigDecimal bd2 = BigDecimal(9);&lt;br /&gt;
 try {&lt;br /&gt;
     return bd1.divide(bd2);&lt;br /&gt;
 } catch(ArithmaticException ae) {&lt;br /&gt;
     return new BigDecimal(bd1.doubleValue() / bd2.doubleValue());&lt;br /&gt;
 }&lt;br /&gt;
Also, since there isn't a BigDecimal equivalent for SQL databases, so storing BigDecimal values either requires conversion to a different format, which can affect accuracy and/or precision, or the value must be stored as a BLOB, which is cumbersome and inefficient.  In general use, calculations with BigDecimal are significantly slower than with integers, and also take up more storage space. &amp;lt;ref&amp;gt;http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Class===&lt;br /&gt;
In ''Object-Oriented Design Using Java'', recommends using a class to represent money, rather than just using a long integer alone.  He gives two reasons for using a class.  The first is encapsulation - by encapsulating money into a class, all the operations specific to working with money can be implemented to work as desired in all cases, which also avoids code duplication.  The second reason is that, in compiled languages like Java, the compiler is better able to detect coding errors, such as adding money to something that isn't money. &amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt; Implementing Money as a class is a generally good idea, but there are multiple ways to go about it.&lt;br /&gt;
&lt;br /&gt;
====Abstract Class====&lt;br /&gt;
If multiple kinds of currencies are needed to be represented, one implementation is to create an abstract Money class and have each possible currency be a subclass of the abstract class.  Since there are over 180 different currencies worldwide, it can be a challenge to implement subclasses for each one required.&lt;br /&gt;
&lt;br /&gt;
The following is the abstract class proposed by Skrien in Object-Oriented Design Using Java:&lt;br /&gt;
 public abstract class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public long getAmount()&lt;br /&gt;
   public String toString()&lt;br /&gt;
   public int compareTo(Money m)&lt;br /&gt;
   public boolean equals(Object o)&lt;br /&gt;
   public int hashCode()&lt;br /&gt;
   public Money plus(Money)&lt;br /&gt;
   public Money minus(Money)&lt;br /&gt;
   public Money times(double factor)&lt;br /&gt;
   public Money dividedBy(double divisor)&lt;br /&gt;
   public Money negate()&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;&amp;gt;Skrien, Dale. Object-Oriented Design Using Java. McGraw Hill, 2009, p. 179-183.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Single Class====&lt;br /&gt;
Rather than implementing a variety of subclasses for each currency, a single class can be used to represent money, with currency specified within.  This is a good idea, since most modern currencies are decimal and as such can be calculated and treated in the same way. The only differences are in decimal locations and the symbols used to present them.  Java has a [http://docs.oracle.com/javase/6/docs/api/java/util/Currency.html Currency class] which holds the information for a given currency.&lt;br /&gt;
&lt;br /&gt;
This method is insufficient for representing a non-decimal currency, such as pre-1971 British currency (240 pence per pound sterling.) &lt;br /&gt;
The following is the Single Class implementation given by Skrien in ''Object-Oriented Design Using Java'':&lt;br /&gt;
&lt;br /&gt;
 public class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   private long amount;&lt;br /&gt;
   private Currency currency;&lt;br /&gt;
   public Money(long amount, Currency currency) {&lt;br /&gt;
     this.amount = amount;&lt;br /&gt;
     this.currency = currency;&lt;br /&gt;
   }&lt;br /&gt;
   public long getAmount() { return amount; }&lt;br /&gt;
   public Currency getCurrency() { &lt;br /&gt;
     return currency; }&lt;br /&gt;
   public String toString() { ...above... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Mixed Money====&lt;br /&gt;
When dealing with multiple currencies it might be necessary to keep track of money consisting of multiple currencies.  Simply converting into one common currency is not a good approach, since exchange rates vary over time, so the amount of each currency should be kept track of.  Skrien suggests using a MixedMoney class, which acts as a sort of wallet holding amounts of different currencies.&lt;br /&gt;
&lt;br /&gt;
The following is Skrien's implementation of MixedMoney:&lt;br /&gt;
 public class MixedMoney implements Money {&lt;br /&gt;
   public MixedMoney() { ... }&lt;br /&gt;
   public String toString() { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
   public Collection getCurrencies() { ... }&lt;br /&gt;
   public long getAmount(Currency currency) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since it uses a lot of the same methods as the regular Money class, Skrien renamed Money to SimpleMoney and created a common Money interface that is as follows:&lt;br /&gt;
&lt;br /&gt;
 public interface Money extends Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public int compareTo(Money o);&lt;br /&gt;
   public Money plus(Money money);&lt;br /&gt;
   public Money minus(Money money);&lt;br /&gt;
   public Money times(double factor);&lt;br /&gt;
   public Money dividedBy(double divisor);&lt;br /&gt;
   public Money negate();&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The money within MixedMoney is suggested to be represented by either a hash map, a collection, or a tree structure.  The hash map implementation is rejected for storage and calculation time issues.  The tree structure is deemed the best approach as it is elegant and avoids checking classes.&lt;br /&gt;
&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Currency Conversion==&lt;br /&gt;
When dealing with multiple currencies, it is often necessary to convert from one to another.&lt;br /&gt;
Therefore some means for doing so must be implemented in any program that contains a representation of money. The best way to implement this is to create a member function of the money class that multiplies the amount by a conversion factor and returns a new money object denominated in the target currency. &amp;lt;ref name=skrien179&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Money Implementations in Practice==&lt;br /&gt;
In practice, many decide similarly to Skrien to encapsulate Money as a class.  The choice of underlying representation does vary, though, between types like int, double, or BigDecimal.  I did not find any sources that used floats or two integers, which Skrien rejected. &amp;lt;ref name=&amp;quot;dobbs&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;Representing Money&amp;quot;&amp;gt;http://www.javapractices.com/topic/TopicAction.do?Id=13&amp;lt;/ref&amp;gt; &amp;lt;ref name=&amp;quot;C++&amp;quot;&amp;gt;http://www.di-mare.com/adolfo/p/money.htm&amp;lt;/ref&amp;gt;  I found one source that addressed the issue of mixed money, which implemented a MoneyBag class that holds an array of Money. &amp;lt;ref name=&amp;quot;testing&amp;quot;&amp;gt;http://junit.sourceforge.net/doc/testinfected/testing.htm&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are also existing projects which seek to provide programmers with a library of classes to use for Money such as [http://timeandmoney.sourceforge.net/ Time and Money] and [http://joda-money.sourceforge.net/ Joda-Money].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56463</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a or</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56463"/>
		<updated>2011-12-02T05:03:48Z</updated>

		<summary type="html">&lt;p&gt;Opevans: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Wiki Chapter: CSC/ECE 517 Fall 2011/ch7 7a or&lt;br /&gt;
&lt;br /&gt;
''7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
Money is very important to people worldwide and is something that often must be represented in computer programs.  This raises the question of how money should be represented, which can vary from program to program.  There is much to be considered such as string formatting, the way money is used, and the variety of different currencies in the world.  In Chapter 6 of Skrien's ''Object-Oriented Design Using Java'', he suggests an implementation that represents money as a class, but there are other approaches that can be taken, each with their respective advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Properties of Money==&lt;br /&gt;
Money has certain properties and uses that must be considered in implementation.&lt;br /&gt;
&lt;br /&gt;
===Responsibilities/Using Money===&lt;br /&gt;
The following are ways in which a representation of money should be able to be used: &amp;lt;ref name=&amp;quot;lec20&amp;quot;&amp;gt;http://courses.ncsu.edu/csc517/common/lectures/notes/lec20.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
*To represent a positive or negative amount of money.&lt;br /&gt;
*Possibly representing multiple components of a currency - i.e. dollars and cents.&lt;br /&gt;
*Possibly representing amounts of multiple currencies - i.e. a mix of US Dollars, euros, and yen.'&lt;br /&gt;
*To have a string formatting potentially including currency symbols like the dollar sign ($) and/or commas and/or decimal points.&lt;br /&gt;
*Addition, subtraction, multiplication, and division.&lt;br /&gt;
The degree to which of the above must be met will depend on the depth of the implementing project.&lt;br /&gt;
&lt;br /&gt;
===Different Currencies===&lt;br /&gt;
Some applications may only handle only one currency such as the US Dollar, but more generally applied applications, such as banking, must be able to handle different kinds of currencies.&lt;br /&gt;
&lt;br /&gt;
There are currently over 180 currencies in use around the world.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/List_of_circulating_currencies&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Implementation must be aware of how to specifically represent each currency used.  Some currencies use decimal points, such as how the US has 100 cents to a dollar, while others have no decimal, such as yen.  Each currency also has a different sign symbol used such as $, €, or ¥.  Different currencies also have different rules about how rounding values is handled.&lt;br /&gt;
&lt;br /&gt;
Special consideration must be taken when using different currencies in conjunction with each other.  Foreign exchange rates can be used to convert between different currencies, but the exchange rate often change, so attention must be paid to that.  In some cases, it is also important to keep track of how much money the user has of more than one currency at a time, so that must be handled somehow. &amp;lt;ref&amp;gt;http://www.xencraft.com/resources/multi-currency.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Immutable vs. Mutable===&lt;br /&gt;
Money values can be either mutable, meaning changeable, or immutable, meaning unchanging.  Mutable money values are useful in that a new value does not need to be created for every change.  However, mutable values cause problems for use in hash maps or with concurrency.  The issue with concurrency is a read and write issue in a system with multiple users.  With changing values, money not be what the user expects and lead to unpredictable behavior.  Skrien feels this is a significant enough disadvantage to prefer an immutable representation for money.&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Possible Representations==&lt;br /&gt;
There are several possible ways to represent money in code.&lt;br /&gt;
&lt;br /&gt;
===Floating Point Number===&lt;br /&gt;
Floating point numbers seem like an appealing option since they already include a fractional component, which lends itself straightforward to currencies like the US dollar which also have a fractional component.  However, floating point numbers come with a drawback due to the way floats are formatted.  Since floats are based on binary representation rather than a decimal representation, a value that would be exact in decimal notation can only be stored approximately in a floats point number.  For instance, 9.48 is stored as 9.479999542236328125.  Performing calculations with these approximated values can lead to inaccurate results. &amp;lt;ref name=&amp;quot;dobbs&amp;quot;&amp;gt;http://drdobbs.com/java/184405653&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another deficiency of floating point numbers is that they lose precision as the number gets larger. This is less of a problem when dealing with small amounts of currency, but when dealing with large amounts, on the order of the United States federal debt, the number of significant figures to the right of the decimal point decreases, again leading to possibly inaccurate results.&amp;lt;ref&amp;gt;Skrien, Dale. ''Object-Oriented Design Using Java''. McGraw Hill, 2009, p. 174.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Two Integers===&lt;br /&gt;
For currencies with a fractional component, using two integers to represent one amount of money is an option.  One integer to represent the main component and one to represent the fractional part.  For US currency, this would mean an integer for dollars and an integer for cents.  This representation makes displaying monetary values simple, as each component is already split out and accurate.  Also, by using integers, calculations with money work out as they should, avoiding the loss of accuracy floating points have.  However, code will need to be implemented to handle when the fraction component carries over.  In the case of dollars and cents, if the cents exceed 100, the cents value should be decreased by 100 and the dollars increased by 1.  This handling of carries adds overhead to calculations.  Some drawbacks of this implementation are the added overhead and storage required&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currencty. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref&amp;gt;http://www.infoplease.com/spot/stockdecimal1.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===One Integer, Implied Decimal Point===&lt;br /&gt;
One integer can be used to represent money, even including the fractional component. The location of the decimal place is simply implied based on the currency.  For instance, a single integer representation of $12.34 would be 1234. When dealing with US currency, this exploits the fact that the fundamental unit of currency in the US is actually the cent, not the dollar &amp;lt;ref name=&amp;quot;dobbs&amp;quot;/&amp;gt;. A specialized print method would have to be constructed to display the monetary amount properly to the user, but all calculations work out properly.  This approach has the same advantage that using two integers of maintaining accuracy, but does not have the added overhead or storage.  In ''Object-Oriented Design Using Java'', Skrien considers this to be the best use of the primitive types for representing money.&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;. This method shares the same deficiency of two integers in that it cannot represent fractions of a fundamental unit of money.&lt;br /&gt;
&lt;br /&gt;
===BigDecimal===&lt;br /&gt;
Java offers a [http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html BigDecimal] class, which many people use for representing money.  BigDecimal is capable of storing any size number since it stores each digit as an entry in an array, which grows as necessary.  It also allows for specification of both the rounding method and the scale, the number of digits past the decimal place, for proper monetary calculations. &amp;lt;ref&amp;gt;http://www.javaranch.com/journal/2003/07/MoneyInJava.html&amp;lt;/ref&amp;gt;  On the downside, this specification for rounding and scale is required, which adds code to the implementation&amp;lt;ref name=&amp;quot;dobbs&amp;quot; /&amp;gt;. BigDecimal values are also immutable, which avoids read/write and write/write conflicts in applications. &amp;lt;ref&amp;gt;http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial&amp;lt;/ref&amp;gt;  While there are a lot of nice things about BigDecimal, there are some more issues with BigDecimal which can cause problems in representing money.  BigDecimal cannot represent numbers with infinite digits such as 1/9 (.11111...) and throws an ArithmeticException on any calculation that would produce such number.  To deal with this, hacks such as the following must be implemented:&lt;br /&gt;
 BigDecimal bd1 = BigDecimal(1);&lt;br /&gt;
 BigDecimal bd2 = BigDecimal(9);&lt;br /&gt;
 try {&lt;br /&gt;
     return bd1.divide(bd2);&lt;br /&gt;
 } catch(ArithmaticException ae) {&lt;br /&gt;
     return new BigDecimal(bd1.doubleValue() / bd2.doubleValue());&lt;br /&gt;
 }&lt;br /&gt;
Also, since there isn't a BigDecimal equivalent for SQL databases, so storing BigDecimal values either requires conversion to a different format, which can affect accuracy and/or precision, or the value must be stored as a BLOB, which is cumbersome and inefficient.  In general use, calculations with BigDecimal are significantly slower than with integers, and also take up more storage space. &amp;lt;ref&amp;gt;http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Class===&lt;br /&gt;
In ''Object-Oriented Design Using Java'', recommends using a class to represent money, rather than just using a long integer alone.  He gives two reasons for using a class.  The first is encapsulation - by encapsulating money into a class, all the operations specific to working with money can be implemented to work as desired in all cases, which also avoids code duplication.  The second reason is that, in compiled languages like Java, the compiler is better able to detect coding errors, such as adding money to something that isn't money. &amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt; Implementing Money as a class is a generally good idea, but there are multiple ways to go about it.&lt;br /&gt;
&lt;br /&gt;
====Abstract Class====&lt;br /&gt;
If multiple kinds of currencies are needed to be represented, one implementation is to create an abstract Money class and have each possible currency be a subclass of the abstract class.  Since there are over 180 different currencies worldwide, it can be a challenge to implement subclasses for each one required.&lt;br /&gt;
&lt;br /&gt;
The following is the abstract class proposed by Skrien in Object-Oriented Design Using Java:&lt;br /&gt;
 public abstract class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public long getAmount()&lt;br /&gt;
   public String toString()&lt;br /&gt;
   public int compareTo(Money m)&lt;br /&gt;
   public boolean equals(Object o)&lt;br /&gt;
   public int hashCode()&lt;br /&gt;
   public Money plus(Money)&lt;br /&gt;
   public Money minus(Money)&lt;br /&gt;
   public Money times(double factor)&lt;br /&gt;
   public Money dividedBy(double divisor)&lt;br /&gt;
   public Money negate()&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;&amp;gt;Skrien, Dale. Object-Oriented Design Using Java. McGraw Hill, 2009, p. 179-183.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Single Class====&lt;br /&gt;
Rather than implementing a variety of subclasses for each currency, a single class can be used to represent money, with currency specified within.  This is a good idea, since most modern currencies are decimal and as such can be calculated and treated in the same way. The only differences are in decimal locations and the symbols used to present them.  Java has a [http://docs.oracle.com/javase/6/docs/api/java/util/Currency.html Currency class] which holds the information for a given currency.&lt;br /&gt;
&lt;br /&gt;
This method is insufficient for representing a non-decimal currency, such as pre-1971 British currency (240 pence per pound sterling.) &lt;br /&gt;
The following is the Single Class implementation given by Skrien in ''Object-Oriented Design Using Java'':&lt;br /&gt;
&lt;br /&gt;
 public class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   private long amount;&lt;br /&gt;
   private Currency currency;&lt;br /&gt;
   public Money(long amount, Currency currency) {&lt;br /&gt;
     this.amount = amount;&lt;br /&gt;
     this.currency = currency;&lt;br /&gt;
   }&lt;br /&gt;
   public long getAmount() { return amount; }&lt;br /&gt;
   public Currency getCurrency() { &lt;br /&gt;
     return currency; }&lt;br /&gt;
   public String toString() { ...above... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Mixed Money====&lt;br /&gt;
When dealing with multiple currencies it might be necessary to keep track of money consisting of multiple currencies.  Simply converting into one common currency is not a good approach, since exchange rates vary over time, so the amount of each currency should be kept track of.  Skrien suggests using a MixedMoney class, which acts as a sort of wallet holding amounts of different currencies.&lt;br /&gt;
&lt;br /&gt;
The following is Skrien's implementation of MixedMoney:&lt;br /&gt;
 public class MixedMoney implements Money {&lt;br /&gt;
   public MixedMoney() { ... }&lt;br /&gt;
   public String toString() { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
   public Collection getCurrencies() { ... }&lt;br /&gt;
   public long getAmount(Currency currency) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since it uses a lot of the same methods as the regular Money class, Skrien renamed Money to SimpleMoney and created a common Money interface that is as follows:&lt;br /&gt;
&lt;br /&gt;
 public interface Money extends Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public int compareTo(Money o);&lt;br /&gt;
   public Money plus(Money money);&lt;br /&gt;
   public Money minus(Money money);&lt;br /&gt;
   public Money times(double factor);&lt;br /&gt;
   public Money dividedBy(double divisor);&lt;br /&gt;
   public Money negate();&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The money within MixedMoney is suggested to be represented by either a hash map, a collection, or a tree structure.  The hash map implementation is rejected for storage and calculation time issues.  The tree structure is deemed the best approach as it is elegant and avoids checking classes.&lt;br /&gt;
&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Currency Conversion==&lt;br /&gt;
Owen to insert useful section here by 12/2&lt;br /&gt;
&lt;br /&gt;
==Money Implementations in Practice==&lt;br /&gt;
In practice, many decide similarly to Skrien to encapsulate Money as a class.  The choice of underlying representation does vary, though, between types like int, double, or BigDecimal.  I did not find any sources that used floats or two integers, which Skrien rejected. &amp;lt;ref name=&amp;quot;dobbs&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;Representing Money&amp;quot;&amp;gt;http://www.javapractices.com/topic/TopicAction.do?Id=13&amp;lt;/ref&amp;gt; &amp;lt;ref name=&amp;quot;C++&amp;quot;&amp;gt;http://www.di-mare.com/adolfo/p/money.htm&amp;lt;/ref&amp;gt;  I found one source that addressed the issue of mixed money, which implemented a MoneyBag class that holds an array of Money. &amp;lt;ref name=&amp;quot;testing&amp;quot;&amp;gt;http://junit.sourceforge.net/doc/testinfected/testing.htm&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are also existing projects which seek to provide programmers with a library of classes to use for Money such as [http://timeandmoney.sourceforge.net/ Time and Money] and [http://joda-money.sourceforge.net/ Joda-Money].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56461</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a or</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56461"/>
		<updated>2011-12-02T04:47:24Z</updated>

		<summary type="html">&lt;p&gt;Opevans: /* Single Class */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Wiki Chapter: CSC/ECE 517 Fall 2011/ch7 7a or&lt;br /&gt;
&lt;br /&gt;
''7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
Money is very important to people worldwide and is something that often must be represented in computer programs.  This raises the question of how money should be represented, which can vary from program to program.  There is much to be considered such as string formatting, the way money is used, and the variety of different currencies in the world.  In Chapter 6 of Skrien's ''Object-Oriented Design Using Java'', he suggests an implementation that represents money as a class, but there are other approaches that can be taken, each with their respective advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Properties of Money==&lt;br /&gt;
Money has certain properties and uses that must be considered in implementation.&lt;br /&gt;
&lt;br /&gt;
===Responsibilities/Using Money===&lt;br /&gt;
The following are ways in which a representation of money should be able to be used: &amp;lt;ref name=&amp;quot;lec20&amp;quot;&amp;gt;http://courses.ncsu.edu/csc517/common/lectures/notes/lec20.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
*To represent a positive or negative amount of money.&lt;br /&gt;
*Possibly representing multiple components of a currency - i.e. dollars and cents.&lt;br /&gt;
*Possibly representing amounts of multiple currencies - i.e. a mix of US Dollars, euros, and yen.'&lt;br /&gt;
*To have a string formatting potentially including currency symbols like the dollar sign ($) and/or commas and/or decimal points.&lt;br /&gt;
*Addition, subtraction, multiplication, and division.&lt;br /&gt;
The degree to which of the above must be met will depend on the depth of the implementing project.&lt;br /&gt;
&lt;br /&gt;
===Different Currencies===&lt;br /&gt;
Some applications may only handle only one currency such as the US Dollar, but more generally applied applications, such as banking, must be able to handle different kinds of currencies.&lt;br /&gt;
&lt;br /&gt;
There are currently over 180 currencies in use around the world.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/List_of_circulating_currencies&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Implementation must be aware of how to specifically represent each currency used.  Some currencies use decimal points, such as how the US has 100 cents to a dollar, while others have no decimal, such as yen.  Each currency also has a different sign symbol used such as $, €, or ¥.  Different currencies also have different rules about how rounding values is handled.&lt;br /&gt;
&lt;br /&gt;
Special consideration must be taken when using different currencies in conjunction with each other.  Foreign exchange rates can be used to convert between different currencies, but the exchange rate often change, so attention must be paid to that.  In some cases, it is also important to keep track of how much money the user has of more than one currency at a time, so that must be handled somehow. &amp;lt;ref&amp;gt;http://www.xencraft.com/resources/multi-currency.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Immutable vs. Mutable===&lt;br /&gt;
Money values can be either mutable, meaning changeable, or immutable, meaning unchanging.  Mutable money values are useful in that a new value does not need to be created for every change.  However, mutable values cause problems for use in hash maps or with concurrency.  The issue with concurrency is a read and write issue in a system with multiple users.  With changing values, money not be what the user expects and lead to unpredictable behavior.  Skrien feels this is a significant enough disadvantage to prefer an immutable representation for money.&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Possible Representations==&lt;br /&gt;
There are several possible ways to represent money in code.&lt;br /&gt;
&lt;br /&gt;
===Floating Point Number===&lt;br /&gt;
Floating point numbers seem like an appealing option since they already include a fractional component, which lends itself straightforward to currencies like the US dollar which also have a fractional component.  However, floating point numbers come with a drawback due to the way floats are formatted.  Since floats are based on binary representation rather than a decimal representation, a value that would be exact in decimal notation can only be stored approximately in a floats point number.  For instance, 9.48 is stored as 9.479999542236328125.  Performing calculations with these approximated values can lead to inaccurate results. &amp;lt;ref name=&amp;quot;dobbs&amp;quot;&amp;gt;http://drdobbs.com/java/184405653&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another deficiency of floating point numbers is that they lose precision as the number gets larger. This is less of a problem when dealing with small amounts of currency, but when dealing with large amounts, on the order of the United States federal debt, the number of significant figures to the right of the decimal point decreases, again leading to possibly inaccurate results.&amp;lt;ref&amp;gt;Skrien, Dale. ''Object-Oriented Design Using Java''. McGraw Hill, 2009, p. 174.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Two Integers===&lt;br /&gt;
For currencies with a fractional component, using two integers to represent one amount of money is an option.  One integer to represent the main component and one to represent the fractional part.  For US currency, this would mean an integer for dollars and an integer for cents.  This representation makes displaying monetary values simple, as each component is already split out and accurate.  Also, by using integers, calculations with money work out as they should, avoiding the loss of accuracy floating points have.  However, code will need to be implemented to handle when the fraction component carries over.  In the case of dollars and cents, if the cents exceed 100, the cents value should be decreased by 100 and the dollars increased by 1.  This handling of carries adds overhead to calculations.  Some drawbacks of this implementation are the added overhead and storage required&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currencty. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref&amp;gt;http://www.infoplease.com/spot/stockdecimal1.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===One Integer, Implied Decimal Point===&lt;br /&gt;
One integer can be used to represent money, even including the fractional component. The location of the decimal place is simply implied based on the currency.  For instance, a single integer representation of $12.34 would be 1234. When dealing with US currency, this exploits the fact that the fundamental unit of currency in the US is actually the cent, not the dollar &amp;lt;ref name=&amp;quot;dobbs&amp;quot;/&amp;gt;. A specialized print method would have to be constructed to display the monetary amount properly to the user, but all calculations work out properly.  This approach has the same advantage that using two integers of maintaining accuracy, but does not have the added overhead or storage.  In ''Object-Oriented Design Using Java'', Skrien considers this to be the best use of the primitive types for representing money.&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;. This method shares the same deficiency of two integers in that it cannot represent fractions of a fundamental unit of money.&lt;br /&gt;
&lt;br /&gt;
===BigDecimal===&lt;br /&gt;
Java offers a [http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html BigDecimal] class, which many people use for representing money.  BigDecimal is capable of storing any size number since it stores each digit as an entry in an array, which grows as necessary.  It also allows for specification of both the rounding method and the scale, the number of digits past the decimal place, for proper monetary calculations. &amp;lt;ref&amp;gt;http://www.javaranch.com/journal/2003/07/MoneyInJava.html&amp;lt;/ref&amp;gt;  On the downside, this specification for rounding and scale is required, which adds code to the implementation&amp;lt;ref name=&amp;quot;dobbs&amp;quot; /&amp;gt;. BigDecimal values are also immutable, which avoids read/write and write/write conflicts in applications. &amp;lt;ref&amp;gt;http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial&amp;lt;/ref&amp;gt;  While there are a lot of nice things about BigDecimal, there are some more issues with BigDecimal which can cause problems in representing money.  BigDecimal cannot represent numbers with infinite digits such as 1/9 (.11111...) and throws an ArithmeticException on any calculation that would produce such number.  To deal with this, hacks such as the following must be implemented:&lt;br /&gt;
 BigDecimal bd1 = BigDecimal(1);&lt;br /&gt;
 BigDecimal bd2 = BigDecimal(9);&lt;br /&gt;
 try {&lt;br /&gt;
     return bd1.divide(bd2);&lt;br /&gt;
 } catch(ArithmaticException ae) {&lt;br /&gt;
     return new BigDecimal(bd1.doubleValue() / bd2.doubleValue());&lt;br /&gt;
 }&lt;br /&gt;
Also, since there isn't a BigDecimal equivalent for SQL databases, so storing BigDecimal values either requires conversion to a different format, which can affect accuracy and/or precision, or the value must be stored as a BLOB, which is cumbersome and inefficient.  In general use, calculations with BigDecimal are significantly slower than with integers, and also take up more storage space. &amp;lt;ref&amp;gt;http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Class===&lt;br /&gt;
In ''Object-Oriented Design Using Java'', recommends using a class to represent money, rather than just using a long integer alone.  He gives two reasons for using a class.  The first is encapsulation - by encapsulating money into a class, all the operations specific to working with money can be implemented to work as desired in all cases, which also avoids code duplication.  The second reason is that, in compiled languages like Java, the compiler is better able to detect coding errors, such as adding money to something that isn't money. &amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt; Implementing Money as a class is a generally good idea, but there are multiple ways to go about it.&lt;br /&gt;
&lt;br /&gt;
====Abstract Class====&lt;br /&gt;
If multiple kinds of currencies are needed to be represented, one implementation is to create an abstract Money class and have each possible currency be a subclass of the abstract class.  Since there are over 180 different currencies worldwide, it can be a challenge to implement subclasses for each one required.&lt;br /&gt;
&lt;br /&gt;
The following is the abstract class proposed by Skrien in Object-Oriented Design Using Java:&lt;br /&gt;
 public abstract class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public long getAmount()&lt;br /&gt;
   public String toString()&lt;br /&gt;
   public int compareTo(Money m)&lt;br /&gt;
   public boolean equals(Object o)&lt;br /&gt;
   public int hashCode()&lt;br /&gt;
   public Money plus(Money)&lt;br /&gt;
   public Money minus(Money)&lt;br /&gt;
   public Money times(double factor)&lt;br /&gt;
   public Money dividedBy(double divisor)&lt;br /&gt;
   public Money negate()&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;&amp;gt;Skrien, Dale. Object-Oriented Design Using Java. McGraw Hill, 2009, p. 179-183.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Single Class====&lt;br /&gt;
Rather than implementing a variety of subclasses for each currency, a single class can be used to represent money, with currency specified within.  This is a good idea, since most modern currencies are decimal and as such can be calculated and treated in the same way. The only differences are in decimal locations and the symbols used to present them.  Java has a [http://docs.oracle.com/javase/6/docs/api/java/util/Currency.html Currency class] which holds the information for a given currency.&lt;br /&gt;
&lt;br /&gt;
This method is insufficient for representing a non-decimal currency, such as pre-1971 British currency (240 pence per pound sterling.) &lt;br /&gt;
The following is the Single Class implementation given by Skrien in ''Object-Oriented Design Using Java'':&lt;br /&gt;
&lt;br /&gt;
 public class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   private long amount;&lt;br /&gt;
   private Currency currency;&lt;br /&gt;
   public Money(long amount, Currency currency) {&lt;br /&gt;
     this.amount = amount;&lt;br /&gt;
     this.currency = currency;&lt;br /&gt;
   }&lt;br /&gt;
   public long getAmount() { return amount; }&lt;br /&gt;
   public Currency getCurrency() { &lt;br /&gt;
     return currency; }&lt;br /&gt;
   public String toString() { ...above... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Mixed Money====&lt;br /&gt;
When dealing with multiple currencies it might be necessary to keep track of money consisting of multiple currencies.  Simply converting into one common currency is not a good approach, since exchange rates vary over time, so the amount of each currency should be kept track of.  Skrien suggests using a MixedMoney class, which acts as a sort of wallet holding amounts of different currencies.&lt;br /&gt;
&lt;br /&gt;
The following is Skrien's implementation of MixedMoney:&lt;br /&gt;
 public class MixedMoney implements Money {&lt;br /&gt;
   public MixedMoney() { ... }&lt;br /&gt;
   public String toString() { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
   public Collection getCurrencies() { ... }&lt;br /&gt;
   public long getAmount(Currency currency) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since it uses a lot of the same methods as the regular Money class, Skrien renamed Money to SimpleMoney and created a common Money interface that is as follows:&lt;br /&gt;
&lt;br /&gt;
 public interface Money extends Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public int compareTo(Money o);&lt;br /&gt;
   public Money plus(Money money);&lt;br /&gt;
   public Money minus(Money money);&lt;br /&gt;
   public Money times(double factor);&lt;br /&gt;
   public Money dividedBy(double divisor);&lt;br /&gt;
   public Money negate();&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The money within MixedMoney is suggested to be represented by either a hash map, a collection, or a tree structure.  The hash map implementation is rejected for storage and calculation time issues.  The tree structure is deemed the best approach as it is elegant and avoids checking classes.&lt;br /&gt;
&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Money Implementations in Practice==&lt;br /&gt;
In practice, many decide similarly to Skrien to encapsulate Money as a class.  The choice of underlying representation does vary, though, between types like int, double, or BigDecimal.  I did not find any sources that used floats or two integers, which Skrien rejected. &amp;lt;ref name=&amp;quot;dobbs&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;Representing Money&amp;quot;&amp;gt;http://www.javapractices.com/topic/TopicAction.do?Id=13&amp;lt;/ref&amp;gt; &amp;lt;ref name=&amp;quot;C++&amp;quot;&amp;gt;http://www.di-mare.com/adolfo/p/money.htm&amp;lt;/ref&amp;gt;  I found one source that addressed the issue of mixed money, which implemented a MoneyBag class that holds an array of Money. &amp;lt;ref name=&amp;quot;testing&amp;quot;&amp;gt;http://junit.sourceforge.net/doc/testinfected/testing.htm&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are also existing projects which seek to provide programmers with a library of classes to use for Money such as [http://timeandmoney.sourceforge.net/ Time and Money] and [http://joda-money.sourceforge.net/ Joda-Money].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56458</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a or</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56458"/>
		<updated>2011-12-02T04:35:44Z</updated>

		<summary type="html">&lt;p&gt;Opevans: /* Class */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Wiki Chapter: CSC/ECE 517 Fall 2011/ch7 7a or&lt;br /&gt;
&lt;br /&gt;
''7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
Money is very important to people worldwide and is something that often must be represented in computer programs.  This raises the question of how money should be represented, which can vary from program to program.  There is much to be considered such as string formatting, the way money is used, and the variety of different currencies in the world.  In Chapter 6 of Skrien's ''Object-Oriented Design Using Java'', he suggests an implementation that represents money as a class, but there are other approaches that can be taken, each with their respective advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Properties of Money==&lt;br /&gt;
Money has certain properties and uses that must be considered in implementation.&lt;br /&gt;
&lt;br /&gt;
===Responsibilities/Using Money===&lt;br /&gt;
The following are ways in which a representation of money should be able to be used: &amp;lt;ref name=&amp;quot;lec20&amp;quot;&amp;gt;http://courses.ncsu.edu/csc517/common/lectures/notes/lec20.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
*To represent a positive or negative amount of money.&lt;br /&gt;
*Possibly representing multiple components of a currency - i.e. dollars and cents.&lt;br /&gt;
*Possibly representing amounts of multiple currencies - i.e. a mix of US Dollars, euros, and yen.'&lt;br /&gt;
*To have a string formatting potentially including currency symbols like the dollar sign ($) and/or commas and/or decimal points.&lt;br /&gt;
*Addition, subtraction, multiplication, and division.&lt;br /&gt;
The degree to which of the above must be met will depend on the depth of the implementing project.&lt;br /&gt;
&lt;br /&gt;
===Different Currencies===&lt;br /&gt;
Some applications may only handle only one currency such as the US Dollar, but more generally applied applications, such as banking, must be able to handle different kinds of currencies.&lt;br /&gt;
&lt;br /&gt;
There are currently over 180 currencies in use around the world.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/List_of_circulating_currencies&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Implementation must be aware of how to specifically represent each currency used.  Some currencies use decimal points, such as how the US has 100 cents to a dollar, while others have no decimal, such as yen.  Each currency also has a different sign symbol used such as $, €, or ¥.  Different currencies also have different rules about how rounding values is handled.&lt;br /&gt;
&lt;br /&gt;
Special consideration must be taken when using different currencies in conjunction with each other.  Foreign exchange rates can be used to convert between different currencies, but the exchange rate often change, so attention must be paid to that.  In some cases, it is also important to keep track of how much money the user has of more than one currency at a time, so that must be handled somehow. &amp;lt;ref&amp;gt;http://www.xencraft.com/resources/multi-currency.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Immutable vs. Mutable===&lt;br /&gt;
Money values can be either mutable, meaning changeable, or immutable, meaning unchanging.  Mutable money values are useful in that a new value does not need to be created for every change.  However, mutable values cause problems for use in hash maps or with concurrency.  The issue with concurrency is a read and write issue in a system with multiple users.  With changing values, money not be what the user expects and lead to unpredictable behavior.  Skrien feels this is a significant enough disadvantage to prefer an immutable representation for money.&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Possible Representations==&lt;br /&gt;
There are several possible ways to represent money in code.&lt;br /&gt;
&lt;br /&gt;
===Floating Point Number===&lt;br /&gt;
Floating point numbers seem like an appealing option since they already include a fractional component, which lends itself straightforward to currencies like the US dollar which also have a fractional component.  However, floating point numbers come with a drawback due to the way floats are formatted.  Since floats are based on binary representation rather than a decimal representation, a value that would be exact in decimal notation can only be stored approximately in a floats point number.  For instance, 9.48 is stored as 9.479999542236328125.  Performing calculations with these approximated values can lead to inaccurate results. &amp;lt;ref name=&amp;quot;dobbs&amp;quot;&amp;gt;http://drdobbs.com/java/184405653&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another deficiency of floating point numbers is that they lose precision as the number gets larger. This is less of a problem when dealing with small amounts of currency, but when dealing with large amounts, on the order of the United States federal debt, the number of significant figures to the right of the decimal point decreases, again leading to possibly inaccurate results.&amp;lt;ref&amp;gt;Skrien, Dale. ''Object-Oriented Design Using Java''. McGraw Hill, 2009, p. 174.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Two Integers===&lt;br /&gt;
For currencies with a fractional component, using two integers to represent one amount of money is an option.  One integer to represent the main component and one to represent the fractional part.  For US currency, this would mean an integer for dollars and an integer for cents.  This representation makes displaying monetary values simple, as each component is already split out and accurate.  Also, by using integers, calculations with money work out as they should, avoiding the loss of accuracy floating points have.  However, code will need to be implemented to handle when the fraction component carries over.  In the case of dollars and cents, if the cents exceed 100, the cents value should be decreased by 100 and the dollars increased by 1.  This handling of carries adds overhead to calculations.  Some drawbacks of this implementation are the added overhead and storage required&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currencty. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref&amp;gt;http://www.infoplease.com/spot/stockdecimal1.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===One Integer, Implied Decimal Point===&lt;br /&gt;
One integer can be used to represent money, even including the fractional component. The location of the decimal place is simply implied based on the currency.  For instance, a single integer representation of $12.34 would be 1234. When dealing with US currency, this exploits the fact that the fundamental unit of currency in the US is actually the cent, not the dollar &amp;lt;ref name=&amp;quot;dobbs&amp;quot;/&amp;gt;. A specialized print method would have to be constructed to display the monetary amount properly to the user, but all calculations work out properly.  This approach has the same advantage that using two integers of maintaining accuracy, but does not have the added overhead or storage.  In ''Object-Oriented Design Using Java'', Skrien considers this to be the best use of the primitive types for representing money.&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;. This method shares the same deficiency of two integers in that it cannot represent fractions of a fundamental unit of money.&lt;br /&gt;
&lt;br /&gt;
===BigDecimal===&lt;br /&gt;
Java offers a [http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html BigDecimal] class, which many people use for representing money.  BigDecimal is capable of storing any size number since it stores each digit as an entry in an array, which grows as necessary.  It also allows for specification of both the rounding method and the scale, the number of digits past the decimal place, for proper monetary calculations. &amp;lt;ref&amp;gt;http://www.javaranch.com/journal/2003/07/MoneyInJava.html&amp;lt;/ref&amp;gt;  On the downside, this specification for rounding and scale is required, which adds code to the implementation&amp;lt;ref name=&amp;quot;dobbs&amp;quot; /&amp;gt;. BigDecimal values are also immutable, which avoids read/write and write/write conflicts in applications. &amp;lt;ref&amp;gt;http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial&amp;lt;/ref&amp;gt;  While there are a lot of nice things about BigDecimal, there are some more issues with BigDecimal which can cause problems in representing money.  BigDecimal cannot represent numbers with infinite digits such as 1/9 (.11111...) and throws an ArithmeticException on any calculation that would produce such number.  To deal with this, hacks such as the following must be implemented:&lt;br /&gt;
 BigDecimal bd1 = BigDecimal(1);&lt;br /&gt;
 BigDecimal bd2 = BigDecimal(9);&lt;br /&gt;
 try {&lt;br /&gt;
     return bd1.divide(bd2);&lt;br /&gt;
 } catch(ArithmaticException ae) {&lt;br /&gt;
     return new BigDecimal(bd1.doubleValue() / bd2.doubleValue());&lt;br /&gt;
 }&lt;br /&gt;
Also, since there isn't a BigDecimal equivalent for SQL databases, so storing BigDecimal values either requires conversion to a different format, which can affect accuracy and/or precision, or the value must be stored as a BLOB, which is cumbersome and inefficient.  In general use, calculations with BigDecimal are significantly slower than with integers, and also take up more storage space. &amp;lt;ref&amp;gt;http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Class===&lt;br /&gt;
In ''Object-Oriented Design Using Java'', recommends using a class to represent money, rather than just using a long integer alone.  He gives two reasons for using a class.  The first is encapsulation - by encapsulating money into a class, all the operations specific to working with money can be implemented to work as desired in all cases, which also avoids code duplication.  The second reason is that, in compiled languages like Java, the compiler is better able to detect coding errors, such as adding money to something that isn't money. &amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt; Implementing Money as a class is a generally good idea, but there are multiple ways to go about it.&lt;br /&gt;
&lt;br /&gt;
====Abstract Class====&lt;br /&gt;
If multiple kinds of currencies are needed to be represented, one implementation is to create an abstract Money class and have each possible currency be a subclass of the abstract class.  Since there are over 180 different currencies worldwide, it can be a challenge to implement subclasses for each one required.&lt;br /&gt;
&lt;br /&gt;
The following is the abstract class proposed by Skrien in Object-Oriented Design Using Java:&lt;br /&gt;
 public abstract class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public long getAmount()&lt;br /&gt;
   public String toString()&lt;br /&gt;
   public int compareTo(Money m)&lt;br /&gt;
   public boolean equals(Object o)&lt;br /&gt;
   public int hashCode()&lt;br /&gt;
   public Money plus(Money)&lt;br /&gt;
   public Money minus(Money)&lt;br /&gt;
   public Money times(double factor)&lt;br /&gt;
   public Money dividedBy(double divisor)&lt;br /&gt;
   public Money negate()&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;&amp;gt;Skrien, Dale. Object-Oriented Design Using Java. McGraw Hill, 2009, p. 179-183.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Single Class====&lt;br /&gt;
Rather than implementing a variety of subclasses for each currency, a single class can be used to represent money, with currency specified within.  This is a good idea, since most modern currencies are base-10 and as such can be calculated and treated in the same way. The only differences are in decimal locations and the symbols used to present them.  Java has a [http://docs.oracle.com/javase/6/docs/api/java/util/Currency.html Currency class] which holds the information for a given currency.&lt;br /&gt;
&lt;br /&gt;
This is insufficient for representing a non-base 10 currency, such as British currency prior to 1971 (240 pence per pound sterling.) &lt;br /&gt;
The following is the Single Class implementation given by Skrien in ''Object-Oriented Design Using Java'':&lt;br /&gt;
&lt;br /&gt;
 public class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   private long amount;&lt;br /&gt;
   private Currency currency;&lt;br /&gt;
   public Money(long amount, Currency currency) {&lt;br /&gt;
     this.amount = amount;&lt;br /&gt;
     this.currency = currency;&lt;br /&gt;
   }&lt;br /&gt;
   public long getAmount() { return amount; }&lt;br /&gt;
   public Currency getCurrency() { &lt;br /&gt;
     return currency; }&lt;br /&gt;
   public String toString() { ...above... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Mixed Money====&lt;br /&gt;
When dealing with multiple currencies it might be necessary to keep track of money consisting of multiple currencies.  Simply converting into one common currency is not a good approach, since exchange rates vary over time, so the amount of each currency should be kept track of.  Skrien suggests using a MixedMoney class, which acts as a sort of wallet holding amounts of different currencies.&lt;br /&gt;
&lt;br /&gt;
The following is Skrien's implementation of MixedMoney:&lt;br /&gt;
 public class MixedMoney implements Money {&lt;br /&gt;
   public MixedMoney() { ... }&lt;br /&gt;
   public String toString() { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
   public Collection getCurrencies() { ... }&lt;br /&gt;
   public long getAmount(Currency currency) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since it uses a lot of the same methods as the regular Money class, Skrien renamed Money to SimpleMoney and created a common Money interface that is as follows:&lt;br /&gt;
&lt;br /&gt;
 public interface Money extends Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public int compareTo(Money o);&lt;br /&gt;
   public Money plus(Money money);&lt;br /&gt;
   public Money minus(Money money);&lt;br /&gt;
   public Money times(double factor);&lt;br /&gt;
   public Money dividedBy(double divisor);&lt;br /&gt;
   public Money negate();&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The money within MixedMoney is suggested to be represented by either a hash map, a collection, or a tree structure.  The hash map implementation is rejected for storage and calculation time issues.  The tree structure is deemed the best approach as it is elegant and avoids checking classes.&lt;br /&gt;
&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Money Implementations in Practice==&lt;br /&gt;
In practice, many decide similarly to Skrien to encapsulate Money as a class.  The choice of underlying representation does vary, though, between types like int, double, or BigDecimal.  I did not find any sources that used floats or two integers, which Skrien rejected. &amp;lt;ref name=&amp;quot;dobbs&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;Representing Money&amp;quot;&amp;gt;http://www.javapractices.com/topic/TopicAction.do?Id=13&amp;lt;/ref&amp;gt; &amp;lt;ref name=&amp;quot;C++&amp;quot;&amp;gt;http://www.di-mare.com/adolfo/p/money.htm&amp;lt;/ref&amp;gt;  I found one source that addressed the issue of mixed money, which implemented a MoneyBag class that holds an array of Money. &amp;lt;ref name=&amp;quot;testing&amp;quot;&amp;gt;http://junit.sourceforge.net/doc/testinfected/testing.htm&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are also existing projects which seek to provide programmers with a library of classes to use for Money such as [http://timeandmoney.sourceforge.net/ Time and Money] and [http://joda-money.sourceforge.net/ Joda-Money].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56457</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a or</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56457"/>
		<updated>2011-12-02T04:13:05Z</updated>

		<summary type="html">&lt;p&gt;Opevans: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Wiki Chapter: CSC/ECE 517 Fall 2011/ch7 7a or&lt;br /&gt;
&lt;br /&gt;
''7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
Money is very important to people worldwide and is something that often must be represented in computer programs.  This raises the question of how money should be represented, which can vary from program to program.  There is much to be considered such as string formatting, the way money is used, and the variety of different currencies in the world.  In Chapter 6 of Skrien's ''Object-Oriented Design Using Java'', he suggests an implementation that represents money as a class, but there are other approaches that can be taken, each with their respective advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Properties of Money==&lt;br /&gt;
Money has certain properties and uses that must be considered in implementation.&lt;br /&gt;
&lt;br /&gt;
===Responsibilities/Using Money===&lt;br /&gt;
The following are ways in which a representation of money should be able to be used: &amp;lt;ref name=&amp;quot;lec20&amp;quot;&amp;gt;http://courses.ncsu.edu/csc517/common/lectures/notes/lec20.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
*To represent a positive or negative amount of money.&lt;br /&gt;
*Possibly representing multiple components of a currency - i.e. dollars and cents.&lt;br /&gt;
*Possibly representing amounts of multiple currencies - i.e. a mix of US Dollars, euros, and yen.'&lt;br /&gt;
*To have a string formatting potentially including currency symbols like the dollar sign ($) and/or commas and/or decimal points.&lt;br /&gt;
*Addition, subtraction, multiplication, and division.&lt;br /&gt;
The degree to which of the above must be met will depend on the depth of the implementing project.&lt;br /&gt;
&lt;br /&gt;
===Different Currencies===&lt;br /&gt;
Some applications may only handle only one currency such as the US Dollar, but more generally applied applications, such as banking, must be able to handle different kinds of currencies.&lt;br /&gt;
&lt;br /&gt;
There are currently over 180 currencies in use around the world.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/List_of_circulating_currencies&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Implementation must be aware of how to specifically represent each currency used.  Some currencies use decimal points, such as how the US has 100 cents to a dollar, while others have no decimal, such as yen.  Each currency also has a different sign symbol used such as $, €, or ¥.  Different currencies also have different rules about how rounding values is handled.&lt;br /&gt;
&lt;br /&gt;
Special consideration must be taken when using different currencies in conjunction with each other.  Foreign exchange rates can be used to convert between different currencies, but the exchange rate often change, so attention must be paid to that.  In some cases, it is also important to keep track of how much money the user has of more than one currency at a time, so that must be handled somehow. &amp;lt;ref&amp;gt;http://www.xencraft.com/resources/multi-currency.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Immutable vs. Mutable===&lt;br /&gt;
Money values can be either mutable, meaning changeable, or immutable, meaning unchanging.  Mutable money values are useful in that a new value does not need to be created for every change.  However, mutable values cause problems for use in hash maps or with concurrency.  The issue with concurrency is a read and write issue in a system with multiple users.  With changing values, money not be what the user expects and lead to unpredictable behavior.  Skrien feels this is a significant enough disadvantage to prefer an immutable representation for money.&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Possible Representations==&lt;br /&gt;
There are several possible ways to represent money in code.&lt;br /&gt;
&lt;br /&gt;
===Floating Point Number===&lt;br /&gt;
Floating point numbers seem like an appealing option since they already include a fractional component, which lends itself straightforward to currencies like the US dollar which also have a fractional component.  However, floating point numbers come with a drawback due to the way floats are formatted.  Since floats are based on binary representation rather than a decimal representation, a value that would be exact in decimal notation can only be stored approximately in a floats point number.  For instance, 9.48 is stored as 9.479999542236328125.  Performing calculations with these approximated values can lead to inaccurate results. &amp;lt;ref name=&amp;quot;dobbs&amp;quot;&amp;gt;http://drdobbs.com/java/184405653&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another deficiency of floating point numbers is that they lose precision as the number gets larger. This is less of a problem when dealing with small amounts of currency, but when dealing with large amounts, on the order of the United States federal debt, the number of significant figures to the right of the decimal point decreases, again leading to possibly inaccurate results.&amp;lt;ref&amp;gt;Skrien, Dale. ''Object-Oriented Design Using Java''. McGraw Hill, 2009, p. 174.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Two Integers===&lt;br /&gt;
For currencies with a fractional component, using two integers to represent one amount of money is an option.  One integer to represent the main component and one to represent the fractional part.  For US currency, this would mean an integer for dollars and an integer for cents.  This representation makes displaying monetary values simple, as each component is already split out and accurate.  Also, by using integers, calculations with money work out as they should, avoiding the loss of accuracy floating points have.  However, code will need to be implemented to handle when the fraction component carries over.  In the case of dollars and cents, if the cents exceed 100, the cents value should be decreased by 100 and the dollars increased by 1.  This handling of carries adds overhead to calculations.  Some drawbacks of this implementation are the added overhead and storage required&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currencty. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref&amp;gt;http://www.infoplease.com/spot/stockdecimal1.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===One Integer, Implied Decimal Point===&lt;br /&gt;
One integer can be used to represent money, even including the fractional component. The location of the decimal place is simply implied based on the currency.  For instance, a single integer representation of $12.34 would be 1234. When dealing with US currency, this exploits the fact that the fundamental unit of currency in the US is actually the cent, not the dollar &amp;lt;ref name=&amp;quot;dobbs&amp;quot;/&amp;gt;. A specialized print method would have to be constructed to display the monetary amount properly to the user, but all calculations work out properly.  This approach has the same advantage that using two integers of maintaining accuracy, but does not have the added overhead or storage.  In ''Object-Oriented Design Using Java'', Skrien considers this to be the best use of the primitive types for representing money.&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;. This method shares the same deficiency of two integers in that it cannot represent fractions of a fundamental unit of money.&lt;br /&gt;
&lt;br /&gt;
===BigDecimal===&lt;br /&gt;
Java offers a [http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html BigDecimal] class, which many people use for representing money.  BigDecimal is capable of storing any size number since it stores each digit as an entry in an array, which grows as necessary.  It also allows for specification of both the rounding method and the scale, the number of digits past the decimal place, for proper monetary calculations. &amp;lt;ref&amp;gt;http://www.javaranch.com/journal/2003/07/MoneyInJava.html&amp;lt;/ref&amp;gt;  On the downside, this specification for rounding and scale is required, which adds code to the implementation&amp;lt;ref name=&amp;quot;dobbs&amp;quot; /&amp;gt;. BigDecimal values are also immutable, which avoids read/write and write/write conflicts in applications. &amp;lt;ref&amp;gt;http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial&amp;lt;/ref&amp;gt;  While there are a lot of nice things about BigDecimal, there are some more issues with BigDecimal which can cause problems in representing money.  BigDecimal cannot represent numbers with infinite digits such as 1/9 (.11111...) and throws an ArithmeticException on any calculation that would produce such number.  To deal with this, hacks such as the following must be implemented:&lt;br /&gt;
 BigDecimal bd1 = BigDecimal(1);&lt;br /&gt;
 BigDecimal bd2 = BigDecimal(9);&lt;br /&gt;
 try {&lt;br /&gt;
     return bd1.divide(bd2);&lt;br /&gt;
 } catch(ArithmaticException ae) {&lt;br /&gt;
     return new BigDecimal(bd1.doubleValue() / bd2.doubleValue());&lt;br /&gt;
 }&lt;br /&gt;
Also, since there isn't a BigDecimal equivalent for SQL databases, so storing BigDecimal values either requires conversion to a different format, which can affect accuracy and/or precision, or the value must be stored as a BLOB, which is cumbersome and inefficient.  In general use, calculations with BigDecimal are significantly slower than with integers, and also take up more storage space. &amp;lt;ref&amp;gt;http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Class===&lt;br /&gt;
In ''Object-Oriented Design Using Java'', recommends using a class to represent money, rather than just using a long integer alone.  He gives two reasons for using a class.  The first is encapsulation - by encapsulating money into a class, all the operations specific to working with money can be implemented to work as desired in all cases, which also avoids code duplication.  The second reason is that the compiler is better able to detect coding errors, such as adding money to something that isn't money. &amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt; Implementing Money as a class is a generally good idea, but there are multiple ways to go about it.&lt;br /&gt;
&lt;br /&gt;
====Abstract Class====&lt;br /&gt;
If multiple kinds of currencies are needed to be represented, one implementation is to create an abstract Money class and have each possible currency be a subclass of the abstract class.  Since there are over 180 different currencies worldwide, it can be a challenge to implement subclasses for each one required.&lt;br /&gt;
&lt;br /&gt;
The following is the abstract class proposed by Skrien in Object-Oriented Design Using Java:&lt;br /&gt;
 public abstract class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public long getAmount()&lt;br /&gt;
   public String toString()&lt;br /&gt;
   public int compareTo(Money m)&lt;br /&gt;
   public boolean equals(Object o)&lt;br /&gt;
   public int hashCode()&lt;br /&gt;
   public Money plus(Money)&lt;br /&gt;
   public Money minus(Money)&lt;br /&gt;
   public Money times(double factor)&lt;br /&gt;
   public Money dividedBy(double divisor)&lt;br /&gt;
   public Money negate()&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;&amp;gt;Skrien, Dale. Object-Oriented Design Using Java. McGraw Hill, 2009, p. 179-183.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Single Class====&lt;br /&gt;
Rather than implementing a variety of subclasses for each currency, a single class can be used to represent money, with currency specified within.  This is a good idea, since currencies basically operate the same calculation-wise, but have variations in decimal locations and the symbols used to present them.  Java has a [http://docs.oracle.com/javase/6/docs/api/java/util/Currency.html Currency class] which holds the information for a given currency.&lt;br /&gt;
&lt;br /&gt;
The following is the Single Class implementation given by Skrien in ''Object-Oriented Design Using Java'':&lt;br /&gt;
&lt;br /&gt;
 public class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   private long amount;&lt;br /&gt;
   private Currency currency;&lt;br /&gt;
   public Money(long amount, Currency currency) {&lt;br /&gt;
     this.amount = amount;&lt;br /&gt;
     this.currency = currency;&lt;br /&gt;
   }&lt;br /&gt;
   public long getAmount() { return amount; }&lt;br /&gt;
   public Currency getCurrency() { &lt;br /&gt;
     return currency; }&lt;br /&gt;
   public String toString() { ...above... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Mixed Money====&lt;br /&gt;
When dealing with multiple currencies it might be necessary to keep track of money consisting of multiple currencies.  Simply converting into one common currency is not a good approach, since exchange rates vary over time, so the amount of each currency should be kept track of.  Skrien suggests using a MixedMoney class, which acts as a sort of wallet holding amounts of different currencies.&lt;br /&gt;
&lt;br /&gt;
The following is Skrien's implementation of MixedMoney:&lt;br /&gt;
 public class MixedMoney implements Money {&lt;br /&gt;
   public MixedMoney() { ... }&lt;br /&gt;
   public String toString() { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
   public Collection getCurrencies() { ... }&lt;br /&gt;
   public long getAmount(Currency currency) { ... }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since it uses a lot of the same methods as the regular Money class, Skrien renamed Money to SimpleMoney and created a common Money interface that is as follows:&lt;br /&gt;
&lt;br /&gt;
 public interface Money extends Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public int compareTo(Money o);&lt;br /&gt;
   public Money plus(Money money);&lt;br /&gt;
   public Money minus(Money money);&lt;br /&gt;
   public Money times(double factor);&lt;br /&gt;
   public Money dividedBy(double divisor);&lt;br /&gt;
   public Money negate();&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;skrien179&amp;quot;/&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The money within MixedMoney is suggested to be represented by either a hash map, a collection, or a tree structure.  The hash map implementation is rejected for storage and calculation time issues.  The tree structure is deemed the best approach as it is elegant and avoids checking classes.&lt;br /&gt;
&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Money Implementations in Practice==&lt;br /&gt;
In practice, many decide similarly to Skrien to encapsulate Money as a class.  The choice of underlying representation does vary, though, between types like int, double, or BigDecimal.  I did not find any sources that used floats or two integers, which Skrien rejected. &amp;lt;ref name=&amp;quot;dobbs&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;Representing Money&amp;quot;&amp;gt;http://www.javapractices.com/topic/TopicAction.do?Id=13&amp;lt;/ref&amp;gt; &amp;lt;ref name=&amp;quot;C++&amp;quot;&amp;gt;http://www.di-mare.com/adolfo/p/money.htm&amp;lt;/ref&amp;gt;  I found one source that addressed the issue of mixed money, which implemented a MoneyBag class that holds an array of Money. &amp;lt;ref name=&amp;quot;testing&amp;quot;&amp;gt;http://junit.sourceforge.net/doc/testinfected/testing.htm&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are also existing projects which seek to provide programmers with a library of classes to use for Money such as [http://timeandmoney.sourceforge.net/ Time and Money] and [http://joda-money.sourceforge.net/ Joda-Money].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56380</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a or</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56380"/>
		<updated>2011-12-01T04:09:37Z</updated>

		<summary type="html">&lt;p&gt;Opevans: /* One Integer, Implied Decimal Point */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Wiki Chapter: CSC/ECE 517 Fall 2011/ch7 7a or&lt;br /&gt;
&lt;br /&gt;
''7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
Money is very important to people worldwide and is something that often must be represented in computer programs.  This raises the question of how money should be represented, which can vary from program to program.  There is much to be considered such as string formatting, the way money is used, and the variety of different currencies in the world.  In Chapter 6 of Skrien's ''Object-Oriented Design Using Java'', he suggests an implementation that represents money as a class, but there are other approaches that can be taken, each with their respective advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Properties of Money==&lt;br /&gt;
Money has certain properties and uses that must be considered in implementation.&lt;br /&gt;
&lt;br /&gt;
===Responsibilities/Using Money===&lt;br /&gt;
The following are ways in which a representation of money should be able to be used: &amp;lt;ref name=&amp;quot;lec20&amp;quot;&amp;gt;http://courses.ncsu.edu/csc517/common/lectures/notes/lec20.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
*To represent a positive or negative amount of money.&lt;br /&gt;
*Possibly representing multiple components of a currency - i.e. dollars and cents.&lt;br /&gt;
*Possibly representing amounts of multiple currencies - i.e. a mix of US Dollars, euros, and yen.'&lt;br /&gt;
*To have a string formatting potentially including currency symbols like the dollar sign ($) and/or commas and/or decimal points.&lt;br /&gt;
*Addition, subtraction, multiplication, and division.&lt;br /&gt;
The degree to which of the above must be met will depend on the depth of the implementing project.&lt;br /&gt;
&lt;br /&gt;
===Different Currencies===&lt;br /&gt;
Some applications may only handle only one currency such as the US Dollar, but more generally applied applications, such as banking, must be able to handle different kinds of currencies.&lt;br /&gt;
&lt;br /&gt;
There are currently over 180 currencies in use around the world.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/List_of_circulating_currencies&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Implementation must be aware of how to specifically represent each currency used.  Some currencies use decimal points, such as how the US has 100 cents to a dollar, while others have no decimal, such as yen.  Each currency also has a different sign symbol used such as $, €, or ¥.  Different currencies also have different rules about how rounding values is handled.&lt;br /&gt;
&lt;br /&gt;
Special consideration must be taken when using different currencies in conjunction with each other.  Foreign exchange rates can be used to convert between different currencies, but the exchange rate often change, so attention must be paid to that.  In some cases, it is also important to keep track of how much money the user has of more than one currency at a time, so that must be handled somehow. &amp;lt;ref&amp;gt;http://www.xencraft.com/resources/multi-currency.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Immutable vs. Mutable===&lt;br /&gt;
Money values can be either mutable, meaning changeable, or immutable, meaning unchanging.  Mutable money values are useful in that a new value does not need to be created for every change.  However, mutable values cause problems for use in hash maps or with concurrency.  The issue with concurrency is a read and write issue in a system with multiple users.  With changing values, money not be what the user expects and lead to unpredictable behavior.  Skrien feels this is a significant enough disadvantage to prefer an immutable representation for money.&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Possible Representations==&lt;br /&gt;
There are several possible ways to represent money in code.&lt;br /&gt;
&lt;br /&gt;
===Floating Point Number===&lt;br /&gt;
Floating point numbers seem like an appealing option since they already include a fractional component, which lends itself straightforward to currencies like the US dollar which also have a fractional component.  However, floating point numbers come with a drawback due to the way floats are formatted.  Since floats are based on binary representation rather than a decimal representation, a value that would be exact in decimal notation can only be stored approximately in a floats point number.  For instance, 9.48 is stored as 9.479999542236328125.  Performing calculations with these approximated values can lead to inaccurate results. &amp;lt;ref name=&amp;quot;dobbs&amp;quot;&amp;gt;http://drdobbs.com/java/184405653&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another deficiency of floating point numbers is that they lose precision as the number gets larger. This is less of a problem when dealing with small amounts of currency, but when dealing with large amounts, on the order of the United States federal debt, the number of significant figures to the right of the decimal point decreases, again leading to possibly inaccurate results.&amp;lt;ref&amp;gt;Skrien, Dale. ''Object-Oriented Design Using Java''. McGraw Hill, 2009, p. 174.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Two Integers===&lt;br /&gt;
For currencies with a fractional component, using two integers to represent one amount of money is an option.  One integer to represent the main component and one to represent the fractional part.  For US currency, this would mean an integer for dollars and an integer for cents.  This representation makes displaying monetary values simple, as each component is already split out and accurate.  Also, by using integers, calculations with money work out as they should, avoiding the loss of accuracy floating points have.  However, code will need to be implemented to handle when the fraction component carries over.  In the case of dollars and cents, if the cents exceed 100, the cents value should be decreased by 100 and the dollars increased by 1.  This handling of carries adds overhead to calculations.  Some drawbacks of this implementation are the added overhead and storage required&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currencty. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref&amp;gt;http://www.infoplease.com/spot/stockdecimal1.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===One Integer, Implied Decimal Point===&lt;br /&gt;
One integer can be used to represent money, even including the fractional component. The location of the decimal place is simply implied based on the currency.  For instance, a single integer representation of $12.34 would be 1234. When dealing with US currency, this exploits the fact that the fundamental unit of currency in the US is actually the cent, not the dollar &amp;lt;ref name=&amp;quot;dobbs&amp;quot;/&amp;gt;. A specialized print method would have to be constructed to display the monetary amount properly to the user, but all calculations work out properly.  This approach has the same advantage that using two integers of maintaining accuracy, but does not have the added overhead or storage.  In ''Object-Oriented Design Using Java'', Skrien considers this to be the best use of the primitive types for representing money.&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;. This method shares the same deficiency of two integers in that it cannot represent fractions of a fundamental unit of money.&lt;br /&gt;
&lt;br /&gt;
===BigDecimal===&lt;br /&gt;
Java offers a [http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html BigDecimal] class, which many people use for representing money.  BigDecimal is capable of storing any size number since it stores each digit as an entry in an array, which grows as necessary.  It also allows for specification of both the rounding method and the scale, the number of digits past the decimal place, for proper monetary calculations. &amp;lt;ref&amp;gt;http://www.javaranch.com/journal/2003/07/MoneyInJava.html&amp;lt;/ref&amp;gt;  On the downside, this specification for rounding and scale is required, which adds code to the implementation&amp;lt;ref name=&amp;quot;dobbs&amp;quot; /&amp;gt;. BigDecimal values are also immutable, which avoids read/write and write/write conflicts in applications. &amp;lt;ref&amp;gt;http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial&amp;lt;/ref&amp;gt;  While there are a lot of nice things about BigDecimal, there are some more issues with BigDecimal which can cause problems in representing money.  BigDecimal cannot represent numbers with infinite digits such as 1/9 (.11111...) and throws an ArithmeticException on any calculation that would produce such number.  To deal with this, hacks such as the following must be implemented:&lt;br /&gt;
 BigDecimal bd1 = BigDecimal(1);&lt;br /&gt;
 BigDecimal bd2 = BigDecimal(9);&lt;br /&gt;
 try {&lt;br /&gt;
     return bd1.divide(bd2);&lt;br /&gt;
 } catch(ArithmaticException ae) {&lt;br /&gt;
     return new BigDecimal(bd1.doubleValue() / bd2.doubleValue());&lt;br /&gt;
 }&lt;br /&gt;
Also, since there isn't a BigDecimal equivalent for SQL databases, so storing BigDecimal values either requires conversion to a different format, which can affect accuracy and/or precision, or the value must be stored as a BLOB, which is cumbersome and inefficient.  In general use, calculations with BigDecimal are significantly slower than with integers, and also take up more storage space. &amp;lt;ref&amp;gt;http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Class===&lt;br /&gt;
In ''Object-Oriented Design Using Java'', recommends using a class to represent money, rather than just using a long integer alone.  He gives two reasons for using a class.  The first is encapsulation - by encapsulating money into a class, all the operations specific to working with money can be implemented to work as desired in all cases, which also avoids code duplication.  The second reason is that the compiler is better able to detect coding errors, such as adding money to something that isn't money. &amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt; Implementing Money as a class is a generally good idea, but there are multiple ways to go about it.&lt;br /&gt;
&lt;br /&gt;
====Abstract Class====&lt;br /&gt;
If multiple kinds of currencies are needed to be represented, one implementation is to create an abstract Money class and have each possible currency be a subclass of the abstract class.  Since there are over 180 different currencies worldwide, it can be a challenge to implement subclasses for each one required.&lt;br /&gt;
&lt;br /&gt;
The following is the abstract class proposed by Skrien in Object-Oriented Design Using Java:&lt;br /&gt;
 public abstract class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public long getAmount()&lt;br /&gt;
   public String toString()&lt;br /&gt;
   public int compareTo(Money m)&lt;br /&gt;
   public boolean equals(Object o)&lt;br /&gt;
   public int hashCode()&lt;br /&gt;
   public Money plus(Money)&lt;br /&gt;
   public Money minus(Money)&lt;br /&gt;
   public Money times(double factor)&lt;br /&gt;
   public Money dividedBy(double divisor)&lt;br /&gt;
   public Money negate()&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Single Class====&lt;br /&gt;
Rather than implementing a variety of subclasses for each currency, a single class can be used to represent money, with currency specified within.  This is a good idea, since currencies basically operate the same calculation-wise, but have variations in decimal locations and the symbols used to present them.  Java has a [http://docs.oracle.com/javase/6/docs/api/java/util/Currency.html Currency class] which holds the information for a given currency.&lt;br /&gt;
&lt;br /&gt;
The following is the Single Class implementation given by Skrien in ''Object-Oriented Design Using Java'':&lt;br /&gt;
&lt;br /&gt;
 public class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   private long amount;&lt;br /&gt;
   private Currency currency;&lt;br /&gt;
   public Money(long amount, Currency currency) {&lt;br /&gt;
     this.amount = amount;&lt;br /&gt;
     this.currency = currency;&lt;br /&gt;
   }&lt;br /&gt;
   public long getAmount() { return amount; }&lt;br /&gt;
   public Currency getCurrency() { &lt;br /&gt;
     return currency; }&lt;br /&gt;
   public String toString() { ...above... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Mixed Money====&lt;br /&gt;
&lt;br /&gt;
====Using an Interface====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Resources==&lt;br /&gt;
*Need to go through these for information&lt;br /&gt;
**[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf PDF from class lecture]&lt;br /&gt;
**[http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Java's Currency class docs]&lt;br /&gt;
**[http://drdobbs.com/java/184405653 A standalone class to tackle the problem (in Java)]&lt;br /&gt;
**[http://www.javapractices.com/topic/TopicAction.do?Id=13 Representing money (Java)]&lt;br /&gt;
**[http://www.javaranch.com/journal/2003/07/MoneyInJava.html Working with money in Java]&lt;br /&gt;
**[http://timeandmoney.sourceforge.net/ Time and Money Java project - doesn't seem to have much documentation, though]&lt;br /&gt;
**[http://joda-money.sourceforge.net/ Joda Money class library for Java]&lt;br /&gt;
**[http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/ BigDecimal and Your Money (a criticism of using BigDecimal format)]&lt;br /&gt;
**[http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial Tutorial on using BigDecimal]&lt;br /&gt;
**[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests (has a Money class example]&lt;br /&gt;
**[http://www.xencraft.com/resources/multi-currency.html Currency Internationalization (i18n), Multiple Currencies and Foreign Exchange (FX)] -nothing programming-wise, but a good overview of the considerations for currency&lt;br /&gt;
**[http://www.di-mare.com/adolfo/p/money.htm Yet Another C++ Money Class ]&lt;br /&gt;
**[http://edenti.deis.unibo.it/utils/Java-tips/Currency%20representation.txt Representing currencies (txt document with example and details)]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56379</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a or</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56379"/>
		<updated>2011-12-01T04:08:55Z</updated>

		<summary type="html">&lt;p&gt;Opevans: /* Two Integers */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Wiki Chapter: CSC/ECE 517 Fall 2011/ch7 7a or&lt;br /&gt;
&lt;br /&gt;
''7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
Money is very important to people worldwide and is something that often must be represented in computer programs.  This raises the question of how money should be represented, which can vary from program to program.  There is much to be considered such as string formatting, the way money is used, and the variety of different currencies in the world.  In Chapter 6 of Skrien's ''Object-Oriented Design Using Java'', he suggests an implementation that represents money as a class, but there are other approaches that can be taken, each with their respective advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Properties of Money==&lt;br /&gt;
Money has certain properties and uses that must be considered in implementation.&lt;br /&gt;
&lt;br /&gt;
===Responsibilities/Using Money===&lt;br /&gt;
The following are ways in which a representation of money should be able to be used: &amp;lt;ref name=&amp;quot;lec20&amp;quot;&amp;gt;http://courses.ncsu.edu/csc517/common/lectures/notes/lec20.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
*To represent a positive or negative amount of money.&lt;br /&gt;
*Possibly representing multiple components of a currency - i.e. dollars and cents.&lt;br /&gt;
*Possibly representing amounts of multiple currencies - i.e. a mix of US Dollars, euros, and yen.'&lt;br /&gt;
*To have a string formatting potentially including currency symbols like the dollar sign ($) and/or commas and/or decimal points.&lt;br /&gt;
*Addition, subtraction, multiplication, and division.&lt;br /&gt;
The degree to which of the above must be met will depend on the depth of the implementing project.&lt;br /&gt;
&lt;br /&gt;
===Different Currencies===&lt;br /&gt;
Some applications may only handle only one currency such as the US Dollar, but more generally applied applications, such as banking, must be able to handle different kinds of currencies.&lt;br /&gt;
&lt;br /&gt;
There are currently over 180 currencies in use around the world.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/List_of_circulating_currencies&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Implementation must be aware of how to specifically represent each currency used.  Some currencies use decimal points, such as how the US has 100 cents to a dollar, while others have no decimal, such as yen.  Each currency also has a different sign symbol used such as $, €, or ¥.  Different currencies also have different rules about how rounding values is handled.&lt;br /&gt;
&lt;br /&gt;
Special consideration must be taken when using different currencies in conjunction with each other.  Foreign exchange rates can be used to convert between different currencies, but the exchange rate often change, so attention must be paid to that.  In some cases, it is also important to keep track of how much money the user has of more than one currency at a time, so that must be handled somehow. &amp;lt;ref&amp;gt;http://www.xencraft.com/resources/multi-currency.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Immutable vs. Mutable===&lt;br /&gt;
Money values can be either mutable, meaning changeable, or immutable, meaning unchanging.  Mutable money values are useful in that a new value does not need to be created for every change.  However, mutable values cause problems for use in hash maps or with concurrency.  The issue with concurrency is a read and write issue in a system with multiple users.  With changing values, money not be what the user expects and lead to unpredictable behavior.  Skrien feels this is a significant enough disadvantage to prefer an immutable representation for money.&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Possible Representations==&lt;br /&gt;
There are several possible ways to represent money in code.&lt;br /&gt;
&lt;br /&gt;
===Floating Point Number===&lt;br /&gt;
Floating point numbers seem like an appealing option since they already include a fractional component, which lends itself straightforward to currencies like the US dollar which also have a fractional component.  However, floating point numbers come with a drawback due to the way floats are formatted.  Since floats are based on binary representation rather than a decimal representation, a value that would be exact in decimal notation can only be stored approximately in a floats point number.  For instance, 9.48 is stored as 9.479999542236328125.  Performing calculations with these approximated values can lead to inaccurate results. &amp;lt;ref name=&amp;quot;dobbs&amp;quot;&amp;gt;http://drdobbs.com/java/184405653&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another deficiency of floating point numbers is that they lose precision as the number gets larger. This is less of a problem when dealing with small amounts of currency, but when dealing with large amounts, on the order of the United States federal debt, the number of significant figures to the right of the decimal point decreases, again leading to possibly inaccurate results.&amp;lt;ref&amp;gt;Skrien, Dale. ''Object-Oriented Design Using Java''. McGraw Hill, 2009, p. 174.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Two Integers===&lt;br /&gt;
For currencies with a fractional component, using two integers to represent one amount of money is an option.  One integer to represent the main component and one to represent the fractional part.  For US currency, this would mean an integer for dollars and an integer for cents.  This representation makes displaying monetary values simple, as each component is already split out and accurate.  Also, by using integers, calculations with money work out as they should, avoiding the loss of accuracy floating points have.  However, code will need to be implemented to handle when the fraction component carries over.  In the case of dollars and cents, if the cents exceed 100, the cents value should be decreased by 100 and the dollars increased by 1.  This handling of carries adds overhead to calculations.  Some drawbacks of this implementation are the added overhead and storage required&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currencty. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref&amp;gt;http://www.infoplease.com/spot/stockdecimal1.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===One Integer, Implied Decimal Point===&lt;br /&gt;
One integer can be used to represent money, even including the fractional component. The location of the decimal place is simply implied based on the currency.  For instance, a single integer representation of $12.34 would be 1234. When dealing with US currency, this exploits the fact that the fundamental unit of currency in the US is actually the cent, not the dollar &amp;lt;ref name=&amp;quot;dobbs&amp;quot;/&amp;gt;. A specialized print method would have to be constructed to display the monetary amount properly to the user, but all calculations work out properly.  This approach has the same advantage that using two integers of maintaining accuracy, but does not have the added overhead or storage.  In ''Object-Oriented Design Using Java'', Skrien considers this to be the best use of the primitive types for representing money.&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===BigDecimal===&lt;br /&gt;
Java offers a [http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html BigDecimal] class, which many people use for representing money.  BigDecimal is capable of storing any size number since it stores each digit as an entry in an array, which grows as necessary.  It also allows for specification of both the rounding method and the scale, the number of digits past the decimal place, for proper monetary calculations. &amp;lt;ref&amp;gt;http://www.javaranch.com/journal/2003/07/MoneyInJava.html&amp;lt;/ref&amp;gt;  On the downside, this specification for rounding and scale is required, which adds code to the implementation&amp;lt;ref name=&amp;quot;dobbs&amp;quot; /&amp;gt;. BigDecimal values are also immutable, which avoids read/write and write/write conflicts in applications. &amp;lt;ref&amp;gt;http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial&amp;lt;/ref&amp;gt;  While there are a lot of nice things about BigDecimal, there are some more issues with BigDecimal which can cause problems in representing money.  BigDecimal cannot represent numbers with infinite digits such as 1/9 (.11111...) and throws an ArithmeticException on any calculation that would produce such number.  To deal with this, hacks such as the following must be implemented:&lt;br /&gt;
 BigDecimal bd1 = BigDecimal(1);&lt;br /&gt;
 BigDecimal bd2 = BigDecimal(9);&lt;br /&gt;
 try {&lt;br /&gt;
     return bd1.divide(bd2);&lt;br /&gt;
 } catch(ArithmaticException ae) {&lt;br /&gt;
     return new BigDecimal(bd1.doubleValue() / bd2.doubleValue());&lt;br /&gt;
 }&lt;br /&gt;
Also, since there isn't a BigDecimal equivalent for SQL databases, so storing BigDecimal values either requires conversion to a different format, which can affect accuracy and/or precision, or the value must be stored as a BLOB, which is cumbersome and inefficient.  In general use, calculations with BigDecimal are significantly slower than with integers, and also take up more storage space. &amp;lt;ref&amp;gt;http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Class===&lt;br /&gt;
In ''Object-Oriented Design Using Java'', recommends using a class to represent money, rather than just using a long integer alone.  He gives two reasons for using a class.  The first is encapsulation - by encapsulating money into a class, all the operations specific to working with money can be implemented to work as desired in all cases, which also avoids code duplication.  The second reason is that the compiler is better able to detect coding errors, such as adding money to something that isn't money. &amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt; Implementing Money as a class is a generally good idea, but there are multiple ways to go about it.&lt;br /&gt;
&lt;br /&gt;
====Abstract Class====&lt;br /&gt;
If multiple kinds of currencies are needed to be represented, one implementation is to create an abstract Money class and have each possible currency be a subclass of the abstract class.  Since there are over 180 different currencies worldwide, it can be a challenge to implement subclasses for each one required.&lt;br /&gt;
&lt;br /&gt;
The following is the abstract class proposed by Skrien in Object-Oriented Design Using Java:&lt;br /&gt;
 public abstract class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public long getAmount()&lt;br /&gt;
   public String toString()&lt;br /&gt;
   public int compareTo(Money m)&lt;br /&gt;
   public boolean equals(Object o)&lt;br /&gt;
   public int hashCode()&lt;br /&gt;
   public Money plus(Money)&lt;br /&gt;
   public Money minus(Money)&lt;br /&gt;
   public Money times(double factor)&lt;br /&gt;
   public Money dividedBy(double divisor)&lt;br /&gt;
   public Money negate()&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Single Class====&lt;br /&gt;
Rather than implementing a variety of subclasses for each currency, a single class can be used to represent money, with currency specified within.  This is a good idea, since currencies basically operate the same calculation-wise, but have variations in decimal locations and the symbols used to present them.  Java has a [http://docs.oracle.com/javase/6/docs/api/java/util/Currency.html Currency class] which holds the information for a given currency.&lt;br /&gt;
&lt;br /&gt;
The following is the Single Class implementation given by Skrien in ''Object-Oriented Design Using Java'':&lt;br /&gt;
&lt;br /&gt;
 public class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   private long amount;&lt;br /&gt;
   private Currency currency;&lt;br /&gt;
   public Money(long amount, Currency currency) {&lt;br /&gt;
     this.amount = amount;&lt;br /&gt;
     this.currency = currency;&lt;br /&gt;
   }&lt;br /&gt;
   public long getAmount() { return amount; }&lt;br /&gt;
   public Currency getCurrency() { &lt;br /&gt;
     return currency; }&lt;br /&gt;
   public String toString() { ...above... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Mixed Money====&lt;br /&gt;
&lt;br /&gt;
====Using an Interface====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Resources==&lt;br /&gt;
*Need to go through these for information&lt;br /&gt;
**[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf PDF from class lecture]&lt;br /&gt;
**[http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Java's Currency class docs]&lt;br /&gt;
**[http://drdobbs.com/java/184405653 A standalone class to tackle the problem (in Java)]&lt;br /&gt;
**[http://www.javapractices.com/topic/TopicAction.do?Id=13 Representing money (Java)]&lt;br /&gt;
**[http://www.javaranch.com/journal/2003/07/MoneyInJava.html Working with money in Java]&lt;br /&gt;
**[http://timeandmoney.sourceforge.net/ Time and Money Java project - doesn't seem to have much documentation, though]&lt;br /&gt;
**[http://joda-money.sourceforge.net/ Joda Money class library for Java]&lt;br /&gt;
**[http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/ BigDecimal and Your Money (a criticism of using BigDecimal format)]&lt;br /&gt;
**[http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial Tutorial on using BigDecimal]&lt;br /&gt;
**[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests (has a Money class example]&lt;br /&gt;
**[http://www.xencraft.com/resources/multi-currency.html Currency Internationalization (i18n), Multiple Currencies and Foreign Exchange (FX)] -nothing programming-wise, but a good overview of the considerations for currency&lt;br /&gt;
**[http://www.di-mare.com/adolfo/p/money.htm Yet Another C++ Money Class ]&lt;br /&gt;
**[http://edenti.deis.unibo.it/utils/Java-tips/Currency%20representation.txt Representing currencies (txt document with example and details)]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56370</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a or</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56370"/>
		<updated>2011-12-01T03:25:53Z</updated>

		<summary type="html">&lt;p&gt;Opevans: /* BigDecimal */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Wiki Chapter: CSC/ECE 517 Fall 2011/ch7 7a or&lt;br /&gt;
&lt;br /&gt;
''7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
Money is very important to people worldwide and is something that often must be represented in computer programs.  This raises the question of how money should be represented, which can vary from program to program.  There is much to be considered such as string formatting, the way money is used, and the variety of different currencies in the world.  In Chapter 6 of Skrien's ''Object-Oriented Design Using Java'', he suggests an implementation that represents money as a class, but there are other approaches that can be taken, each with their respective advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Properties of Money==&lt;br /&gt;
Money has certain properties and uses that must be considered in implementation.&lt;br /&gt;
&lt;br /&gt;
===Responsibilities/Using Money===&lt;br /&gt;
The following are ways in which a representation of money should be able to be used: &amp;lt;ref name=&amp;quot;lec20&amp;quot;&amp;gt;http://courses.ncsu.edu/csc517/common/lectures/notes/lec20.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
*To represent a positive or negative amount of money.&lt;br /&gt;
*Possibly representing multiple components of a currency - i.e. dollars and cents.&lt;br /&gt;
*Possibly representing amounts of multiple currencies - i.e. a mix of US Dollars, euros, and yen.'&lt;br /&gt;
*To have a string formatting potentially including currency symbols like the dollar sign ($) and/or commas and/or decimal points.&lt;br /&gt;
*Addition, subtraction, multiplication, and division.&lt;br /&gt;
The degree to which of the above must be met will depend on the depth of the implementing project.&lt;br /&gt;
&lt;br /&gt;
===Different Currencies===&lt;br /&gt;
Some applications may only handle only one currency such as the US Dollar, but more generally applied applications, such as banking, must be able to handle different kinds of currencies.&lt;br /&gt;
&lt;br /&gt;
There are currently over 180 currencies in use around the world.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/List_of_circulating_currencies&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Implementation must be aware of how to specifically represent each currency used.  Some currencies use decimal points, such as how the US has 100 cents to a dollar, while others have no decimal, such as yen.  Each currency also has a different sign symbol used such as $, €, or ¥.  Different currencies also have different rules about how rounding values is handled.&lt;br /&gt;
&lt;br /&gt;
Special consideration must be taken when using different currencies in conjunction with each other.  Foreign exchange rates can be used to convert between different currencies, but the exchange rate often change, so attention must be paid to that.  In some cases, it is also important to keep track of how much money the user has of more than one currency at a time, so that must be handled somehow. &amp;lt;ref&amp;gt;http://www.xencraft.com/resources/multi-currency.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Immutable vs. Mutable===&lt;br /&gt;
Money values can be either mutable, meaning changeable, or immutable, meaning unchanging.  Mutable money values are useful in that a new value does not need to be created for every change.  However, mutable values cause problems for use in hash maps or with concurrency.  The issue with concurrency is a read and write issue in a system with multiple users.  With changing values, money not be what the user expects and lead to unpredictable behavior.  Skrien feels this is a significant enough disadvantage to prefer an immutable representation for money.&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Possible Representations==&lt;br /&gt;
There are several possible ways to represent money in code.&lt;br /&gt;
&lt;br /&gt;
===Floating Point Number===&lt;br /&gt;
Floating point numbers seem like an appealing option since they already include a fractional component, which lends itself straightforward to currencies like the US dollar which also have a fractional component.  However, floating point numbers come with a drawback due to the way floats are formatted.  Since floats are based on binary representation rather than a decimal representation, a value that would be exact in decimal notation can only be stored approximately in a floats point number.  For instance, 9.48 is stored as 9.479999542236328125.  Performing calculations with these approximated values can lead to inaccurate results. &amp;lt;ref name=&amp;quot;dobbs&amp;quot;&amp;gt;http://drdobbs.com/java/184405653&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another deficiency of floating point numbers is that they lose precision as the number gets larger. This is less of a problem when dealing with small amounts of currency, but when dealing with large amounts, on the order of the United States federal debt, the number of significant figures to the right of the decimal point decreases, again leading to possibly inaccurate results.&amp;lt;ref&amp;gt;Skrien, Dale. ''Object-Oriented Design Using Java''. McGraw Hill, 2009, p. 174.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Two Integers===&lt;br /&gt;
For currencies with a fractional component, using two integers to represent one amount of money is an option.  One integer to represent the main component and one to represent the fractional part.  For US currency, this would mean an integer for dollars and an integer for cents.  This representation makes displaying monetary values simple, as each component is already split out and accurate.  Also, by using integers, calculations with money work out as they should, avoiding the loss of accuracy floating points have.  However, code will need to be implemented to handle when the fraction component carries over.  In the case of dollars and cents, if the cents exceed 100, the cents value should be decreased by 100 and the dollars increased by 1.  This handling of carries adds overhead to calculations.  The main drawbacks of this implementation is the added overhead and storage required. &amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===One Integer, Implied Decimal Point===&lt;br /&gt;
One integer can be used to represent money, even including the fractional component. The location of the decimal place is simply implied based on the currency.  For instance, a single integer representation of $12.34 would be 1234. When dealing with US currency, this exploits the fact that the fundamental unit of currency in the US is actually the cent, not the dollar &amp;lt;ref name=&amp;quot;dobbs&amp;quot;/&amp;gt;. A specialized print method would have to be constructed to display the monetary amount properly to the user, but all calculations work out properly.  This approach has the same advantage that using two integers of maintaining accuracy, but does not have the added overhead or storage.  In ''Object-Oriented Design Using Java'', Skrien considers this to be the best use of the primitive types for representing money.&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===BigDecimal===&lt;br /&gt;
Java offers a [http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html BigDecimal] class, which many people use for representing money.  BigDecimal is capable of storing any size number since it stores each digit as an entry in an array, which grows as necessary.  It also allows for specification of both the rounding method and the scale, the number of digits past the decimal place, for proper monetary calculations. &amp;lt;ref&amp;gt;http://www.javaranch.com/journal/2003/07/MoneyInJava.html&amp;lt;/ref&amp;gt;  On the downside, this specification for rounding and scale is required, which adds code to the implementation&amp;lt;ref name=&amp;quot;dobbs&amp;quot; /&amp;gt;. BigDecimal values are also immutable, which avoids read/write and write/write conflicts in applications. &amp;lt;ref&amp;gt;http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial&amp;lt;/ref&amp;gt;  While there are a lot of nice things about BigDecimal, there are some more issues with BigDecimal which can cause problems in representing money.  BigDecimal cannot represent numbers with infinite digits such as 1/9 (.11111...) and throws an ArithmeticException on any calculation that would produce such number.  To deal with this, hacks such as the following must be implemented:&lt;br /&gt;
 BigDecimal bd1 = BigDecimal(1);&lt;br /&gt;
 BigDecimal bd2 = BigDecimal(9);&lt;br /&gt;
 try {&lt;br /&gt;
     return bd1.divide(bd2);&lt;br /&gt;
 } catch(ArithmaticException ae) {&lt;br /&gt;
     return new BigDecimal(bd1.doubleValue() / bd2.doubleValue());&lt;br /&gt;
 }&lt;br /&gt;
Also, since there isn't a BigDecimal equivalent for SQL databases, so storing BigDecimal values either requires conversion to a different format, which can affect accuracy and/or precision, or the value must be stored as a BLOB, which is cumbersome and inefficient.  In general use, calculations with BigDecimal are significantly slower than with integers, and also take up more storage space. &amp;lt;ref&amp;gt;http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Class===&lt;br /&gt;
In ''Object-Oriented Design Using Java'', recommends using a class to represent money, rather than just using a long integer alone.  He gives two reasons for using a class.  The first is encapsulation - by encapsulating money into a class, all the operations specific to working with money can be implemented to work as desired in all cases, which also avoids code duplication.  The second reason is that the compiler is better able to detect coding errors, such as adding money to something that isn't money. &amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt; Implementing Money as a class is a generally good idea, but there are multiple ways to go about it.&lt;br /&gt;
&lt;br /&gt;
====Abstract Class====&lt;br /&gt;
If multiple kinds of currencies are needed to be represented, one implementation is to create an abstract Money class and have each possible currency be a subclass of the abstract class.  Since there are over 180 different currencies worldwide, it can be a challenge to implement subclasses for each one required.&lt;br /&gt;
&lt;br /&gt;
The following is the abstract class proposed by Skrien in Object-Oriented Design Using Java:&lt;br /&gt;
 public abstract class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public long getAmount()&lt;br /&gt;
   public String toString()&lt;br /&gt;
   public int compareTo(Money m)&lt;br /&gt;
   public boolean equals(Object o)&lt;br /&gt;
   public int hashCode()&lt;br /&gt;
   public Money plus(Money)&lt;br /&gt;
   public Money minus(Money)&lt;br /&gt;
   public Money times(double factor)&lt;br /&gt;
   public Money dividedBy(double divisor)&lt;br /&gt;
   public Money negate()&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Single Class====&lt;br /&gt;
Rather than implementing a variety of subclasses for each currency, a single class can be used to represent money, with currency specified within.  This is a good idea, since currencies basically operate the same calculation-wise, but have variations in decimal locations and the symbols used to present them.  Java has a [http://docs.oracle.com/javase/6/docs/api/java/util/Currency.html Currency class] which holds the information for a given currency.&lt;br /&gt;
&lt;br /&gt;
The following is the Single Class implementation given by Skrien in ''Object-Oriented Design Using Java'':&lt;br /&gt;
&lt;br /&gt;
 public class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   private long amount;&lt;br /&gt;
   private Currency currency;&lt;br /&gt;
   public Money(long amount, Currency currency) {&lt;br /&gt;
     this.amount = amount;&lt;br /&gt;
     this.currency = currency;&lt;br /&gt;
   }&lt;br /&gt;
   public long getAmount() { return amount; }&lt;br /&gt;
   public Currency getCurrency() { &lt;br /&gt;
     return currency; }&lt;br /&gt;
   public String toString() { ...above... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Mixed Money====&lt;br /&gt;
&lt;br /&gt;
====Using an Interface====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Resources==&lt;br /&gt;
*Need to go through these for information&lt;br /&gt;
**[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf PDF from class lecture]&lt;br /&gt;
**[http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Java's Currency class docs]&lt;br /&gt;
**[http://drdobbs.com/java/184405653 A standalone class to tackle the problem (in Java)]&lt;br /&gt;
**[http://www.javapractices.com/topic/TopicAction.do?Id=13 Representing money (Java)]&lt;br /&gt;
**[http://www.javaranch.com/journal/2003/07/MoneyInJava.html Working with money in Java]&lt;br /&gt;
**[http://timeandmoney.sourceforge.net/ Time and Money Java project - doesn't seem to have much documentation, though]&lt;br /&gt;
**[http://joda-money.sourceforge.net/ Joda Money class library for Java]&lt;br /&gt;
**[http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/ BigDecimal and Your Money (a criticism of using BigDecimal format)]&lt;br /&gt;
**[http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial Tutorial on using BigDecimal]&lt;br /&gt;
**[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests (has a Money class example]&lt;br /&gt;
**[http://www.xencraft.com/resources/multi-currency.html Currency Internationalization (i18n), Multiple Currencies and Foreign Exchange (FX)] -nothing programming-wise, but a good overview of the considerations for currency&lt;br /&gt;
**[http://www.di-mare.com/adolfo/p/money.htm Yet Another C++ Money Class ]&lt;br /&gt;
**[http://edenti.deis.unibo.it/utils/Java-tips/Currency%20representation.txt Representing currencies (txt document with example and details)]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56369</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a or</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56369"/>
		<updated>2011-12-01T03:25:07Z</updated>

		<summary type="html">&lt;p&gt;Opevans: /* BigDecimal */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Wiki Chapter: CSC/ECE 517 Fall 2011/ch7 7a or&lt;br /&gt;
&lt;br /&gt;
''7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
Money is very important to people worldwide and is something that often must be represented in computer programs.  This raises the question of how money should be represented, which can vary from program to program.  There is much to be considered such as string formatting, the way money is used, and the variety of different currencies in the world.  In Chapter 6 of Skrien's ''Object-Oriented Design Using Java'', he suggests an implementation that represents money as a class, but there are other approaches that can be taken, each with their respective advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Properties of Money==&lt;br /&gt;
Money has certain properties and uses that must be considered in implementation.&lt;br /&gt;
&lt;br /&gt;
===Responsibilities/Using Money===&lt;br /&gt;
The following are ways in which a representation of money should be able to be used: &amp;lt;ref name=&amp;quot;lec20&amp;quot;&amp;gt;http://courses.ncsu.edu/csc517/common/lectures/notes/lec20.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
*To represent a positive or negative amount of money.&lt;br /&gt;
*Possibly representing multiple components of a currency - i.e. dollars and cents.&lt;br /&gt;
*Possibly representing amounts of multiple currencies - i.e. a mix of US Dollars, euros, and yen.'&lt;br /&gt;
*To have a string formatting potentially including currency symbols like the dollar sign ($) and/or commas and/or decimal points.&lt;br /&gt;
*Addition, subtraction, multiplication, and division.&lt;br /&gt;
The degree to which of the above must be met will depend on the depth of the implementing project.&lt;br /&gt;
&lt;br /&gt;
===Different Currencies===&lt;br /&gt;
Some applications may only handle only one currency such as the US Dollar, but more generally applied applications, such as banking, must be able to handle different kinds of currencies.&lt;br /&gt;
&lt;br /&gt;
There are currently over 180 currencies in use around the world.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/List_of_circulating_currencies&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Implementation must be aware of how to specifically represent each currency used.  Some currencies use decimal points, such as how the US has 100 cents to a dollar, while others have no decimal, such as yen.  Each currency also has a different sign symbol used such as $, €, or ¥.  Different currencies also have different rules about how rounding values is handled.&lt;br /&gt;
&lt;br /&gt;
Special consideration must be taken when using different currencies in conjunction with each other.  Foreign exchange rates can be used to convert between different currencies, but the exchange rate often change, so attention must be paid to that.  In some cases, it is also important to keep track of how much money the user has of more than one currency at a time, so that must be handled somehow. &amp;lt;ref&amp;gt;http://www.xencraft.com/resources/multi-currency.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Immutable vs. Mutable===&lt;br /&gt;
Money values can be either mutable, meaning changeable, or immutable, meaning unchanging.  Mutable money values are useful in that a new value does not need to be created for every change.  However, mutable values cause problems for use in hash maps or with concurrency.  The issue with concurrency is a read and write issue in a system with multiple users.  With changing values, money not be what the user expects and lead to unpredictable behavior.  Skrien feels this is a significant enough disadvantage to prefer an immutable representation for money.&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Possible Representations==&lt;br /&gt;
There are several possible ways to represent money in code.&lt;br /&gt;
&lt;br /&gt;
===Floating Point Number===&lt;br /&gt;
Floating point numbers seem like an appealing option since they already include a fractional component, which lends itself straightforward to currencies like the US dollar which also have a fractional component.  However, floating point numbers come with a drawback due to the way floats are formatted.  Since floats are based on binary representation rather than a decimal representation, a value that would be exact in decimal notation can only be stored approximately in a floats point number.  For instance, 9.48 is stored as 9.479999542236328125.  Performing calculations with these approximated values can lead to inaccurate results. &amp;lt;ref name=&amp;quot;dobbs&amp;quot;&amp;gt;http://drdobbs.com/java/184405653&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another deficiency of floating point numbers is that they lose precision as the number gets larger. This is less of a problem when dealing with small amounts of currency, but when dealing with large amounts, on the order of the United States federal debt, the number of significant figures to the right of the decimal point decreases, again leading to possibly inaccurate results.&amp;lt;ref&amp;gt;Skrien, Dale. ''Object-Oriented Design Using Java''. McGraw Hill, 2009, p. 174.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Two Integers===&lt;br /&gt;
For currencies with a fractional component, using two integers to represent one amount of money is an option.  One integer to represent the main component and one to represent the fractional part.  For US currency, this would mean an integer for dollars and an integer for cents.  This representation makes displaying monetary values simple, as each component is already split out and accurate.  Also, by using integers, calculations with money work out as they should, avoiding the loss of accuracy floating points have.  However, code will need to be implemented to handle when the fraction component carries over.  In the case of dollars and cents, if the cents exceed 100, the cents value should be decreased by 100 and the dollars increased by 1.  This handling of carries adds overhead to calculations.  The main drawbacks of this implementation is the added overhead and storage required. &amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===One Integer, Implied Decimal Point===&lt;br /&gt;
One integer can be used to represent money, even including the fractional component. The location of the decimal place is simply implied based on the currency.  For instance, a single integer representation of $12.34 would be 1234. When dealing with US currency, this exploits the fact that the fundamental unit of currency in the US is actually the cent, not the dollar &amp;lt;ref name=&amp;quot;dobbs&amp;quot;/&amp;gt;. A specialized print method would have to be constructed to display the monetary amount properly to the user, but all calculations work out properly.  This approach has the same advantage that using two integers of maintaining accuracy, but does not have the added overhead or storage.  In ''Object-Oriented Design Using Java'', Skrien considers this to be the best use of the primitive types for representing money.&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===BigDecimal===&lt;br /&gt;
Java offers a [http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html BigDecimal] class, which many people use for representing money.  BigDecimal is capable of storing any size number since it stores each digit as an entry in an array, which grows as necessary.  It also allows for specification of both the rounding method and the scale, the number of digits past the decimal place, for proper monetary calculations. &amp;lt;ref&amp;gt;http://www.javaranch.com/journal/2003/07/MoneyInJava.html&amp;lt;/ref&amp;gt;  On the downside, this specification for rounding and scale is required, which adds code to the implementation. &amp;lt;ref name=&amp;quot;dobbs&amp;quot; /&amp;gt;BigDecimal values are also immutable, which avoids read/write and write/write conflicts in applications. [http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial]  While there are a lot of nice things about BigDecimal, there are some more issues with BigDecimal which can cause problems in representing money.  BigDecimal cannot represent numbers with infinite digits such as 1/9 (.11111...) and throws an ArithmeticException on any calculation that would produce such number.  To deal with this, hacks such as the following must be implemented:&lt;br /&gt;
 BigDecimal bd1 = BigDecimal(1);&lt;br /&gt;
 BigDecimal bd2 = BigDecimal(9);&lt;br /&gt;
 try {&lt;br /&gt;
     return bd1.divide(bd2);&lt;br /&gt;
 } catch(ArithmaticException ae) {&lt;br /&gt;
     return new BigDecimal(bd1.doubleValue() / bd2.doubleValue());&lt;br /&gt;
 }&lt;br /&gt;
Also, since there isn't a BigDecimal equivalent for SQL databases, so storing BigDecimal values either requires conversion to a different format, which can affect accuracy and/or precision, or the value must be stored as a BLOB, which is cumbersome and inefficient.  In general use, calculations with BigDecimal are significantly slower than with integers, and also take up more storage space. &amp;lt;ref&amp;gt;http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Class===&lt;br /&gt;
In ''Object-Oriented Design Using Java'', recommends using a class to represent money, rather than just using a long integer alone.  He gives two reasons for using a class.  The first is encapsulation - by encapsulating money into a class, all the operations specific to working with money can be implemented to work as desired in all cases, which also avoids code duplication.  The second reason is that the compiler is better able to detect coding errors, such as adding money to something that isn't money. &amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt; Implementing Money as a class is a generally good idea, but there are multiple ways to go about it.&lt;br /&gt;
&lt;br /&gt;
====Abstract Class====&lt;br /&gt;
If multiple kinds of currencies are needed to be represented, one implementation is to create an abstract Money class and have each possible currency be a subclass of the abstract class.  Since there are over 180 different currencies worldwide, it can be a challenge to implement subclasses for each one required.&lt;br /&gt;
&lt;br /&gt;
The following is the abstract class proposed by Skrien in Object-Oriented Design Using Java:&lt;br /&gt;
 public abstract class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public long getAmount()&lt;br /&gt;
   public String toString()&lt;br /&gt;
   public int compareTo(Money m)&lt;br /&gt;
   public boolean equals(Object o)&lt;br /&gt;
   public int hashCode()&lt;br /&gt;
   public Money plus(Money)&lt;br /&gt;
   public Money minus(Money)&lt;br /&gt;
   public Money times(double factor)&lt;br /&gt;
   public Money dividedBy(double divisor)&lt;br /&gt;
   public Money negate()&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Single Class====&lt;br /&gt;
Rather than implementing a variety of subclasses for each currency, a single class can be used to represent money, with currency specified within.  This is a good idea, since currencies basically operate the same calculation-wise, but have variations in decimal locations and the symbols used to present them.  Java has a [http://docs.oracle.com/javase/6/docs/api/java/util/Currency.html Currency class] which holds the information for a given currency.&lt;br /&gt;
&lt;br /&gt;
The following is the Single Class implementation given by Skrien in ''Object-Oriented Design Using Java'':&lt;br /&gt;
&lt;br /&gt;
 public class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   private long amount;&lt;br /&gt;
   private Currency currency;&lt;br /&gt;
   public Money(long amount, Currency currency) {&lt;br /&gt;
     this.amount = amount;&lt;br /&gt;
     this.currency = currency;&lt;br /&gt;
   }&lt;br /&gt;
   public long getAmount() { return amount; }&lt;br /&gt;
   public Currency getCurrency() { &lt;br /&gt;
     return currency; }&lt;br /&gt;
   public String toString() { ...above... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Mixed Money====&lt;br /&gt;
&lt;br /&gt;
====Using an Interface====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Resources==&lt;br /&gt;
*Need to go through these for information&lt;br /&gt;
**[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf PDF from class lecture]&lt;br /&gt;
**[http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Java's Currency class docs]&lt;br /&gt;
**[http://drdobbs.com/java/184405653 A standalone class to tackle the problem (in Java)]&lt;br /&gt;
**[http://www.javapractices.com/topic/TopicAction.do?Id=13 Representing money (Java)]&lt;br /&gt;
**[http://www.javaranch.com/journal/2003/07/MoneyInJava.html Working with money in Java]&lt;br /&gt;
**[http://timeandmoney.sourceforge.net/ Time and Money Java project - doesn't seem to have much documentation, though]&lt;br /&gt;
**[http://joda-money.sourceforge.net/ Joda Money class library for Java]&lt;br /&gt;
**[http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/ BigDecimal and Your Money (a criticism of using BigDecimal format)]&lt;br /&gt;
**[http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial Tutorial on using BigDecimal]&lt;br /&gt;
**[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests (has a Money class example]&lt;br /&gt;
**[http://www.xencraft.com/resources/multi-currency.html Currency Internationalization (i18n), Multiple Currencies and Foreign Exchange (FX)] -nothing programming-wise, but a good overview of the considerations for currency&lt;br /&gt;
**[http://www.di-mare.com/adolfo/p/money.htm Yet Another C++ Money Class ]&lt;br /&gt;
**[http://edenti.deis.unibo.it/utils/Java-tips/Currency%20representation.txt Representing currencies (txt document with example and details)]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56368</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a or</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56368"/>
		<updated>2011-12-01T03:23:42Z</updated>

		<summary type="html">&lt;p&gt;Opevans: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Wiki Chapter: CSC/ECE 517 Fall 2011/ch7 7a or&lt;br /&gt;
&lt;br /&gt;
''7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
Money is very important to people worldwide and is something that often must be represented in computer programs.  This raises the question of how money should be represented, which can vary from program to program.  There is much to be considered such as string formatting, the way money is used, and the variety of different currencies in the world.  In Chapter 6 of Skrien's ''Object-Oriented Design Using Java'', he suggests an implementation that represents money as a class, but there are other approaches that can be taken, each with their respective advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Properties of Money==&lt;br /&gt;
Money has certain properties and uses that must be considered in implementation.&lt;br /&gt;
&lt;br /&gt;
===Responsibilities/Using Money===&lt;br /&gt;
The following are ways in which a representation of money should be able to be used: &amp;lt;ref name=&amp;quot;lec20&amp;quot;&amp;gt;http://courses.ncsu.edu/csc517/common/lectures/notes/lec20.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
*To represent a positive or negative amount of money.&lt;br /&gt;
*Possibly representing multiple components of a currency - i.e. dollars and cents.&lt;br /&gt;
*Possibly representing amounts of multiple currencies - i.e. a mix of US Dollars, euros, and yen.'&lt;br /&gt;
*To have a string formatting potentially including currency symbols like the dollar sign ($) and/or commas and/or decimal points.&lt;br /&gt;
*Addition, subtraction, multiplication, and division.&lt;br /&gt;
The degree to which of the above must be met will depend on the depth of the implementing project.&lt;br /&gt;
&lt;br /&gt;
===Different Currencies===&lt;br /&gt;
Some applications may only handle only one currency such as the US Dollar, but more generally applied applications, such as banking, must be able to handle different kinds of currencies.&lt;br /&gt;
&lt;br /&gt;
There are currently over 180 currencies in use around the world.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/List_of_circulating_currencies&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Implementation must be aware of how to specifically represent each currency used.  Some currencies use decimal points, such as how the US has 100 cents to a dollar, while others have no decimal, such as yen.  Each currency also has a different sign symbol used such as $, €, or ¥.  Different currencies also have different rules about how rounding values is handled.&lt;br /&gt;
&lt;br /&gt;
Special consideration must be taken when using different currencies in conjunction with each other.  Foreign exchange rates can be used to convert between different currencies, but the exchange rate often change, so attention must be paid to that.  In some cases, it is also important to keep track of how much money the user has of more than one currency at a time, so that must be handled somehow. &amp;lt;ref&amp;gt;http://www.xencraft.com/resources/multi-currency.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Immutable vs. Mutable===&lt;br /&gt;
Money values can be either mutable, meaning changeable, or immutable, meaning unchanging.  Mutable money values are useful in that a new value does not need to be created for every change.  However, mutable values cause problems for use in hash maps or with concurrency.  The issue with concurrency is a read and write issue in a system with multiple users.  With changing values, money not be what the user expects and lead to unpredictable behavior.  Skrien feels this is a significant enough disadvantage to prefer an immutable representation for money.&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Possible Representations==&lt;br /&gt;
There are several possible ways to represent money in code.&lt;br /&gt;
&lt;br /&gt;
===Floating Point Number===&lt;br /&gt;
Floating point numbers seem like an appealing option since they already include a fractional component, which lends itself straightforward to currencies like the US dollar which also have a fractional component.  However, floating point numbers come with a drawback due to the way floats are formatted.  Since floats are based on binary representation rather than a decimal representation, a value that would be exact in decimal notation can only be stored approximately in a floats point number.  For instance, 9.48 is stored as 9.479999542236328125.  Performing calculations with these approximated values can lead to inaccurate results. &amp;lt;ref name=&amp;quot;dobbs&amp;quot;&amp;gt;http://drdobbs.com/java/184405653&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another deficiency of floating point numbers is that they lose precision as the number gets larger. This is less of a problem when dealing with small amounts of currency, but when dealing with large amounts, on the order of the United States federal debt, the number of significant figures to the right of the decimal point decreases, again leading to possibly inaccurate results.&amp;lt;ref&amp;gt;Skrien, Dale. ''Object-Oriented Design Using Java''. McGraw Hill, 2009, p. 174.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Two Integers===&lt;br /&gt;
For currencies with a fractional component, using two integers to represent one amount of money is an option.  One integer to represent the main component and one to represent the fractional part.  For US currency, this would mean an integer for dollars and an integer for cents.  This representation makes displaying monetary values simple, as each component is already split out and accurate.  Also, by using integers, calculations with money work out as they should, avoiding the loss of accuracy floating points have.  However, code will need to be implemented to handle when the fraction component carries over.  In the case of dollars and cents, if the cents exceed 100, the cents value should be decreased by 100 and the dollars increased by 1.  This handling of carries adds overhead to calculations.  The main drawbacks of this implementation is the added overhead and storage required. &amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===One Integer, Implied Decimal Point===&lt;br /&gt;
One integer can be used to represent money, even including the fractional component. The location of the decimal place is simply implied based on the currency.  For instance, a single integer representation of $12.34 would be 1234. When dealing with US currency, this exploits the fact that the fundamental unit of currency in the US is actually the cent, not the dollar &amp;lt;ref name=&amp;quot;dobbs&amp;quot;/&amp;gt;. A specialized print method would have to be constructed to display the monetary amount properly to the user, but all calculations work out properly.  This approach has the same advantage that using two integers of maintaining accuracy, but does not have the added overhead or storage.  In ''Object-Oriented Design Using Java'', Skrien considers this to be the best use of the primitive types for representing money.&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===BigDecimal===&lt;br /&gt;
Java offers a [http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html BigDecimal] class, which many people use for representing money.  BigDecimal is capable of storing any size number since it stores each digit as an entry in an array, which grows as necessary.  It also allows for specification of both the rounding method and the scale, the number of digits past the decimal place, for proper monetary calculations. [http://www.javaranch.com/journal/2003/07/MoneyInJava.html]  On the downside, this specification for rounding and scale is required, which adds code to the implementation. [http://drdobbs.com/java/184405653]  BigDecimal values are also immutable, which avoids read/write and write/write conflicts in applications. [http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial]  While there are a lot of nice things about BigDecimal, there are some more issues with BigDecimal which can cause problems in representing money.  BigDecimal cannot represent numbers with infinite digits such as 1/9 (.11111...) and throws an ArithmeticException on any calculation that would produce such number.  To deal with this, hacks such as the following must be implemented:&lt;br /&gt;
 BigDecimal bd1 = BigDecimal(1);&lt;br /&gt;
 BigDecimal bd2 = BigDecimal(9);&lt;br /&gt;
 try {&lt;br /&gt;
     return bd1.divide(bd2);&lt;br /&gt;
 } catch(ArithmaticException ae) {&lt;br /&gt;
     return new BigDecimal(bd1.doubleValue() / bd2.doubleValue());&lt;br /&gt;
 }&lt;br /&gt;
Also, since there isn't a BigDecimal equivalent for SQL databases, so storing BigDecimal values either requires conversion to a different format, which can affect accuracy and/or precision, or the value must be stored as a BLOB, which is cumbersome and inefficient.  In general use, calculations with BigDecimal are significantly slower than with integers, and also take up more storage space. [http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/]&lt;br /&gt;
&lt;br /&gt;
===Class===&lt;br /&gt;
In ''Object-Oriented Design Using Java'', recommends using a class to represent money, rather than just using a long integer alone.  He gives two reasons for using a class.  The first is encapsulation - by encapsulating money into a class, all the operations specific to working with money can be implemented to work as desired in all cases, which also avoids code duplication.  The second reason is that the compiler is better able to detect coding errors, such as adding money to something that isn't money. &amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt; Implementing Money as a class is a generally good idea, but there are multiple ways to go about it.&lt;br /&gt;
&lt;br /&gt;
====Abstract Class====&lt;br /&gt;
If multiple kinds of currencies are needed to be represented, one implementation is to create an abstract Money class and have each possible currency be a subclass of the abstract class.  Since there are over 180 different currencies worldwide, it can be a challenge to implement subclasses for each one required.&lt;br /&gt;
&lt;br /&gt;
The following is the abstract class proposed by Skrien in Object-Oriented Design Using Java:&lt;br /&gt;
 public abstract class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public long getAmount()&lt;br /&gt;
   public String toString()&lt;br /&gt;
   public int compareTo(Money m)&lt;br /&gt;
   public boolean equals(Object o)&lt;br /&gt;
   public int hashCode()&lt;br /&gt;
   public Money plus(Money)&lt;br /&gt;
   public Money minus(Money)&lt;br /&gt;
   public Money times(double factor)&lt;br /&gt;
   public Money dividedBy(double divisor)&lt;br /&gt;
   public Money negate()&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Single Class====&lt;br /&gt;
Rather than implementing a variety of subclasses for each currency, a single class can be used to represent money, with currency specified within.  This is a good idea, since currencies basically operate the same calculation-wise, but have variations in decimal locations and the symbols used to present them.  Java has a [http://docs.oracle.com/javase/6/docs/api/java/util/Currency.html Currency class] which holds the information for a given currency.&lt;br /&gt;
&lt;br /&gt;
The following is the Single Class implementation given by Skrien in ''Object-Oriented Design Using Java'':&lt;br /&gt;
&lt;br /&gt;
 public class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   private long amount;&lt;br /&gt;
   private Currency currency;&lt;br /&gt;
   public Money(long amount, Currency currency) {&lt;br /&gt;
     this.amount = amount;&lt;br /&gt;
     this.currency = currency;&lt;br /&gt;
   }&lt;br /&gt;
   public long getAmount() { return amount; }&lt;br /&gt;
   public Currency getCurrency() { &lt;br /&gt;
     return currency; }&lt;br /&gt;
   public String toString() { ...above... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;ref name=&amp;quot;lec20&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Mixed Money====&lt;br /&gt;
&lt;br /&gt;
====Using an Interface====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Resources==&lt;br /&gt;
*Need to go through these for information&lt;br /&gt;
**[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf PDF from class lecture]&lt;br /&gt;
**[http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Java's Currency class docs]&lt;br /&gt;
**[http://drdobbs.com/java/184405653 A standalone class to tackle the problem (in Java)]&lt;br /&gt;
**[http://www.javapractices.com/topic/TopicAction.do?Id=13 Representing money (Java)]&lt;br /&gt;
**[http://www.javaranch.com/journal/2003/07/MoneyInJava.html Working with money in Java]&lt;br /&gt;
**[http://timeandmoney.sourceforge.net/ Time and Money Java project - doesn't seem to have much documentation, though]&lt;br /&gt;
**[http://joda-money.sourceforge.net/ Joda Money class library for Java]&lt;br /&gt;
**[http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/ BigDecimal and Your Money (a criticism of using BigDecimal format)]&lt;br /&gt;
**[http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial Tutorial on using BigDecimal]&lt;br /&gt;
**[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests (has a Money class example]&lt;br /&gt;
**[http://www.xencraft.com/resources/multi-currency.html Currency Internationalization (i18n), Multiple Currencies and Foreign Exchange (FX)] -nothing programming-wise, but a good overview of the considerations for currency&lt;br /&gt;
**[http://www.di-mare.com/adolfo/p/money.htm Yet Another C++ Money Class ]&lt;br /&gt;
**[http://edenti.deis.unibo.it/utils/Java-tips/Currency%20representation.txt Representing currencies (txt document with example and details)]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56367</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a or</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56367"/>
		<updated>2011-12-01T03:12:14Z</updated>

		<summary type="html">&lt;p&gt;Opevans: /* Different Currencies */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Wiki Chapter: CSC/ECE 517 Fall 2011/ch7 7a or&lt;br /&gt;
&lt;br /&gt;
''7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
Money is very important to people worldwide and is something that often must be represented in computer programs.  This raises the question of how money should be represented, which can vary from program to program.  There is much to be considered such as string formatting, the way money is used, and the variety of different currencies in the world.  In Chapter 6 of Skrien's ''Object-Oriented Design Using Java'', he suggests an implementation that represents money as a class, but there are other approaches that can be taken, each with their respective advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Properties of Money==&lt;br /&gt;
Money has certain properties and uses that must be considered in implementation.&lt;br /&gt;
&lt;br /&gt;
===Responsibilities/Using Money===&lt;br /&gt;
The following are ways in which a representation of money should be able to be used: [http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
*To represent a positive or negative amount of money.&lt;br /&gt;
*Possibly representing multiple components of a currency - i.e. dollars and cents.&lt;br /&gt;
*Possibly representing amounts of multiple currencies - i.e. a mix of US Dollars, euros, and yen.'&lt;br /&gt;
*To have a string formatting potentially including currency symbols like the dollar sign ($) and/or commas and/or decimal points.&lt;br /&gt;
*Addition, subtraction, multiplication, and division.&lt;br /&gt;
The degree to which of the above must be met will depend on the depth of the implementing project.&lt;br /&gt;
&lt;br /&gt;
===Different Currencies===&lt;br /&gt;
Some applications may only handle only one currency such as the US Dollar, but more generally applied applications, such as banking, must be able to handle different kinds of currencies.&lt;br /&gt;
&lt;br /&gt;
There are currently over 180 currencies in use around the world.[http://en.wikipedia.org/wiki/List_of_circulating_currencies]&lt;br /&gt;
&lt;br /&gt;
Implementation must be aware of how to specifically represent each currency used.  Some currencies use decimal points, such as how the US has 100 cents to a dollar, while others have no decimal, such as yen.  Each currency also has a different sign symbol used such as $, €, or ¥.  Different currencies also have different rules about how rounding values is handled.&lt;br /&gt;
&lt;br /&gt;
Special consideration must be taken when using different currencies in conjunction with each other.  Foreign exchange rates can be used to convert between different currencies, but the exchange rate often change, so attention must be paid to that.  In some cases, it is also important to keep track of how much money the user has of more than one currency at a time, so that must be handled somehow. &amp;lt;ref&amp;gt;http://www.xencraft.com/resources/multi-currency.html&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Immutable vs. Mutable===&lt;br /&gt;
Money values can be either mutable, meaning changeable, or immutable, meaning unchanging.  Mutable money values are useful in that a new value does not need to be created for every change.  However, mutable values cause problems for use in hash maps or with concurrency.  The issue with concurrency is a read and write issue in a system with multiple users.  With changing values, money not be what the user expects and lead to unpredictable behavior.  Skrien feels this is a significant enough disadvantage to prefer an immutable representation for money.[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
==Possible Representations==&lt;br /&gt;
There are several possible ways to represent money in code.&lt;br /&gt;
&lt;br /&gt;
===Floating Point Number===&lt;br /&gt;
Floating point numbers seem like an appealing option since they already include a fractional component, which lends itself straightforward to currencies like the US dollar which also have a fractional component.  However, floating point numbers come with a drawback due to the way floats are formatted.  Since floats are based on binary representation rather than a decimal representation, a value that would be exact in decimal notation can only be stored approximately in a floats point number.  For instance, 9.48 is stored as 9.479999542236328125.  Performing calculations with these approximated values can lead to inaccurate results. [http://drdobbs.com/java/184405653]&lt;br /&gt;
&lt;br /&gt;
Another deficiency of floating point numbers is that they lose precision as the number gets larger. This is less of a problem when dealing with small amounts of currency, but when dealing with large amounts, on the order of the United States federal debt, the number of significant figures to the right of the decimal point decreases, again leading to possibly inaccurate results.&amp;lt;ref&amp;gt;Skrien, Dale. ''Object-Oriented Design Using Java''. McGraw Hill, 2009, p. 174.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Two Integers===&lt;br /&gt;
For currencies with a fractional component, using two integers to represent one amount of money is an option.  One integer to represent the main component and one to represent the fractional part.  For US currency, this would mean an integer for dollars and an integer for cents.  This representation makes displaying monetary values simple, as each component is already split out and accurate.  Also, by using integers, calculations with money work out as they should, avoiding the loss of accuracy floating points have.  However, code will need to be implemented to handle when the fraction component carries over.  In the case of dollars and cents, if the cents exceed 100, the cents value should be decreased by 100 and the dollars increased by 1.  This handling of carries adds overhead to calculations.  The main drawbacks of this implementation is the added overhead and storage required. [http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
===One Integer, Implied Decimal Point===&lt;br /&gt;
One integer can be used to represent money, even including the fractional component. The location of the decimal place is simply implied based on the currency.  For instance, a single integer representation of $12.34 would be 1234. When dealing with US currency, this exploits the fact that the fundamental unit of currency in the US is actually the cent, not the dollar &amp;lt;ref&amp;gt;http://drdobbs.com/java/184405653&amp;lt;/ref&amp;gt;. A specialized print method would have to be constructed to display the monetary amount properly to the user, but all calculations work out properly.  This approach has the same advantage that using two integers of maintaining accuracy, but does not have the added overhead or storage.  In ''Object-Oriented Design Using Java'', Skrien considers this to be the best use of the primitive types for representing money.&amp;lt;ref&amp;gt;http://courses.ncsu.edu/csc517/common/lectures/notes/lec20.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===BigDecimal===&lt;br /&gt;
Java offers a [http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html BigDecimal] class, which many people use for representing money.  BigDecimal is capable of storing any size number since it stores each digit as an entry in an array, which grows as necessary.  It also allows for specification of both the rounding method and the scale, the number of digits past the decimal place, for proper monetary calculations. [http://www.javaranch.com/journal/2003/07/MoneyInJava.html]  On the downside, this specification for rounding and scale is required, which adds code to the implementation. [http://drdobbs.com/java/184405653]  BigDecimal values are also immutable, which avoids read/write and write/write conflicts in applications. [http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial]  While there are a lot of nice things about BigDecimal, there are some more issues with BigDecimal which can cause problems in representing money.  BigDecimal cannot represent numbers with infinite digits such as 1/9 (.11111...) and throws an ArithmeticException on any calculation that would produce such number.  To deal with this, hacks such as the following must be implemented:&lt;br /&gt;
 BigDecimal bd1 = BigDecimal(1);&lt;br /&gt;
 BigDecimal bd2 = BigDecimal(9);&lt;br /&gt;
 try {&lt;br /&gt;
     return bd1.divide(bd2);&lt;br /&gt;
 } catch(ArithmaticException ae) {&lt;br /&gt;
     return new BigDecimal(bd1.doubleValue() / bd2.doubleValue());&lt;br /&gt;
 }&lt;br /&gt;
Also, since there isn't a BigDecimal equivalent for SQL databases, so storing BigDecimal values either requires conversion to a different format, which can affect accuracy and/or precision, or the value must be stored as a BLOB, which is cumbersome and inefficient.  In general use, calculations with BigDecimal are significantly slower than with integers, and also take up more storage space. [http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/]&lt;br /&gt;
&lt;br /&gt;
===Class===&lt;br /&gt;
In ''Object-Oriented Design Using Java'', recommends using a class to represent money, rather than just using a long integer alone.  He gives two reasons for using a class.  The first is encapsulation - by encapsulating money into a class, all the operations specific to working with money can be implemented to work as desired in all cases, which also avoids code duplication.  The second reason is that the compiler is better able to detect coding errors, such as adding money to something that isn't money. [http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf] Implementing Money as a class is a generally good idea, but there are multiple ways to go about it.&lt;br /&gt;
&lt;br /&gt;
====Abstract Class====&lt;br /&gt;
If multiple kinds of currencies are needed to be represented, one implementation is to create an abstract Money class and have each possible currency be a subclass of the abstract class.  Since there are over 180 different currencies worldwide, it can be a challenge to implement subclasses for each one required.&lt;br /&gt;
&lt;br /&gt;
The following is the abstract class proposed by Skrien in Object-Oriented Design Using Java:&lt;br /&gt;
 public abstract class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public long getAmount()&lt;br /&gt;
   public String toString()&lt;br /&gt;
   public int compareTo(Money m)&lt;br /&gt;
   public boolean equals(Object o)&lt;br /&gt;
   public int hashCode()&lt;br /&gt;
   public Money plus(Money)&lt;br /&gt;
   public Money minus(Money)&lt;br /&gt;
   public Money times(double factor)&lt;br /&gt;
   public Money dividedBy(double divisor)&lt;br /&gt;
   public Money negate()&lt;br /&gt;
 }&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
====Single Class====&lt;br /&gt;
Rather than implementing a variety of subclasses for each currency, a single class can be used to represent money, with currency specified within.  This is a good idea, since currencies basically operate the same calculation-wise, but have variations in decimal locations and the symbols used to present them.  Java has a [http://docs.oracle.com/javase/6/docs/api/java/util/Currency.html Currency class] which holds the information for a given currency.&lt;br /&gt;
&lt;br /&gt;
The following is the Single Class implementation given by Skrien in ''Object-Oriented Design Using Java'':&lt;br /&gt;
&lt;br /&gt;
 public class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   private long amount;&lt;br /&gt;
   private Currency currency;&lt;br /&gt;
   public Money(long amount, Currency currency) {&lt;br /&gt;
     this.amount = amount;&lt;br /&gt;
     this.currency = currency;&lt;br /&gt;
   }&lt;br /&gt;
   public long getAmount() { return amount; }&lt;br /&gt;
   public Currency getCurrency() { &lt;br /&gt;
     return currency; }&lt;br /&gt;
   public String toString() { ...above... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
 }&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
====Mixed Money====&lt;br /&gt;
&lt;br /&gt;
====Using an Interface====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Resources==&lt;br /&gt;
*Need to go through these for information&lt;br /&gt;
**[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf PDF from class lecture]&lt;br /&gt;
**[http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Java's Currency class docs]&lt;br /&gt;
**[http://drdobbs.com/java/184405653 A standalone class to tackle the problem (in Java)]&lt;br /&gt;
**[http://www.javapractices.com/topic/TopicAction.do?Id=13 Representing money (Java)]&lt;br /&gt;
**[http://www.javaranch.com/journal/2003/07/MoneyInJava.html Working with money in Java]&lt;br /&gt;
**[http://timeandmoney.sourceforge.net/ Time and Money Java project - doesn't seem to have much documentation, though]&lt;br /&gt;
**[http://joda-money.sourceforge.net/ Joda Money class library for Java]&lt;br /&gt;
**[http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/ BigDecimal and Your Money (a criticism of using BigDecimal format)]&lt;br /&gt;
**[http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial Tutorial on using BigDecimal]&lt;br /&gt;
**[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests (has a Money class example]&lt;br /&gt;
**[http://www.xencraft.com/resources/multi-currency.html Currency Internationalization (i18n), Multiple Currencies and Foreign Exchange (FX)] -nothing programming-wise, but a good overview of the considerations for currency&lt;br /&gt;
**[http://www.di-mare.com/adolfo/p/money.htm Yet Another C++ Money Class ]&lt;br /&gt;
**[http://edenti.deis.unibo.it/utils/Java-tips/Currency%20representation.txt Representing currencies (txt document with example and details)]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56366</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a or</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56366"/>
		<updated>2011-12-01T03:11:14Z</updated>

		<summary type="html">&lt;p&gt;Opevans: /* One Integer, Implied Decimal Point */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Wiki Chapter: CSC/ECE 517 Fall 2011/ch7 7a or&lt;br /&gt;
&lt;br /&gt;
''7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
Money is very important to people worldwide and is something that often must be represented in computer programs.  This raises the question of how money should be represented, which can vary from program to program.  There is much to be considered such as string formatting, the way money is used, and the variety of different currencies in the world.  In Chapter 6 of Skrien's ''Object-Oriented Design Using Java'', he suggests an implementation that represents money as a class, but there are other approaches that can be taken, each with their respective advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Properties of Money==&lt;br /&gt;
Money has certain properties and uses that must be considered in implementation.&lt;br /&gt;
&lt;br /&gt;
===Responsibilities/Using Money===&lt;br /&gt;
The following are ways in which a representation of money should be able to be used: [http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
*To represent a positive or negative amount of money.&lt;br /&gt;
*Possibly representing multiple components of a currency - i.e. dollars and cents.&lt;br /&gt;
*Possibly representing amounts of multiple currencies - i.e. a mix of US Dollars, euros, and yen.'&lt;br /&gt;
*To have a string formatting potentially including currency symbols like the dollar sign ($) and/or commas and/or decimal points.&lt;br /&gt;
*Addition, subtraction, multiplication, and division.&lt;br /&gt;
The degree to which of the above must be met will depend on the depth of the implementing project.&lt;br /&gt;
&lt;br /&gt;
===Different Currencies===&lt;br /&gt;
Some applications may only handle only one currency such as the US Dollar, but more generally applied applications, such as banking, must be able to handle different kinds of currencies.&lt;br /&gt;
&lt;br /&gt;
There are currently over 180 currencies in use around the world.[http://en.wikipedia.org/wiki/List_of_circulating_currencies]&lt;br /&gt;
&lt;br /&gt;
Implementation must be aware of how to specifically represent each currency used.  Some currencies use decimal points, such as how the US has 100 cents to a dollar, while others have no decimal, such as yen.  Each currency also has a different sign symbol used such as $, €, or ¥.  Different currencies also have different rules about how rounding values is handled.&lt;br /&gt;
&lt;br /&gt;
Special consideration must be taken when using different currencies in conjunction with each other.  Foreign exchange rates can be used to convert between different currencies, but the exchange rate often change, so attention must be paid to that.  In some cases, it is also important to keep track of how much money the user has of more than one currency at a time, so that must be handled somehow. [http://www.xencraft.com/resources/multi-currency.html][http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
===Immutable vs. Mutable===&lt;br /&gt;
Money values can be either mutable, meaning changeable, or immutable, meaning unchanging.  Mutable money values are useful in that a new value does not need to be created for every change.  However, mutable values cause problems for use in hash maps or with concurrency.  The issue with concurrency is a read and write issue in a system with multiple users.  With changing values, money not be what the user expects and lead to unpredictable behavior.  Skrien feels this is a significant enough disadvantage to prefer an immutable representation for money.[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
==Possible Representations==&lt;br /&gt;
There are several possible ways to represent money in code.&lt;br /&gt;
&lt;br /&gt;
===Floating Point Number===&lt;br /&gt;
Floating point numbers seem like an appealing option since they already include a fractional component, which lends itself straightforward to currencies like the US dollar which also have a fractional component.  However, floating point numbers come with a drawback due to the way floats are formatted.  Since floats are based on binary representation rather than a decimal representation, a value that would be exact in decimal notation can only be stored approximately in a floats point number.  For instance, 9.48 is stored as 9.479999542236328125.  Performing calculations with these approximated values can lead to inaccurate results. [http://drdobbs.com/java/184405653]&lt;br /&gt;
&lt;br /&gt;
Another deficiency of floating point numbers is that they lose precision as the number gets larger. This is less of a problem when dealing with small amounts of currency, but when dealing with large amounts, on the order of the United States federal debt, the number of significant figures to the right of the decimal point decreases, again leading to possibly inaccurate results.&amp;lt;ref&amp;gt;Skrien, Dale. ''Object-Oriented Design Using Java''. McGraw Hill, 2009, p. 174.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Two Integers===&lt;br /&gt;
For currencies with a fractional component, using two integers to represent one amount of money is an option.  One integer to represent the main component and one to represent the fractional part.  For US currency, this would mean an integer for dollars and an integer for cents.  This representation makes displaying monetary values simple, as each component is already split out and accurate.  Also, by using integers, calculations with money work out as they should, avoiding the loss of accuracy floating points have.  However, code will need to be implemented to handle when the fraction component carries over.  In the case of dollars and cents, if the cents exceed 100, the cents value should be decreased by 100 and the dollars increased by 1.  This handling of carries adds overhead to calculations.  The main drawbacks of this implementation is the added overhead and storage required. [http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
===One Integer, Implied Decimal Point===&lt;br /&gt;
One integer can be used to represent money, even including the fractional component. The location of the decimal place is simply implied based on the currency.  For instance, a single integer representation of $12.34 would be 1234. When dealing with US currency, this exploits the fact that the fundamental unit of currency in the US is actually the cent, not the dollar &amp;lt;ref&amp;gt;http://drdobbs.com/java/184405653&amp;lt;/ref&amp;gt;. A specialized print method would have to be constructed to display the monetary amount properly to the user, but all calculations work out properly.  This approach has the same advantage that using two integers of maintaining accuracy, but does not have the added overhead or storage.  In ''Object-Oriented Design Using Java'', Skrien considers this to be the best use of the primitive types for representing money.&amp;lt;ref&amp;gt;http://courses.ncsu.edu/csc517/common/lectures/notes/lec20.pdf&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===BigDecimal===&lt;br /&gt;
Java offers a [http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html BigDecimal] class, which many people use for representing money.  BigDecimal is capable of storing any size number since it stores each digit as an entry in an array, which grows as necessary.  It also allows for specification of both the rounding method and the scale, the number of digits past the decimal place, for proper monetary calculations. [http://www.javaranch.com/journal/2003/07/MoneyInJava.html]  On the downside, this specification for rounding and scale is required, which adds code to the implementation. [http://drdobbs.com/java/184405653]  BigDecimal values are also immutable, which avoids read/write and write/write conflicts in applications. [http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial]  While there are a lot of nice things about BigDecimal, there are some more issues with BigDecimal which can cause problems in representing money.  BigDecimal cannot represent numbers with infinite digits such as 1/9 (.11111...) and throws an ArithmeticException on any calculation that would produce such number.  To deal with this, hacks such as the following must be implemented:&lt;br /&gt;
 BigDecimal bd1 = BigDecimal(1);&lt;br /&gt;
 BigDecimal bd2 = BigDecimal(9);&lt;br /&gt;
 try {&lt;br /&gt;
     return bd1.divide(bd2);&lt;br /&gt;
 } catch(ArithmaticException ae) {&lt;br /&gt;
     return new BigDecimal(bd1.doubleValue() / bd2.doubleValue());&lt;br /&gt;
 }&lt;br /&gt;
Also, since there isn't a BigDecimal equivalent for SQL databases, so storing BigDecimal values either requires conversion to a different format, which can affect accuracy and/or precision, or the value must be stored as a BLOB, which is cumbersome and inefficient.  In general use, calculations with BigDecimal are significantly slower than with integers, and also take up more storage space. [http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/]&lt;br /&gt;
&lt;br /&gt;
===Class===&lt;br /&gt;
In ''Object-Oriented Design Using Java'', recommends using a class to represent money, rather than just using a long integer alone.  He gives two reasons for using a class.  The first is encapsulation - by encapsulating money into a class, all the operations specific to working with money can be implemented to work as desired in all cases, which also avoids code duplication.  The second reason is that the compiler is better able to detect coding errors, such as adding money to something that isn't money. [http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf] Implementing Money as a class is a generally good idea, but there are multiple ways to go about it.&lt;br /&gt;
&lt;br /&gt;
====Abstract Class====&lt;br /&gt;
If multiple kinds of currencies are needed to be represented, one implementation is to create an abstract Money class and have each possible currency be a subclass of the abstract class.  Since there are over 180 different currencies worldwide, it can be a challenge to implement subclasses for each one required.&lt;br /&gt;
&lt;br /&gt;
The following is the abstract class proposed by Skrien in Object-Oriented Design Using Java:&lt;br /&gt;
 public abstract class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public long getAmount()&lt;br /&gt;
   public String toString()&lt;br /&gt;
   public int compareTo(Money m)&lt;br /&gt;
   public boolean equals(Object o)&lt;br /&gt;
   public int hashCode()&lt;br /&gt;
   public Money plus(Money)&lt;br /&gt;
   public Money minus(Money)&lt;br /&gt;
   public Money times(double factor)&lt;br /&gt;
   public Money dividedBy(double divisor)&lt;br /&gt;
   public Money negate()&lt;br /&gt;
 }&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
====Single Class====&lt;br /&gt;
Rather than implementing a variety of subclasses for each currency, a single class can be used to represent money, with currency specified within.  This is a good idea, since currencies basically operate the same calculation-wise, but have variations in decimal locations and the symbols used to present them.  Java has a [http://docs.oracle.com/javase/6/docs/api/java/util/Currency.html Currency class] which holds the information for a given currency.&lt;br /&gt;
&lt;br /&gt;
The following is the Single Class implementation given by Skrien in ''Object-Oriented Design Using Java'':&lt;br /&gt;
&lt;br /&gt;
 public class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   private long amount;&lt;br /&gt;
   private Currency currency;&lt;br /&gt;
   public Money(long amount, Currency currency) {&lt;br /&gt;
     this.amount = amount;&lt;br /&gt;
     this.currency = currency;&lt;br /&gt;
   }&lt;br /&gt;
   public long getAmount() { return amount; }&lt;br /&gt;
   public Currency getCurrency() { &lt;br /&gt;
     return currency; }&lt;br /&gt;
   public String toString() { ...above... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
 }&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
====Mixed Money====&lt;br /&gt;
&lt;br /&gt;
====Using an Interface====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Resources==&lt;br /&gt;
*Need to go through these for information&lt;br /&gt;
**[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf PDF from class lecture]&lt;br /&gt;
**[http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Java's Currency class docs]&lt;br /&gt;
**[http://drdobbs.com/java/184405653 A standalone class to tackle the problem (in Java)]&lt;br /&gt;
**[http://www.javapractices.com/topic/TopicAction.do?Id=13 Representing money (Java)]&lt;br /&gt;
**[http://www.javaranch.com/journal/2003/07/MoneyInJava.html Working with money in Java]&lt;br /&gt;
**[http://timeandmoney.sourceforge.net/ Time and Money Java project - doesn't seem to have much documentation, though]&lt;br /&gt;
**[http://joda-money.sourceforge.net/ Joda Money class library for Java]&lt;br /&gt;
**[http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/ BigDecimal and Your Money (a criticism of using BigDecimal format)]&lt;br /&gt;
**[http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial Tutorial on using BigDecimal]&lt;br /&gt;
**[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests (has a Money class example]&lt;br /&gt;
**[http://www.xencraft.com/resources/multi-currency.html Currency Internationalization (i18n), Multiple Currencies and Foreign Exchange (FX)] -nothing programming-wise, but a good overview of the considerations for currency&lt;br /&gt;
**[http://www.di-mare.com/adolfo/p/money.htm Yet Another C++ Money Class ]&lt;br /&gt;
**[http://edenti.deis.unibo.it/utils/Java-tips/Currency%20representation.txt Representing currencies (txt document with example and details)]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56365</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a or</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56365"/>
		<updated>2011-12-01T03:07:00Z</updated>

		<summary type="html">&lt;p&gt;Opevans: /* Floating Point Number */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Wiki Chapter: CSC/ECE 517 Fall 2011/ch7 7a or&lt;br /&gt;
&lt;br /&gt;
''7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
Money is very important to people worldwide and is something that often must be represented in computer programs.  This raises the question of how money should be represented, which can vary from program to program.  There is much to be considered such as string formatting, the way money is used, and the variety of different currencies in the world.  In Chapter 6 of Skrien's ''Object-Oriented Design Using Java'', he suggests an implementation that represents money as a class, but there are other approaches that can be taken, each with their respective advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Properties of Money==&lt;br /&gt;
Money has certain properties and uses that must be considered in implementation.&lt;br /&gt;
&lt;br /&gt;
===Responsibilities/Using Money===&lt;br /&gt;
The following are ways in which a representation of money should be able to be used: [http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
*To represent a positive or negative amount of money.&lt;br /&gt;
*Possibly representing multiple components of a currency - i.e. dollars and cents.&lt;br /&gt;
*Possibly representing amounts of multiple currencies - i.e. a mix of US Dollars, euros, and yen.'&lt;br /&gt;
*To have a string formatting potentially including currency symbols like the dollar sign ($) and/or commas and/or decimal points.&lt;br /&gt;
*Addition, subtraction, multiplication, and division.&lt;br /&gt;
The degree to which of the above must be met will depend on the depth of the implementing project.&lt;br /&gt;
&lt;br /&gt;
===Different Currencies===&lt;br /&gt;
Some applications may only handle only one currency such as the US Dollar, but more generally applied applications, such as banking, must be able to handle different kinds of currencies.&lt;br /&gt;
&lt;br /&gt;
There are currently over 180 currencies in use around the world.[http://en.wikipedia.org/wiki/List_of_circulating_currencies]&lt;br /&gt;
&lt;br /&gt;
Implementation must be aware of how to specifically represent each currency used.  Some currencies use decimal points, such as how the US has 100 cents to a dollar, while others have no decimal, such as yen.  Each currency also has a different sign symbol used such as $, €, or ¥.  Different currencies also have different rules about how rounding values is handled.&lt;br /&gt;
&lt;br /&gt;
Special consideration must be taken when using different currencies in conjunction with each other.  Foreign exchange rates can be used to convert between different currencies, but the exchange rate often change, so attention must be paid to that.  In some cases, it is also important to keep track of how much money the user has of more than one currency at a time, so that must be handled somehow. [http://www.xencraft.com/resources/multi-currency.html][http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
===Immutable vs. Mutable===&lt;br /&gt;
Money values can be either mutable, meaning changeable, or immutable, meaning unchanging.  Mutable money values are useful in that a new value does not need to be created for every change.  However, mutable values cause problems for use in hash maps or with concurrency.  The issue with concurrency is a read and write issue in a system with multiple users.  With changing values, money not be what the user expects and lead to unpredictable behavior.  Skrien feels this is a significant enough disadvantage to prefer an immutable representation for money.[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
==Possible Representations==&lt;br /&gt;
There are several possible ways to represent money in code.&lt;br /&gt;
&lt;br /&gt;
===Floating Point Number===&lt;br /&gt;
Floating point numbers seem like an appealing option since they already include a fractional component, which lends itself straightforward to currencies like the US dollar which also have a fractional component.  However, floating point numbers come with a drawback due to the way floats are formatted.  Since floats are based on binary representation rather than a decimal representation, a value that would be exact in decimal notation can only be stored approximately in a floats point number.  For instance, 9.48 is stored as 9.479999542236328125.  Performing calculations with these approximated values can lead to inaccurate results. [http://drdobbs.com/java/184405653]&lt;br /&gt;
&lt;br /&gt;
Another deficiency of floating point numbers is that they lose precision as the number gets larger. This is less of a problem when dealing with small amounts of currency, but when dealing with large amounts, on the order of the United States federal debt, the number of significant figures to the right of the decimal point decreases, again leading to possibly inaccurate results.&amp;lt;ref&amp;gt;Skrien, Dale. ''Object-Oriented Design Using Java''. McGraw Hill, 2009, p. 174.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Two Integers===&lt;br /&gt;
For currencies with a fractional component, using two integers to represent one amount of money is an option.  One integer to represent the main component and one to represent the fractional part.  For US currency, this would mean an integer for dollars and an integer for cents.  This representation makes displaying monetary values simple, as each component is already split out and accurate.  Also, by using integers, calculations with money work out as they should, avoiding the loss of accuracy floating points have.  However, code will need to be implemented to handle when the fraction component carries over.  In the case of dollars and cents, if the cents exceed 100, the cents value should be decreased by 100 and the dollars increased by 1.  This handling of carries adds overhead to calculations.  The main drawbacks of this implementation is the added overhead and storage required. [http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
===One Integer, Implied Decimal Point===&lt;br /&gt;
One integer can be used to represent money, even including the fractional component.  The location of the decimal place is simply implied based on the currency.  For instance, a single integer representation of $12.34 would be 1234.  A specialized print method would have to be constructed to display the monetary amount properly to the user, but all calculations work out properly.  This approach has the same advantage that using two integers of maintaining accuracy, but does not have the added overhead or storage.  In ''Object-Oriented Design Using Java'', Skrien considers this to be the best use of the primitive types for representing money. [http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
===BigDecimal===&lt;br /&gt;
Java offers a [http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html BigDecimal] class, which many people use for representing money.  BigDecimal is capable of storing any size number since it stores each digit as an entry in an array, which grows as necessary.  It also allows for specification of both the rounding method and the scale, the number of digits past the decimal place, for proper monetary calculations. [http://www.javaranch.com/journal/2003/07/MoneyInJava.html]  On the downside, this specification for rounding and scale is required, which adds code to the implementation. [http://drdobbs.com/java/184405653]  BigDecimal values are also immutable, which avoids read/write and write/write conflicts in applications. [http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial]  While there are a lot of nice things about BigDecimal, there are some more issues with BigDecimal which can cause problems in representing money.  BigDecimal cannot represent numbers with infinite digits such as 1/9 (.11111...) and throws an ArithmeticException on any calculation that would produce such number.  To deal with this, hacks such as the following must be implemented:&lt;br /&gt;
 BigDecimal bd1 = BigDecimal(1);&lt;br /&gt;
 BigDecimal bd2 = BigDecimal(9);&lt;br /&gt;
 try {&lt;br /&gt;
     return bd1.divide(bd2);&lt;br /&gt;
 } catch(ArithmaticException ae) {&lt;br /&gt;
     return new BigDecimal(bd1.doubleValue() / bd2.doubleValue());&lt;br /&gt;
 }&lt;br /&gt;
Also, since there isn't a BigDecimal equivalent for SQL databases, so storing BigDecimal values either requires conversion to a different format, which can affect accuracy and/or precision, or the value must be stored as a BLOB, which is cumbersome and inefficient.  In general use, calculations with BigDecimal are significantly slower than with integers, and also take up more storage space. [http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/]&lt;br /&gt;
&lt;br /&gt;
===Class===&lt;br /&gt;
In ''Object-Oriented Design Using Java'', recommends using a class to represent money, rather than just using a long integer alone.  He gives two reasons for using a class.  The first is encapsulation - by encapsulating money into a class, all the operations specific to working with money can be implemented to work as desired in all cases, which also avoids code duplication.  The second reason is that the compiler is better able to detect coding errors, such as adding money to something that isn't money. [http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf] Implementing Money as a class is a generally good idea, but there are multiple ways to go about it.&lt;br /&gt;
&lt;br /&gt;
====Abstract Class====&lt;br /&gt;
If multiple kinds of currencies are needed to be represented, one implementation is to create an abstract Money class and have each possible currency be a subclass of the abstract class.  Since there are over 180 different currencies worldwide, it can be a challenge to implement subclasses for each one required.&lt;br /&gt;
&lt;br /&gt;
The following is the abstract class proposed by Skrien in Object-Oriented Design Using Java:&lt;br /&gt;
 public abstract class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public long getAmount()&lt;br /&gt;
   public String toString()&lt;br /&gt;
   public int compareTo(Money m)&lt;br /&gt;
   public boolean equals(Object o)&lt;br /&gt;
   public int hashCode()&lt;br /&gt;
   public Money plus(Money)&lt;br /&gt;
   public Money minus(Money)&lt;br /&gt;
   public Money times(double factor)&lt;br /&gt;
   public Money dividedBy(double divisor)&lt;br /&gt;
   public Money negate()&lt;br /&gt;
 }&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
====Single Class====&lt;br /&gt;
Rather than implementing a variety of subclasses for each currency, a single class can be used to represent money, with currency specified within.  This is a good idea, since currencies basically operate the same calculation-wise, but have variations in decimal locations and the symbols used to present them.  Java has a [http://docs.oracle.com/javase/6/docs/api/java/util/Currency.html Currency class] which holds the information for a given currency.&lt;br /&gt;
&lt;br /&gt;
The following is the Single Class implementation given by Skrien in ''Object-Oriented Design Using Java'':&lt;br /&gt;
&lt;br /&gt;
 public class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   private long amount;&lt;br /&gt;
   private Currency currency;&lt;br /&gt;
   public Money(long amount, Currency currency) {&lt;br /&gt;
     this.amount = amount;&lt;br /&gt;
     this.currency = currency;&lt;br /&gt;
   }&lt;br /&gt;
   public long getAmount() { return amount; }&lt;br /&gt;
   public Currency getCurrency() { &lt;br /&gt;
     return currency; }&lt;br /&gt;
   public String toString() { ...above... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
 }&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
====Mixed Money====&lt;br /&gt;
&lt;br /&gt;
====Using an Interface====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Resources==&lt;br /&gt;
*Need to go through these for information&lt;br /&gt;
**[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf PDF from class lecture]&lt;br /&gt;
**[http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Java's Currency class docs]&lt;br /&gt;
**[http://drdobbs.com/java/184405653 A standalone class to tackle the problem (in Java)]&lt;br /&gt;
**[http://www.javapractices.com/topic/TopicAction.do?Id=13 Representing money (Java)]&lt;br /&gt;
**[http://www.javaranch.com/journal/2003/07/MoneyInJava.html Working with money in Java]&lt;br /&gt;
**[http://timeandmoney.sourceforge.net/ Time and Money Java project - doesn't seem to have much documentation, though]&lt;br /&gt;
**[http://joda-money.sourceforge.net/ Joda Money class library for Java]&lt;br /&gt;
**[http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/ BigDecimal and Your Money (a criticism of using BigDecimal format)]&lt;br /&gt;
**[http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial Tutorial on using BigDecimal]&lt;br /&gt;
**[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests (has a Money class example]&lt;br /&gt;
**[http://www.xencraft.com/resources/multi-currency.html Currency Internationalization (i18n), Multiple Currencies and Foreign Exchange (FX)] -nothing programming-wise, but a good overview of the considerations for currency&lt;br /&gt;
**[http://www.di-mare.com/adolfo/p/money.htm Yet Another C++ Money Class ]&lt;br /&gt;
**[http://edenti.deis.unibo.it/utils/Java-tips/Currency%20representation.txt Representing currencies (txt document with example and details)]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56364</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a or</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56364"/>
		<updated>2011-12-01T03:04:17Z</updated>

		<summary type="html">&lt;p&gt;Opevans: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Wiki Chapter: CSC/ECE 517 Fall 2011/ch7 7a or&lt;br /&gt;
&lt;br /&gt;
''7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
Money is very important to people worldwide and is something that often must be represented in computer programs.  This raises the question of how money should be represented, which can vary from program to program.  There is much to be considered such as string formatting, the way money is used, and the variety of different currencies in the world.  In Chapter 6 of Skrien's ''Object-Oriented Design Using Java'', he suggests an implementation that represents money as a class, but there are other approaches that can be taken, each with their respective advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Properties of Money==&lt;br /&gt;
Money has certain properties and uses that must be considered in implementation.&lt;br /&gt;
&lt;br /&gt;
===Responsibilities/Using Money===&lt;br /&gt;
The following are ways in which a representation of money should be able to be used: [http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
*To represent a positive or negative amount of money.&lt;br /&gt;
*Possibly representing multiple components of a currency - i.e. dollars and cents.&lt;br /&gt;
*Possibly representing amounts of multiple currencies - i.e. a mix of US Dollars, euros, and yen.'&lt;br /&gt;
*To have a string formatting potentially including currency symbols like the dollar sign ($) and/or commas and/or decimal points.&lt;br /&gt;
*Addition, subtraction, multiplication, and division.&lt;br /&gt;
The degree to which of the above must be met will depend on the depth of the implementing project.&lt;br /&gt;
&lt;br /&gt;
===Different Currencies===&lt;br /&gt;
Some applications may only handle only one currency such as the US Dollar, but more generally applied applications, such as banking, must be able to handle different kinds of currencies.&lt;br /&gt;
&lt;br /&gt;
There are currently over 180 currencies in use around the world.[http://en.wikipedia.org/wiki/List_of_circulating_currencies]&lt;br /&gt;
&lt;br /&gt;
Implementation must be aware of how to specifically represent each currency used.  Some currencies use decimal points, such as how the US has 100 cents to a dollar, while others have no decimal, such as yen.  Each currency also has a different sign symbol used such as $, €, or ¥.  Different currencies also have different rules about how rounding values is handled.&lt;br /&gt;
&lt;br /&gt;
Special consideration must be taken when using different currencies in conjunction with each other.  Foreign exchange rates can be used to convert between different currencies, but the exchange rate often change, so attention must be paid to that.  In some cases, it is also important to keep track of how much money the user has of more than one currency at a time, so that must be handled somehow. [http://www.xencraft.com/resources/multi-currency.html][http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
===Immutable vs. Mutable===&lt;br /&gt;
Money values can be either mutable, meaning changeable, or immutable, meaning unchanging.  Mutable money values are useful in that a new value does not need to be created for every change.  However, mutable values cause problems for use in hash maps or with concurrency.  The issue with concurrency is a read and write issue in a system with multiple users.  With changing values, money not be what the user expects and lead to unpredictable behavior.  Skrien feels this is a significant enough disadvantage to prefer an immutable representation for money.[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
==Possible Representations==&lt;br /&gt;
There are several possible ways to represent money in code.&lt;br /&gt;
&lt;br /&gt;
===Floating Point Number===&lt;br /&gt;
Floating point numbers seem like an appealing option since they already include a fractional component, which lends itself straightforward to currencies like the US dollar which also have a fractional component.  However, floating point numbers come with a drawback due to the way floats are formatted.  Since floats are based on binary representation rather than a decimal representation, a value that would be exact in decimal notation can only be stored approximately in a floats point number.  For instance, 9.48 is stored as 9.479999542236328125.  Performing calculations with these approximated values can lead to inaccurate results. [http://drdobbs.com/java/184405653]&lt;br /&gt;
&lt;br /&gt;
Another deficiency of floating point numbers is that they lose precision as the number gets larger. This is less of a problem when dealing with small amounts of currency, but when dealing with large amounts, on the order of the United States federal debt, the number of significant figures to the right of the decimal point decreases, again leading to possibly inaccurate results, especially with larger numbers. &amp;lt;ref&amp;gt;Skrien, Dale. ''Object-Oriented Design Using Java''. McGraw Hill, 2009, p. 174.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Two Integers===&lt;br /&gt;
For currencies with a fractional component, using two integers to represent one amount of money is an option.  One integer to represent the main component and one to represent the fractional part.  For US currency, this would mean an integer for dollars and an integer for cents.  This representation makes displaying monetary values simple, as each component is already split out and accurate.  Also, by using integers, calculations with money work out as they should, avoiding the loss of accuracy floating points have.  However, code will need to be implemented to handle when the fraction component carries over.  In the case of dollars and cents, if the cents exceed 100, the cents value should be decreased by 100 and the dollars increased by 1.  This handling of carries adds overhead to calculations.  The main drawbacks of this implementation is the added overhead and storage required. [http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
===One Integer, Implied Decimal Point===&lt;br /&gt;
One integer can be used to represent money, even including the fractional component.  The location of the decimal place is simply implied based on the currency.  For instance, a single integer representation of $12.34 would be 1234.  A specialized print method would have to be constructed to display the monetary amount properly to the user, but all calculations work out properly.  This approach has the same advantage that using two integers of maintaining accuracy, but does not have the added overhead or storage.  In ''Object-Oriented Design Using Java'', Skrien considers this to be the best use of the primitive types for representing money. [http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
===BigDecimal===&lt;br /&gt;
Java offers a [http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html BigDecimal] class, which many people use for representing money.  BigDecimal is capable of storing any size number since it stores each digit as an entry in an array, which grows as necessary.  It also allows for specification of both the rounding method and the scale, the number of digits past the decimal place, for proper monetary calculations. [http://www.javaranch.com/journal/2003/07/MoneyInJava.html]  On the downside, this specification for rounding and scale is required, which adds code to the implementation. [http://drdobbs.com/java/184405653]  BigDecimal values are also immutable, which avoids read/write and write/write conflicts in applications. [http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial]  While there are a lot of nice things about BigDecimal, there are some more issues with BigDecimal which can cause problems in representing money.  BigDecimal cannot represent numbers with infinite digits such as 1/9 (.11111...) and throws an ArithmeticException on any calculation that would produce such number.  To deal with this, hacks such as the following must be implemented:&lt;br /&gt;
 BigDecimal bd1 = BigDecimal(1);&lt;br /&gt;
 BigDecimal bd2 = BigDecimal(9);&lt;br /&gt;
 try {&lt;br /&gt;
     return bd1.divide(bd2);&lt;br /&gt;
 } catch(ArithmaticException ae) {&lt;br /&gt;
     return new BigDecimal(bd1.doubleValue() / bd2.doubleValue());&lt;br /&gt;
 }&lt;br /&gt;
Also, since there isn't a BigDecimal equivalent for SQL databases, so storing BigDecimal values either requires conversion to a different format, which can affect accuracy and/or precision, or the value must be stored as a BLOB, which is cumbersome and inefficient.  In general use, calculations with BigDecimal are significantly slower than with integers, and also take up more storage space. [http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/]&lt;br /&gt;
&lt;br /&gt;
===Class===&lt;br /&gt;
In ''Object-Oriented Design Using Java'', recommends using a class to represent money, rather than just using a long integer alone.  He gives two reasons for using a class.  The first is encapsulation - by encapsulating money into a class, all the operations specific to working with money can be implemented to work as desired in all cases, which also avoids code duplication.  The second reason is that the compiler is better able to detect coding errors, such as adding money to something that isn't money. [http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf] Implementing Money as a class is a generally good idea, but there are multiple ways to go about it.&lt;br /&gt;
&lt;br /&gt;
====Abstract Class====&lt;br /&gt;
If multiple kinds of currencies are needed to be represented, one implementation is to create an abstract Money class and have each possible currency be a subclass of the abstract class.  Since there are over 180 different currencies worldwide, it can be a challenge to implement subclasses for each one required.&lt;br /&gt;
&lt;br /&gt;
The following is the abstract class proposed by Skrien in Object-Oriented Design Using Java:&lt;br /&gt;
 public abstract class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public long getAmount()&lt;br /&gt;
   public String toString()&lt;br /&gt;
   public int compareTo(Money m)&lt;br /&gt;
   public boolean equals(Object o)&lt;br /&gt;
   public int hashCode()&lt;br /&gt;
   public Money plus(Money)&lt;br /&gt;
   public Money minus(Money)&lt;br /&gt;
   public Money times(double factor)&lt;br /&gt;
   public Money dividedBy(double divisor)&lt;br /&gt;
   public Money negate()&lt;br /&gt;
 }&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
====Single Class====&lt;br /&gt;
Rather than implementing a variety of subclasses for each currency, a single class can be used to represent money, with currency specified within.  This is a good idea, since currencies basically operate the same calculation-wise, but have variations in decimal locations and the symbols used to present them.  Java has a [http://docs.oracle.com/javase/6/docs/api/java/util/Currency.html Currency class] which holds the information for a given currency.&lt;br /&gt;
&lt;br /&gt;
The following is the Single Class implementation given by Skrien in ''Object-Oriented Design Using Java'':&lt;br /&gt;
&lt;br /&gt;
 public class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   private long amount;&lt;br /&gt;
   private Currency currency;&lt;br /&gt;
   public Money(long amount, Currency currency) {&lt;br /&gt;
     this.amount = amount;&lt;br /&gt;
     this.currency = currency;&lt;br /&gt;
   }&lt;br /&gt;
   public long getAmount() { return amount; }&lt;br /&gt;
   public Currency getCurrency() { &lt;br /&gt;
     return currency; }&lt;br /&gt;
   public String toString() { ...above... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
 }&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
====Mixed Money====&lt;br /&gt;
&lt;br /&gt;
====Using an Interface====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Resources==&lt;br /&gt;
*Need to go through these for information&lt;br /&gt;
**[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf PDF from class lecture]&lt;br /&gt;
**[http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Java's Currency class docs]&lt;br /&gt;
**[http://drdobbs.com/java/184405653 A standalone class to tackle the problem (in Java)]&lt;br /&gt;
**[http://www.javapractices.com/topic/TopicAction.do?Id=13 Representing money (Java)]&lt;br /&gt;
**[http://www.javaranch.com/journal/2003/07/MoneyInJava.html Working with money in Java]&lt;br /&gt;
**[http://timeandmoney.sourceforge.net/ Time and Money Java project - doesn't seem to have much documentation, though]&lt;br /&gt;
**[http://joda-money.sourceforge.net/ Joda Money class library for Java]&lt;br /&gt;
**[http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/ BigDecimal and Your Money (a criticism of using BigDecimal format)]&lt;br /&gt;
**[http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial Tutorial on using BigDecimal]&lt;br /&gt;
**[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests (has a Money class example]&lt;br /&gt;
**[http://www.xencraft.com/resources/multi-currency.html Currency Internationalization (i18n), Multiple Currencies and Foreign Exchange (FX)] -nothing programming-wise, but a good overview of the considerations for currency&lt;br /&gt;
**[http://www.di-mare.com/adolfo/p/money.htm Yet Another C++ Money Class ]&lt;br /&gt;
**[http://edenti.deis.unibo.it/utils/Java-tips/Currency%20representation.txt Representing currencies (txt document with example and details)]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56362</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a or</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56362"/>
		<updated>2011-12-01T03:00:08Z</updated>

		<summary type="html">&lt;p&gt;Opevans: /* Floating Point Number */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Wiki Chapter: CSC/ECE 517 Fall 2011/ch7 7a or&lt;br /&gt;
&lt;br /&gt;
''7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
Money is very important to people worldwide and is something that often must be represented in computer programs.  This raises the question of how money should be represented, which can vary from program to program.  There is much to be considered such as string formatting, the way money is used, and the variety of different currencies in the world.  In Chapter 6 of Skrien's ''Object-Oriented Design Using Java'', he suggests an implementation that represents money as a class, but there are other approaches that can be taken, each with their respective advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Properties of Money==&lt;br /&gt;
Money has certain properties and uses that must be considered in implementation.&lt;br /&gt;
&lt;br /&gt;
===Responsibilities/Using Money===&lt;br /&gt;
The following are ways in which a representation of money should be able to be used: [http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
*To represent a positive or negative amount of money.&lt;br /&gt;
*Possibly representing multiple components of a currency - i.e. dollars and cents.&lt;br /&gt;
*Possibly representing amounts of multiple currencies - i.e. a mix of US Dollars, euros, and yen.'&lt;br /&gt;
*To have a string formatting potentially including currency symbols like the dollar sign ($) and/or commas and/or decimal points.&lt;br /&gt;
*Addition, subtraction, multiplication, and division.&lt;br /&gt;
The degree to which of the above must be met will depend on the depth of the implementing project.&lt;br /&gt;
&lt;br /&gt;
===Different Currencies===&lt;br /&gt;
Some applications may only handle only one currency such as the US Dollar, but more generally applied applications, such as banking, must be able to handle different kinds of currencies.&lt;br /&gt;
&lt;br /&gt;
There are currently over 180 currencies in use around the world.[http://en.wikipedia.org/wiki/List_of_circulating_currencies]&lt;br /&gt;
&lt;br /&gt;
Implementation must be aware of how to specifically represent each currency used.  Some currencies use decimal points, such as how the US has 100 cents to a dollar, while others have no decimal, such as yen.  Each currency also has a different sign symbol used such as $, €, or ¥.  Different currencies also have different rules about how rounding values is handled.&lt;br /&gt;
&lt;br /&gt;
Special consideration must be taken when using different currencies in conjunction with each other.  Foreign exchange rates can be used to convert between different currencies, but the exchange rate often change, so attention must be paid to that.  In some cases, it is also important to keep track of how much money the user has of more than one currency at a time, so that must be handled somehow. [http://www.xencraft.com/resources/multi-currency.html][http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
===Immutable vs. Mutable===&lt;br /&gt;
Money values can be either mutable, meaning changeable, or immutable, meaning unchanging.  Mutable money values are useful in that a new value does not need to be created for every change.  However, mutable values cause problems for use in hash maps or with concurrency.  The issue with concurrency is a read and write issue in a system with multiple users.  With changing values, money not be what the user expects and lead to unpredictable behavior.  Skrien feels this is a significant enough disadvantage to prefer an immutable representation for money.[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
==Possible Representations==&lt;br /&gt;
There are several possible ways to represent money in code.&lt;br /&gt;
&lt;br /&gt;
===Floating Point Number===&lt;br /&gt;
Floating point numbers seem like an appealing option since they already include a fractional component, which lends itself straightforward to currencies like the US dollar which also have a fractional component.  However, floating point numbers come with a drawback due to the way floats are formatted.  Since floats are based on binary representation rather than a decimal representation, a value that would be exact in decimal notation can only be stored approximately in a floats point number.  For instance, 9.48 is stored as 9.479999542236328125.  Performing calculations with these approximated values can lead to inaccurate results. [http://drdobbs.com/java/184405653]&lt;br /&gt;
&lt;br /&gt;
Another deficiency of floating point numbers is that they lose precision as the number gets larger. This is less of a problem when dealing with small amounts of currency, but when dealing with large amounts, on the order of the United States federal debt, the number of significant figures to the right of the decimal point decreases, again leading to possibly inaccurate results, especially with larger numbers. &amp;lt;ref&amp;gt;Skrien, Dale. ''Object-Oriented Design Using Java''. McGraw Hill, 2009, p. 174.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Two Integers===&lt;br /&gt;
For currencies with a fractional component, using two integers to represent one amount of money is an option.  One integer to represent the main component and one to represent the fractional part.  For US currency, this would mean an integer for dollars and an integer for cents.  This representation makes displaying monetary values simple, as each component is already split out and accurate.  Also, by using integers, calculations with money work out as they should, avoiding the loss of accuracy floating points have.  However, code will need to be implemented to handle when the fraction component carries over.  In the case of dollars and cents, if the cents exceed 100, the cents value should be decreased by 100 and the dollars increased by 1.  This handling of carries adds overhead to calculations.  The main drawbacks of this implementation is the added overhead and storage required. [http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
===One Integer, Implied Decimal Point===&lt;br /&gt;
One integer can be used to represent money, even including the fractional component.  The location of the decimal place is simply implied based on the currency.  For instance, a single integer representation of $12.34 would be 1234.  A specialized print method would have to be constructed to display the monetary amount properly to the user, but all calculations work out properly.  This approach has the same advantage that using two integers of maintaining accuracy, but does not have the added overhead or storage.  In ''Object-Oriented Design Using Java'', Skrien considers this to be the best use of the primitive types for representing money. [http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
===BigDecimal===&lt;br /&gt;
Java offers a [http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html BigDecimal] class, which many people use for representing money.  BigDecimal is capable of storing any size number since it stores each digit as an entry in an array, which grows as necessary.  It also allows for specification of both the rounding method and the scale, the number of digits past the decimal place, for proper monetary calculations. [http://www.javaranch.com/journal/2003/07/MoneyInJava.html]  On the downside, this specification for rounding and scale is required, which adds code to the implementation. [http://drdobbs.com/java/184405653]  BigDecimal values are also immutable, which avoids read/write and write/write conflicts in applications. [http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial]  While there are a lot of nice things about BigDecimal, there are some more issues with BigDecimal which can cause problems in representing money.  BigDecimal cannot represent numbers with infinite digits such as 1/9 (.11111...) and throws an ArithmeticException on any calculation that would produce such number.  To deal with this, hacks such as the following must be implemented:&lt;br /&gt;
 BigDecimal bd1 = BigDecimal(1);&lt;br /&gt;
 BigDecimal bd2 = BigDecimal(9);&lt;br /&gt;
 try {&lt;br /&gt;
     return bd1.divide(bd2);&lt;br /&gt;
 } catch(ArithmaticException ae) {&lt;br /&gt;
     return new BigDecimal(bd1.doubleValue() / bd2.doubleValue());&lt;br /&gt;
 }&lt;br /&gt;
Also, since there isn't a BigDecimal equivalent for SQL databases, so storing BigDecimal values either requires conversion to a different format, which can affect accuracy and/or precision, or the value must be stored as a BLOB, which is cumbersome and inefficient.  In general use, calculations with BigDecimal are significantly slower than with integers, and also take up more storage space. [http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/]&lt;br /&gt;
&lt;br /&gt;
===Class===&lt;br /&gt;
In ''Object-Oriented Design Using Java'', recommends using a class to represent money, rather than just using a long integer alone.  He gives two reasons for using a class.  The first is encapsulation - by encapsulating money into a class, all the operations specific to working with money can be implemented to work as desired in all cases, which also avoids code duplication.  The second reason is that the compiler is better able to detect coding errors, such as adding money to something that isn't money. [http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf] Implementing Money as a class is a generally good idea, but there are multiple ways to go about it.&lt;br /&gt;
&lt;br /&gt;
====Abstract Class====&lt;br /&gt;
If multiple kinds of currencies are needed to be represented, one implementation is to create an abstract Money class and have each possible currency be a subclass of the abstract class.  Since there are over 180 different currencies worldwide, it can be a challenge to implement subclasses for each one required.&lt;br /&gt;
&lt;br /&gt;
The following is the abstract class proposed by Skrien in Object-Oriented Design Using Java:&lt;br /&gt;
 public abstract class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public long getAmount()&lt;br /&gt;
   public String toString()&lt;br /&gt;
   public int compareTo(Money m)&lt;br /&gt;
   public boolean equals(Object o)&lt;br /&gt;
   public int hashCode()&lt;br /&gt;
   public Money plus(Money)&lt;br /&gt;
   public Money minus(Money)&lt;br /&gt;
   public Money times(double factor)&lt;br /&gt;
   public Money dividedBy(double divisor)&lt;br /&gt;
   public Money negate()&lt;br /&gt;
 }&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
====Single Class====&lt;br /&gt;
Rather than implementing a variety of subclasses for each currency, a single class can be used to represent money, with currency specified within.  This is a good idea, since currencies basically operate the same calculation-wise, but have variations in decimal locations and the symbols used to present them.  Java has a [http://docs.oracle.com/javase/6/docs/api/java/util/Currency.html Currency class] which holds the information for a given currency.&lt;br /&gt;
&lt;br /&gt;
The following is the Single Class implementation given by Skrien in ''Object-Oriented Design Using Java'':&lt;br /&gt;
&lt;br /&gt;
 public class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   private long amount;&lt;br /&gt;
   private Currency currency;&lt;br /&gt;
   public Money(long amount, Currency currency) {&lt;br /&gt;
     this.amount = amount;&lt;br /&gt;
     this.currency = currency;&lt;br /&gt;
   }&lt;br /&gt;
   public long getAmount() { return amount; }&lt;br /&gt;
   public Currency getCurrency() { &lt;br /&gt;
     return currency; }&lt;br /&gt;
   public String toString() { ...above... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
 }&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
====Mixed Money====&lt;br /&gt;
&lt;br /&gt;
====Using an Interface====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Resources==&lt;br /&gt;
*Need to go through these for information&lt;br /&gt;
**[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf PDF from class lecture]&lt;br /&gt;
**[http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Java's Currency class docs]&lt;br /&gt;
**[http://drdobbs.com/java/184405653 A standalone class to tackle the problem (in Java)]&lt;br /&gt;
**[http://www.javapractices.com/topic/TopicAction.do?Id=13 Representing money (Java)]&lt;br /&gt;
**[http://www.javaranch.com/journal/2003/07/MoneyInJava.html Working with money in Java]&lt;br /&gt;
**[http://timeandmoney.sourceforge.net/ Time and Money Java project - doesn't seem to have much documentation, though]&lt;br /&gt;
**[http://joda-money.sourceforge.net/ Joda Money class library for Java]&lt;br /&gt;
**[http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/ BigDecimal and Your Money (a criticism of using BigDecimal format)]&lt;br /&gt;
**[http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial Tutorial on using BigDecimal]&lt;br /&gt;
**[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests (has a Money class example]&lt;br /&gt;
**[http://www.xencraft.com/resources/multi-currency.html Currency Internationalization (i18n), Multiple Currencies and Foreign Exchange (FX)] -nothing programming-wise, but a good overview of the considerations for currency&lt;br /&gt;
**[http://www.di-mare.com/adolfo/p/money.htm Yet Another C++ Money Class ]&lt;br /&gt;
**[http://edenti.deis.unibo.it/utils/Java-tips/Currency%20representation.txt Representing currencies (txt document with example and details)]&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56360</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a or</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56360"/>
		<updated>2011-12-01T02:52:09Z</updated>

		<summary type="html">&lt;p&gt;Opevans: /* Floating Point Number */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Wiki Chapter: CSC/ECE 517 Fall 2011/ch7 7a or&lt;br /&gt;
&lt;br /&gt;
''7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
Money is very important to people worldwide and is something that often must be represented in computer programs.  This raises the question of how money should be represented, which can vary from program to program.  There is much to be considered such as string formatting, the way money is used, and the variety of different currencies in the world.  In Chapter 6 of Skrien's ''Object-Oriented Design Using Java'', he suggests an implementation that represents money as a class, but there are other approaches that can be taken, each with their respective advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Properties of Money==&lt;br /&gt;
Money has certain properties and uses that must be considered in implementation.&lt;br /&gt;
&lt;br /&gt;
===Responsibilities/Using Money===&lt;br /&gt;
The following are ways in which a representation of money should be able to be used: [http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
*To represent a positive or negative amount of money.&lt;br /&gt;
*Possibly representing multiple components of a currency - i.e. dollars and cents.&lt;br /&gt;
*Possibly representing amounts of multiple currencies - i.e. a mix of US Dollars, euros, and yen.'&lt;br /&gt;
*To have a string formatting potentially including currency symbols like the dollar sign ($) and/or commas and/or decimal points.&lt;br /&gt;
*Addition, subtraction, multiplication, and division.&lt;br /&gt;
The degree to which of the above must be met will depend on the depth of the implementing project.&lt;br /&gt;
&lt;br /&gt;
===Different Currencies===&lt;br /&gt;
Some applications may only handle only one currency such as the US Dollar, but more generally applied applications, such as banking, must be able to handle different kinds of currencies.&lt;br /&gt;
&lt;br /&gt;
There are currently over 180 currencies in use around the world.[http://en.wikipedia.org/wiki/List_of_circulating_currencies]&lt;br /&gt;
&lt;br /&gt;
Implementation must be aware of how to specifically represent each currency used.  Some currencies use decimal points, such as how the US has 100 cents to a dollar, while others have no decimal, such as yen.  Each currency also has a different sign symbol used such as $, €, or ¥.  Different currencies also have different rules about how rounding values is handled.&lt;br /&gt;
&lt;br /&gt;
Special consideration must be taken when using different currencies in conjunction with each other.  Foreign exchange rates can be used to convert between different currencies, but the exchange rate often change, so attention must be paid to that.  In some cases, it is also important to keep track of how much money the user has of more than one currency at a time, so that must be handled somehow. [http://www.xencraft.com/resources/multi-currency.html][http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
===Immutable vs. Mutable===&lt;br /&gt;
Money values can be either mutable, meaning changeable, or immutable, meaning unchanging.  Mutable money values are useful in that a new value does not need to be created for every change.  However, mutable values cause problems for use in hash maps or with concurrency.  The issue with concurrency is a read and write issue in a system with multiple users.  With changing values, money not be what the user expects and lead to unpredictable behavior.  Skrien feels this is a significant enough disadvantage to prefer an immutable representation for money.[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
==Possible Representations==&lt;br /&gt;
There are several possible ways to represent money in code.&lt;br /&gt;
&lt;br /&gt;
===Floating Point Number===&lt;br /&gt;
Floating point numbers seem like an appealing option since they already include a fractional component, which lends itself straightforward to currencies like the US dollar which also have a fractional component.  However, floating point numbers come with a drawback due to the way floats are formatted.  Since floats are based on binary representation rather than a decimal representation, a value that would be exact in decimal notation can only be stored approximately in a floats point number.  For instance, 9.48 is stored as 9.479999542236328125.  Performing calculations with these approximated values can lead to inaccurate results. [http://drdobbs.com/java/184405653]&lt;br /&gt;
&lt;br /&gt;
Another deficiency of floating point numbers is that they lose precision as the number gets larger. This is less of a problem when dealing with small amounts of currency, but when dealing with large amounts, on the order of the United States federal debt, the number of significant figures to the right of the decimal point decreases, again leading to possibly inaccurate results, especially with larger numbers.&lt;br /&gt;
&lt;br /&gt;
===Two Integers===&lt;br /&gt;
For currencies with a fractional component, using two integers to represent one amount of money is an option.  One integer to represent the main component and one to represent the fractional part.  For US currency, this would mean an integer for dollars and an integer for cents.  This representation makes displaying monetary values simple, as each component is already split out and accurate.  Also, by using integers, calculations with money work out as they should, avoiding the loss of accuracy floating points have.  However, code will need to be implemented to handle when the fraction component carries over.  In the case of dollars and cents, if the cents exceed 100, the cents value should be decreased by 100 and the dollars increased by 1.  This handling of carries adds overhead to calculations.  The main drawbacks of this implementation is the added overhead and storage required. [http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
===One Integer, Implied Decimal Point===&lt;br /&gt;
One integer can be used to represent money, even including the fractional component.  The location of the decimal place is simply implied based on the currency.  For instance, a single integer representation of $12.34 would be 1234.  A specialized print method would have to be constructed to display the monetary amount properly to the user, but all calculations work out properly.  This approach has the same advantage that using two integers of maintaining accuracy, but does not have the added overhead or storage.  In ''Object-Oriented Design Using Java'', Skrien considers this to be the best use of the primitive types for representing money. [http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
===BigDecimal===&lt;br /&gt;
Java offers a [http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html BigDecimal] class, which many people use for representing money.  BigDecimal is capable of storing any size number since it stores each digit as an entry in an array, which grows as necessary.  It also allows for specification of both the rounding method and the scale, the number of digits past the decimal place, for proper monetary calculations. [http://www.javaranch.com/journal/2003/07/MoneyInJava.html]  On the downside, this specification for rounding and scale is required, which adds code to the implementation. [http://drdobbs.com/java/184405653]  BigDecimal values are also immutable, which avoids read/write and write/write conflicts in applications. [http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial]  While there are a lot of nice things about BigDecimal, there are some more issues with BigDecimal which can cause problems in representing money.  BigDecimal cannot represent numbers with infinite digits such as 1/9 (.11111...) and throws an ArithmeticException on any calculation that would produce such number.  To deal with this, hacks such as the following must be implemented:&lt;br /&gt;
 BigDecimal bd1 = BigDecimal(1);&lt;br /&gt;
 BigDecimal bd2 = BigDecimal(9);&lt;br /&gt;
 try {&lt;br /&gt;
     return bd1.divide(bd2);&lt;br /&gt;
 } catch(ArithmaticException ae) {&lt;br /&gt;
     return new BigDecimal(bd1.doubleValue() / bd2.doubleValue());&lt;br /&gt;
 }&lt;br /&gt;
Also, since there isn't a BigDecimal equivalent for SQL databases, so storing BigDecimal values either requires conversion to a different format, which can affect accuracy and/or precision, or the value must be stored as a BLOB, which is cumbersome and inefficient.  In general use, calculations with BigDecimal are significantly slower than with integers, and also take up more storage space. [http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/]&lt;br /&gt;
&lt;br /&gt;
===Class===&lt;br /&gt;
In ''Object-Oriented Design Using Java'', recommends using a class to represent money, rather than just using a long integer alone.  He gives two reasons for using a class.  The first is encapsulation - by encapsulating money into a class, all the operations specific to working with money can be implemented to work as desired in all cases, which also avoids code duplication.  The second reason is that the compiler is better able to detect coding errors, such as adding money to something that isn't money. [http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf] Implementing Money as a class is a generally good idea, but there are multiple ways to go about it.&lt;br /&gt;
&lt;br /&gt;
====Abstract Class====&lt;br /&gt;
If multiple kinds of currencies are needed to be represented, one implementation is to create an abstract Money class and have each possible currency be a subclass of the abstract class.  Since there are over 180 different currencies worldwide, it can be a challenge to implement subclasses for each one required.&lt;br /&gt;
&lt;br /&gt;
The following is the abstract class proposed by Skrien in Object-Oriented Design Using Java:&lt;br /&gt;
 public abstract class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public long getAmount()&lt;br /&gt;
   public String toString()&lt;br /&gt;
   public int compareTo(Money m)&lt;br /&gt;
   public boolean equals(Object o)&lt;br /&gt;
   public int hashCode()&lt;br /&gt;
   public Money plus(Money)&lt;br /&gt;
   public Money minus(Money)&lt;br /&gt;
   public Money times(double factor)&lt;br /&gt;
   public Money dividedBy(double divisor)&lt;br /&gt;
   public Money negate()&lt;br /&gt;
 }&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
====Single Class====&lt;br /&gt;
Rather than implementing a variety of subclasses for each currency, a single class can be used to represent money, with currency specified within.  This is a good idea, since currencies basically operate the same calculation-wise, but have variations in decimal locations and the symbols used to present them.  Java has a [http://docs.oracle.com/javase/6/docs/api/java/util/Currency.html Currency class] which holds the information for a given currency.&lt;br /&gt;
&lt;br /&gt;
The following is the Single Class implementation given by Skrien in ''Object-Oriented Design Using Java'':&lt;br /&gt;
&lt;br /&gt;
 public class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   private long amount;&lt;br /&gt;
   private Currency currency;&lt;br /&gt;
   public Money(long amount, Currency currency) {&lt;br /&gt;
     this.amount = amount;&lt;br /&gt;
     this.currency = currency;&lt;br /&gt;
   }&lt;br /&gt;
   public long getAmount() { return amount; }&lt;br /&gt;
   public Currency getCurrency() { &lt;br /&gt;
     return currency; }&lt;br /&gt;
   public String toString() { ...above... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
 }&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
====Mixed Money====&lt;br /&gt;
&lt;br /&gt;
====Using an Interface====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Resources==&lt;br /&gt;
*Need to go through these for information&lt;br /&gt;
**[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf PDF from class lecture]&lt;br /&gt;
**[http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Java's Currency class docs]&lt;br /&gt;
**[http://drdobbs.com/java/184405653 A standalone class to tackle the problem (in Java)]&lt;br /&gt;
**[http://www.javapractices.com/topic/TopicAction.do?Id=13 Representing money (Java)]&lt;br /&gt;
**[http://www.javaranch.com/journal/2003/07/MoneyInJava.html Working with money in Java]&lt;br /&gt;
**[http://timeandmoney.sourceforge.net/ Time and Money Java project - doesn't seem to have much documentation, though]&lt;br /&gt;
**[http://joda-money.sourceforge.net/ Joda Money class library for Java]&lt;br /&gt;
**[http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/ BigDecimal and Your Money (a criticism of using BigDecimal format)]&lt;br /&gt;
**[http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial Tutorial on using BigDecimal]&lt;br /&gt;
**[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests (has a Money class example]&lt;br /&gt;
**[http://www.xencraft.com/resources/multi-currency.html Currency Internationalization (i18n), Multiple Currencies and Foreign Exchange (FX)] -nothing programming-wise, but a good overview of the considerations for currency&lt;br /&gt;
**[http://www.di-mare.com/adolfo/p/money.htm Yet Another C++ Money Class ]&lt;br /&gt;
**[http://edenti.deis.unibo.it/utils/Java-tips/Currency%20representation.txt Representing currencies (txt document with example and details)]&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56359</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a or</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_or&amp;diff=56359"/>
		<updated>2011-12-01T02:51:21Z</updated>

		<summary type="html">&lt;p&gt;Opevans: /* Floating Point Number */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Wiki Chapter: CSC/ECE 517 Fall 2011/ch7 7a or&lt;br /&gt;
&lt;br /&gt;
''7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
Money is very important to people worldwide and is something that often must be represented in computer programs.  This raises the question of how money should be represented, which can vary from program to program.  There is much to be considered such as string formatting, the way money is used, and the variety of different currencies in the world.  In Chapter 6 of Skrien's ''Object-Oriented Design Using Java'', he suggests an implementation that represents money as a class, but there are other approaches that can be taken, each with their respective advantages and disadvantages.&lt;br /&gt;
&lt;br /&gt;
==Properties of Money==&lt;br /&gt;
Money has certain properties and uses that must be considered in implementation.&lt;br /&gt;
&lt;br /&gt;
===Responsibilities/Using Money===&lt;br /&gt;
The following are ways in which a representation of money should be able to be used: [http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
*To represent a positive or negative amount of money.&lt;br /&gt;
*Possibly representing multiple components of a currency - i.e. dollars and cents.&lt;br /&gt;
*Possibly representing amounts of multiple currencies - i.e. a mix of US Dollars, euros, and yen.'&lt;br /&gt;
*To have a string formatting potentially including currency symbols like the dollar sign ($) and/or commas and/or decimal points.&lt;br /&gt;
*Addition, subtraction, multiplication, and division.&lt;br /&gt;
The degree to which of the above must be met will depend on the depth of the implementing project.&lt;br /&gt;
&lt;br /&gt;
===Different Currencies===&lt;br /&gt;
Some applications may only handle only one currency such as the US Dollar, but more generally applied applications, such as banking, must be able to handle different kinds of currencies.&lt;br /&gt;
&lt;br /&gt;
There are currently over 180 currencies in use around the world.[http://en.wikipedia.org/wiki/List_of_circulating_currencies]&lt;br /&gt;
&lt;br /&gt;
Implementation must be aware of how to specifically represent each currency used.  Some currencies use decimal points, such as how the US has 100 cents to a dollar, while others have no decimal, such as yen.  Each currency also has a different sign symbol used such as $, €, or ¥.  Different currencies also have different rules about how rounding values is handled.&lt;br /&gt;
&lt;br /&gt;
Special consideration must be taken when using different currencies in conjunction with each other.  Foreign exchange rates can be used to convert between different currencies, but the exchange rate often change, so attention must be paid to that.  In some cases, it is also important to keep track of how much money the user has of more than one currency at a time, so that must be handled somehow. [http://www.xencraft.com/resources/multi-currency.html][http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
===Immutable vs. Mutable===&lt;br /&gt;
Money values can be either mutable, meaning changeable, or immutable, meaning unchanging.  Mutable money values are useful in that a new value does not need to be created for every change.  However, mutable values cause problems for use in hash maps or with concurrency.  The issue with concurrency is a read and write issue in a system with multiple users.  With changing values, money not be what the user expects and lead to unpredictable behavior.  Skrien feels this is a significant enough disadvantage to prefer an immutable representation for money.[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
==Possible Representations==&lt;br /&gt;
There are several possible ways to represent money in code.&lt;br /&gt;
&lt;br /&gt;
===Floating Point Number===&lt;br /&gt;
Floating point numbers seem like an appealing option since they already include a fractional component, which lends itself straightforward to currencies like the US dollar which also have a fractional component.  However, floating point numbers come with a drawback due to the way floats are formatted.  Since floats are based on binary representation rather than a decimal representation, a value that would be exact in decimal notation can only be stored approximately in a floats point number.  For instance, 9.48 is stored as 9.479999542236328125.  Performing calculations with these approximated values can lead to inaccurate results. [http://drdobbs.com/java/184405653] Another deficiency of floating point numbers is that they lose precision as the number gets larger. This is less of a problem when dealing with small amounts of currency, but when dealing with large amounts, on the order of the United States federal debt, the number of significant figures to the right of the decimal point decreases, again leading to possibly inaccurate results, especially with larger numbers.&lt;br /&gt;
&lt;br /&gt;
===Two Integers===&lt;br /&gt;
For currencies with a fractional component, using two integers to represent one amount of money is an option.  One integer to represent the main component and one to represent the fractional part.  For US currency, this would mean an integer for dollars and an integer for cents.  This representation makes displaying monetary values simple, as each component is already split out and accurate.  Also, by using integers, calculations with money work out as they should, avoiding the loss of accuracy floating points have.  However, code will need to be implemented to handle when the fraction component carries over.  In the case of dollars and cents, if the cents exceed 100, the cents value should be decreased by 100 and the dollars increased by 1.  This handling of carries adds overhead to calculations.  The main drawbacks of this implementation is the added overhead and storage required. [http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
===One Integer, Implied Decimal Point===&lt;br /&gt;
One integer can be used to represent money, even including the fractional component.  The location of the decimal place is simply implied based on the currency.  For instance, a single integer representation of $12.34 would be 1234.  A specialized print method would have to be constructed to display the monetary amount properly to the user, but all calculations work out properly.  This approach has the same advantage that using two integers of maintaining accuracy, but does not have the added overhead or storage.  In ''Object-Oriented Design Using Java'', Skrien considers this to be the best use of the primitive types for representing money. [http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
===BigDecimal===&lt;br /&gt;
Java offers a [http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html BigDecimal] class, which many people use for representing money.  BigDecimal is capable of storing any size number since it stores each digit as an entry in an array, which grows as necessary.  It also allows for specification of both the rounding method and the scale, the number of digits past the decimal place, for proper monetary calculations. [http://www.javaranch.com/journal/2003/07/MoneyInJava.html]  On the downside, this specification for rounding and scale is required, which adds code to the implementation. [http://drdobbs.com/java/184405653]  BigDecimal values are also immutable, which avoids read/write and write/write conflicts in applications. [http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial]  While there are a lot of nice things about BigDecimal, there are some more issues with BigDecimal which can cause problems in representing money.  BigDecimal cannot represent numbers with infinite digits such as 1/9 (.11111...) and throws an ArithmeticException on any calculation that would produce such number.  To deal with this, hacks such as the following must be implemented:&lt;br /&gt;
 BigDecimal bd1 = BigDecimal(1);&lt;br /&gt;
 BigDecimal bd2 = BigDecimal(9);&lt;br /&gt;
 try {&lt;br /&gt;
     return bd1.divide(bd2);&lt;br /&gt;
 } catch(ArithmaticException ae) {&lt;br /&gt;
     return new BigDecimal(bd1.doubleValue() / bd2.doubleValue());&lt;br /&gt;
 }&lt;br /&gt;
Also, since there isn't a BigDecimal equivalent for SQL databases, so storing BigDecimal values either requires conversion to a different format, which can affect accuracy and/or precision, or the value must be stored as a BLOB, which is cumbersome and inefficient.  In general use, calculations with BigDecimal are significantly slower than with integers, and also take up more storage space. [http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/]&lt;br /&gt;
&lt;br /&gt;
===Class===&lt;br /&gt;
In ''Object-Oriented Design Using Java'', recommends using a class to represent money, rather than just using a long integer alone.  He gives two reasons for using a class.  The first is encapsulation - by encapsulating money into a class, all the operations specific to working with money can be implemented to work as desired in all cases, which also avoids code duplication.  The second reason is that the compiler is better able to detect coding errors, such as adding money to something that isn't money. [http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf] Implementing Money as a class is a generally good idea, but there are multiple ways to go about it.&lt;br /&gt;
&lt;br /&gt;
====Abstract Class====&lt;br /&gt;
If multiple kinds of currencies are needed to be represented, one implementation is to create an abstract Money class and have each possible currency be a subclass of the abstract class.  Since there are over 180 different currencies worldwide, it can be a challenge to implement subclasses for each one required.&lt;br /&gt;
&lt;br /&gt;
The following is the abstract class proposed by Skrien in Object-Oriented Design Using Java:&lt;br /&gt;
 public abstract class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   public long getAmount()&lt;br /&gt;
   public String toString()&lt;br /&gt;
   public int compareTo(Money m)&lt;br /&gt;
   public boolean equals(Object o)&lt;br /&gt;
   public int hashCode()&lt;br /&gt;
   public Money plus(Money)&lt;br /&gt;
   public Money minus(Money)&lt;br /&gt;
   public Money times(double factor)&lt;br /&gt;
   public Money dividedBy(double divisor)&lt;br /&gt;
   public Money negate()&lt;br /&gt;
 }&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
====Single Class====&lt;br /&gt;
Rather than implementing a variety of subclasses for each currency, a single class can be used to represent money, with currency specified within.  This is a good idea, since currencies basically operate the same calculation-wise, but have variations in decimal locations and the symbols used to present them.  Java has a [http://docs.oracle.com/javase/6/docs/api/java/util/Currency.html Currency class] which holds the information for a given currency.&lt;br /&gt;
&lt;br /&gt;
The following is the Single Class implementation given by Skrien in ''Object-Oriented Design Using Java'':&lt;br /&gt;
&lt;br /&gt;
 public class Money implements Comparable&amp;lt;Money&amp;gt; {&lt;br /&gt;
   private long amount;&lt;br /&gt;
   private Currency currency;&lt;br /&gt;
   public Money(long amount, Currency currency) {&lt;br /&gt;
     this.amount = amount;&lt;br /&gt;
     this.currency = currency;&lt;br /&gt;
   }&lt;br /&gt;
   public long getAmount() { return amount; }&lt;br /&gt;
   public Currency getCurrency() { &lt;br /&gt;
     return currency; }&lt;br /&gt;
   public String toString() { ...above... }&lt;br /&gt;
   public int compareTo(Money o) { ... }&lt;br /&gt;
   public boolean equals(Object o) { ... }&lt;br /&gt;
   public int hashCode() { ... }&lt;br /&gt;
   public Money plus(Money) { ... }&lt;br /&gt;
   public Money minus(Money) { ... }&lt;br /&gt;
   public Money times(double factor) { ... }&lt;br /&gt;
   public Money dividedBy(double divisor) { ... }&lt;br /&gt;
   public Money negate() { ... }&lt;br /&gt;
 }&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf]&lt;br /&gt;
&lt;br /&gt;
====Mixed Money====&lt;br /&gt;
&lt;br /&gt;
====Using an Interface====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Resources==&lt;br /&gt;
*Need to go through these for information&lt;br /&gt;
**[http://courses.ncsu.edu/csc517//common/lectures/notes/lec20.pdf PDF from class lecture]&lt;br /&gt;
**[http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Java's Currency class docs]&lt;br /&gt;
**[http://drdobbs.com/java/184405653 A standalone class to tackle the problem (in Java)]&lt;br /&gt;
**[http://www.javapractices.com/topic/TopicAction.do?Id=13 Representing money (Java)]&lt;br /&gt;
**[http://www.javaranch.com/journal/2003/07/MoneyInJava.html Working with money in Java]&lt;br /&gt;
**[http://timeandmoney.sourceforge.net/ Time and Money Java project - doesn't seem to have much documentation, though]&lt;br /&gt;
**[http://joda-money.sourceforge.net/ Joda Money class library for Java]&lt;br /&gt;
**[http://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/ BigDecimal and Your Money (a criticism of using BigDecimal format)]&lt;br /&gt;
**[http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial Tutorial on using BigDecimal]&lt;br /&gt;
**[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests (has a Money class example]&lt;br /&gt;
**[http://www.xencraft.com/resources/multi-currency.html Currency Internationalization (i18n), Multiple Currencies and Foreign Exchange (FX)] -nothing programming-wise, but a good overview of the considerations for currency&lt;br /&gt;
**[http://www.di-mare.com/adolfo/p/money.htm Yet Another C++ Money Class ]&lt;br /&gt;
**[http://edenti.deis.unibo.it/utils/Java-tips/Currency%20representation.txt Representing currencies (txt document with example and details)]&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_oe&amp;diff=51936</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a oe</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_oe&amp;diff=51936"/>
		<updated>2011-10-13T03:07:14Z</updated>

		<summary type="html">&lt;p&gt;Opevans: /* Ruby Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Ruby Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby is an interpreted, dynamically-typed, polymorphic, object-oriented programming language. The author, Yukuhiro &amp;quot;Matz&amp;quot; Matsumoto, describes it as blending aspects of Perl, Smalltalk, Eiffel, Ada, and Lisp. [http://www.ruby-lang.org/en/about/] As a result, it is very flexible language from top to bottom. It can be used as a procedural scripting language for rudimentary scripts, or as object oriented language.&lt;br /&gt;
&lt;br /&gt;
Originally released in 1995, it gained widespread recognition a decade later with the availabiliy and widespread adoption of the web programming framework Ruby on Rails. [http://rubyonrails.org/]&lt;br /&gt;
&lt;br /&gt;
== Basic syntax ==&lt;br /&gt;
&lt;br /&gt;
In comparison with some other object-oriented or procedural languages such as Java or C, Ruby has a flexible syntax. Many aspects of a statement that would be required in Java are optional in Ruby.&lt;br /&gt;
&lt;br /&gt;
* Semicolons at the end of each line are optional. They are generally used only when combining multiple statements on a single line.&lt;br /&gt;
* A return statement at the end of a method is not necessary. If there is no return statement, the return value of the last executed statement is returned.&lt;br /&gt;
* Method arguments. Methods can be created that will accept any number of arguments without using overloading in the conventional sense. Methods can be called with or without parentheses around arguments.&lt;br /&gt;
* Variable types. Types for variables do not need to be declared in Ruby.&lt;br /&gt;
&lt;br /&gt;
In other cases, where there may be only one way to write a statement in Java, for example, there may be nearly limitless ways to do so in Ruby. The programmer is allowed to determine which way works best for the situation at hand.&lt;br /&gt;
&lt;br /&gt;
=== Control Strucures ===&lt;br /&gt;
&lt;br /&gt;
There are some significant differences in control structures between Ruby and Java.&lt;br /&gt;
&lt;br /&gt;
==== Blocks and closures ====&lt;br /&gt;
&lt;br /&gt;
==== Loops and iterators ====&lt;br /&gt;
These constructs all achieve the same result of printing the nubmers 1 through 5. It is left up to the programmer to decide which method works the best for the particular situation.&lt;br /&gt;
&lt;br /&gt;
 for i in 1..5 do puts i.to_s; end&lt;br /&gt;
 for i in [1,2,3,4,5] do puts i.to_s; end&lt;br /&gt;
 (1..5).each { |i| puts i.to_s }&lt;br /&gt;
 [1,2,3,4,5].each { |i| puts i.to_s }&lt;br /&gt;
 1.upto(5) { |i| puts i.to_s }&lt;br /&gt;
 1.step(5,1) { |i| puts i.to_s }&lt;br /&gt;
&lt;br /&gt;
== Object-Oriented principles ==&lt;br /&gt;
&lt;br /&gt;
=== Everything is an object ===&lt;br /&gt;
&lt;br /&gt;
In comparison with some other Object Oriented languages, Ruby is a very &amp;quot;pure&amp;quot; language, in that essentially everything is an object. There are no primitive types, and even numbers or literal strings are treated as objects and can have methods run on them.&lt;br /&gt;
 5.times do puts &amp;quot;hello&amp;quot;; end&lt;br /&gt;
 &amp;quot;hello&amp;quot;.index('e')    -&amp;gt;returns 1&lt;br /&gt;
&lt;br /&gt;
=== Unbounded polymorphism ===&lt;br /&gt;
&lt;br /&gt;
Object types and classes are not checked by the compiler (there is no compiler!) or at runtime. An object's class does not matter as long as it contains all the instance methods and variables that are accessed. This is also known as &amp;quot;Duck Typing,&amp;quot; a reference to the famous &amp;quot;Duck test&amp;quot;: If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.&lt;br /&gt;
&lt;br /&gt;
=== Dynamic typing ===&lt;br /&gt;
&lt;br /&gt;
Data types of objects are handled and checked at runtime, as opposed to a statically typed language where they are checked at compile time. In a statically typed language like Java, when you define an array, it must be an array containing elements all of the same type. In dynamically typed languages like Ruby, each element of an array can be any type.&lt;br /&gt;
&lt;br /&gt;
== Basic types/classes ==&lt;br /&gt;
&lt;br /&gt;
=== Array ===&lt;br /&gt;
http://www.ruby-doc.org/core/Array.html&lt;br /&gt;
&lt;br /&gt;
An array is an ordered one dimensional vector of objects. The objects in the vector are indexed with consecutive integers starting from zero. Any element in any array can be of any object type. An array is created as below:&lt;br /&gt;
 @a1 = ['a', 2, :blah]&lt;br /&gt;
  or&lt;br /&gt;
 @a2[0] = 'a'&lt;br /&gt;
 @a2[1] = 2&lt;br /&gt;
 @a2[2] = :blah&lt;br /&gt;
&lt;br /&gt;
The individual elements are accessed as below:&lt;br /&gt;
 @a[0]&lt;br /&gt;
 --&amp;gt; 'a'&lt;br /&gt;
&lt;br /&gt;
Unlike some other languages, arrays in Ruby are dynamically allocated and can shrink and grow as necessary. Array elements can be accessed by negative indices as well. An index of -1 will retrieve the ultimate element in the array; an index of -2 will retrieve the penultimate element, and so on. An undefined array element that is written to by index will be allocated and defined automatically, while an undefined element read by index will return nil. Therefore, there will never be an out of bounds error for an array in Ruby.&lt;br /&gt;
&lt;br /&gt;
Examples of useful instance methods:&lt;br /&gt;
* array.push(object): adds an element to the end of an array.,&lt;br /&gt;
* array.pop adds or retrieves the last element to/from the front of any array.&lt;br /&gt;
* array.unshift(object): adds an element to the front of an array.&lt;br /&gt;
* array.shift: removes and returns the element to the front of the array.&lt;br /&gt;
* array.each { |val| block }: iterator that executes the block once for each element in the array&lt;br /&gt;
* array.length: returns the number of elements in the array&lt;br /&gt;
* array.concat: Returns an array containing the array concatenated with another&lt;br /&gt;
&lt;br /&gt;
=== Hash ===&lt;br /&gt;
http://www.ruby-doc.org/core/Hash.html&lt;br /&gt;
&lt;br /&gt;
A hash is similar to an array, however it is not ordered and it is indexed by keys, which can be any object type. It functions as a mapping from the key to the value. An interesting point is that a hash could be treated just like an array, if all elements in the hash are indexed by integers.&lt;br /&gt;
&lt;br /&gt;
A hash is defined as below:&lt;br /&gt;
 @h1 = {'a' =&amp;gt; 'foo', 2 =&amp;gt; 'bar'}&lt;br /&gt;
  or&lt;br /&gt;
 @h2['a'] = 'foo'&lt;br /&gt;
 @h2[2] = 'bar'&lt;br /&gt;
&lt;br /&gt;
Examples of useful instance methods:&lt;br /&gt;
* hash.keys: returns an array populated with all the keys of the hash.&lt;br /&gt;
* hash.values: returns an array populated with all the values of the hash. If both keys and values are retrieved without modifying the hash between, the keys and values will be in the same order.&lt;br /&gt;
* array.each { |key,val| block }: iterator that executes the block once for each element in the hash&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== String ===&lt;br /&gt;
http://www.ruby-doc.org/core/String.html&lt;br /&gt;
&lt;br /&gt;
A string is a sequence of bytes (Frequently characters.) It can be used to store data in any encoding: ASCII, unicode, or binary.&lt;br /&gt;
&lt;br /&gt;
A string is defined as follows:&lt;br /&gt;
 @s = &amp;quot;hello world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Examples of useful instance methods:&lt;br /&gt;
* string.length: returns the number of bytes in the string.&lt;br /&gt;
* string.index(pattern) returns the index of the first occurence of a pattern or [http://www.ruby-doc.org/core-1.9.2/Regexp.html regular expression]&lt;br /&gt;
* string.sub(pattern, replacement): Substitutes the first occurrence of a substring or regular expression pattern with replacement.&lt;br /&gt;
&lt;br /&gt;
=== Symbol ===&lt;br /&gt;
http://www.ruby-doc.org/core/Symbol.html&lt;br /&gt;
&lt;br /&gt;
A symbol is similar to a string, with the exception that it is immutable - it cannot be changed once defined. Any reference to a particular symbol will refer to the same object for the entire time that a program is running. A symbol is defined as follows:&lt;br /&gt;
&lt;br /&gt;
 :Foo&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_oe&amp;diff=51918</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a oe</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_oe&amp;diff=51918"/>
		<updated>2011-10-10T00:58:27Z</updated>

		<summary type="html">&lt;p&gt;Opevans: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Ruby Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby is a very flexible language from top to bottom. It can be used as a procedural scripting language for rudimentary scripts, or as object oriented language.&lt;br /&gt;
&lt;br /&gt;
== Basic syntax ==&lt;br /&gt;
&lt;br /&gt;
In comparison with some other object-oriented or procedural languages such as Java or C, Ruby has a flexible syntax. Many aspects of a statement that would be required in Java are optional in Ruby.&lt;br /&gt;
&lt;br /&gt;
-Semicolons at the end of each line are optional. They are generally used only when combining multiple statements on a single line.&lt;br /&gt;
-A return statement at the end of a method is not necessary. If there is no return statement, the return value of the last executed statement is returned.&lt;br /&gt;
-Method arguments. Methods can be created that will accept any number of arguments without using overloading in the conventional sense. Methods can be called with or without parentheses around arguments.&lt;br /&gt;
&lt;br /&gt;
In other cases, where there may be only one way to write a statement in Java, for example, there may be nearly limitless ways to do so in Ruby. The programmer is allowed to determine which way works best for the situation at hand.&lt;br /&gt;
&lt;br /&gt;
-Loops and iterators. These constructs all achieve the same result of printing the nubmers 1 through 5. It is left up to the programmer to decide which method works the best for the particular situation.&lt;br /&gt;
&lt;br /&gt;
 for i in 1..5 do puts i.to_s; end&lt;br /&gt;
 for i in [1,2,3,4,5] do puts i.to_s; end&lt;br /&gt;
 (1..5).each { |i| puts i.to_s }&lt;br /&gt;
 [1,2,3,4,5].each { |i| puts i.to_s }&lt;br /&gt;
 1.upto(5) { |i| puts i.to_s }&lt;br /&gt;
 1.step(5,1) { |i| puts i.to_s }&lt;br /&gt;
&lt;br /&gt;
== Object-Oriented principles ==&lt;br /&gt;
&lt;br /&gt;
=== Everything is an object ===&lt;br /&gt;
&lt;br /&gt;
In comparison with some other Object Oriented languages, Ruby is a very &amp;quot;pure&amp;quot; language, in that essentially everything is an object. There are no primitive types, and even numbers or literal strings are treated as objects and can have methods run on them.&lt;br /&gt;
 5.times do puts &amp;quot;hello&amp;quot;; end&lt;br /&gt;
 &amp;quot;hello&amp;quot;.index('e')    -&amp;gt;returns 1&lt;br /&gt;
&lt;br /&gt;
=== Unbounded polymorphism ===&lt;br /&gt;
&lt;br /&gt;
Object types and classes are not checked by the compiler (there is no compiler!) or at runtime. An object's class does not matter as long as it contains all the instance methods and variables that are accessed. This is also known as &amp;quot;Duck Typing,&amp;quot; a reference to the famous &amp;quot;Duck test&amp;quot;: If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.&lt;br /&gt;
&lt;br /&gt;
=== Dynamic typing ===&lt;br /&gt;
&lt;br /&gt;
Data types of objects are handled and checked at runtime, as opposed to a statically typed language where they are checked at compile time. In a statically typed language like Java, when you define an array, it must be an array containing elements all of the same type. In dynamically typed languages like Ruby, each element of an array can be any type.&lt;br /&gt;
&lt;br /&gt;
== Basic types/classes ==&lt;br /&gt;
&lt;br /&gt;
=== Array ===&lt;br /&gt;
http://www.ruby-doc.org/core/Array.html&lt;br /&gt;
&lt;br /&gt;
An array is an ordered one dimensional vector of objects. The objects in the vector are indexed with consecutive integers starting from zero. Any element in any array can be of any object type. An array is created as below:&lt;br /&gt;
 @a1 = ['a', 2, :blah]&lt;br /&gt;
  or&lt;br /&gt;
 @a2[0] = 'a'&lt;br /&gt;
 @a2[1] = 2&lt;br /&gt;
 @a2[2] = :blah&lt;br /&gt;
&lt;br /&gt;
The individual elements are accessed as below:&lt;br /&gt;
 @a[0]&lt;br /&gt;
 --&amp;gt; 'a'&lt;br /&gt;
&lt;br /&gt;
Unlike some other languages, arrays in Ruby are dynamically allocated and can shrink and grow as necessary. Array elements can be accessed by negative indices as well. An index of -1 will retrieve the ultimate element in the array; an index of -2 will retrieve the penultimate element, and so on. An undefined array element that is written to by index will be allocated and defined automatically, while an undefined element read by index will return nil. Therefore, there will never be an out of bounds error for an array in Ruby.&lt;br /&gt;
&lt;br /&gt;
Examples of useful instance methods:&lt;br /&gt;
* array.push(object): adds an element to the end of an array.,&lt;br /&gt;
* array.pop adds or retrieves the last element to/from the front of any array.&lt;br /&gt;
* array.unshift(object): adds an element to the front of an array.&lt;br /&gt;
* array.shift: removes and returns the element to the front of the array.&lt;br /&gt;
* array.each { |val| block }: iterator that executes the block once for each element in the array&lt;br /&gt;
* array.length: returns the number of elements in the array&lt;br /&gt;
* array.concat: Returns an array containing the array concatenated with another&lt;br /&gt;
&lt;br /&gt;
=== Hash ===&lt;br /&gt;
http://www.ruby-doc.org/core/Hash.html&lt;br /&gt;
&lt;br /&gt;
A hash is similar to an array, however it is not ordered and it is indexed by keys, which can be any object type. It functions as a mapping from the key to the value. An interesting point is that a hash could be treated just like an array, if all elements in the hash are indexed by integers.&lt;br /&gt;
&lt;br /&gt;
A hash is defined as below:&lt;br /&gt;
 @h1 = {'a' =&amp;gt; 'foo', 2 =&amp;gt; 'bar'}&lt;br /&gt;
  or&lt;br /&gt;
 @h2['a'] = 'foo'&lt;br /&gt;
 @h2[2] = 'bar'&lt;br /&gt;
&lt;br /&gt;
Examples of useful instance methods:&lt;br /&gt;
* hash.keys: returns an array populated with all the keys of the hash.&lt;br /&gt;
* hash.values: returns an array populated with all the values of the hash. If both keys and values are retrieved without modifying the hash between, the keys and values will be in the same order.&lt;br /&gt;
* array.each { |key,val| block }: iterator that executes the block once for each element in the hash&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== String ===&lt;br /&gt;
http://www.ruby-doc.org/core/String.html&lt;br /&gt;
&lt;br /&gt;
A string is a sequence of bytes (Frequently characters.) It can be used to store data in any encoding: ASCII, unicode, or binary.&lt;br /&gt;
&lt;br /&gt;
A string is defined as follows:&lt;br /&gt;
 @s = &amp;quot;hello world&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Examples of useful instance methods:&lt;br /&gt;
* string.length: returns the number of bytes in the string.&lt;br /&gt;
* string.index(pattern) returns the index of the first occurence of a pattern or [http://www.ruby-doc.org/core-1.9.2/Regexp.html regular expression]&lt;br /&gt;
* string.sub(pattern, replacement): Substitutes the first occurrence of a substring or regular expression pattern with replacement.&lt;br /&gt;
&lt;br /&gt;
=== Symbol ===&lt;br /&gt;
http://www.ruby-doc.org/core/Symbol.html&lt;br /&gt;
&lt;br /&gt;
A symbol is similar to a string, with the exception that it is immutable - it cannot be changed once defined. Any reference to a particular symbol will refer to the same object for the entire time that a program is running. A symbol is defined as follows:&lt;br /&gt;
&lt;br /&gt;
 :Foo&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_oe&amp;diff=51884</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a oe</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_oe&amp;diff=51884"/>
		<updated>2011-10-07T02:45:59Z</updated>

		<summary type="html">&lt;p&gt;Opevans: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Ruby Introduction =&lt;br /&gt;
&lt;br /&gt;
Ruby is a very flexible language from top to bottom. It can be used as a procedural scripting language for rudimentary scripts, or as object oriented language.&lt;br /&gt;
&lt;br /&gt;
== Basic syntax ==&lt;br /&gt;
&lt;br /&gt;
In comparison with some other object-oriented or procedural languages such as Java or C, Ruby has a flexible syntax. Many aspects of a statement that would be required in Java are optional in Ruby.&lt;br /&gt;
&lt;br /&gt;
-Semicolons at the end of each line are optional. They are generally used only when combining multiple statements on a single line.&lt;br /&gt;
-A return statement at the end of a method is not necessary. If there is no return statement, the return value of the last executed statement is returned.&lt;br /&gt;
-Method arguments. Methods can be created that will accept any number of arguments without using overloading in the conventional sense. Methods can be called with or without parentheses around arguments.&lt;br /&gt;
&lt;br /&gt;
In other cases, where there may be only one way to write a statement in Java, for example, there may be nearly limitless ways to do so in Ruby. The programmer is allowed to determine which way works best for the situation at hand.&lt;br /&gt;
&lt;br /&gt;
-Loops and iterators. These constructs all achieve the same result of printing the nubmers 1 through 5. The &lt;br /&gt;
&lt;br /&gt;
 for i in 1..5 do puts i.to_s; end&lt;br /&gt;
 for i in [1,2,3,4,5] do puts i.to_s; end&lt;br /&gt;
 (1..5).each { |i| puts i.to_s }&lt;br /&gt;
 [1,2,3,4,5].each { |i| puts i.to_s }&lt;br /&gt;
 1.upto(5) { |i| puts i.to_s }&lt;br /&gt;
 1.step(5,1) { |i| puts i.to_s }&lt;br /&gt;
&lt;br /&gt;
== Object-Oriented principles ==&lt;br /&gt;
&lt;br /&gt;
=== Everything is an object ===&lt;br /&gt;
&lt;br /&gt;
In comparison with some other Object Oriented languages, Ruby is a very &amp;quot;pure&amp;quot; language, in that essentially everything is an object. There are no primitive types, and even numbers or literal strings are treated as objects and can have methods run on them.&lt;br /&gt;
 5.times do puts &amp;quot;hello&amp;quot;; end&lt;br /&gt;
 &amp;quot;hello&amp;quot;.index('e')    -&amp;gt;returns 1&lt;br /&gt;
&lt;br /&gt;
=== Unbounded polymorphism ===&lt;br /&gt;
&lt;br /&gt;
Object types and classes are not checked by the compiler (there is no compiler!) or at runtime. An object's class does not matter as long as it contains all the instance methods and variables that are accessed. This is also known as &amp;quot;Duck Typing,&amp;quot; a reference to the famous &amp;quot;Duck test&amp;quot;: If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.&lt;br /&gt;
&lt;br /&gt;
=== Dynamic typing ===&lt;br /&gt;
&lt;br /&gt;
Data types of objects are handled and checked at runtime, as opposed to a statically typed language where they are checked at compile time. In a statically typed language like Java, when you define an array, it must be an array containing elements all of the same type. In dynamically typed languages like Ruby, each element of an array can be any type.&lt;br /&gt;
&lt;br /&gt;
== Basic types/classes ==&lt;br /&gt;
&lt;br /&gt;
=== Array ===&lt;br /&gt;
http://www.ruby-doc.org/core/Array.html&lt;br /&gt;
&lt;br /&gt;
An array is an ordered one dimensional vector of objects. The objects in the vector are indexed with consecutive integers starting from zero. Any element in any array can be of any object type. An array is created as below:&lt;br /&gt;
 @a = ['a', 2, :blah]&lt;br /&gt;
&lt;br /&gt;
The individual elements are accessed as below:&lt;br /&gt;
 @a[0]&lt;br /&gt;
 --&amp;gt; 'a'&lt;br /&gt;
&lt;br /&gt;
Unlike some other languages, array elements can be accessed by negative indices as well. An index of -1 will retrieve the ultimate element in the array; an index of -2 will retrieve the penultimate element, and so on.&lt;br /&gt;
&lt;br /&gt;
Examples of useful instance methods:&lt;br /&gt;
array.each { block }: iterator that executes the block once for each element in the array&lt;br /&gt;
array.length: returns the number of elements in the array&lt;br /&gt;
array.concat: Returns an array containing the array concatenated with another&lt;br /&gt;
&lt;br /&gt;
=== Hash ===&lt;br /&gt;
http://www.ruby-doc.org/core/Hash.html&lt;br /&gt;
&lt;br /&gt;
A hash is similar to an array, however it is not ordered and it is indexed by keys, which can be any object type. It functions as a mapping from the key to the value.&lt;br /&gt;
&lt;br /&gt;
=== String ===&lt;br /&gt;
http://www.ruby-doc.org/core/String.html&lt;br /&gt;
&lt;br /&gt;
=== Symbol ===&lt;br /&gt;
http://www.ruby-doc.org/core/Symbol.html&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_oe&amp;diff=51880</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a oe</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_oe&amp;diff=51880"/>
		<updated>2011-10-07T02:40:14Z</updated>

		<summary type="html">&lt;p&gt;Opevans: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Basic syntax&lt;br /&gt;
 Ruby is a very flexible language from top to bottom. It can be used as a procedural scripting language or as object oriented language.&lt;br /&gt;
&lt;br /&gt;
In comparison with some other object-oriented or procedural languages such as Java or C, Ruby has a flexible syntax. Many aspects of a statement that would be required in Java are optional in Ruby.&lt;br /&gt;
&lt;br /&gt;
-Semicolons at the end of each line are optional. They are generally used only when combining multiple statements on a single line.&lt;br /&gt;
-A return statement at the end of a method is not necessary. If there is no return statement, the return value of the last executed statement is returned.&lt;br /&gt;
-Method arguments. Methods can be created that will accept any number of arguments without using overloading in the conventional sense. Methods can be called with or without parentheses around arguments.&lt;br /&gt;
&lt;br /&gt;
In other cases, where there may be only one way to write a statement in Java, for example, there may be nearly limitless ways to do so in Ruby. The programmer is allowed to determine which way works best for the situation at hand.&lt;br /&gt;
&lt;br /&gt;
-Loops and iterators. These constructs all achieve the same result of printing the nubmers 1 through 5. The &lt;br /&gt;
&lt;br /&gt;
for i in 1..5 do puts i.to_s; end&lt;br /&gt;
for i in [1,2,3,4,5] do puts i.to_s; end&lt;br /&gt;
(1..5).each { |i| puts i.to_s }&lt;br /&gt;
[1,2,3,4,5].each { |i| puts i.to_s }&lt;br /&gt;
1.upto(5) { |i| puts i.to_s }&lt;br /&gt;
1.step(5,1) { |i| puts i.to_s }&lt;br /&gt;
&lt;br /&gt;
Object-Oriented principles&lt;br /&gt;
&lt;br /&gt;
Everything is an object.&lt;br /&gt;
&lt;br /&gt;
 In comparison with some other Object Oriented languages, Ruby is a very &amp;quot;pure&amp;quot; language, in that essentially everything is an object. There are no primitive types, and even numbers or literal strings are treated as objects and can have methods run on them.&lt;br /&gt;
5.times do puts &amp;quot;hello&amp;quot;; end&lt;br /&gt;
&amp;quot;hello&amp;quot;.index('e')    -&amp;gt;returns 1&lt;br /&gt;
&lt;br /&gt;
Unbounded polymorphism&lt;br /&gt;
&lt;br /&gt;
Object types and classes are not checked by the compiler (there is no compiler!) or at runtime. An object's class does not matter as long as it contains all the instance methods and variables that are accessed. This is also known as &amp;quot;Duck Typing,&amp;quot; a reference to the famous &amp;quot;Duck test&amp;quot;: If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing&lt;br /&gt;
&lt;br /&gt;
Data types of objects are handled and checked at runtime, as opposed to a statically typed language where they are checked at compile time. In a statically typed language like Java, when you define an array, it must be an array containing elements all of the same type. In dynamically typed languages like Ruby, each element of an array can be any type.&lt;br /&gt;
&lt;br /&gt;
Basic classes&lt;br /&gt;
&lt;br /&gt;
Array&lt;br /&gt;
www.ruby-doc.org/core/Array.html&lt;br /&gt;
&lt;br /&gt;
An array is an ordered one dimensional vector of objects. The objects in the vector are indexed with consecutive integers starting from zero. Any element in any array can be of any object type. An array is created as below:&lt;br /&gt;
@a = ['a', 2, :blah]&lt;br /&gt;
&lt;br /&gt;
The individual elements are accessed as below:&lt;br /&gt;
@a[0]&lt;br /&gt;
--&amp;gt; 'a'&lt;br /&gt;
&lt;br /&gt;
Unlike some other languages, array elements can be accessed by negative indices as well. An index of -1 will retrieve the ultimate element in the array; an index of -2 will retrieve the penultimate element, and so on.&lt;br /&gt;
&lt;br /&gt;
Examples of useful instance methods:&lt;br /&gt;
array.each { block }: iterator that executes the block once for each element in the array&lt;br /&gt;
array.length: returns the number of elements in the array&lt;br /&gt;
array.concat: Returns an array containing the array concatenated with another&lt;br /&gt;
&lt;br /&gt;
Hash&lt;br /&gt;
www.ruby-doc.org/core/Hash.html&lt;br /&gt;
&lt;br /&gt;
A hash is similar to an array, however it is not ordered and it is indexed by keys, which can be any object type. It functions as a mapping from the key to the value.&lt;br /&gt;
&lt;br /&gt;
String&lt;br /&gt;
www.ruby-doc.org/core/String.html&lt;br /&gt;
&lt;br /&gt;
Symbol&lt;br /&gt;
http://www.ruby-doc.org/core/Symbol.html&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011&amp;diff=51806</id>
		<title>CSC/ECE 517 Fall 2011</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011&amp;diff=51806"/>
		<updated>2011-10-07T00:30:23Z</updated>

		<summary type="html">&lt;p&gt;Opevans: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Link title]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a ms]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a cs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a ri]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a lj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b ds]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b tj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c cm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c sj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c ka]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d sr]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e vs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e aa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a sc]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e dm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e an]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e lm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g vn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1f rs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1f sv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g jn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1h ps]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e sm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i zf]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g rn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i cl]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d ss]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i lj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1h hs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d gs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b ns]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b jp]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2a av]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2f jm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e ad]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e kt]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e gp]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b qu]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2c bs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2c rs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2a ca]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b rv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2c ds]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2b sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2f vh]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e ps]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3a oe]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3h rr]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3h ss]]&lt;br /&gt;
&lt;br /&gt;
*[[trial]]&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_oe&amp;diff=51804</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3a oe</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3a_oe&amp;diff=51804"/>
		<updated>2011-10-07T00:29:31Z</updated>

		<summary type="html">&lt;p&gt;Opevans: Created page with &amp;quot;Testing&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Testing&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_S10_ms&amp;diff=43376</id>
		<title>CSC/ECE 517 Fall 2010/ch1 S10 ms</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_S10_ms&amp;diff=43376"/>
		<updated>2010-12-07T21:06:52Z</updated>

		<summary type="html">&lt;p&gt;Opevans: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE 517 Fall 2010/ch1 1a vc]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 1a br]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 1b mg]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 1c JF]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 1e az]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 1e bb]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 1f vn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 25 ag]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 2b dg]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 2e RI]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 S6 aa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 S6 km]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 S10 GP]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 S10 MS]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 S10 MM]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch1 S10 PH]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 2a CB]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 2a mw]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 2c ck]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 S23 GP]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 S24 NS]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 S23 SS]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 S23 NR]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 S20 TT]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 2d AS]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch2 S24 rm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch3 3a SN]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch3 3b ka]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch3 3e br]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch3 3f lj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch3 3h az]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch3 3h PW]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch3 3i IC]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch3 3i MM]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch3 3j KS]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch3 S30 SK]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch3 4b mt]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch4 4e ms]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch4 4f sv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch4 4g HW]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch4 4g km]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch4 4h am]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch5 5b mt]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch5 5b jz]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch5 5c ck]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch5 5c IC]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch5 5f SN]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch5 5a KR]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch5 5b RR]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch5 5e ms]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/chd 6d isb]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch6 6b SK]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch6 6c AW]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch6 6d bb]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch6 6h AS]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch6 6f AZ]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch6 6b AK]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch6 6g ss]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2010/ch6 6d NM]]&lt;br /&gt;
&lt;br /&gt;
*[[ CSC/ECE 517 Fall 2010/ch6 6a PC]]&lt;br /&gt;
&lt;br /&gt;
*[[ CSC/ECE 517 Fall 2010/ch2 4d RB]]&lt;br /&gt;
&lt;br /&gt;
*[[ CSC/ECE 517 Fall 2010/ch7 71 ed]]&lt;br /&gt;
&lt;br /&gt;
*[[ CSC/ECE 517 Fall 2010/ch7 7c ed]]&lt;br /&gt;
&lt;br /&gt;
*[[ CSC/ECE 517 Fall 2010/ch7 7e GP]]&lt;br /&gt;
&lt;br /&gt;
*[[ CSC/ECE 517 Fall 2010/ch7 7e GS]]&lt;br /&gt;
&lt;br /&gt;
*[[ CSC/ECE 517 Fall 2010/ch7 7f PW]]&lt;br /&gt;
&lt;br /&gt;
*[[ CSC/ECE 517 Fall 2010/ch7 7g ms]]&lt;br /&gt;
&lt;br /&gt;
*[[ CSC/ECE 517 Fall 2010/ch7 7i sr]]&lt;br /&gt;
&lt;br /&gt;
*[[ CSC/ECE_517_Fall_2010/ch7 7b JB]]&lt;br /&gt;
&lt;br /&gt;
*[[ CSC/ECE_517_Fall_2010/ch7 S36 JB]]&lt;br /&gt;
&lt;br /&gt;
*[[ CSC/ECE_517_Fall_2010/ch7 n 7i OE]]&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_n_7i_OE&amp;diff=43375</id>
		<title>CSC/ECE 517 Fall 2010/ch7 n 7i OE</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_n_7i_OE&amp;diff=43375"/>
		<updated>2010-12-07T21:02:38Z</updated>

		<summary type="html">&lt;p&gt;Opevans: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Visual Object Oriented Programming ==&lt;br /&gt;
&lt;br /&gt;
Visual programming languages are a class of languages where the program is defined by a series of interconnected pictoral icons rather than textual statements. Many graphical programming languages are designed to be accessable to those without formal training in programming. Theoretically, a visual programming language might be easier for a novice to comprehend, as rather than memorizing syntax and methods, the user is presented with a pallette of icons to choose from and can lay them out in a logical and familiar way, similar to an electric circut diagram.&lt;br /&gt;
&lt;br /&gt;
Visual programming languages often have several disadvantages. Though the basic functionality may be more accessible to novices than text-based languages, there is a tradeoff. It may be more difficult to implement complicated algorithms or control paths. Complicated programs are difficult to keep clean.  and &amp;quot;spaghetti&amp;quot; code may result. Many visual programming languages have been developed for a host of reasons, but few have been widely adopted. The two examples below are exceptions, and have garnered a significant user base.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Labview / G ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Labview is a proprietary programming language developed by National Instruments. It is widely used in laboratory environments for control and operation of experimental equipment. LabView's target users generally do not have formal training in computer programming so it is designed to be intuitive. The program is graphically represented in a manner similar to an electrical schematic. Labview is considered to be a dataflow language, where operations are performed as soon as their inputs become valid, rather than when they are encountered in code.&lt;br /&gt;
&lt;br /&gt;
As of version 8.2, LabView or G incorporates several object-oriented principles. Given LabView's orientation towards non-programmers, the language's designers made some tradeoffs between implementing all aspects of object-oriented languages, and maintaining simplicity. The result was that two of the tenets of OO programming were implemented: Encapsulation and Inheritence.&lt;br /&gt;
&lt;br /&gt;
'''Encapsulation'''. LabView implements classes and supports the concepts of public, private, and protected methods, but private data only. Private data within classes is accessed and modified by accessor methods (known as virtual instruments, or VIs.)&lt;br /&gt;
&lt;br /&gt;
'''Inheritance'''. Classes can inherit other classes. Because there is no protected or public data, all data from parent classes must be accessed through the accessor methods.&lt;br /&gt;
&lt;br /&gt;
There are several significant differences between the implementation of Object-Oriented principles in an imperitave language like Java or C++ and LabView. In Java, classes are passed by reference; in C++ they can be passed by reference or value. In LabView, they are passed by value only and therefore may be duplicated by the compiler. Many existing OO design patterns, specifically those from the Gang of Four book, depend on classes that are passed by reference. Therefore, the LabView developers have designed a different set of design patterns&lt;br /&gt;
LabVIEW Object-Oriented Programming: The Decisions Behind the Design [[http://zone.ni.com/devzone/cda/tut/p/id/3574]] &lt;br /&gt;
[[http://decibel.ni.com/content/docs/DOC-2875]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Prograph ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Prograph is a programming language developed in the 1980s and 1990s primarily for the Macintosh in the 1980s and 1990s. While LabView is designed specifically for controlling and collecting data from experimental equipment, Prograph is intended as more of a multipurpose language. Similar to LabView, it follows the dataflow paradigm.&lt;br /&gt;
&lt;br /&gt;
Prograph supports encapsulation and data abstraction through classes with single inheritance.&lt;br /&gt;
&lt;br /&gt;
Prograph achieved its greatest adoption for small applications developed in-house potentially by non-programmers. Though it faded to obscurity after the company developing it went bankrupt in 1995, since then, user groups have brought about its reemergence as an open standard.&lt;br /&gt;
&lt;br /&gt;
Qinwei Zhu, Jiunwei Chen, Visual Programming. [[http://courses.cs.vt.edu/~cs6704/VPL.ppt]]&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_n_7i_OE&amp;diff=43374</id>
		<title>CSC/ECE 517 Fall 2010/ch7 n 7i OE</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_n_7i_OE&amp;diff=43374"/>
		<updated>2010-12-07T21:01:58Z</updated>

		<summary type="html">&lt;p&gt;Opevans: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Visual Object Oriented Programming ==&lt;br /&gt;
&lt;br /&gt;
Visual programming languages are a class of languages where the program is defined by a series of interconnected pictoral icons rather than textual statements. Many graphical programming languages are designed to be accessable to those without formal training in programming. Theoretically, a visual programming language might be easier for a novice to comprehend, as rather than memorizing syntax and methods, the user is presented with a pallette of icons to choose from and can lay them out in a logical and familiar way, similar to an electric circut diagram.&lt;br /&gt;
&lt;br /&gt;
Visual programming languages often have several disadvantages. Though the basic functionality may be more accessible to novices than text-based languages, there is a tradeoff. It may be more difficult to implement complicated algorithms or control paths. Complicated programs are difficult to keep clean.  and &amp;quot;spaghetti&amp;quot; code may result. Many visual programming languages have been developed for a host of reasons, but few have been widely adopted. The two examples below are exceptions, and have garnered a significant user base.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Labview / G ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Labview is a proprietary programming language developed by National Instruments. It is widely used in laboratory environments for control and operation of experimental equipment. LabView's target users generally do not have formal training in computer programming so it is designed to be intuitive. The program is graphically represented in a manner similar to an electrical schematic. Labview is considered to be a dataflow language, where operations are performed as soon as their inputs become valid, rather than when they are encountered in code.&lt;br /&gt;
&lt;br /&gt;
As of version 8.2, LabView or G incorporates several object-oriented principles. Given LabView's orientation towards non-programmers, the language's designers made some tradeoffs between implementing all aspects of object-oriented languages, and maintaining simplicity. The result was that two of the tenets of OO programming were implemented: Encapsulation and Inheritence.&lt;br /&gt;
&lt;br /&gt;
Encapsulation. LabView implements classes and supports the concepts of public, private, and protected methods, but private data only. Private data within classes is accessed and modified by accessor methods (known as virtual instruments, or VIs.)&lt;br /&gt;
&lt;br /&gt;
Inheritance. Classes can inherit other classes. Because there is no protected or public data, all data from parent classes must be accessed through the accessor methods.&lt;br /&gt;
&lt;br /&gt;
There are several significant differences between the implementation of Object-Oriented principles in an imperitave language like Java or C++ and LabView. In Java, classes are passed by reference; in C++ they can be passed by reference or value. In LabView, they are passed by value only and therefore may be duplicated by the compiler. Many existing OO design patterns, specifically those from the Gang of Four book, depend on classes that are passed by reference. Therefore, the LabView developers have designed a different set of design patterns&lt;br /&gt;
LabVIEW Object-Oriented Programming: The Decisions Behind the Design [[http://zone.ni.com/devzone/cda/tut/p/id/3574]] &lt;br /&gt;
[[http://decibel.ni.com/content/docs/DOC-2875]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Prograph ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Prograph is a programming language developed in the 1980s and 1990s primarily for the Macintosh in the 1980s and 1990s. While LabView is designed specifically for controlling and collecting data from experimental equipment, Prograph is intended as more of a multipurpose language. Similar to LabView, it follows the dataflow paradigm.&lt;br /&gt;
&lt;br /&gt;
Prograph supports encapsulation and data abstraction through classes with single inheritance.&lt;br /&gt;
&lt;br /&gt;
Prograph achieved its greatest adoption for small applications developed in-house potentially by non-programmers. Though it faded to obscurity after the company developing it went bankrupt in 1995, since then, user groups have brought about its reemergence as an open standard.&lt;br /&gt;
&lt;br /&gt;
Qinwei Zhu, Jiunwei Chen, Visual Programming. [[http://courses.cs.vt.edu/~cs6704/VPL.ppt]]&lt;/div&gt;</summary>
		<author><name>Opevans</name></author>
	</entry>
</feed>