diff --git a/Sd1/P/Fraction1/.classpath b/Sd1/P/Fraction1/.classpath new file mode 100644 index 0000000000000000000000000000000000000000..fb5011632c0ab8d6649a148c6fb5845a1b34c747 --- /dev/null +++ b/Sd1/P/Fraction1/.classpath @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" path="src"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/Sd1/P/Fraction1/.gitignore b/Sd1/P/Fraction1/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..5e56e040ec0902e58df8573adaec65c5da6e9304 --- /dev/null +++ b/Sd1/P/Fraction1/.gitignore @@ -0,0 +1 @@ +/bin diff --git a/Sd1/P/Fraction1/.project b/Sd1/P/Fraction1/.project new file mode 100644 index 0000000000000000000000000000000000000000..31ca8b37b1ff372a24e9dcf84149f3afb624cbfb --- /dev/null +++ b/Sd1/P/Fraction1/.project @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>Fraction1</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> diff --git a/Sd1/P/Fraction1/src/math/Driver.java b/Sd1/P/Fraction1/src/math/Driver.java new file mode 100644 index 0000000000000000000000000000000000000000..4ce1f897a0b4e4994eb627ce4134c5aeb5602420 --- /dev/null +++ b/Sd1/P/Fraction1/src/math/Driver.java @@ -0,0 +1,15 @@ +package math; + +public class Driver { + + public static void main(String[] args) { + Fraction twoThird = new Fraction(2, 3), // Construct a fraction object (2/3) + threeSeven = new Fraction(3, 7); // Construct a fraction object (3/7) + + Fraction sum = twoThird.add(threeSeven), // (2/3) + (3/7) + product = twoThird.mult(threeSeven); // (2/3) * (3/7) + + System.out.println("(2/3) + (3/7) = (23/21) = " + sum.getValue()); + System.out.println("(2/3) * (3/7) = (2/7) = " + product.getValue()); + } +} diff --git a/Sd1/P/Fraction1/src/math/Fraction.java b/Sd1/P/Fraction1/src/math/Fraction.java new file mode 100644 index 0000000000000000000000000000000000000000..d06638b90bdee2d12526b7f111326f34cacff5bb --- /dev/null +++ b/Sd1/P/Fraction1/src/math/Fraction.java @@ -0,0 +1,134 @@ +package math; + +public class Fraction { + + long numerator,denominator; + + /** + * @param numerator The fraction's numerator. + * @param denominator The fraction's denominator. + */ + public Fraction(long numerator, long denominator) { + setNumerator(numerator); + setDenominator(denominator); + } + /** + * @return The fraction's decimal value e.g. 1/3 = 0.3333... + */ + public double getValue(){ + return (double) numerator / denominator; + } + /** + * @return The fraction's numerator + */ + public long getNumerator() { + return numerator; + } + /** + * @param numerator Set the fraction's numerator. + */ + public void setNumerator(long numerator) { + this.numerator = numerator; + } + /** + * @return + * The fraction's denominator value + */ + public long getDenominator() { + return denominator; + } + /** + * + * @param denominator Set the fraction's denominator + */ + public void setDenominator(long denominator) { + this.denominator = denominator; + } + + /** + * Add a second fraction to the current instance. + * + * + * @return The sum of the current instance and f according to: + * +<math display="block" xmlns="http://www.w3.org/1998/Math/MathML"> + <mrow> + <mrow> + <mfrac> + <mi>a</mi> + <mi>b</mi> + </mfrac> + <mo>+</mo> + <mfrac> + <mi>x</mi> + <mi>y</mi> + </mfrac> + </mrow> + <mo>=</mo> + <mfrac> + <mrow> + <mrow> + <mi>a</mi> + <mo></mo> + <mi>y</mi> + </mrow> + <mo>+</mo> + <mrow> + <mi>b</mi> + <mo></mo> + <mi>x</mi> + </mrow> + </mrow> + <mrow> + <mi>b</mi> + <mo></mo> + <mi>y</mi> + </mrow> + </mfrac> + </mrow> +</math> + */ + public Fraction add(Fraction f) { + return new Fraction( numerator * f.denominator + denominator * f.numerator, + denominator * f.denominator); + } + /** + * Multiply a second fraction by this instance + * @param f Building the product with this second fraction. + * @return The result of multiplying the current instance by f according to: +<math display="block" xmlns="http://www.w3.org/1998/Math/MathML"> + <mrow> + <mrow> + <mrow> + <mfrac> + <mi>a</mi> + <mi>b</mi> + </mfrac> + <mo></mo> + <mfrac> + <mi>x</mi> + <mi>y</mi> + </mfrac> + </mrow> + <mo>=</mo> + <mfrac> + <mrow> + <mi>a</mi> + <mo></mo> + <mi>x</mi> + </mrow> + <mrow> + <mi>b</mi> + <mo></mo> + <mi>y</mi> + </mrow> + </mfrac> + </mrow> + </mrow> +</math> + */ + public Fraction mult(Fraction f) { + return new Fraction(numerator * f.numerator, + denominator * f.denominator); + } +}