Fraction.java - package printFraction import java.util.Scanner import printTestFraction.TestFraction public class Fraction member variables private int

Fraction.java - package printFraction import...

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

package printFraction; import java.util.Scanner; import printTestFraction.TestFraction; public class Fraction { // member variables private int numerator, denominator; // stores the fraction data public Fraction() { numerator = denominator = 0; } public int getNumerator() { return numerator; } public void setNumerator(int num) { numerator=num; } public int getDenominator() { return denominator; } public void setDenominator(int den) { denominator=den; } public Fraction add(Fraction b) { // check preconditions if ((denominator == 0) || (b.denominator == 0)) throw new IllegalArgumentException("invalid denominator"); // find lowest common denominator int common = lcd(denominator, b.denominator); // convert fractions to lcd Fraction commonA = new Fraction(); Fraction commonB = new Fraction(); commonA = convert(common); commonB = b.convert(common); // create new fraction to return as sum Fraction sum = new Fraction(); // calculate sum sum.numerator = commonA.numerator + commonB.numerator; sum.denominator = common; // reduce the resulting fraction sum = sum.reduce(); return sum; } public void input() { // prompt user to enter numerator
Image of page 1
System.out.print("Please enter an integer for numerator: ");
Image of page 2
Image of page 3

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture