Rational.java - import java.util.Scanner class Rational...

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

import java.util.Scanner; class Rational { private int numerator; private int denominator; public Rational() { numerator = 1; denominator = 2; } public Rational(int numerator, int denominator) { int g =gcd(numerator,denominator); this.numerator = numerator/g; this.denominator = denominator/g; } public int getNumerator() { return numerator; } public void setNumerator(int numerator) { this.numerator = numerator; } public int getDenominator() { return denominator; } public void setDenominator(int denominator) { this.denominator = denominator; } public Rational add(Rational r) { int num = numerator * r.denominator + r.numerator * denominator; int denom = denominator * r.denominator; return new Rational(num, denom); } public Rational subtract(Rational r) { int num = numerator * r.denominator - r.numerator * denominator; int denom = denominator * r.denominator; return new Rational(num, denom); } public Rational multiply(Rational r) { int num = numerator * r.numerator; int denom = denominator * r.denominator; return new Rational(num, denom); } public Rational divide(Rational r) { int num = numerator * r.denominator; int denom = denominator * r.numerator;

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture