Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This class contains atributes (suret and mexrec) of Kesr.
- // It will help to reference to suret and mexrec separately
- public class Kesr {
- int suret;
- int mexrec;
- public Kesr(int suret, int mexrec) {
- this.suret = suret;
- this.mexrec = mexrec;
- }
- // This factory method (checkForZero) is for making sure
- // if the suret of Kesr is equal to zero not to create Kesr object
- public static Kesr checkAndCreate(int suret, int mexrec) {
- if (mexrec == 0)
- return null;
- else
- return new Kesr(suret, mexrec);
- }
- public void kesrLook() {
- System.out.println(this.suret + "/" + this.mexrec);
- }
- public Kesr kesrBol(Kesr kesr2) {
- return new Kesr(this.suret * kesr2.suret, this.mexrec * kesr2.mexrec);
- }
- public Kesr kesrVur(Kesr kesr2) {
- return new Kesr(this.suret * kesr2.mexrec, this.mexrec * kesr2.suret);
- }
- public Kesr kesrCem(Kesr kesr2) {
- int ortaq = lcd(this.mexrec, kesr2.mexrec);
- return new Kesr(this.suret * (ortaq / this.mexrec) + kesr2.suret * (ortaq / kesr2.mexrec) , ortaq );
- }
- // gcd is Great Common divisor (en boyuk ortaq bolen)
- public int gcd(int a, int b) {
- if (b == 0) return a;
- int r = a % b;
- return gcd(b, r);
- }
- // lcd is Least Common Divisor (en kicik ortaq bolunen)
- public int lcd(int a, int b) {
- return a * b / gcd(a, b);
- }
- public double valueOfKesr() {
- return this.suret / this.mexrec;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement