Title: Fraction
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Fraction

Fraction.java



import java.io.PrintStream;
import java.util.Scanner;

public class Fraction
{
    static private Scanner scanner;
    static private PrintStream printStream;

    private int num=1;
    private int denom=1;

    public Fraction()
    {
    }

    public Fraction(int n, int d)
    {
        setFraction(n,d);
    }

    public void setFraction(int n, int d)
    {
        num = n;
        denom = d;
        reduce();
    }

    public Fraction add(Fraction op)
    {
        int n = num * op.denom + denom * op.num;
        int d = denom * op.denom;
        Fraction retval = new Fraction(n,d);
        return retval;
     }

    public Fraction subtract(Fraction op)
    {
        int n = num * op.denom - denom * op.num;
        int d = denom * op.denom;
        Fraction retval = new Fraction(n,d);
        return retval;
     }

    public Fraction multiply(Fraction op)
    {
        int n = num * op.num;
        int d = denom * op.denom;
        Fraction retval = new Fraction(n,d);
        return retval;
     }

    public Fraction divide(Fraction op)
    {
        int n = num * op.denom;
        int d = denom * op.num;
        Fraction retval = new Fraction(n,d);
        return retval;
     }

     private int abs(int x)
     {
        if (x < 0) return -x;
        return x;
     }

     private void reduce()
     {
        int divisor, smaller = abs(num);
        if (abs(denom) < smaller) smaller = abs(denom);

        divisor = 2;
        while (divisor <= smaller)
        {
            while (num % divisor == 0  && denom % divisor == 0)
            {
                num /= divisor;
                denom /= divisor;
                smaller /= divisor;
            }
            divisor += 1;
        }
     }

     public boolean equals(Object ob)
     {
         Fraction f= (Fraction)ob;
        // Assuming all fractions are reduced
        if (num == f.num && denom == f.denom)
            return true;
        return false;
     }

     public String toString()
     {
        return num +"/" + denom;
     }

     public void readin(String label)
     {
        while (true) // Keep trying if bad input is received
        {
            printStream.print(label);
            String temp = scanner.next();
            temp = temp.trim(); // get rid of white space at the beginning and end
            int index = temp.indexOf('/');
            if (index >= 0)
            {
                String numStr = temp.substring(0, index);
                String denomStr = temp.substring(index+1);
                int n = Integer.parseInt(numStr);
                int d = Integer.parseInt(denomStr);
                setFraction(n,d);
                return;
            }
            else
                printStream.println("Input Fraction missing / ");
        }//Keep trying until you get it right
     }

     public static boolean do_one_operation(Scanner scanner_parm, PrintStream printStream_parm)
     {
         scanner = scanner_parm;
         printStream = printStream_parm;

         Fraction f1= new Fraction();
         Fraction f2 = new Fraction();
         Fraction f3;

         printStream.println();  // Add a blank line
         printStream.print("Enter operation: + - * / q (q ==> quit) : ");
         String input = scanner.next();
         if (input.charAt(0) == 'q')
             return false; // All done

         f1.readin("Enter Fraction 1: ");
         f2.readin("Enter Fraction 2: ");
         printStream.println("f1 = " + f1);
         printStream.println("f2 = " + f2);

         if (f1.equals(f2))
             printStream.println("f1 and f2 are equal");
         else
             printStream.println("f1 and f2 are not equal");

         switch (input.charAt(0))
         {
         case '+':
             f3 = f1.add(f2);
             printStream.println("f1+f2=" + f3);
         break;

         case '-':
             f3 = f1.subtract(f2);
             printStream.println("f1-f2=" + f3);
             break;

         case '*':
             f3 = f1.multiply(f2);
             printStream.println("f1*f2="+f3);
             break;

         case '/':
             f3 = f1.divide(f2);
             printStream.println("f1/f2="+f3);
             break;

         default:
             printStream.println("Illegal command: "  + input );
             break;

         }
         return true;
     }

     public static void main(String[] args)
     {
        Scanner keyboard = new Scanner(System.in);

        while (do_one_operation(keyboard, System.out))
            ; // One of the few times this is what I want

        System.out.println("Exitting Fraction");

     } //end of main
 }




Last Updated: June 9, 2015 2:04 PM