Java 小例子:類比分數的類 Fraction__Java

來源:互聯網
上載者:User

前一陣子 CSDN 部落格奇慢無比,今天好些了。

 

這裡是一個類比分數運算的例子:Fraction 類。分數運算完後要用最大公約數除分子分母。所以這裡也有個用輾轉相除法求最大公約數的例子。另外在構造 Fraction 對象時如果分母為零將會拋出異常,這也是必要的檢查。

public class FractionTest { public static void main(String[] args) { Fraction a = new Fraction(7, 32); Fraction b = new Fraction(13, 32); System.out.println(a + " + " + b + " = " + a.add(b) + "(" + a.add(b).doubleValue() + ")"); System.out.println(a + " - " + b + " = " + a.minus(b) + "(" + a.minus(b).doubleValue() + ")"); System.out.println(a + " * " + b + " = " + a.multiply(b) + "(" + a.multiply(b).doubleValue() + ")"); System.out.println(a + " / " + b + " = " + a.devide(b) + "(" + a.devide(b).doubleValue() + ")"); } } // 分數 class Fraction { private int numerator; // 分子 private int denominator; // 分母 Fraction(int numerator, int denominator) { if (denominator == 0) { throw new IllegalArgumentException("分母不能為 0"); } this.numerator = numerator; this.denominator = denominator; shrink(); } Fraction() { this(0, 1); } public int getNumerator() { return numerator; } public void setNumerator(int numerator) { this.numerator = numerator; } public int getDenominator() { return denominator; } public void setDenominator(int denominator) { this.denominator = denominator; } // 分子分母同除以最大公約數 private Fraction shrink() { int maxCommonDivisor = getMaxCommonDivisor(this.denominator, this.numerator); this.numerator /= maxCommonDivisor; this.denominator /= maxCommonDivisor; return this; } // 輾轉相除法求最大公約數 private int getMaxCommonDivisor(int a, int b) { int mod = a % b; if (mod == 0) { return b; } else { return getMaxCommonDivisor(b, mod); } } // 分數加法 public Fraction add(Fraction that) { return new Fraction(this.numerator * that.denominator + this.denominator * that.numerator, this.denominator * that.denominator); } // 分數減法 public Fraction minus(Fraction that) { return new Fraction(this.numerator * that.denominator - this.denominator * that.numerator, this.denominator * that.denominator); } // 分數乘法 public Fraction multiply(Fraction that) { return new Fraction(this.numerator * that.numerator, this.denominator * that.denominator); } // 分數除法 public Fraction devide(Fraction that) { return new Fraction(this.numerator * that.denominator, this.denominator * that.numerator); } public double doubleValue() { return (double) numerator / denominator; } @Override public String toString() { return String.format("{%d/%d}", this.numerator, this.denominator); } }

 

運行輸出:

{7/32} + {13/32} = {5/8}(0.625)
{7/32} - {13/32} = {-3/16}(-0.1875)
{7/32} * {13/32} = {91/1024}(0.0888671875)
{7/32} / {13/32} = {7/13}(0.5384615384615384)

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.