{"id":1339,"date":"2016-09-02T11:40:52","date_gmt":"2016-09-02T08:40:52","guid":{"rendered":"http:\/\/java.mazurok.com\/?p=1339"},"modified":"2016-11-14T09:26:40","modified_gmt":"2016-11-14T06:26:40","slug":"fractions","status":"publish","type":"post","link":"https:\/\/java.mazurok.com\/?p=1339","title":{"rendered":"\u041a\u043b\u0430\u0441\u0441 \u0440\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0445 \u0434\u0440\u043e\u0431\u0435\u0439"},"content":{"rendered":"<h4>\u0417\u0430\u0434\u0430\u0447\u0430<\/h4>\n<p>\u041d\u0430\u043f\u0438\u0448\u0438\u0442\u0435 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u043c\u0438 (immutable) \u0440\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0434\u0440\u043e\u0431\u044f\u043c\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b.<\/p>\n<h4>\u041a\u043e\u0434<\/h4>\n<pre class=\"lang:java decode:true\">import java.lang.*;\r\n\r\nclass Rational {\r\n    private int n, d;\r\n\r\n    \/**\r\n     * @param n is a numerator of Fraction\r\n     * @param d is a divider of Fraction\r\n     *\/\r\n    public Rational(int n, int d) throws ArithmeticException {\r\n        if (d == 0) throw new ArithmeticException(\"wrong \");\r\n        int divisor = gcd(n, d);\r\n        this.n = n \/ divisor;\r\n        this.d = d \/ divisor;\r\n    }\r\n\r\n    \/**\r\n     * @param n is a numerator of Fraction\r\n     * @param d is a divider of Fraction\r\n     * @return the greatest common divisor of numerator and divider\r\n     *\/\r\n    private static int gcd(int n, int d) {\r\n        return (d == 0 ? n : gcd(d, n % d));\r\n    }\r\n\r\n    \/**\r\n     * Check fractions for equality\r\n     *\r\n     * @param x the first fraction\r\n     * @param y the seconds fraction\r\n     * @return true if they are equals, otherwise returns false\r\n     *\/\r\n    public static boolean equals(Rational x, Rational y) {\r\n        if (x.n != y.n || x.d != y.d) return false;\r\n        else return true;\r\n    }\r\n\r\n    \/**\r\n     * Add two Fractions x and y\r\n     *\r\n     * @param x the first Fraction\r\n     * @param y the second Fraction\r\n     * @return the result of there addition\r\n     *\/\r\n    public static Rational add(Rational x, Rational y) {\r\n        return new Rational(x.n * y.d + y.n * x.d, x.d * y.d);\r\n    }\r\n\r\n    \/**\r\n     * Subtract two Franctions x and y\r\n     *\r\n     * @param x the first Fraction\r\n     * @param y the second Fraction\r\n     * @return the result of there subtraction\r\n     *\/\r\n    public static Rational sub(Rational x, Rational y) {\r\n        return new Rational(x.n * y.d - y.n * x.d, x.d * y.d);\r\n    }\r\n\r\n    \/**\r\n     * Multiply two Fractions x and y\r\n     *\r\n     * @param x the first Fraction\r\n     * @param y the seconds Fraction\r\n     * @return the result of there multiplication\r\n     *\/\r\n    public static Rational mul(Rational x, Rational y) {\r\n        return new Rational(x.n * y.n, x.d * y.d);\r\n    }\r\n\r\n    \/**\r\n     * Divide two Fractions x and y\r\n     *\r\n     * @param x the first Fraction\r\n     * @param y the second Fraction\r\n     * @return the result of there division\r\n     *\/\r\n    public static Rational div(Rational x, Rational y) {\r\n        return new Rational(x.n * y.d, x.d * y.n);\r\n    }\r\n\r\n    \/**\r\n     * Convert fraction to string value\r\n     *\r\n     * @return fraction\r\n     *\/\r\n    public String toString() {\r\n        return n + \"\/\" + d;\r\n    }\r\n\r\n    \/**\r\n     * Compare two fractions and write to console a result\r\n     *\r\n     * @param x x the first Fraction\r\n     * @param y the second Fraction\r\n     *\/\r\n    public static void cmp(Rational x, Rational y) {\r\n        if (!equals(x, y)) {\r\n            if ((x.n * y.d) &gt; (y.n * x.d)) System.out.println(x.toString() + \" &gt; \" + y.toString());\r\n            else System.out.println(y.toString() + \" &gt; \" + x.toString());\r\n        } else System.out.println(x.toString() + \" = \" + y.toString());\r\n    }\r\n\r\n    public static void main(String[] args) {\r\n        try {\r\n            Rational a = new Rational(4, 5);\r\n            Rational b = new Rational(1, 2);\r\n            Rational c = new Rational(2, 5);\r\n            System.out.println(\"Equality - \" + equals(a, b));\r\n            System.out.println(\"Equality - \" + equals(mul(a, b), c));\r\n            System.out.println(c.toString() + \"+\" + b.toString() + \"=\" + add(c, b));\r\n            System.out.println(a.toString() + \"-\" + b.toString() + \"=\" + sub(a, b));\r\n            System.out.println(b.toString() + \"-\" + a.toString() + \"=\" + sub(b, a));\r\n            System.out.println(a.toString() + \"*\" + b.toString() + \"=\" + mul(c, b));\r\n            System.out.println(a.toString() + \"%\" + b.toString() + \"=\" + div(a, b));\r\n            cmp(a, b);\r\n            cmp(b, c);\r\n            cmp(mul(a, b), c);\r\n        } catch (ArithmeticException e) {\r\n            System.out.println(\"dividing on 0\");\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>\u041a\u043e\u0434 \u043d\u0430 <a href=\"https:\/\/ideone.com\/kLPWYa\">Ideone<\/a>.<\/p>\n<h4>\u0422\u0435\u0441\u0442<\/h4>\n<table style=\"height: 442px;width: 453px\">\n<tbody>\n<tr style=\"height: 48px\">\n<td style=\"width: 78px;height: 48px\">\u0412\u0445\u043e\u0434\u044f\u0449\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/td>\n<td style=\"width: 211px;height: 48px\">\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f<\/td>\n<td style=\"width: 144px;height: 48px\">\u0412\u044b\u0445\u043e\u0434\u044f\u0449\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/td>\n<\/tr>\n<tr style=\"height: 96px\">\n<td style=\"width: 78px;height: 96px\">4\/5<\/p>\n<p>1\/2<\/td>\n<td style=\"width: 211px;height: 96px\">\u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0430<\/td>\n<td style=\"width: 144px;height: 96px\">false<\/td>\n<\/tr>\n<tr style=\"height: 136px\">\n<td style=\"width: 78px;height: 136px\">4\/5<\/p>\n<p>1\/2<\/p>\n<p>2\/5<\/td>\n<td style=\"width: 211px;height: 136px\">\u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u043e \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0443 \u0434\u0440\u043e\u0431\u0438 \u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0434\u0432\u0443\u0445 \u0434\u0440\u0443\u0433\u0438\u0445<\/td>\n<td style=\"width: 144px;height: 136px\">true<\/td>\n<\/tr>\n<tr style=\"height: 96px\">\n<td style=\"width: 78px;height: 96px\">2\/5<\/p>\n<p>1\/2<\/td>\n<td style=\"width: 211px;height: 96px\">\u0441\u043b\u043e\u0436\u0435\u043d\u0438\u0435<\/td>\n<td style=\"width: 144px;height: 96px\">9\/10<\/td>\n<\/tr>\n<tr style=\"height: 96px\">\n<td style=\"width: 78px;height: 96px\">4\/5<\/p>\n<p>1\/2<\/td>\n<td style=\"width: 211px;height: 96px\">\u0432\u044b\u0447\u0438\u0442\u0430\u043d\u0438\u0435<\/td>\n<td style=\"width: 144px;height: 96px\">3\/10<\/td>\n<\/tr>\n<tr style=\"height: 96px\">\n<td style=\"width: 78px;height: 96px\">4\/5<\/p>\n<p>1\/2<\/td>\n<td style=\"width: 211px;height: 96px\">\u0432\u044b\u0447\u0438\u0442\u0430\u043d\u0438\u0435<\/td>\n<td style=\"width: 144px;height: 96px\">-3\/10<\/td>\n<\/tr>\n<tr style=\"height: 96px\">\n<td style=\"width: 78px;height: 96px\">2\/5<\/p>\n<p>1\/2<\/td>\n<td style=\"width: 211px;height: 96px\">\u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435<\/td>\n<td style=\"width: 144px;height: 96px\">1\/5<\/td>\n<\/tr>\n<tr style=\"height: 96.4375px\">\n<td style=\"width: 78px;height: 96.4375px\">4\/5<\/p>\n<p>1\/2<\/td>\n<td style=\"width: 211px;height: 96.4375px\">\u0434\u0435\u043b\u0435\u043d\u0438\u0435<\/td>\n<td style=\"width: 144px;height: 96.4375px\">8\/5<\/td>\n<\/tr>\n<tr style=\"height: 96.4375px\">\n<td style=\"width: 78px;height: 96.4375px\">4\/5<\/p>\n<p>1\/2<\/td>\n<td style=\"width: 211px;height: 96.4375px\">\u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435<\/td>\n<td style=\"width: 144px;height: 96.4375px\">\n4\/5 &gt; 1\/2<\/td>\n<\/tr>\n<tr style=\"height: 96.4375px\">\n<td style=\"width: 78px;height: 96.4375px\">4\/5<\/p>\n<p>1\/2<\/td>\n<td style=\"width: 211px;height: 96.4375px\">\u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435<\/td>\n<td style=\"width: 144px;height: 96.4375px\">1\/2 &gt; 2\/5<\/td>\n<\/tr>\n<tr style=\"height: 96.4375px\">\n<td style=\"width: 78px;height: 96.4375px\">4\/5<\/p>\n<p>1\/2<\/p>\n<p>2\/5<\/td>\n<td style=\"width: 211px;height: 96.4375px\">\u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u0434\u0440\u043e\u0431\u0438 \u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0434\u0432\u0443\u0445 \u0434\u0440\u0443\u0433\u0438\u0445<\/td>\n<td style=\"width: 144px;height: 96.4375px\">2\/5 = 2\/5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0417\u0430\u0434\u0430\u0447\u0430 \u041d\u0430\u043f\u0438\u0448\u0438\u0442\u0435 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u043c\u0438 (immutable) \u0440\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0434\u0440\u043e\u0431\u044f\u043c\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b. \u041a\u043e\u0434 import java.lang.*; class Rational { private int n, d; \/** * @param n is a numerator of Fraction * @param d is a divider of Fraction *\/ public Rational(int n, int d) throws ArithmeticException { if (d == 0) throw &hellip; <a href=\"https:\/\/java.mazurok.com\/?p=1339\" class=\"more-link\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":80,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[30,163],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/posts\/1339"}],"collection":[{"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/users\/80"}],"replies":[{"embeddable":true,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1339"}],"version-history":[{"count":5,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/posts\/1339\/revisions"}],"predecessor-version":[{"id":1620,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/posts\/1339\/revisions\/1620"}],"wp:attachment":[{"href":"https:\/\/java.mazurok.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}