Code refactor for ADTFraction improvements (#3016)

Fix #3015

Co-authored-by: Yang Libin <szuyanglb@outlook.com>
This commit is contained in:
Cristiano Jesus
2022-04-20 09:20:47 +01:00
committed by GitHub
parent 5eed0849ef
commit 4b15b2c021
2 changed files with 112 additions and 79 deletions

View File

@ -1,100 +1,81 @@
package com.thealgorithms.maths; package com.thealgorithms.maths;
public class ADTFraction { public record ADTFraction(int numerator, int denominator) {
public static void main(String[] args) { /**
// TODO code application logic here * Initializes a newly created {@code ADTFraction} object so that it represents
* a fraction with the {@code numerator} and {@code denominator} provided as arguments.
ADTFraction f1 = new ADTFraction(3, 5); *
f1.display(); * @param numerator The fraction numerator
ADTFraction f2 = new ADTFraction(7, 8); * @param denominator The fraction denominator
f2.display(); */
ADTFraction f3 = f1.plus(f2); public ADTFraction {
f3.display(); if (denominator == 0) {
ADTFraction f4 = f1.times(f2); throw new IllegalArgumentException("Denominator cannot be 0");
f4.display();
ADTFraction f5 = f1.times(4);
f5.display();
}
private int n; //numerator
private int d; //denomenator
public ADTFraction() {
this.n = 0;
this.d = 1;
}
public ADTFraction(int a, int b) {//parameter constructor
if (b != 0) {
this.n = a;
this.d = b;
} else {
this.n = 0;
this.d = 1;
System.out.println("denomerator cannot be 0,default values assinged");
} }
} }
public void set(int a, int b) {//set numerator and denomenator /**
* Add two fractions.
if (b != 0) { *
this.n = a; * @param fraction the {@code ADTFraction} to add
this.d = b; * @return A new {@code ADTFraction} containing the result of the operation
} else { */
this.n = 0; public ADTFraction plus(ADTFraction fraction) {
this.d = 1; var numerator = this.denominator * fraction.numerator + this.numerator * fraction.denominator;
System.out.println("denomerator cannot be 0,default values assinged"); var denominator = this.denominator * fraction.denominator;
} return new ADTFraction(numerator, denominator);
} }
//add two fractions /**
public ADTFraction plus(ADTFraction x) { * Multiply fraction by a number.
*
int num, den; * @param number the number to multiply
num = this.d * x.n + this.n * x.d; * @return A new {@code ADTFraction} containing the result of the operation
den = this.d * x.d; */
ADTFraction f = new ADTFraction(num, den); public ADTFraction times(int number) {
return f; return times(new ADTFraction(number, 1));
} }
public ADTFraction times(int a) {//multiply fraction by a number /**
* Multiply two fractions.
int num, den; *
num = this.n * a; * @param fraction the {@code ADTFraction} to multiply
den = this.d; * @return A new {@code ADTFraction} containing the result of the operation
ADTFraction f1 = new ADTFraction(num, den); */
return f1; public ADTFraction times(ADTFraction fraction) {
var numerator = this.numerator * fraction.numerator;
var denominator = this.denominator * fraction.denominator;
return new ADTFraction(numerator, denominator);
} }
public ADTFraction times(ADTFraction x) {//multiply two fractions /**
* Generates the reciprocal of the fraction.
int num, dem; *
num = this.n * x.n; * @return A new {@code ADTFraction} with the {@code numerator} and {@code denominator} switched
dem = this.d * x.d; */
ADTFraction f3 = new ADTFraction(num, dem);
return f3;
}
//reciprocal of a fraction
public ADTFraction reciprocal() { public ADTFraction reciprocal() {
ADTFraction f1 = new ADTFraction(this.d, this.n); return new ADTFraction(this.denominator, this.numerator);
return f1;
} }
//numerical value of a fraction /**
* Calculates the result of the fraction.
*
* @return The numerical result of the division between {@code numerator} and {@code denominator}
*/
public float value() { public float value() {
return (float) this.n / this.d; return (float) this.numerator / this.denominator;
} }
//display the fraction in the format n/d /**
public void display() { * Returns a string representation of this {@code ADTFraction} in the format
System.out.println(this.n + "/" + this.d); * {@code numerator}/{@code denominator}.
*
* @return A string representation of this {@code ADTFraction}
*/
@Override
public String toString() {
return String.format("%d/%d", this.numerator, this.denominator);
} }
} }

View File

@ -0,0 +1,52 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class ADTFractionTest {
private final ADTFraction fraction1 = new ADTFraction(3, 5);
private final ADTFraction fraction2 = new ADTFraction(7, 8);
@Test
void testConstructorWithDenominatorEqualToZero() {
Exception exception = assertThrows(IllegalArgumentException.class, () -> new ADTFraction(1, 0));
assertEquals("Denominator cannot be 0", exception.getMessage());
}
@Test
public void testPlus() {
assertEquals(new ADTFraction(59, 40), fraction1.plus(fraction2));
}
@Test
public void testTimes() {
assertEquals(new ADTFraction(12, 5), fraction1.times(4));
assertEquals(new ADTFraction(21, 40), fraction1.times(fraction2));
}
@Test
public void testReciprocal() {
assertEquals(new ADTFraction(5, 3), fraction1.reciprocal());
}
@Test
public void testValue() {
assertEquals(0.6F, fraction1.value());
}
@Test
public void testEqualsAndHashCode() {
ADTFraction fraction3 = new ADTFraction(3, 5);
assertTrue(fraction1.equals(fraction3) && fraction3.equals(fraction1));
assertEquals(fraction1.hashCode(), fraction3.hashCode());
}
@Test
public void testToString() {
assertEquals("3/5", fraction1.toString());
}
}