{"id":177,"date":"2015-07-29T17:24:15","date_gmt":"2015-07-29T14:24:15","guid":{"rendered":"http:\/\/java.mazurok.com\/?p=177"},"modified":"2015-08-14T07:38:53","modified_gmt":"2015-08-14T04:38:53","slug":"fractions-and-operations","status":"publish","type":"post","link":"https:\/\/java.mazurok.com\/?p=177","title":{"rendered":"Immutable fractions"},"content":{"rendered":"<h1>\u0417\u0430\u0434\u0430\u043d\u0438\u0435<\/h1>\n<p>\u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0441 \u0434\u0440\u043e\u0431\u044f\u043c\u0438. \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435 \u0434\u0440\u043e\u0431\u0438 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f, \u0430 \u043c\u0435\u0442\u043e\u0434\u044b, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0438\u0435 \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u043c\u0438. \u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0439\u0442\u0435 \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0434\u0440\u043e\u0431\u0438 \u043f\u0440\u0438 \u043f\u0435\u0447\u0430\u0442\u0438. \u041f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0439\u0442\u0435 \u0441\u0432\u043e\u0451 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 main().<br \/>\n\u0416\u0435\u043b\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 \u0441\u043e\u043a\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u0434\u0440\u043e\u0431\u0435\u0439 \u0447\u0435\u0440\u0435\u0437 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0415\u0432\u043a\u043b\u0438\u0434\u0430 \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0435\u0433\u043e \u043e\u0431\u0449\u0435\u0433\u043e \u0434\u0435\u043b\u0438\u0442\u0435\u043b\u044f (\u041d\u041e\u0414, GCD). \u0412\u043f\u043e\u043b\u043d\u0435 \u0443\u043c\u0435\u0441\u0442\u043d\u043e\u0439 \u0431\u044b\u043b\u043e \u0431\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u044d\u0442\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 Comparable \u0438 \u043f\u0440\u043e\u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u043c\u0435\u0442\u043e\u0434\u0430 java.util.Arrays.sort().<\/p>\n<h1>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/h1>\n<pre class=\"lang:java decode:true    \">\r\nclass Fraction {\r\n\tfinal int numerator, denominator;\r\n\tstatic String delimiter = \"\/\";\r\n\t\r\n\tprivate int gcd(int a, int b){\r\n\t\twhile(b != 0){\r\n\t\t\tint c = a % b;\r\n\t\t\ta = b;\r\n\t\t\tb = c;\r\n\t\t}\r\n\t\treturn a;\r\n\t}\r\n\r\n\tpublic static Fraction addition(Fraction x, Fraction y) {\r\n\t\tFraction z = new Fraction(x.numerator * y.denominator + y.numerator * x.denominator, x.denominator * y.denominator);\r\n\t\treturn z;\r\n\t}\r\n\t\r\n\tpublic static Fraction substraction(Fraction x, Fraction y){\r\n\t\tFraction z = new Fraction(x.numerator * y.denominator - y.numerator * x.denominator, x.denominator * y.denominator);\r\n\t\treturn z;\r\n\t}\r\n\t\r\n\tpublic static Fraction multiplication(Fraction x, Fraction y){\r\n\t\tFraction z = new Fraction(x.numerator * y.numerator, x.denominator * y.denominator);\r\n\t\treturn z;\r\n\t}\r\n\t\r\n\tpublic static Fraction division (Fraction x, Fraction y){\r\n\t\tFraction z = new Fraction(x.numerator * y.denominator, x.denominator * y.numerator);\r\n\t\treturn z;\r\n\t}\r\n\tpublic String toString(){\r\n\t\treturn numerator  + delimiter + denominator; \r\n\t}\r\n\t\r\n\tpublic Fraction(){\/\/ name ofthe constructor starts from upper case\r\n\t\tnumerator = 1;\r\n\t\tdenominator = 2;\r\n\t}\t\t\r\n\t\t\r\n\tpublic Fraction(int d){\r\n\t\tnumerator = 1;\r\n\t\tdenominator = d;\r\n\t}\r\n\r\n\tpublic Fraction(int n, int d){\r\n\t\tint devider = gcd(n, d);\r\n\t\tnumerator  = n \/ devider;\r\n\t\tdenominator = d \/ devider;\r\n\t}\r\n\t\r\n\tpublic Fraction(Fraction x){\r\n\t\tnumerator = x.numerator;\r\n\t\tdenominator = x.denominator;\r\n\t}\r\n\r\n\tpublic static void main (String[] args) {\t\r\n\t\tSystem.out.println(new Fraction(2147483647, 2147483645));\r\n\t\tFraction x = new Fraction(5);\r\n\t\tSystem.out.println(x);\r\n\t\tFraction y = x;\r\n\t\tSystem.out.println(y);\r\n\t\tFraction z = new Fraction(x); \/\/copy of x\r\n\t\tSystem.out.println(z);\r\n\t\tSystem.out.println(Fraction.addition(x, z));\r\n\t\tSystem.out.println(Fraction.substraction(z, x));\r\n\t\tSystem.out.println(Fraction.multiplication(x, z));\r\n\t\tSystem.out.println(Fraction.division(x, z));\r\n\t}\r\n}\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0417\u0430\u0434\u0430\u043d\u0438\u0435 \u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0441 \u0434\u0440\u043e\u0431\u044f\u043c\u0438. \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435 \u0434\u0440\u043e\u0431\u0438 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f, \u0430 \u043c\u0435\u0442\u043e\u0434\u044b, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0438\u0435 \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u043c\u0438. \u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0439\u0442\u0435 \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0434\u0440\u043e\u0431\u0438 \u043f\u0440\u0438 \u043f\u0435\u0447\u0430\u0442\u0438. \u041f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0439\u0442\u0435 \u0441\u0432\u043e\u0451 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 main(). \u0416\u0435\u043b\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 \u0441\u043e\u043a\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u0434\u0440\u043e\u0431\u0435\u0439 \u0447\u0435\u0440\u0435\u0437 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0415\u0432\u043a\u043b\u0438\u0434\u0430 \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0435\u0433\u043e \u043e\u0431\u0449\u0435\u0433\u043e \u0434\u0435\u043b\u0438\u0442\u0435\u043b\u044f &hellip; <a href=\"https:\/\/java.mazurok.com\/?p=177\" class=\"more-link\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":201,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25,27],"tags":[32,33,31,30,29,28],"jetpack_featured_media_url":"https:\/\/java.mazurok.com\/wp-content\/uploads\/cats-300x2891.jpg","_links":{"self":[{"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/posts\/177"}],"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\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=177"}],"version-history":[{"count":7,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/posts\/177\/revisions"}],"predecessor-version":[{"id":225,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/posts\/177\/revisions\/225"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/media\/201"}],"wp:attachment":[{"href":"https:\/\/java.mazurok.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}