Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.35 KB | None | 0 0
  1. package fractions;
  2.  
  3. import java.util.Scanner;
  4.  
  5. /**
  6.  * Class for working with fractions
  7.  *
  8.  */
  9. public class Fraction {
  10.  
  11.     /**
  12.      * Read in input
  13.      */
  14.     private Scanner scan;
  15.  
  16.     /**
  17.      * Numerator
  18.      */
  19.     private int num;
  20.  
  21.     /**
  22.      * Denominator
  23.      */
  24.     private int denom;
  25.  
  26.     /**
  27.      * Constructor
  28.      */
  29.     public Fraction() {
  30.         scan = new Scanner(System.in);
  31.     }
  32.  
  33.     /**
  34.      * Sets values and reduces
  35.      * @param n
  36.      * @param d
  37.      */
  38.     public void setFraction(int n, int d) {
  39.         this.num = n;
  40.         this.denom = d;
  41.         reduce();
  42.     }
  43.  
  44.     /**
  45.      * Adds the fraction and returns
  46.      * @param op
  47.      * @return
  48.      */
  49.     public Fraction add(Fraction op) {
  50.         //null check
  51.         if(op == null) {
  52.             return this;
  53.         }
  54.  
  55.         //general case -- get a common denominator by getting product of denominators and then adding numerators
  56.         int previousDenom = this.denom;
  57.         this.denom *= op.denom;
  58.         this.num *=op.denom;
  59.         this.num += op.num * previousDenom;
  60.  
  61.         reduce();  
  62.  
  63.         return this;
  64.     }
  65.  
  66.     /**
  67.      * Subtracts fractions
  68.      * @param op
  69.      * @return
  70.      */
  71.     public Fraction subtract(Fraction op) {
  72.         //null check
  73.         if(op == null) {
  74.             return this;
  75.         }
  76.  
  77.         //subtracting is the same as adding the opposite:)
  78.         op.setFraction(op.num * -1, op.denom);
  79.         return add(op);
  80.     }
  81.  
  82.     /**
  83.      * Multiply and return
  84.      * @param op
  85.      * @return
  86.      */
  87.     public Fraction multiply(Fraction op) {
  88.         //null check
  89.         if(op == null) {
  90.             return this;
  91.         }
  92.  
  93.         //multiplying is easy -- multiply straight across and we are good:)
  94.         this.num *= op.num;
  95.         this.denom *= op.denom;
  96.  
  97.         reduce();
  98.  
  99.         return this;
  100.     }
  101.  
  102.     /**
  103.      * Divides fractions
  104.      * @param op
  105.      * @return
  106.      */
  107.     public Fraction divide(Fraction op) {
  108.         //null check
  109.         if(op == null) {
  110.             return this;
  111.         }
  112.  
  113.         //division is the same as multiplying by reciprocal
  114.         int oldNum = op.num;       
  115.         op.setFraction(op.denom, oldNum);
  116.         multiply(op);
  117.  
  118.         reduce();
  119.  
  120.         return this;
  121.     }
  122.  
  123.     /**
  124.      * Override equals method.  Is true if both num and denom are the same
  125.      * assumes both are already reduced.
  126.      */
  127.     @Override
  128.     public boolean equals(Object other) {
  129.         //null
  130.         if(other == null) {
  131.             return false;
  132.         }
  133.  
  134.         //instance of check
  135.         if(!(other instanceof Fraction)) {
  136.             return false;
  137.         }
  138.  
  139.         //all fields match
  140.         return ((Fraction)other).num == num && ((Fraction)other).denom == denom;
  141.     }
  142.  
  143.  
  144.     /**
  145.      * Override to string
  146.      */
  147.     @Override
  148.     public String toString() {
  149.         return num + "/" + denom;
  150.     }
  151.  
  152.     /**
  153.      * Read in fraction
  154.      * @param label
  155.      */
  156.     public void readin(String label) {
  157.         while (true) // Keep trying if bad input is received        
  158.         {          
  159.             System.out.print(label);  
  160.             String temp = scan.next();          
  161.             temp = temp.trim(); // get rid of white space at the beginning and end        
  162.             int index = temp.indexOf('/');          
  163.             if (index >= 0)             {            
  164.                 String numStr = temp.substring(0, index);    
  165.                 String denomStr = temp.substring(index+1);          
  166.                 int n = Integer.parseInt(numStr);          
  167.                 int d = Integer.parseInt(denomStr);        
  168.                 setFraction(n,d);              
  169.                 return;      
  170.             }    
  171.             else        
  172.                 System.out.println("Input Fraction missing / ");    
  173.         }//Keep trying until you get it right
  174.     }
  175.  
  176.  
  177.     /**
  178.      * Reduces this representation of a fraction
  179.      */
  180.     private void reduce() {
  181.         //find the gcd
  182.         int gcd = findGreatestCommonFactor(num, denom);
  183.  
  184.         //divide both numerator and denominator by gcd
  185.         num /=gcd;
  186.         denom /=gcd;
  187.     }
  188.  
  189.     /**
  190.      * Recursively find the greatest common denominator of two numbers.
  191.      * @param a
  192.      * @param b
  193.      * @return
  194.      */
  195.     private int findGreatestCommonFactor(int a, int b) {
  196.         return (b == 0)? a: findGreatestCommonFactor(b,  a % b);
  197.     }
  198.  
  199.     /**
  200.      * Entry point.
  201.      * @param args command line args
  202.      */
  203.     public static void main(String[] args) {
  204.         Fraction f1= new Fraction();    
  205.         Fraction f2= new Fraction();      
  206.         Fraction f3=null;      
  207.         Scanner scan = new Scanner(System.in);    
  208.         while(true)         {      
  209.             System.out.println();  // Add a blank line      
  210.             System.out.print("Enter operation: + - * / q (q ==> quit) : ");      
  211.             String input = scan.next();          
  212.             if (input.charAt(0) == 'q')                
  213.                 break; // All done                    
  214.             f1.readin("Enter Fraction 1: ");          
  215.             f2.readin("Enter Fraction 2: ");          
  216.             System.out.println("f1 = " + f1);    
  217.             System.out.println("f2 = " + f2);            
  218.             if (f1.equals(f2))              
  219.                 System.out.println("f1 and f2 are equal");  
  220.             else        
  221.                 System.out.println("f1 and f2 are not equal");  
  222.             switch (input.charAt(0))             {      
  223.             case '+':              
  224.                 f3 = f1.add(f2);          
  225.                 System.out.println("f1+f2=" + f3);        
  226.                 break;
  227.             case '-':          
  228.                 f3 = f1.subtract(f2);
  229.                 System.out.println("f1-f2=" + f3);        
  230.                 break;
  231.             case '*':        
  232.                 f3 = f1.multiply(f2);      
  233.                 System.out.println("f1*f2="+f3);            
  234.                 break;
  235.             case '/':      
  236.                 f3 = f1.divide(f2);    
  237.                 System.out.println("f1/f2="+f3);      
  238.                 break;
  239.             default:        
  240.                 System.out.println("Illegal command: "  + input );          
  241.                 break;
  242.  
  243.             }      
  244.         }// end of while loop  
  245.         System.out.println("Bye");    
  246.        
  247.         //don't forget to close our scan resource :)
  248.         if(scan != null) {
  249.             scan.close();
  250.         }
  251.     }
  252.  
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement