letsdoitjava

Fraction

Jul 16th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.74 KB | None | 0 0
  1. // Fractions program that does the math for user with operations of + - * /
  2. // Renee Waggoner
  3. // July 17, 2019
  4. // Special Requirements: None
  5. package fractions;
  6. import java.util.Scanner;
  7.  
  8. public class Fraction {
  9.     private Scanner scan = new Scanner(System.in);
  10.     private int num=1;
  11.     private int denom=1;
  12.  
  13.     public Fraction()
  14.     {
  15.         num = 1;
  16.         denom = 1;
  17.     }
  18.  
  19.     public Fraction(int n, int d)
  20.     {
  21.         // Fill in code (good to use setFraction)
  22.         setFraction(n,d);
  23.     }
  24.  
  25.     public void setFraction(int n, int d)
  26.     {
  27.         //Fill in code ... don't forget to reduce
  28.         num = n;
  29.         denom = d;
  30.         reduce();
  31.     }
  32.  
  33.     public Fraction add(Fraction op)
  34.     {
  35.         //Fill in code ... don't forget to reduce
  36.         // Algebra HINT: a/b + c/d = (a*d + b*c)/(b*d)
  37.         int greatestDenom = op.denom * denom;
  38.         int multiplyx = greatestDenom / op.denom;
  39.         int multiply = greatestDenom / denom;
  40.         int d, n;
  41.         d = op.denom * denom;
  42.         n = (op.num * multiplyx) + (num * multiply);
  43.         // creating new fraction
  44.         Fraction rootDenom = new Fraction(n,d);
  45.         return rootDenom;
  46.     }
  47.  
  48.     public Fraction subtract(Fraction op)
  49.     {
  50.         //Fill in code ... don't forget to reduce
  51.         // Algebra HINT: a/b - c/d = (a*d - b*c)/(b*d)
  52.         int greatestDenom = op.denom * denom;
  53.         int multiplyx = greatestDenom / op.denom;
  54.         int multiply = greatestDenom / denom;
  55.         int d = 0;
  56.         int n;
  57.         d = op.denom * denom;
  58.  
  59.         if(op.num > num)
  60.         {
  61.             n = (op.num * multiplyx) - (num * multiply);
  62.         }
  63.         else
  64.         {
  65.             n = (num * multiply) - (op.num * multiplyx);
  66.         }
  67.         // creating new fraction
  68.         Fraction rootDenom = new Fraction(n,d);
  69.         return rootDenom;
  70.     }
  71.  
  72.     public Fraction multiply(Fraction op)
  73.     {
  74.         //Fill in code ... don't forget to reduce
  75.         // Algebra HINT: a/b * c/d = (a*c)/ (b*d)
  76.         int n,d;
  77.         n = num * op.num;
  78.         d = denom * op.denom;
  79.         Fraction rootDenom = new Fraction(n,d);
  80.         return rootDenom;
  81.  
  82.     }
  83.  
  84.     public Fraction divide(Fraction op)
  85.     {
  86.         //Fill in code ... don't forget to reduce
  87.         // Algebra HINT: a/b / c/d = (a*d)/ (b*c)
  88.         int n, d;
  89.         n = num * op.denom;
  90.         d = denom * op.num;
  91.         Fraction rootDenom = new Fraction(n,d);
  92.         return rootDenom;
  93.     }
  94.  
  95.     private void reduce()
  96.     {
  97.         // Pseudo code:
  98.         // set smaller = minimum ( abs(num), abs(denom));
  99.         int smaller = Math.abs(num) < Math.abs(denom) ? num : denom;
  100.  
  101.         // Loop through the possible divisors: 2, 3, 4, ... smaller
  102.         for(int i = 2; i <= smaller; i++)
  103.  
  104.             // For each possible divisor:
  105.         {
  106.             // while (num and denom are evenly divisible by divisor)
  107.             while(num % i == 0 && denom % i == 0)
  108.             {
  109.                 num /= i;
  110.                 denom /= i;
  111.                 smaller /= i;
  112.             }
  113.         }      
  114.     }
  115.     public boolean equals(Fraction f)
  116.     {
  117.         // Assuming all fractions are reduced. Fill in code
  118.         reduce();
  119.         f.reduce();
  120.         if (f.num == num && f.denom == denom) {
  121.             return true;
  122.         } else {
  123.             return false;
  124.         }
  125.     }
  126.  
  127.     public String toString()
  128.     {
  129.         return num + "/" + denom;
  130.     }
  131.  
  132.     public void readin(String label)
  133.     {
  134.         while (true) // Keep trying if bad input is received
  135.         {
  136.             System.out.print(label);
  137.             String temp = scan.next();
  138.             temp = temp.trim(); // get rid of white space at the beginning and end
  139.             int index = temp.indexOf('/');
  140.             if (index >= 0)
  141.             {
  142.                 String numStr = temp.substring(0, index);
  143.                 String denomStr = temp.substring(index+1);
  144.                 int n = Integer.parseInt(numStr);
  145.                 int d = Integer.parseInt(denomStr);
  146.                 setFraction(n,d);
  147.                 return;
  148.             }
  149.             else
  150.                 System.out.println("Input Fraction missing / ");
  151.         }//Keep trying until you get it right
  152.     }
  153.  
  154.     public static void main(String[] args)
  155.     {
  156.         Fraction f1 = new Fraction();
  157.         Fraction f2 = new Fraction();
  158.         Fraction f3 = null;
  159.         Scanner scan = new Scanner(System.in);
  160.  
  161.         while(true)
  162.         {
  163.             System.out.println(); // Add a blank line
  164.             System.out.print("Enter operation: + - * / q (q ==> quit) : ");
  165.             String input = scan.next();
  166.             if (input.charAt(0) == 'q')
  167.                 break; // All done
  168.  
  169.             f1.readin("Enter Fraction 1: ");
  170.             f2.readin("Enter Fraction 2: ");
  171.             System.out.println("f1 = " + f1);
  172.             System.out.println("f2 = " + f2);
  173.  
  174.             if (f1.equals(f2))
  175.                 System.out.println("f1 and f2 are equal");
  176.             else
  177.                 System.out.println("f1 and f2 are not equal");
  178.  
  179.             switch (input.charAt(0))
  180.             {
  181.             case '+':
  182.                 f3 = f1.add(f2);
  183.                 System.out.println("f1 + f2 = " + f3);
  184.                 break;
  185.             case '-':
  186.                 f3 = f1.subtract(f2);
  187.                 System.out.println("f1 - f2 = " + f3);
  188.                 break;
  189.             case '*':
  190.                 f3 = f1.multiply(f2);
  191.                 System.out.println("f1 * f2 = " + f3);
  192.                 break;
  193.             case '/':
  194.                 f3 = f1.divide(f2);
  195.                 System.out.println("f1 / f2 = " + f3);
  196.                 break;
  197.             default:
  198.                 System.out.println("Illegal command: " + input );
  199.                 break;
  200.             }// end of switch statement
  201.         }// end of while loop
  202.         System.out.println("Bye");
  203.         scan.close();
  204.     } // end of main
  205. }
Add Comment
Please, Sign In to add comment