Advertisement
Guest User

Kesr

a guest
Feb 17th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 KB | None | 0 0
  1. // This class contains atributes (suret and mexrec) of Kesr.
  2. // It will help to reference to suret and mexrec separately
  3.  
  4. public class Kesr {
  5.     int suret;
  6.     int mexrec;
  7.  
  8.     public Kesr(int suret, int mexrec) {
  9.         this.suret = suret;
  10.         this.mexrec = mexrec;
  11.     }
  12.  
  13.     // This factory method (checkForZero) is for making sure
  14.     // if the suret of Kesr is equal to zero not to create Kesr object
  15.     public static Kesr checkAndCreate(int suret, int mexrec) {
  16.         if (mexrec == 0)
  17.             return null;
  18.         else
  19.             return new Kesr(suret, mexrec);
  20.     }
  21.  
  22.     public void kesrLook() {
  23.  
  24.         System.out.println(this.suret + "/" + this.mexrec);
  25.     }
  26.  
  27.     public Kesr kesrBol(Kesr kesr2) {
  28.  
  29.         return new Kesr(this.suret * kesr2.suret, this.mexrec * kesr2.mexrec);
  30.     }
  31.  
  32.     public Kesr kesrVur(Kesr kesr2) {
  33.  
  34.         return new Kesr(this.suret * kesr2.mexrec, this.mexrec * kesr2.suret);
  35.     }
  36.  
  37.     public Kesr kesrCem(Kesr kesr2) {
  38.  
  39.         int ortaq = lcd(this.mexrec, kesr2.mexrec);
  40.  
  41.         return new Kesr(this.suret * (ortaq / this.mexrec) + kesr2.suret * (ortaq / kesr2.mexrec) , ortaq );
  42.     }
  43.  
  44.    
  45.     // gcd is Great Common divisor (en boyuk ortaq bolen)
  46.     public int gcd(int a, int b) {
  47.         if (b == 0) return a;
  48.         int r = a % b;
  49.         return gcd(b, r);
  50.     }
  51.    
  52.  
  53.     // lcd is Least Common Divisor (en kicik ortaq bolunen)
  54.     public int lcd(int a, int b) {
  55.         return a * b / gcd(a, b);
  56.     }
  57.  
  58.     public double valueOfKesr() {
  59.         return  this.suret / this.mexrec;
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement