10 of 20 tests pass
This commit is contained in:
parent
b9ed53ac27
commit
17d4bef66e
@ -10,28 +10,33 @@ public class Lfraction implements Comparable<Lfraction> {
|
|||||||
// TODO!!! Your debugging tests here
|
// TODO!!! Your debugging tests here
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO!!! instance variables here
|
private long nu;
|
||||||
|
private long de;
|
||||||
|
|
||||||
/** Constructor.
|
/** Constructor.
|
||||||
* @param a numerator
|
* @param a numerator
|
||||||
* @param b denominator > 0
|
* @param b denominator > 0
|
||||||
*/
|
*/
|
||||||
public Lfraction (long a, long b) {
|
public Lfraction (long a, long b) {
|
||||||
// TODO!!!
|
if (b < 1) {
|
||||||
|
throw new RuntimeException("Denominator can't be 0 or less");
|
||||||
|
}
|
||||||
|
this.nu = a;
|
||||||
|
this.de = b;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Public method to access the numerator field.
|
/** Public method to access the numerator field.
|
||||||
* @return numerator
|
* @return numerator
|
||||||
*/
|
*/
|
||||||
public long getNumerator() {
|
public long getNumerator() {
|
||||||
return 0L; // TODO!!!
|
return this.nu;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Public method to access the denominator field.
|
/** Public method to access the denominator field.
|
||||||
* @return denominator
|
* @return denominator
|
||||||
*/
|
*/
|
||||||
public long getDenominator() {
|
public long getDenominator() {
|
||||||
return 1L; // TODO!!!
|
return this.de;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Conversion to string.
|
/** Conversion to string.
|
||||||
@ -39,7 +44,7 @@ public class Lfraction implements Comparable<Lfraction> {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return null; // TODO!!!
|
return ""+Long.toString(this.nu)+"/"+Long.toString(this.de);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Equality test.
|
/** Equality test.
|
||||||
@ -48,7 +53,21 @@ public class Lfraction implements Comparable<Lfraction> {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean equals (Object m) {
|
public boolean equals (Object m) {
|
||||||
return false; // TODO!!!
|
Lfraction other = (Lfraction) m;
|
||||||
|
if (other.de == this.de) {
|
||||||
|
return this.nu == other.nu;
|
||||||
|
} else {
|
||||||
|
double dif;
|
||||||
|
if (other.de > this.de) {
|
||||||
|
dif = other.de / this.de;
|
||||||
|
} else {
|
||||||
|
dif = this.de / other.de;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("dif = " + dif);
|
||||||
|
return (this.nu*dif) == other.nu;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Hashcode has to be equal for equal fractions.
|
/** Hashcode has to be equal for equal fractions.
|
||||||
@ -79,7 +98,12 @@ public class Lfraction implements Comparable<Lfraction> {
|
|||||||
* @return inverse of this fraction: 1/this
|
* @return inverse of this fraction: 1/this
|
||||||
*/
|
*/
|
||||||
public Lfraction inverse() {
|
public Lfraction inverse() {
|
||||||
return null; // TODO!!!
|
if (this.nu < 1) {
|
||||||
|
return new Lfraction(-this.de, -this.nu);
|
||||||
|
} else {
|
||||||
|
return new Lfraction(this.de, this.nu);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Opposite of the fraction. n/d becomes -n/d.
|
/** Opposite of the fraction. n/d becomes -n/d.
|
||||||
@ -119,14 +143,14 @@ public class Lfraction implements Comparable<Lfraction> {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Object clone() throws CloneNotSupportedException {
|
public Object clone() throws CloneNotSupportedException {
|
||||||
return null; // TODO!!!
|
return new Lfraction(this.nu, this.de);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Integer part of the (improper) fraction.
|
/** Integer part of the (improper) fraction.
|
||||||
* @return integer part of this fraction
|
* @return integer part of this fraction
|
||||||
*/
|
*/
|
||||||
public long integerPart() {
|
public long integerPart() {
|
||||||
return 0L; // TODO!!!
|
return this.nu/this.de;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Extract fraction part of the (improper) fraction
|
/** Extract fraction part of the (improper) fraction
|
||||||
@ -141,7 +165,7 @@ public class Lfraction implements Comparable<Lfraction> {
|
|||||||
* @return numeric value of this fraction
|
* @return numeric value of this fraction
|
||||||
*/
|
*/
|
||||||
public double toDouble() {
|
public double toDouble() {
|
||||||
return 0.; // TODO!!!
|
return new Double(this.nu)/this.de;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Double value f presented as a fraction with denominator d > 0.
|
/** Double value f presented as a fraction with denominator d > 0.
|
||||||
@ -159,7 +183,7 @@ public class Lfraction implements Comparable<Lfraction> {
|
|||||||
* @return fraction represented by s
|
* @return fraction represented by s
|
||||||
*/
|
*/
|
||||||
public static Lfraction valueOf (String s) {
|
public static Lfraction valueOf (String s) {
|
||||||
return null; // TODO!!!
|
String[] parts = s.split("/");
|
||||||
|
return new Lfraction(Long.parseLong(parts[0]), Long.parseLong(parts[1]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user