0

I have tried to write TestFraction in such a way that the main method calls on the methods getFraction1 and getFraction2 to create two Fractions: fr1 (a/b) and fr2 (a/b). getFraction1 and getFraction2 prompt the user for two integers, a and b, and call on getNumber to capture these integers. Fraction then performs calculations on fr1 and fr2. The problem is, when I run TestFraction, the values of a and b are left at 1, which is what they're set to in Fraction. Can anyone give me a hint as to why getFraction1 and getFraction2 aren't actually passing the values of a and b into fr1 and fr2?

Here is my code for TestFraction:

package Fraction;

import java.io.*;
import java.util.*;

public class TestFraction
{  

    static Scanner console = new Scanner (System.in);

    private static int getNumber() throws InputMismatchException
    {
                    return console.nextInt();
    }

    private static Fraction getFraction1()
    {
            int a=1, b=1;
            Fraction frac = new Fraction ();

            while (true) {

                    // prompt to enter a numerator and a denominator
                    System.out.println("Input 1st fraction numerator and denominator");
                    // input the numerator and the denominator using getNumber()
                    try {
                            a = getNumber();
                            b = getNumber();
                            }
                    catch (Exception e)
                    {
                            System.out.println("Exception: "+e.toString());
                            console.nextLine();
                            continue;
                    }
                    return frac;
                    // return new Fraction if OK
                    // otherwise print an error message
            }
    }

    private static Fraction getFraction2()
    {
            int a=1, b=1;
            Fraction frac = new Fraction ();

            while (true) {

                    // prompt to enter a numerator and a denominator
                    System.out.println("Input 2nd fraction numerator and denominator");
                    // input the numerator and the denominator using getNumber()
                    try {
                            a = getNumber();
                            b = getNumber();
                    }

                    catch (Exception e)
                    {
                            System.out.println("Exception: "+e.toString());
                            console.nextLine();
                            continue;
                    }
                    return frac;
                    // return new Fraction if OK
                    // otherwise print an error message
            }
    }
    public static void main(String[] args)
    {  
            Fraction fr1 = new Fraction ();
            fr1 = getFraction1();

            Fraction fr2 = new Fraction ();
            fr2 = getFraction2();

            Fraction res = new Fraction();

            // define other variables including res and fr2

            res  = Fraction.add  (fr1, fr2);
            System.out.println(fr1+" + "+fr2+" = "+res);  

            res      =      Fraction.subtract (fr1, fr2);
            System.out.println(fr1+" - "+fr2+" = "+res);
            res          =     Fraction.multiply (fr1, fr2);
            System.out.println(fr1+" * "+fr2+" = "+res);
            res          =     Fraction.divide (fr1, fr2);
            System.out.println(fr1+" / "+fr2+" = "+res);
            res          =     Fraction.lessThan (fr1, fr2);
            System.out.println(fr1+" "+res+" "+fr2);

            // test subtract, multiply, divide, lessThan methods
            //    each test has to print a description, a result,
            //    and, possibly, a error message if the calculation fails

    }

}

And here is Fraction:

package Fraction;

public class Fraction 
{
protected int a;
protected int b;
public Fraction()
{
    a = 1;
    b = 1;
}
public Fraction (int a, int b)
{
    this.a=a;
    this.b=b;
}
public int getNumerator()
{
    return a;
}
public int getDenominator() 
{
    return b;
}
public void setNumerator(int a)
{
    this.a=a;
}

public void setDenominator(int b)
{
    this.b=b;
}
public String toString ()
{
    return a+"/"+b;
}
public int gcd(int a, int b)
{
    //ToDo   implement Euclide algorithm
    if (b==0) return a;
    return gcd(b, a%b);
}
public void lowestTerms()
{   
    int g=gcd(a,b);
    a=a/g;
    b=b/g;
}
public static Fraction add(Fraction first, Fraction second)
{
    Fraction result = new Fraction();
    result.setNumerator(first.getNumerator()*second.getDenominator()
                      + first.getDenominator()*second.getNumerator());
    result.setDenominator(first.getDenominator()*second.getDenominator());
    result.lowestTerms();
    return result;
}
//ToDo methods subtract, multiply, divide, lessThan 
public static Fraction subtract(Fraction first, Fraction second)
{
    Fraction result = new Fraction();
    result.setNumerator(first.getNumerator()*second.getDenominator() 
                      - first.getDenominator()*second.getNumerator());
    result.setDenominator(first.getDenominator()*second.getDenominator());
    result.lowestTerms();
    return result;
}
public static Fraction multiply(Fraction first, Fraction second)
{
    Fraction result = new Fraction();
    result.setNumerator(first.getNumerator()*second.getNumerator());
    result.setDenominator(first.getDenominator()*second.getDenominator());
    result.lowestTerms();
    return result;
}
public static Fraction divide(Fraction first, Fraction second)
{
    Fraction result = new Fraction();
    result.setNumerator(first.getNumerator()*second.getDenominator());
    result.setDenominator(first.getDenominator()*second.getNumerator());
    result.lowestTerms();
    return result;
}
public static Fraction lessThan(Fraction first, Fraction second)
{
    if (first.getNumerator()*second.getDenominator() <= 
            first.getDenominator()*second.getNumerator()){
    return first;
    }
    else {
    return second;
    } 
}
}
4

2 回答 2

1

您必须使用Faction(int, int)构造函数而不是Fraction()构造函数:

Fraction frac = new Fraction ();

一定是

Fraction frac = null;
//later in the code once you have a and b variables set and no exceptions...
frac = new Fraction (a, b);

使用设置器:

frac.setNumerator(a);
frac.setDenominator(b);

在返回frac变量之前。

于 2013-06-22T18:41:01.800 回答
0

看起来您没有设置从控制台读取的值

a = getNumber();
b = getNumber();

frac.setNumerator(a);
frac.setDenominator(b);

默认情况下ab设置为1, 当你说

Fraction frac = new Fraction();

因为Fraction默认构造函数是这样定义的

public Fraction()
{
    a = 1;
    b = 1;
}
于 2013-06-22T18:43:04.083 回答