Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fractions;
- import java.util.*;
- public class Fraction
- {
- private Scanner scan = new Scanner(System.in);
- private int num=1;
- private int denom=1;
- public Fraction()
- {
- }
- public Fraction(int n, int d)
- {
- this.num = n;
- this.denom = d;
- }
- public void setFraction(int n, int d)
- {
- this.num = n;
- this.denom = d;
- }
- public Fraction add(Fraction op)
- {
- // Algebra HINT: a/b + c/d = (a*d + b*c)/(b*d)
- op.num = (this.num * op.denom + this.denom * op.num);
- op.denom = (this.denom * op.denom);
- op.reduce();
- return op;
- }
- public Fraction subtract(Fraction op)
- {
- // Algebra HINT: a/b - c/d = (a*d - b*c)/(b*d)
- op.num = (this.num * op.denom - this.denom * op.num);
- op.denom = (this.denom * op.denom);
- op.reduce();
- return op;
- }
- public Fraction multiply(Fraction op)
- {
- // Algebra HINT: a/b * c/d = (a*c)/ (b*d)
- op.num = (this.num * op.num);
- op.denom = (this.denom * op.denom);
- op.reduce();
- return op;
- }
- public Fraction divide(Fraction op)
- {
- // Algebra HINT: a/b / c/d = (a*d)/ (b*c)
- this.num = (this.num * op.denom);
- this.denom = (this.denom * op.num);
- this.reduce();
- return this;
- }
- private void reduce()
- {
- int smaller;
- if(this.num > this.denom)
- {
- smaller = denom;
- }
- else
- {
- smaller = num;
- }
- for(int divisor = 2; divisor < smaller; divisor++)
- {
- while ((num%divisor == 0)&&(denom%divisor == 0))
- {
- num /= divisor;
- denom /= divisor;
- }
- }
- }
- public boolean equals(Fraction f)
- {
- f.reduce();
- this.reduce();
- if((this.num == f.num)&&(this.denom == f.denom))
- {
- return true;
- }
- else return false;
- }
- public String toString()
- {
- this.reduce();
- return (this.num + "/" + this.denom);
- }
- public void readin(String label)
- {
- while (true) // Keep trying if bad input is received
- {
- System.out.print(label);
- String temp = scan.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
- System.out.println("Input Fraction missing / ");
- }//Keep trying until you get it right
- }
- public static void main(String[] args)
- {
- Fraction f1= new Fraction();
- Fraction f2= new Fraction();
- Fraction f3= null;
- Scanner scan = new Scanner(System.in);
- while(true)
- {
- System.out.println(); // Add a blank line
- System.out.print("Enter operation: + - * / q (q ==> quit) : ");
- String input = scan.next();
- if (input.charAt(0) == 'q')
- break; // All done
- f1.readin("Enter Fraction 1: ");
- f2.readin("Enter Fraction 2: ");
- System.out.println("f1 = " + f1);
- System.out.println("f2 = " + f2);
- if (f1.equals(f2))
- System.out.println("f1 and f2 are equal");
- else
- System.out.println("f1 and f2 are not equal");
- switch (input.charAt(0))
- {
- case '+':
- f3 = f1.add(f2);
- System.out.println("f1+f2=" + f3);
- break;
- case '-':
- f3 = f1.subtract(f2);
- System.out.println("f1-f2=" + f3);
- break;
- case '*':
- f3 = f1.multiply(f2);
- System.out.println("f1*f2="+f3);
- break;
- case '/':
- f3 = f1.divide(f2);
- System.out.println("f1/f2="+f3);
- break;
- default:
- System.out.println("Illegal command: " + input );
- break;
- }
- }// end of while loop
- System.out.println("Bye");
- scan.close();
- } // end of main
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement