/* * Parke Godfrey 2009-10-22 * * Reads in a list of fractions, prompting the user for each. * Finds the fraction with the smallest denominator and the * fraction with the largest denominator acros the list, * considering the fractions in reduced form. It also reports * whether the fraction with the smallest denominator is smaller than, * the same size as, or larger than the one with the largest denominator. */ import java.io.PrintStream; import java.util.Scanner; import type.lib.Fraction; public class FractionAction { public static void main(String[] args) { PrintStream output = System.out; Scanner input = new Scanner(System.in); output.println("Number of fractions to enter?"); int total = input.nextInt(); // Get the first fraction from the user. // This is the fraction with both the smallest and largest denominator // we have seen so far! output.println("Enter fraction #1 (numerator denominator): "); Fraction smallestDenom = new Fraction(input.nextLong(), input.nextLong()); Fraction largestDenom = smallestDenom; // Read the rest of the fractions. Check each to see if // it should be the new smallest or largest denominator fraction. // Note that Fraction keeps the fraction in reduced form, // so we do not have to do this ourselves. for (int i = 2; i <= total; i++) { output.println("Enter fraction #" + i + " (numerator denominator): "); Fraction frac = new Fraction(input.nextLong(), input.nextLong()); if (frac.getDenominator() < smallestDenom.getDenominator()) { smallestDenom = frac; } if (frac.getDenominator() > largestDenom.getDenominator()) { largestDenom = frac; } } output.println("The fraction with the smallest denominator seen was " + smallestDenom + "."); output.println("The fraction with the largest denominator seen was " + largestDenom + "."); if (smallestDenom.compareTo(largestDenom) < 0) { output.println(smallestDenom + " is smaller than " + largestDenom + "."); } else if (smallestDenom.compareTo(largestDenom) > 0) { output.println(smallestDenom + " is larger than " + largestDenom + "."); } else { output.println(smallestDenom + " is the same as " + largestDenom + "."); } } }