package ru kpfu itis group11402 batyrova Task050 author Elvira Batyrov

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
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 = new RationalFraction(x * rat.getY() + rat.getX() * y, y * rat.getY());
rt.reduce();
return rt;
}
public void add2(RationalFraction rat) {
x = x * rat.getY() + rat.getX() * y;
y = y * rat.getY();
reduce();
}
public RationalFraction sub(RationalFraction rat) {
RationalFraction rt = new RationalFraction(x * rat.getY() - rat.getX() * y, y * rat.getY());
rt.reduce();
return rt;
}
public void sub2(RationalFraction rat) {
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;
}
}