Fraction.java - package printFraction import java.util.Scanner public class Fraction private int numerator private int denominator public void input

Fraction.java - package printFraction import...

This preview shows page 1 - 3 out of 3 pages.

package printFraction; import java.util.Scanner; public class Fraction { private int numerator; private int denominator; public void input() { Scanner sc = new Scanner(System.in); while (true) { try { System.out.print("Enter numerator : "); this.numerator = sc.nextInt(); break; } catch (Exception e) { System.out.println("Invalid input, Please try again"); } } while (true) { try { System.out.print("Please enter denominator : "); this.denominator = sc.nextInt(); if (denominator == 0) { System.out.println("Please enter number greater than 0"); } else { break; } } catch (Exception e) { System.out.println("Invalid input, Please try again"); } } } public void output() { char sign=' '; if(numerator<0 && denominator <0) { sign = ' '; } else if(numerator <0 || denominator <0) { sign = '-'; } if(numerator<0){ numerator = -1*numerator; } if(denominator<0) { denominator = denominator*-1; } System.out.println(sign+""+numerator+" / "+denominator); }
Image of page 1
public boolean isZero() { if(this.numerator == 0) { return true; } else { return false; } } public boolean isEqual(Fraction f) { if(this.numerator == f.numerator && this.denominator == f.denominator) { return true; } else { return false; } }
Image of page 2
Image of page 3

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture