Ejemplo n.º 1
0
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int numerator, denominator;
    try {
      System.out.print("Numerator: ");
      numerator = sc.nextInt();
      System.out.print("Denominator: ");
      denominator = sc.nextInt();
    } catch (InputMismatchException e) {
      System.out.println("That's not an integer!");
      sc.close();
      return;
    }
    sc.close();

    if (denominator == 0) {
      System.out.println("You can't have a denominator of 0!");
      return;
    }
    Fraction frac = new Fraction(numerator, denominator);
    System.out.println("Fraction is " + frac);
    System.out.println("Value is " + frac.getValue());
    System.out.println("Reducing...");
    frac.reduce();
    System.out.println("Reduced fraction is " + frac);
    System.out.println("Reduced value is still " + frac.getValue());
  }
Ejemplo n.º 2
0
  /**
   * (Subtracts the two fractions)
   *
   * @param (fraction the operation is being applied to)
   * @return (output fraction)
   */
  public Fraction minus(Fraction f) { // subtracts 2 fractions and returns reduce fraction result

    if (f.den != den) {
      f.num *= den;
      num *= f.den;
      f.den *= den;
      den = f.den;
    }

    f.num -= num;
    f.reduce();
    return f;
  }
Ejemplo n.º 3
0
  /**
   * (Adds the two fractions)
   *
   * @param (fraction the operation is being applied to)
   * @return (output fraction)
   */
  public Fraction plus(Fraction f) { // adds 2 fractions and returns reduce fraction result

    if (f.den != den) {
      f.num *= den;
      num *= f.den;
      f.den *= den;
      den = f.den;
    }

    f.num += num;
    f.reduce();
    return f;
  }
Ejemplo n.º 4
0
 /**
  * (Divides the two fractions)
  *
  * @param (fraction the operation is being applied to)
  * @return (output fraction)
  */
 public Fraction divide(Fraction f) { // divides 2 fractions and returns reduce fraction result
   f.num *= den;
   f.den *= num;
   f.reduce();
   return f;
 }
Ejemplo n.º 5
0
 /**
  * (Multiplies the two fractions)
  *
  * @param (fraction the operation is being applied to)
  * @return (output fraction)
  */
 public Fraction times(Fraction f) { // multiplies 2 fractions and returns reduce fraction result
   f.num *= num;
   f.den *= den;
   f.reduce();
   return f;
 }