Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions fraction.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import { roundTo } from "./utils.ts";
import { gcdBruteForce } from "./gcd.ts";

export class Fraction {
constructor(
private numerator: number,
private denominator: number,
) {}
) {
if (denominator === 0) {
throw new Error("denominator cannot be 0");
}

this.cancel();
}

public add(other: Fraction) {
const newNumerator =
this.numerator * other.denominator + other.numerator * this.denominator;
const newDenominator = this.denominator * other.denominator;
this.numerator = newNumerator;
this.denominator = newDenominator;

this.cancel();
}

public subtract(other: Fraction) {
Expand All @@ -20,20 +29,26 @@ export class Fraction {
const newDenominator = this.denominator * other.denominator;
this.numerator = newNumerator;
this.denominator = newDenominator;

this.cancel();
}

public multiply(other: Fraction) {
const newNumerator = this.numerator * other.numerator;
const newDenominator = this.denominator * other.denominator;
this.numerator = newNumerator;
this.denominator = newDenominator;

this.cancel();
}

public divide(other: Fraction) {
const newNumerator = this.numerator * other.denominator;
const newDenominator = this.denominator * other.numerator;
this.numerator = newNumerator;
this.denominator = newDenominator;

this.cancel();
}

public toFloat(precision: number): number {
Expand All @@ -44,16 +59,33 @@ export class Fraction {
return `${this.numerator}/${this.denominator}`;
}

public cancel(): Fraction {
const gcd = gcdBruteForce(this.numerator, this.denominator);

this.numerator /= gcd;
this.denominator /= gcd;

if (this.denominator < 0) {
this.numerator *= -1;
this.denominator *= -1;
}

return this;
}

public static parse(expression: string): Fraction {
const parts = expression.split("/");
if (parts.length != 2) {
throw new Error(`illegal syntax: "[numerator]/[denominator]" required`);
}
const numerator = Number.parseInt(parts[0].trim());
const denominator = Number.parseFloat(parts[1].trim());
const denominator = Number.parseInt(parts[1].trim());
if (Number.isNaN(numerator) || Number.isNaN(denominator)) {
throw new Error(`non-numeric numerator/denominator`);
}
if (denominator === 0) {
throw new Error("denominator cannot be 0");
}
return new Fraction(numerator, denominator);
}
}
131 changes: 130 additions & 1 deletion fraction_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertAlmostEquals, assertEquals } from "@std/assert";
import { assertAlmostEquals, assertEquals, assertThrows } from "@std/assert";
import { Fraction } from "./fraction.ts";

Deno.test("fraction of 1/1 is 1.0", () => {
Expand Down Expand Up @@ -34,3 +34,132 @@ Deno.test("1/3 + 2/6 = 2/3 is roughly 0.67", () => {
// Assert
assertAlmostEquals(left.toFloat(0.01), 0.67);
});

Deno.test("1/3 - 2/6 = 0", () => {
// Arrange
const left = new Fraction(1, 3);
const right = new Fraction(2, 6);

// Act
left.subtract(right);

// Assert
assertAlmostEquals(left.toFloat(0.01), 0);
});

Deno.test("1/3 * 2/6 = 1/9 roughly 0.11", () => {
// Arrange
const left = new Fraction(1, 3);
const right = new Fraction(2, 6);

// Act
left.multiply(right);

// Assert
assertAlmostEquals(left.toFloat(0.01), 0.11);
});

Deno.test("1/3 / 2/6 = 1", () => {
// Arrange
const left = new Fraction(1, 3);
const right = new Fraction(2, 6);

// Act
left.divide(right);

// Assert
assertAlmostEquals(left.toFloat(0.01), 1);
});

Deno.test("toString returns numerator/denominator", () => {
// Arrange
const fraction = new Fraction(3, 4);

//Act
const str = fraction.toString();

// Assert
assertEquals(str, "3/4");
});

Deno.test("parse creates a Fraction from a string", () => {
//Arrange
const fractionStr = "5/6"
//Act
const fraction = Fraction.parse(fractionStr);

//Assert
assertEquals(fraction.toString(), "5/6");
});

Deno.test("parse trims spaces correctly", () => {
//Arrange
const fractionStr = "7 / 8"

//Act
const fraction = Fraction.parse(fractionStr);

//Assert
assertEquals(fraction.toString(), "7/8");
});

Deno.test("parse throws error for invalid syntax", () => {
//Arrange
const parseNumber = "5";

//Act & Assert
assertThrows(() => { Fraction.parse(parseNumber); }, `illegal syntax: "[numerator]/[denominator]" required`);
});

Deno.test("parse throws error for non-numeric values", () => {
//Arrange
const parseNumber = "a/b";

//Act & Assert
assertThrows(() => { Fraction.parse(parseNumber); }, `non-numeric numerator/denominator`);
});

Deno.test("negative fraction", () => {
const fraction = new Fraction(-1, 2);

assertEquals(fraction.toString(), "-1/2");
});

Deno.test("2/3 * 3/2 = 1", () => {
const f1 = new Fraction(2, 3);
const f2 = new Fraction(3, 2);

f1.multiply(f2);

assertAlmostEquals(f1.toFloat(0.01), 1);
});

Deno.test("constructor throws error for denominator 0", () => {
assertThrows(() => { new Fraction(3, 0); }, "denominator cannot be 0");
});

Deno.test("parse throws error for denominator 0", () => {
assertThrows(() => { Fraction.parse("3/0"); }, "denominator cannot be 0");
});

Deno.test("Kürzen von 10/10", () => {
const f = new Fraction(10, 10);

f.cancel();

assertEquals(f.toString(), "1/1");
});

Deno.test("Kürzen von 6/8", () => {
const f = new Fraction(6, 8);
f.cancel();

assertEquals(f.toString(), "3/4");
});

Deno.test("Negativer Nenner", () => {
const f = new Fraction(6, -8);
f.cancel();

assertEquals(f.toString(), "-3/4");
});
14 changes: 14 additions & 0 deletions gcd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function gcdBruteForce(a: number, b: number): number {
a = Math.abs(a);
b = Math.abs(b);

const min = Math.min(a, b);

for (let i = min; i >= 1; i--) {
if (a % i === 0 && b % i === 0) {
return i;
}
}

return 1;
}
12 changes: 12 additions & 0 deletions gcd_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { assertEquals } from "@std/assert";
import { gcdBruteForce } from "./gcd.ts";

Deno.test("ggT von 10 und 10", () => {
const result = gcdBruteForce(10, 10);
assertEquals(result, 10);
});

Deno.test("ggT von 6 und 9", () => {
const result = gcdBruteForce(6, 9);
assertEquals(result, 3);
});
70 changes: 69 additions & 1 deletion geometry_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertAlmostEquals } from "@std/assert";
import { Circle, Point2D } from "./geometry.ts";
import { Circle, Point2D, Rectangle } from "./geometry.ts";

Deno.test("circumference of a circle with radius 5 is roughtly 31.416", () => {
// Given
Expand All @@ -11,3 +11,71 @@ Deno.test("circumference of a circle with radius 5 is roughtly 31.416", () => {
// Then
assertAlmostEquals(actual, 31.416, 0.01);
});

Deno.test("distance between (0,0) and (3,4) is 5", () => {
const p1 = new Point2D(0, 0);
const p2 = new Point2D(3, 4);

const actual = p1.distanceTo(p2);

assertAlmostEquals(actual, 5);
});

Deno.test("area of circle radius 5 is roughly 78.54", () => {
const circle = new Circle(new Point2D(0, 0), 5);

const actual = circle.area()

assertAlmostEquals(actual, 78.54, 0.01);
});

Deno.test("diameter of circle radius 5 is 10", () => {
const circle = new Circle(new Point2D(0, 0), 5);

const diameter = circle.diameter();

assertAlmostEquals(diameter, 10);
});

Deno.test("rectangle circumference is correct", () => {
const rect = new Rectangle(new Point2D(0, 0), new Point2D(4, 3));

const circumference = rect.circumference();

assertAlmostEquals(circumference, 20);
});

Deno.test("rectangle area is correct", () => {
const rect = new Rectangle(new Point2D(0, 0), new Point2D(4, 3));

const area = rect.area();

assertAlmostEquals(area, 12);
});

Deno.test("rectangle diagonal is correct (3-4-5 triangle)", () => {
const rect = new Rectangle(
new Point2D(0, 0),
new Point2D(3, 4),
);

const diagonal = rect.diagonal();

assertAlmostEquals(diagonal, 5);
});

Deno.test("area of circle radius 5", () => {
const circle = new Circle(new Point2D(0, 0), 5);

const area = circle.area();

assertAlmostEquals(area, 78.54, 0.01);
});

Deno.test("diameter of circle radius 5", () => {
const circle = new Circle(new Point2D(0, 0), 5);

const diameter = circle.diameter();

assertAlmostEquals(diameter, 10);
});