Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fractions;
- import java.util.Scanner;
- /**
- * Class for working with fractions
- *
- */
- public class Fraction {
- /**
- * Read in input
- */
- private Scanner scan;
- /**
- * Numerator
- */
- private int num;
- /**
- * Denominator
- */
- private int denom;
- /**
- * Constructor
- */
- public Fraction() {
- scan = new Scanner(System.in);
- }
- /**
- * Sets values and reduces
- * @param n
- * @param d
- */
- public void setFraction(int n, int d) {
- this.num = n;
- this.denom = d;
- reduce();
- }
- /**
- * Adds the fraction and returns
- * @param op
- * @return
- */
- public Fraction add(Fraction op) {
- //null check
- if(op == null) {
- return this;
- }
- //general case -- get a common denominator by getting product of denominators and then adding numerators
- int previousDenom = this.denom;
- this.denom *= op.denom;
- this.num *=op.denom;
- this.num += op.num * previousDenom;
- reduce();
- return this;
- }
- /**
- * Subtracts fractions
- * @param op
- * @return
- */
- public Fraction subtract(Fraction op) {
- //null check
- if(op == null) {
- return this;
- }
- //subtracting is the same as adding the opposite:)
- op.setFraction(op.num * -1, op.denom);
- return add(op);
- }
- /**
- * Multiply and return
- * @param op
- * @return
- */
- public Fraction multiply(Fraction op) {
- //null check
- if(op == null) {
- return this;
- }
- //multiplying is easy -- multiply straight across and we are good:)
- this.num *= op.num;
- this.denom *= op.denom;
- reduce();
- return this;
- }
- /**
- * Divides fractions
- * @param op
- * @return
- */
- public Fraction divide(Fraction op) {
- //null check
- if(op == null) {
- return this;
- }
- //division is the same as multiplying by reciprocal
- int oldNum = op.num;
- op.setFraction(op.denom, oldNum);
- multiply(op);
- reduce();
- return this;
- }
- /**
- * Override equals method. Is true if both num and denom are the same
- * assumes both are already reduced.
- */
- @Override
- public boolean equals(Object other) {
- //null
- if(other == null) {
- return false;
- }
- //instance of check
- if(!(other instanceof Fraction)) {
- return false;
- }
- //all fields match
- return ((Fraction)other).num == num && ((Fraction)other).denom == denom;
- }
- /**
- * Override to string
- */
- @Override
- public String toString() {
- return num + "/" + denom;
- }
- /**
- * Read in fraction
- * @param label
- */
- 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
- }
- /**
- * Reduces this representation of a fraction
- */
- private void reduce() {
- //find the gcd
- int gcd = findGreatestCommonFactor(num, denom);
- //divide both numerator and denominator by gcd
- num /=gcd;
- denom /=gcd;
- }
- /**
- * Recursively find the greatest common denominator of two numbers.
- * @param a
- * @param b
- * @return
- */
- private int findGreatestCommonFactor(int a, int b) {
- return (b == 0)? a: findGreatestCommonFactor(b, a % b);
- }
- /**
- * Entry point.
- * @param args command line args
- */
- 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");
- //don't forget to close our scan resource :)
- if(scan != null) {
- scan.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement