alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (Ratio.java)

This example Java source code file (Ratio.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

bigdecimal, biginteger, comparable, math, number, ratio, string

The Ratio.java Java example source code

/**
 *   Copyright (c) Rich Hickey. All rights reserved.
 *   The use and distribution terms for this software are covered by the
 *   Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
 *   which can be found in the file epl-v10.html at the root of this distribution.
 *   By using this software in any fashion, you are agreeing to be bound by
 * 	 the terms of this license.
 *   You must not remove this notice, or any other, from this software.
 **/

/* rich Mar 31, 2008 */

package clojure.lang;

import java.math.BigInteger;
import java.math.BigDecimal;
import java.math.MathContext;

public class Ratio extends Number implements Comparable{
final public BigInteger numerator;
final public BigInteger denominator;

public Ratio(BigInteger numerator, BigInteger denominator){
	this.numerator = numerator;
	this.denominator = denominator;
}

public boolean equals(Object arg0){
	return arg0 != null
	       && arg0 instanceof Ratio
	       && ((Ratio) arg0).numerator.equals(numerator)
	       && ((Ratio) arg0).denominator.equals(denominator);
}

public int hashCode(){
	return numerator.hashCode() ^ denominator.hashCode();
}

public String toString(){
	return numerator.toString() + "/" + denominator.toString();
}

public int intValue(){
	return (int) doubleValue();
}

public long longValue(){
	return bigIntegerValue().longValue();
}

public float floatValue(){
	return (float)doubleValue();
}

public double doubleValue(){
	return decimalValue(MathContext.DECIMAL64).doubleValue();
}

public BigDecimal decimalValue(){
	return decimalValue(MathContext.UNLIMITED);
}

public BigDecimal decimalValue(MathContext mc){
	BigDecimal numerator = new BigDecimal(this.numerator);
	BigDecimal denominator = new BigDecimal(this.denominator);

	return numerator.divide(denominator, mc);
}

public BigInteger bigIntegerValue(){
	return numerator.divide(denominator);
}

public int compareTo(Object o){
	Number other = (Number)o;
	return Numbers.compare(this, other);
}
}

Other Java examples (source code examples)

Here is a short list of links related to this Java Ratio.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.