Advertisement
mmayoub

Rational.java

Jan 28th, 2023
757
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.80 KB | None | 0 0
  1.  
  2. public class Rational {
  3.     private int x, y; // x/y
  4.  
  5.     public Rational(int x, int y) {
  6.         this.x = x;
  7.         this.y = y;
  8.     }
  9.  
  10.     public int getNumerator() {
  11.         return x;
  12.     }
  13.    
  14.     public int getDenom() {
  15.         return y;
  16.     }
  17.    
  18.     public boolean isEqual(Rational num) {
  19.         // return this.x * num.y == this.y*num.x;
  20.         if (this.x * num.y == this.y*num.x)
  21.             return true;
  22.         else {
  23.                 return false;
  24.         }
  25.     }
  26.    
  27.     public Rational multiply(Rational num) {
  28.         int mulX = this.x *num.x;
  29.         int mulY = this.y *num.y;
  30.        
  31.         Rational res = new Rational(mulX, mulY);
  32.        
  33.         return res;
  34.     }
  35.    
  36.     public Rational divide (Rational num) {
  37.         Rational makloob = new Rational(num.y, num.x);
  38.         return this.multiply(makloob);
  39.     }
  40.    
  41.     public String toString() {
  42.         return this.x + "/" + this.y;
  43.     }
  44.    
  45. }
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement