import java.util.InputMismatchException;
import java.util.Scanner;

/**
* Prompts the user for two rational numbers and outputs the sum, difference, product,
* quotient, and the sum of their squares.
*
* @author Drue Coles
*/
public class RationalCalculator {

public static void main(String[] args) {

System.out.print("Enter two rational numbers (without slashes): ");
Scanner in = new Scanner(System.in);

int n1, d1, n2, d2;
while (true) {
try {
n1 = in.nextInt();
d1 = in.nextInt();
n2 = in.nextInt();
d2 = in.nextInt();
break; // exit loop
} catch (InputMismatchException e) {
System.out.print("Invalid input. Try again: ");
in.nextLine(); // clear input stream
}
}

RationalNumber a = new RationalNumber(n1, d1);
RationalNumber b = new RationalNumber(n2, d2);

// Compute the sum, difference, and product.
RationalNumber x = a.subtract(b);
RationalNumber y = a.multiply(b);

System.out.printf("%s + %s = %s %n", a, b, w);
System.out.printf("%s - %s = %s %n", a, b, x);
System.out.printf("%s * %s = %s %n", a, b, y);

// Compute a/b if possible.
try {
RationalNumber z = a.divide(b);
System.out.printf("%s / %s = %s %n", a, b, z);
} catch (ArithmeticException e) {
System.out.printf("Cannot divide %s by 0%n", a);
}

// Compute the sum of the squares of a and b.