Java Doc for Fraction.java in  » Library » Apache-common-lang » org » apache » commons » lang » math » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache common lang » org.apache.commons.lang.math 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   java.lang.Number
      org.apache.commons.lang.math.Fraction

Fraction
final public class Fraction extends Number implements Comparable(Code)

Fraction is a Number implementation that stores fractions accurately.

This class is immutable, and interoperable with most methods that accept a Number.


author:
   Travis Reeder
author:
   Stephen Colebourne
author:
   Tim O'Brien
author:
   Pete Gieser
author:
   C. Scott Ananian
since:
   2.0
version:
   $Id: Fraction.java 489733 2006-12-22 19:29:53Z bayard $


Field Summary
final public static  FractionFOUR_FIFTHS
     Fraction representation of 4/5.
final public static  FractionONE
     Fraction representation of 1.
final public static  FractionONE_FIFTH
     Fraction representation of 1/5.
final public static  FractionONE_HALF
     Fraction representation of 1/2.
final public static  FractionONE_QUARTER
     Fraction representation of 1/4.
final public static  FractionONE_THIRD
     Fraction representation of 1/3.
final public static  FractionTHREE_FIFTHS
     Fraction representation of 3/5.
final public static  FractionTHREE_QUARTERS
     Fraction representation of 3/4.
final public static  FractionTWO_FIFTHS
     Fraction representation of 2/5.
final public static  FractionTWO_QUARTERS
     Fraction representation of 2/4.
final public static  FractionTWO_THIRDS
     Fraction representation of 2/3.
final public static  FractionZERO
     Fraction representation of 0.


Method Summary
public  Fractionabs()
    
public  Fractionadd(Fraction fraction)
    

Adds the value of this fraction to another, returning the result in reduced form.

public  intcompareTo(Object object)
    

Compares this object to another based on size.

Note: this class has a natural ordering that is inconsistent with equals, because, for example, equals treats 1/2 and 2/4 as different, whereas compareTo treats them as equal.

public  FractiondivideBy(Fraction fraction)
    
public  doubledoubleValue()
    

Gets the fraction as a double.

public  booleanequals(Object obj)
    

Compares this fraction to another object to test if they are equal.

.

To be equal, both values must be equal.

public  floatfloatValue()
    

Gets the fraction as a float.

public  intgetDenominator()
    
public static  FractiongetFraction(int numerator, int denominator)
    
public static  FractiongetFraction(int whole, int numerator, int denominator)
    
public static  FractiongetFraction(double value)
    
public static  FractiongetFraction(String str)
    
public  intgetNumerator()
    
public  intgetProperNumerator()
    

Gets the proper numerator, always positive.

An improper fraction 7/4 can be resolved into a proper one, 1 3/4.

public  intgetProperWhole()
    

Gets the proper whole part of the fraction.

An improper fraction 7/4 can be resolved into a proper one, 1 3/4.

public static  FractiongetReducedFraction(int numerator, int denominator)
    
public  inthashCode()
    
public  intintValue()
    

Gets the fraction as an int.

public  Fractioninvert()
    
public  longlongValue()
    

Gets the fraction as a long.

public  FractionmultiplyBy(Fraction fraction)
    
public  Fractionnegate()
    
public  Fractionpow(int power)
    
public  Fractionreduce()
    
public  Fractionsubtract(Fraction fraction)
    
public  StringtoProperString()
    

Gets the fraction as a proper String in the format X Y/Z.

The format used in 'wholeNumber numerator/denominator'. If the whole number is zero it will be ommitted.

public  StringtoString()
    

Gets the fraction as a String.

The format used is 'numerator/denominator' always.


Field Detail
FOUR_FIFTHS
final public static Fraction FOUR_FIFTHS(Code)
Fraction representation of 4/5.



ONE
final public static Fraction ONE(Code)
Fraction representation of 1.



ONE_FIFTH
final public static Fraction ONE_FIFTH(Code)
Fraction representation of 1/5.



ONE_HALF
final public static Fraction ONE_HALF(Code)
Fraction representation of 1/2.



ONE_QUARTER
final public static Fraction ONE_QUARTER(Code)
Fraction representation of 1/4.



ONE_THIRD
final public static Fraction ONE_THIRD(Code)
Fraction representation of 1/3.



THREE_FIFTHS
final public static Fraction THREE_FIFTHS(Code)
Fraction representation of 3/5.



THREE_QUARTERS
final public static Fraction THREE_QUARTERS(Code)
Fraction representation of 3/4.



TWO_FIFTHS
final public static Fraction TWO_FIFTHS(Code)
Fraction representation of 2/5.



TWO_QUARTERS
final public static Fraction TWO_QUARTERS(Code)
Fraction representation of 2/4.



TWO_THIRDS
final public static Fraction TWO_THIRDS(Code)
Fraction representation of 2/3.



ZERO
final public static Fraction ZERO(Code)
Fraction representation of 0.





Method Detail
abs
public Fraction abs()(Code)

Gets a fraction that is the positive equivalent of this one.

More precisely: (fraction >= 0 ? this : -fraction)

The returned fraction is not reduced.

this if it is positive, or a new positive fractioninstance with the opposite signed numerator



add
public Fraction add(Fraction fraction)(Code)

Adds the value of this fraction to another, returning the result in reduced form. The algorithm follows Knuth, 4.5.1.


Parameters:
  fraction - the fraction to add, must not be null a Fraction instance with the resulting values
throws:
  IllegalArgumentException - if the fraction is null
throws:
  ArithmeticException - if the resulting numerator or denominator exceedsInteger.MAX_VALUE



compareTo
public int compareTo(Object object)(Code)

Compares this object to another based on size.

Note: this class has a natural ordering that is inconsistent with equals, because, for example, equals treats 1/2 and 2/4 as different, whereas compareTo treats them as equal.
Parameters:
  object - the object to compare to -1 if this is less, 0 if equal, +1 if greater
throws:
  ClassCastException - if the object is not a Fraction
throws:
  NullPointerException - if the object is null




divideBy
public Fraction divideBy(Fraction fraction)(Code)

Divide the value of this fraction by another.


Parameters:
  fraction - the fraction to divide by, must not be null a Fraction instance with the resulting values
throws:
  IllegalArgumentException - if the fraction is null
throws:
  ArithmeticException - if the fraction to divide by is zero
throws:
  ArithmeticException - if the resulting numerator or denominator exceedsInteger.MAX_VALUE



doubleValue
public double doubleValue()(Code)

Gets the fraction as a double. This calculates the fraction as the numerator divided by denominator.

the fraction as a double



equals
public boolean equals(Object obj)(Code)

Compares this fraction to another object to test if they are equal.

.

To be equal, both values must be equal. Thus 2/4 is not equal to 1/2.


Parameters:
  obj - the reference object with which to compare true if this object is equal



floatValue
public float floatValue()(Code)

Gets the fraction as a float. This calculates the fraction as the numerator divided by denominator.

the fraction as a float



getDenominator
public int getDenominator()(Code)

Gets the denominator part of the fraction.

the denominator fraction part



getFraction
public static Fraction getFraction(int numerator, int denominator)(Code)

Creates a Fraction instance with the 2 parts of a fraction Y/Z.

Any negative signs are resolved to be on the numerator.


Parameters:
  numerator - the numerator, for example the three in 'three sevenths'
Parameters:
  denominator - the denominator, for example the seven in 'three sevenths' a new fraction instance
throws:
  ArithmeticException - if the denomiator is zero



getFraction
public static Fraction getFraction(int whole, int numerator, int denominator)(Code)

Creates a Fraction instance with the 3 parts of a fraction X Y/Z.

The negative sign must be passed in on the whole number part.


Parameters:
  whole - the whole number, for example the one in 'one and three sevenths'
Parameters:
  numerator - the numerator, for example the three in 'one and three sevenths'
Parameters:
  denominator - the denominator, for example the seven in 'one and three sevenths' a new fraction instance
throws:
  ArithmeticException - if the denomiator is zero
throws:
  ArithmeticException - if the denominator is negative
throws:
  ArithmeticException - if the numerator is negative
throws:
  ArithmeticException - if the resulting numerator exceeds Integer.MAX_VALUE



getFraction
public static Fraction getFraction(double value)(Code)

Creates a Fraction instance from a double value.

This method uses the continued fraction algorithm, computing a maximum of 25 convergents and bounding the denominator by 10,000.


Parameters:
  value - the double value to convert a new fraction instance that is close to the value
throws:
  ArithmeticException - if |value| > Integer.MAX_VALUE or value = NaN
throws:
  ArithmeticException - if the calculated denominator is zero
throws:
  ArithmeticException - if the the algorithm does not converge



getFraction
public static Fraction getFraction(String str)(Code)

Creates a Fraction from a String.

The formats accepted are:

  1. double String containing a dot
  2. 'X Y/Z'
  3. 'Y/Z'
  4. 'X' (a simple whole number)
and a .


Parameters:
  str - the string to parse, must not be null the new Fraction instance
throws:
  IllegalArgumentException - if the string is null
throws:
  NumberFormatException - if the number format is invalid



getNumerator
public int getNumerator()(Code)

Gets the numerator part of the fraction.

This method may return a value greater than the denominator, an improper fraction, such as the seven in 7/4.

the numerator fraction part



getProperNumerator
public int getProperNumerator()(Code)

Gets the proper numerator, always positive.

An improper fraction 7/4 can be resolved into a proper one, 1 3/4. This method returns the 3 from the proper fraction.

If the fraction is negative such as -7/4, it can be resolved into -1 3/4, so this method returns the positive proper numerator, 3.

the numerator fraction part of a proper fraction, always positive



getProperWhole
public int getProperWhole()(Code)

Gets the proper whole part of the fraction.

An improper fraction 7/4 can be resolved into a proper one, 1 3/4. This method returns the 1 from the proper fraction.

If the fraction is negative such as -7/4, it can be resolved into -1 3/4, so this method returns the positive whole part -1.

the whole fraction part of a proper fraction, that includes the sign



getReducedFraction
public static Fraction getReducedFraction(int numerator, int denominator)(Code)

Creates a reduced Fraction instance with the 2 parts of a fraction Y/Z.

For example, if the input parameters represent 2/4, then the created fraction will be 1/2.

Any negative signs are resolved to be on the numerator.


Parameters:
  numerator - the numerator, for example the three in 'three sevenths'
Parameters:
  denominator - the denominator, for example the seven in 'three sevenths' a new fraction instance, with the numerator and denominator reduced
throws:
  ArithmeticException - if the denominator is zero



hashCode
public int hashCode()(Code)

Gets a hashCode for the fraction.

a hash code value for this object



intValue
public int intValue()(Code)

Gets the fraction as an int. This returns the whole number part of the fraction.

the whole number fraction part



invert
public Fraction invert()(Code)

Gets a fraction that is the inverse (1/fraction) of this one.

The returned fraction is not reduced.

a new fraction instance with the numerator and denominatorinverted.
throws:
  ArithmeticException - if the fraction represents zero.



longValue
public long longValue()(Code)

Gets the fraction as a long. This returns the whole number part of the fraction.

the whole number fraction part



multiplyBy
public Fraction multiplyBy(Fraction fraction)(Code)

Multiplies the value of this fraction by another, returning the result in reduced form.


Parameters:
  fraction - the fraction to multiply by, must not be null a Fraction instance with the resulting values
throws:
  IllegalArgumentException - if the fraction is null
throws:
  ArithmeticException - if the resulting numerator or denominator exceedsInteger.MAX_VALUE



negate
public Fraction negate()(Code)

Gets a fraction that is the negative (-fraction) of this one.

The returned fraction is not reduced.

a new fraction instance with the opposite signed numerator



pow
public Fraction pow(int power)(Code)

Gets a fraction that is raised to the passed in power.

The returned fraction is in reduced form.


Parameters:
  power - the power to raise the fraction to this if the power is one, ONE if the poweris zero (even if the fraction equals ZERO) or a new fraction instance raised to the appropriate power
throws:
  ArithmeticException - if the resulting numerator or denominator exceedsInteger.MAX_VALUE



reduce
public Fraction reduce()(Code)

Reduce the fraction to the smallest values for the numerator and denominator, returning the result.

For example, if this fraction represents 2/4, then the result will be 1/2.

a new reduced fraction instance, or this if no simplification possible



subtract
public Fraction subtract(Fraction fraction)(Code)

Subtracts the value of another fraction from the value of this one, returning the result in reduced form.


Parameters:
  fraction - the fraction to subtract, must not be null a Fraction instance with the resulting values
throws:
  IllegalArgumentException - if the fraction is null
throws:
  ArithmeticException - if the resulting numerator or denominatorcannot be represented in an int.



toProperString
public String toProperString()(Code)

Gets the fraction as a proper String in the format X Y/Z.

The format used in 'wholeNumber numerator/denominator'. If the whole number is zero it will be ommitted. If the numerator is zero, only the whole number is returned.

a String form of the fraction



toString
public String toString()(Code)

Gets the fraction as a String.

The format used is 'numerator/denominator' always. a String form of the fraction




Methods inherited from java.lang.Number
public byte byteValue()(Code)(Java Doc)
abstract public double doubleValue()(Code)(Java Doc)
abstract public float floatValue()(Code)(Java Doc)
abstract public int intValue()(Code)(Java Doc)
abstract public long longValue()(Code)(Java Doc)
public short shortValue()(Code)(Java Doc)

Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.