2012-01-19 14:07:33Morris

[JAVA][作業練習] Lab3

/**
 *
 * @author Shiang-Yun Yang
Define a class called Fraction for performing arithmetic with fractions.
Write a main method to test all the functionalities of the Fraction class.
Use integer variables to represent the private data of the class-- the
numerator and the denominator. Provide a no-argument constructor
that initializes numerator and denominator to 0 and 1, respectively.
Also, provide a two-argument constructor that initializes numerator
and denominator to the initial values given at the declaration of the
object. The constructor should store the fraction in reduced form. For
example, the fraction 2/4 would be stored in the object as 1 in the
numerator and 2 in the denominator.

Provide public methods that perform each of the following tasks:
(a) Adding two Fraction numbers. The result should be stored in
    reduced form.
(b) Multiplying two Fraction numbers. The result should be stored in
    reduced form.
(c) Printing Fraction numbers in the form a/b, where a is the
    numerator and b is the denominator.
(d) Printing Fraction numbers in floating-point format.
 */

import java.util.Scanner;
import java.util.StringTokenizer;
public class Fraction {
    /**
     * @param args the command line arguments
     */
     public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        StringTokenizer input = new StringTokenizer(read.nextLine(), "/ ");
        int innum, inden;
        innum = Integer.parseInt(input.nextToken());
        inden = Integer.parseInt(input.nextToken());
        
        Fraction A = new Fraction(innum, inden);
        System.out.println("Fraction A = " + A.toString());
        System.out.printf("Fraction A = %f\n", A.toDouble());
       
        innum = Integer.parseInt(input.nextToken());
        inden = Integer.parseInt(input.nextToken());
        Fraction B = new Fraction(innum, inden);
        System.out.println("Fraction B = " + B.toString());
        System.out.printf("Fraction A = %f\n", B.toDouble());
       
        Fraction R = new Fraction();
        R = add(A, B);
        System.out.println("Result = " + R.toString());
        System.out.printf("Result = %f\n", R.toDouble());

    }
    private int numerator, denominator;
    Fraction() {
        numerator = 0;
        denominator = 1;
    }
    Fraction(int innum, int inden) {
        numerator = innum;
        denominator = inden;
        Reduce();
    }
    Fraction(Fraction New) {
        numerator = New.getNumerator();
        denominator = New.getDenominator();
        Reduce();
    }
   
    public static Fraction add(Fraction FA, Fraction FB) {
        int denA = FA.getDenominator(), denB = FB.getDenominator();
        int numA = FA.getNumerator(), numB = FB.getNumerator();
        int GcdDen = gcd(denA, denB);
        int newden = denA*denB / GcdDen;
        int newnum = denB/GcdDen*numA + denA/GcdDen*numB;
        Fraction result = new Fraction(newnum, newden);
        return new Fraction(result);
    }
    public Fraction add(Fraction FB) {
        int denA = this.denominator, denB = FB.getDenominator();
        int numA = this.numerator, numB = FB.getNumerator();
        int GcdDen = gcd(denA, denB);
        int newden = denA*denB / GcdDen;
        int newnum = denB/GcdDen*numA + denA/GcdDen*numB;
        Fraction result = new Fraction(newnum, newden);
        return new Fraction(result);
    }
   
    public static Fraction multiply(Fraction FA, Fraction FB) {
        int newnum = FA.getNumerator()*FB.getNumerator();
        int newden = FA.getDenominator()*FB.getDenominator();
        Fraction result = new Fraction(newnum, newden);
        return new Fraction(result);
    }
    public Fraction multiply(Fraction FB) {
        int newnum = this.numerator * FB.getNumerator();
        int newden = this.denominator * FB.getNumerator();
        Fraction result = new Fraction(newnum, newden);
        return new Fraction(result);
    }
   
    public int getNumerator() {
        return numerator;       
    }
    public int getDenominator() {
        return denominator;
    }
    public void setNumerator(int num) {
        this.numerator = num;
    }
    public void setDenominator(int den) {
        this.denominator = den;
    }
   
    private static int gcd(int x, int y) {
        int tmp;
        while(x%y != 0) {
            tmp = x;
            x = y;
            y = tmp%y;
        }
        return y;
    }
    private void Reduce() {
        if(denominator < 0) {
            numerator *= -1;
            denominator *= -1;
        }
        int Gcd;
        Gcd = gcd(Math.abs(numerator), denominator);
        numerator /= Gcd;
        denominator /= Gcd;
    }
    public String toString() {
        if(denominator != 1)
            return numerator + "/" + denominator;
        else
            return Integer.toString(numerator);
    }
    public double toDouble() {
        return (double)numerator/denominator;
    }
}