分数减少功能

时间:2015-01-11 14:49:27

标签: java

我正在编写一个可以实现乘法,除法和减少分数的程序。当我测试reduce函数虽然它不起作用并返回0/1。它仅适用于测试reduce函数的第三类测试输出。这是代码和输出。

import java.util.*;
import java.util.Scanner.*;

class Fraction
{
    protected int numerator, denominator;

    public static void main(String args[])
    {
        System.out.println("Chp. 25 LAB 1");
        System.out.println();


//   main commands for testing first class Fraction

        Fraction f1 = new Fraction(2,4);
        System.out.println("numerator of fraction1 is  " + f1.getNum());
        System.out.println("denominator of fraction1 is  " + f1.getDen());
        f1.displayFraction();
        f1.alterFraction(3,6);
        f1.displayFraction();


//    main commands for testing second class FractionArithmetic

        f1 = new Fraction(2,4);
        Fraction f2 = new Fraction(4,6);

        FractionArithmetic f3 = new FractionArithmetic();
        f3.multFracts(f1, f2);
        f3.display();

        f3 = new FractionArithmetic();
        f3.divFracts(f1, f2);
        f3. display();


//    main commands for testing third class FractionOperations
System.out.println("THIRD CLASS TESTING");
        f1 = new Fraction(2,4);
        f2 = new Fraction(4,6);

        FractionOperations fract = new FractionOperations();
        fract.multFracts(f1, f2);
        fract.display();
        fract.reduce();
        fract.getFraction().displayFraction(); 

        fract = new FractionOperations();
        fract.divFracts(f1, f2);
        fract.display();
        fract.reduce();
        fract.getFraction().displayFraction();  
    }

    Fraction(int n, int d)
    {
        numerator = n;
        denominator = d;
    }
    public int getNum()
    {
        return numerator;
    }
    public int getDen()
    {
        return denominator;
    }
    public void alterFraction(int n, int d)
    {
        numerator = n;
        denominator = d;
    }
    public void displayFraction()
    {
        System.out.println("Fractioni = " + numerator + "/" + denominator);
    }
}

class FractionArithmetic
{   
    protected Fraction fraction;

    FractionArithmetic()
    {
        fraction = new Fraction(0, 1);
    }
    FractionArithmetic(int n, int d)
    {
        fraction = new Fraction(n, d);
    }
    public void multFracts(Fraction a, Fraction b)
    {
//      System.out.println(a.getNum() + " " + a.getDen() + " " + b.getNum() + " " + b.getDen());
        fraction.alterFraction(a.getNum() * b.getNum(), a.getDen() * b.getDen());
    }

    public void divFracts(Fraction a, Fraction b)
    {
        fraction.alterFraction(a.getNum() * b.getDen(), a.getDen() * b.getNum());
    }
    public void display()
    {
        System.out.println("The fraction below is an unreduced fraction");
        fraction.displayFraction();
    }
    public Fraction getFraction()
    {
        return fraction;
    }
}

class FractionOperations extends FractionArithmetic
{
    protected Fraction reducedFraction;

    FractionOperations()
    {
        reducedFraction = new Fraction(0, 1);
    }
    FractionOperations(int n, int d)
    {
        super(n, d);
        reducedFraction = new Fraction(n, d);
    }
    public void reduce()
    {
        int gcf = getGCF(reducedFraction.numerator, reducedFraction.denominator);
        fraction.alterFraction(reducedFraction.numerator / gcf, reducedFraction.denominator / gcf);
    }   
    private static int getGCF(int a, int b)
    {
        int gcf = 1;
        a = Math.abs(a);
        b = Math.abs(b);
        if(a > b)
        {
            int dummy = b;
            b = a;
            a = dummy;
        }
        for(int i = 1; i <= b; i++)
        {
            if(b % i == 0 && a % i == 0)
                gcf = i;
        }
        return gcf;
    }
    public void display()
    {
        super.display();
        fraction.displayFraction();
    }
    public Fraction getFraction()
    {
        return reducedFraction;
    }   
}




Chp. 25 LAB 1

numerator of fraction1 is  2
denominator of fraction1 is  4
Fractioni = 2/4
Fractioni = 3/6
The fraction below is an unreduced fraction
Fractioni = 8/24
The fraction below is an unreduced fraction
Fractioni = 12/16
THIRD CLASS TESTING
The fraction below is an unreduced fraction
Fractioni = 8/24
Fractioni = 8/24
Before the reducing, 0/1
reduced fraction :0/1
Fractioni = 0/1
The fraction below is an unreduced fraction
Fractioni = 12/16
Fractioni = 12/16
Fractioni = 0/1

1 个答案:

答案 0 :(得分:0)

这是FractionArithmetic和FractionOperations的结果,为您的设计增加了不必要的复杂性。添加,乘法等和减少的方法应该是分数的方法。

我已经通过实验将reduce添加到Fraction(并使GCF可见):

public void reduce(){
    int gcf = getGCF( numerator, denominator);
    this.alterFraction(numerator / gcf, denominator / gcf );
}

现在例如

f2 = new Fraction(4,6);
f2.reduce();
f2.displayFraction();

效果很好。