0

I'm writing a program wherein I define the class Function, and have methods to perform arithmetic operations with them. In the output, in my FractionDemo class, I run into problems trying to write the output, getting the @12f9de etc stuff. So I know I need to override the toString function, but I'm running into a cannot find symbol error.

Here's my code:

Function:

public class Fraction
{
    int numerator;
    int denominator;

    public Fraction(int numerator, int denominator)
    {
        this.numerator = numerator;
        this.denominator = denominator;
    }

    public void setNumerator(int numerator)
    {
        this.numerator = numerator;
        return;
    }

    public void setDenominator(int denominator)
    {
        this.denominator = denominator;
        return;
    }

    public int getNumerator()
    {
        return numerator;
    }

    public int getDenominator()
    {
        return denominator;
    }

    public static Fraction add(Fraction fraction1, Fraction fraction2)
    {
        Fraction fraction3 = new Fraction(((fraction1.getNumerator() * fraction2.getDenominator()) + (fraction1.getDenominator() * fraction2.getNumerator())), (fraction1.getDenominator() * fraction2.getDenominator()));
        return fraction3;
    }

    public static Fraction subtract(Fraction fraction1, Fraction fraction2)
    {
        Fraction fraction3 = new Fraction(((fraction1.getNumerator() * fraction2.getDenominator()) - (fraction1.getDenominator() + fraction2.getNumerator())), (fraction1.getDenominator() * fraction2.getDenominator()));
        return fraction3;
    }

    public static Fraction multiply(Fraction fraction1, Fraction fraction2)
    {
        Fraction fraction3 = new Fraction((fraction1.getNumerator() * fraction2.getNumerator()), (fraction1.getDenominator() * fraction2.getDenominator()));
        return fraction3;
    }

    public static Fraction divide(Fraction fraction1, Fraction fraction2)
    {
        if (fraction2.getNumerator() != 0)
        {
            Fraction fraction3 = new Fraction((fraction1.getNumerator() * fraction2.getDenominator()), (fraction1.getDenominator() * fraction2.getNumerator()));
            return fraction3;
        }
        else throw new IllegalArgumentException("Division by Zero");
    }
}

FractionDemo:

public class FractionDemo
{      
    public static void main(String[] args)
    {
        Fraction fraction1 = new Fraction(4,7);
        Fraction fraction2 = new Fraction(5,6);

        Fraction fractionSum = Fraction.add(fraction1, fraction2);
        System.out.println(fractionSum);

        Fraction fractionDif = Fraction.subtract(fraction1, fraction2);
        System.out.println(fractionDif);

        Fraction fractionProd = Fraction.multiply(fraction1, fraction2);
        System.out.println(fractionProd);

        Fraction fractionDividend = Fraction.divide(fraction1, fraction2);
        System.out.println(fractionDividend);
    }

    public String toString()
    {
        return ("The result is:  " + numerator + "/" + denominator);
    }
}

Anyway, is there any way I can get the output to be correctly formatted? It says it can't find my variables in the toString section at the bottom of FractionDemo. No matter whether I use the variable names, a function call like [fractionSum.getNumerator], or anything. I'll say right away that I don't really understand the overriding toString thing, I'm pretty new to Java.

Let me know if there's anything I should add.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
user1786288
  • 21
  • 1
  • 1
  • 4

2 Answers2

3

Your toString method needs to be in your Fraction class, not in FractionDemo.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
Jamey Sharp
  • 8,363
  • 2
  • 29
  • 42
  • +1 - and since it is in the wrong class, it cannot refer to instance variables of the right class. Hence the compilation errors. – Stephen C Oct 31 '12 at 02:12
1

The toString has to be defined in the Fraction class, not in the FractionDemo class.

UPDATE: And since you're at it, make the class final, make the fields private final, remove the setters and make your toString return the raw fraction representation, without the phrase "The result is: " (that should be the job of FractionDemo):

public final class Fraction 
{

  private final int numerator;
  private final int denominator;

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

  ...
}

The rationale is that Fraction should be an immutable value class.

Also, I believe you should not accept a denominator of zero:

public Fraction(int numerator, int denominator)
{
    if (denominator == 0) throw new IllegalArgumentException("Denominator can't be zero!");
    this.numerator = numerator;
    this.denominator = denominator;
}

And, you should implement equals and hashCode to make your class have true value semantics.

Jordão
  • 55,340
  • 13
  • 112
  • 144
  • That seemed to do it. It's possible that That was what I did before, when I used it in a lab exercise. I didn't really understand it at the time, so I honestly don't remember where I defined it. I guess that does it. Now all I have to do is wait for my work to be critiqued. :P – user1786288 Oct 31 '12 at 01:11
  • @user1786288: added some more ideas. – Jordão Oct 31 '12 at 01:16
  • 2
    Agreed with all of the above - in addition you may want to look at this thread for considerations of performance and flexibility: http://stackoverflow.com/questions/925423/is-it-better-practice-to-use-string-format-over-string-concatenation-in-java – Romski Oct 31 '12 at 02:17
  • @Romski, the Java compiler translates this kind of string concatenation to `new StringBuilder().append(numerator).append("/").append(denominator).toString()`, which is hard to beat. – Jamey Sharp Oct 31 '12 at 02:20