import java.io.PrintWriter; import java.io.BufferedReader; import java.io.InputStreamReader; /** * Test the fascinating Rational numbers we're developing. */ public class TestRational { public static void main(String[] args) { // Prepare input and screenput. BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); PrintWriter screen = new PrintWriter(System.out, true); // Some simple tests. screen.println("Rationals Created as Fractions"); Rational a = new Rational(3,241*18); Rational b = new Rational(241,12); screen.println("a = " + a + " (or " + a.doubleValue() + ")"); screen.println("b = " + b + " (or " + b.doubleValue() + ")"); screen.println("Product of a and b"); Rational c = Rational.multiply(a,b); screen.println("c = " + c + " (or " + c.doubleValue() + ")"); screen.println("***********************************************"); screen.println("Some rationals created from reals."); testReal(0.5f, screen); testReal(0.3333f, screen); testReal(1000000.5f, screen); testReal(.000000001f, screen); screen.println("***********************************************"); testString("267/900", screen); testString("1/2", screen); testString("900/267", screen); testString("100/5", screen); testString("123", screen); // That's it, we're done. System.exit(0); } // main(String[]) public static void testReal(float f, PrintWriter screen) { Rational rat = new Rational(f); screen.println(f + " -> " + rat + " = " + rat.doubleValue()); } // testReal(Float, PrintWriter) public static void testString(String s, PrintWriter screen) { Rational rat = new Rational(s); screen.println(s + " -> " + rat); } } // class TestRational