2

I have two classes.

import java.util.Scanner;
public class Fraction
{
  private int numerator;
  private int denominator;

  public void inputValues()
  {
    Scanner kb = new Scanner(System.in);
    System.out.println("Enter your numerator: ");
    numerator = kb.nextInt();
    System.out.println("Enter your denominator: ");
    denominator = kb.nextInt();                   
  }

  public int getNumerator()            //GetMethod
  {return numerator;}


  public boolean isZero()
  {
    if (getNumerator ==0)
    return false;
  }
}

I want my program to stop looping once the value for numerator is 0. and I've made a silly mistake somewhere but I cant seem to see it or figure out why. Many thanks in advance and much appreciated.

public class FractionDemo{
  public static void main (String[]argv) {
    Fraction f1 = new Fraction();
    Fraction f2 = new Fraction();
    f1.inputValues();
    f2.inputValues();

    while(f1.isZero())
    {
      f1.inputValues();
      f2.inputValues();
    }

  }
}
3
  • Do you want the program to stop when either numerator or denominator is 0? Also why are you creating 2 objects of Fraction? Commented Feb 10, 2019 at 6:48
  • Well only numerator for now. I am creating 2 objects of Fraction as I want user to input 2 fractions in my program to add, multiply, divide etc. But for now, I can't even get it to loop until a 0 is being input. Commented Feb 10, 2019 at 6:53
  • 1
    I've included an answer, hope it helps. Commented Feb 10, 2019 at 6:55

3 Answers 3

1

Well firstly your code doesn't compile. You need to return a value when the if condition evaluates to false, add the else condition to your isZero()

public boolean isZero() {
    if (getNumerator() == 0)
        return false;
    else
        return true;
}

Or a more cleaner way using ternary operator :

public boolean isZero() {
   return getNumerator() == 0 ? false: true;
}

Now you need just one object of the class Fraction

public static void main(String[] argv) {
    Fraction f1 = new Fraction();
    f1.inputValues();

    while (f1.isZero()) {
        f1.inputValues();
    }

}

Your program will now stop after the user enters 0 as the numerator.

Sign up to request clarification or add additional context in comments.

Comments

0

First of all, your code won't compile. Methods that return a value must do so in all cases. Your isZero() method doesn't return true when it should. Modify it to the following:

public boolean isZero()
{
   if (getNumerator() == 0)
      return false;
   else 
      return true;
} 

Comments

0

You should probably change your boolean

public boolean isZero() {

    if (getNumerator() == 0) {     //if is zero, return true, else, return false
        return true;
    } 
    return false;               //booleans default to false so you should return true when necessary
}

Syntactically, if you want the loop to continue while(true) change it to

 while (!f1.isZero()) {
        f1.inputValues();
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.