0

Possible Duplicate:
Division in Java always results in zero (0)?

So I'm writing this program and I thought it was good. The GUI window popped up and I entered a numerator and a demoninator. But no matter what I enter, it always says that it equals 0. So if I put in 2 for the numerator and 3 for the demoninator, the output will be 2/3 = 0. What's the problem?

I changed "int dec" to "double dec" as shown below and put "this.dec = dec" under the Rational class, but that didn't fix anything

import javax.swing.JOptionPane;


public class lab8
{
public static void main (String args[])
{
    String strNbr1 = JOptionPane.showInputDialog("Enter Numerator ");
    String strNbr2 = JOptionPane.showInputDialog("Enter Denominator ");

    int num = Integer.parseInt(strNbr1);
    int den = Integer.parseInt(strNbr2);

    Rational r = new Rational(num,den);
    JOptionPane.showMessageDialog(null,r.getNum()+"/"+r.getDen()+" equals "+r.getDecimal());

    System.exit(0);
}
}



class Rational
{
private int num;
private int den;
private double dec;

public Rational(int num, int den){
 this.num = num;
 this.den = den;
 this.dec = dec;
}
public int getNum()
{
    return num;
}

public int getDen()
{
    return den;
}

public double getDecimal()
{
    return dec;
}

private int getGCF(int n1,int n2)
{
    int rem = 0;
    int gcf = 0;
    do
    {
        rem = n1 % n2;
        if (rem == 0)
            gcf = n2;
        else
        {
            n1 = n2;
            n2 = rem;
        }
    }
    while (rem != 0);
    return gcf;
}
}
Community
  • 1
  • 1

1 Answers1

3

In class Rational, dec is not initialized, so it defaults to 0. Thus, when you later call getDecimal(), it always returns 0.

public Rational(int num, int den){
  this.num = num;
  this.den = den;

  // TODO: initialize dec here, otherwise it is implicitly set to 0.
}
Philipp Claßen
  • 41,306
  • 31
  • 146
  • 239
  • 1
    and `getDecimal` returns an `int` – D.Shawley Jan 19 '13 at 02:33
  • i put this.dec = dec in class Rational, but it didn't fix anything – user1991954 Jan 19 '13 at 02:34
  • @D.Shawley I did what you told me but still no dice, anything else that needs to be fixed? – user1991954 Jan 19 '13 at 02:42
  • 1
    Try: (1) remove `dec`, (2) implement `getDecimal` to return the result of dividing `num` by `den`, (3) do one more thing to make it work – D.Shawley Jan 19 '13 at 02:47
  • @user1991954 you have to compute the value: `this.dec = (double) num / den;`. Note the cast to `double`, otherwise it will be truncated (`1 / 2 == 0`). But, please, do not edit the question directly because it will be very difficult to follow for your other readers. The common way is to write "Edit" at the end of the question and clarify what to want to change. – Philipp Claßen Jan 19 '13 at 02:49
  • @D.Sahwley okay, that ALMOST fixed the problem. Now it's rounding it to the nearest 1 instead of giving me a decimal. – user1991954 Jan 19 '13 at 02:51
  • Okay I got everything figured out. Thanks for putting up with me – user1991954 Jan 19 '13 at 02:53