all tests pass

master
Arti Zirk 6 years ago
parent 17d4bef66e
commit 1a11524519
  1. 72
      src/Lfraction.java

@ -63,8 +63,6 @@ public class Lfraction implements Comparable<Lfraction> {
} else {
dif = this.de / other.de;
}
System.out.println("dif = " + dif);
return (this.nu*dif) == other.nu;
}
@ -75,7 +73,37 @@ public class Lfraction implements Comparable<Lfraction> {
*/
@Override
public int hashCode() {
return 0; // TODO!!!
return Objects.hash(this.nu, this.de);
}
/** Reduce the fraction
* @return reduced Lfraction
*/
public Lfraction reduce() {
long common;
if (this.nu == this.de) {
common = this.nu;
} else {
long de1;
long de2;
if (this.nu > this.de) {
de1 = this.nu;
de2 = this.de;
} else {
de1 = this.de;
de2 = this.nu;
}
long f;
while (de2 != 0) {
f = de2;
de2 = de1 % de2;
de1 = f;
}
common = de1;
}
return new Lfraction(this.nu / common, this.de / common);
}
/** Sum of fractions.
@ -83,7 +111,11 @@ public class Lfraction implements Comparable<Lfraction> {
* @return this+m
*/
public Lfraction plus (Lfraction m) {
return null; // TODO!!!
if (this.de == m.de) {
return new Lfraction(this.nu + m.nu, this.de);
} else {
return new Lfraction((m.nu*this.de)+(this.nu*m.de), (m.de*this.de));
}
}
/** Multiplication of fractions.
@ -91,7 +123,7 @@ public class Lfraction implements Comparable<Lfraction> {
* @return this*m
*/
public Lfraction times (Lfraction m) {
return null; // TODO!!!
return new Lfraction(this.nu * m.nu, this.de * m.de).reduce();
}
/** Inverse of the fraction. n/d becomes d/n.
@ -110,7 +142,7 @@ public class Lfraction implements Comparable<Lfraction> {
* @return opposite of this fraction: -this
*/
public Lfraction opposite() {
return null; // TODO!!!
return new Lfraction(-this.nu, this.de);
}
/** Difference of fractions.
@ -118,7 +150,11 @@ public class Lfraction implements Comparable<Lfraction> {
* @return this-m
*/
public Lfraction minus (Lfraction m) {
return null; // TODO!!!
if (this.de == m.de) {
return new Lfraction(this.nu - m.nu, this.de);
} else {
return new Lfraction((this.nu*m.de)-(m.nu*this.de), (m.de*this.de)).reduce();
}
}
/** Quotient of fractions.
@ -126,7 +162,9 @@ public class Lfraction implements Comparable<Lfraction> {
* @return this/m
*/
public Lfraction divideBy (Lfraction m) {
return null; // TODO!!!
long nu = Math.abs(this.nu * m.de);
long de = Math.abs(this.de * m.nu);
return new Lfraction(nu, de).reduce();
}
/** Comparision of fractions.
@ -135,7 +173,13 @@ public class Lfraction implements Comparable<Lfraction> {
*/
@Override
public int compareTo (Lfraction m) {
return 0; // TODO!!!
if (this.equals(m)) {
return 0;
} else if (this.nu * m.de < m.nu * this.de) {
return -1;
} else {
return 1;
}
}
/** Clone of the fraction.
@ -158,14 +202,18 @@ public class Lfraction implements Comparable<Lfraction> {
* @return fraction part of this fraction
*/
public Lfraction fractionPart() {
return null; // TODO!!!
if (this.nu >= this.de || -this.nu >= this.de) {
return new Lfraction(this.nu % this.de, this.de);
} else {
return new Lfraction(this.nu, this.de);
}
}
/** Approximate value of the fraction.
* @return numeric value of this fraction
*/
public double toDouble() {
return new Double(this.nu)/this.de;
return (double)this.nu/this.de;
}
/** Double value f presented as a fraction with denominator d > 0.
@ -174,7 +222,7 @@ public class Lfraction implements Comparable<Lfraction> {
* @return f as an approximate fraction of form n/d
*/
public static Lfraction toLfraction (double f, long d) {
return null; // TODO!!!
return new Lfraction(Math.round(f * d), d);
}
/** Conversion from string to the fraction. Accepts strings of form

Loading…
Cancel
Save