Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. // Info
  2. // Info
  3.  
  4. public class Main {
  5.  
  6.     public static void main(String[] args) {
  7.  
  8.         Rational a = new Rational(8,-7);
  9.         Rational b = new Rational(4,6);
  10.  
  11.         System.out.println(a + " + " + b + " = " + a.addition(b));
  12.         System.out.println(a + " - " + b + " = " + a.subtraction(b));
  13.         System.out.println(a + " * " + b + " = " + a.multiplication(b));
  14.         System.out.println(a + " / " + b + " = " + a.division(b));
  15.     }
  16. }
  17.  
  18. class Rational {
  19.    
  20.     private int num;
  21.     private int denu;
  22.  
  23.     public Rational(int x, int y) {
  24.  
  25.         // Now there will never be a fraction with denominator 0
  26.         if(y == 0){
  27.             y = 1;
  28.         }
  29.        
  30.         // if it is 3/-4; make it -3/4
  31.         if(y < 0){
  32.             y *= -1; // same thing as y = y * -1;
  33.             x = x * -1;
  34.         }
  35.        
  36.        
  37.         num = x;
  38.         denu = y;
  39.        
  40.         // Now reduce fractions so that 4/8 is stored as 1/2 instead
  41.         reduce();
  42.     }
  43.    
  44.     public int getNumerator(){
  45.         return num;
  46.     }
  47.    
  48.     public int getDenominator(){
  49.         return denu;
  50.     }
  51.    
  52.     public void setNumerator(int n){
  53.         num = n;
  54.     }
  55.    
  56.     public void setDenominator(int d){
  57.         denu = d;
  58.     }
  59.    
  60.     public Rational addition(Rational f) {
  61.  
  62.         Rational result;
  63.         result = new Rational (num*f.denu + f.num*denu, denu*f.denu );
  64.         return result;
  65.     }
  66.  
  67.     public Rational division(Rational f) {
  68.  
  69.  
  70.         Rational result;
  71.         result = new Rational (num*f.denu, denu*f.num);
  72.         return result;
  73.     }
  74.  
  75.     public Rational multiplication(Rational f) {
  76.  
  77.         Rational result;
  78.         result = new Rational (num*f.num,denu*f.denu);
  79.         return result;
  80.     }
  81.  
  82.     public Rational subtraction(Rational f) {
  83.  
  84.         Rational result;
  85.         result = new Rational (num*f.denu - f.num*denu, denu*f.denu );
  86.         return result;
  87.     }
  88.  
  89.     public String toString() {
  90.  
  91.         return "(" + num + "/" + denu + ")";
  92.     }
  93.    
  94.     private void reduce(){
  95.         int x = num;
  96.         if(x < 0){
  97.             x *= -1;
  98.         }
  99.         int y = denu;
  100.         int gcd = gcd(x, y);
  101.         num /= gcd;
  102.         denu /= gcd;
  103.     }
  104.    
  105.     private int gcd(int x, int y){
  106.         for(; x != y;){
  107.             if(x > y){
  108.                 x -= y;
  109.             }
  110.             else{
  111.                 y -= x;
  112.             }
  113.         }
  114.         return x;
  115.     }
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement