package ru.kpfu.itis.group11402.batyrova.Task050; /** * @author Elvira Batyrova * 402 * 050 */ public class RationalFraction { private int x; private int y; public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public RationalFraction() { this(0, 0); } public RationalFraction(int x, int y) { this.x = x; this.y = y; } public void reduce() { for (int i = x; i >= 1; i--) { if (x % i == 0 && y % i == 0) { x /= i; y /= i; } } } public RationalFraction add(RationalFraction rat) { RationalFraction rt; if (x * y == 0) { rt = new RationalFraction(rat.getX(), rat.getY()); } else { rt = new RationalFraction(x * rat.getY() + rat.getX() * y, y * rat.getY()); } rt.reduce(); return rt; } public void add2(RationalFraction rat) { if(x * y == 0) { x = rat.getX(); y = rat.getY(); } else { x = x * rat.getY() + rat.getX() * y; y = y * rat.getY(); reduce(); } } public RationalFraction sub(RationalFraction rat) { RationalFraction rt; if (x * y == 0) { rt = new RationalFraction(rat.getX(), rat.getY()); } else { rt = new RationalFraction(x * rat.getY() - rat.getX() * y, y * rat.getY()); } rt.reduce(); return rt; } public void sub2(RationalFraction rat) { if(x * y == 0) { x = rat.getX(); y = rat.getY(); } else { x = x * rat.getY() - rat.getX() * y; y = y * rat.getY(); reduce(); } } public RationalFraction mult(RationalFraction rat) { RationalFraction rt = new RationalFraction(x * rat.getX(), y * rat.getY()); rt.reduce(); return rt; } public void mult2(RationalFraction rat) { x = x * rat.getX(); y = y * rat.getY(); this.reduce(); } public RationalFraction div(RationalFraction rat) { RationalFraction rt = new RationalFraction(x * rat.getY(), y * rat.getX()); rt.reduce(); return rt; } public void div2(RationalFraction rat) { x = x * rat.getY(); y = y * rat.getX(); reduce(); } public String toString() { return (x + "/" + y); } public double value() { return 1.0 * x / y; } public boolean equals(RationalFraction rat) { rat.reduce(); this.reduce(); return (x == rat.getX() && y == rat.getY()); } public int numberPart() { return x / y; } }