[java] abc
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- import java.lang.String;
- import java.lang.Math;
- import java.util.Scanner;
- public class Solution {
- private int numerator;
- private int denominator = 1;
- /**.
- * getNumerator
- */
- public int getNumerator() {
- return numerator;
- }
- /**.
- * setNumerator
- * @param numerator setNumerator
- */
- public void setNumerator(int numerator) {
- this.numerator = numerator;
- }
- /**.
- * getDenominator
- */
- public int getDenominator() {
- return denominator;
- }
- /**.
- * setDenominator
- * @param denominator setDenominator
- */
- public void setDenominator(int denominator) {
- if (denominator == 0) {
- denominator = 1;
- }
- this.denominator = denominator;
- }
- /**.
- * Solution
- */
- public Solution() {
- this.numerator = 1;
- }
- /**.
- * Solution
- * @param numerator Solution
- * @param denominator Solution
- */
- public Solution(int numerator, int denominator) {
- this.numerator = numerator;
- if (denominator == 0) {
- denominator = 1;
- }
- this.denominator = denominator;
- }
- /**.
- * gcd
- * @param a gcd
- * @param b gcd
- */
- public static int gcd(int a, int b) {
- if (a == Integer.MAX_VALUE || b == Integer.MAX_VALUE
- || a == Integer.MIN_VALUE || b == Integer.MIN_VALUE) {
- return 1;
- }
- if (a < 0 || b < 0) {
- a = Math.abs(a);
- b = Math.abs(b);
- }
- if (a == 0 || b == 0) {
- return a + b;
- }
- while (a != b) {
- if (a > b) {
- a -= b;
- } else {
- b -= a;
- }
- }
- return a;
- }
- /**.
- * reduce
- */
- public Solution reduce() {
- int ucln = gcd(this.getNumerator(), this.getDenominator());
- this.setNumerator(this.getNumerator() / ucln);
- this.setDenominator(this.getDenominator() / ucln);
- return this;
- }
- /**.
- * add
- * @param x add
- */
- public Solution add(Solution x) {
- this.setNumerator(this.denominator * x.numerator + this.numerator * x.denominator);
- this.setDenominator(this.denominator * x.denominator);
- return this.reduce();
- }
- /**.
- * subtract
- * @param x subtract
- */
- public Solution subtract(Solution x) {
- this.setNumerator(this.denominator * x.numerator - this.numerator * x.denominator);
- this.setDenominator(this.denominator * x.denominator);
- return this.reduce();
- }
- /**.
- * multiply
- * @param x multiply
- */
- public Solution multiply(Solution x) {
- this.setNumerator(x.getNumerator() * this.getNumerator());
- this.setDenominator(this.getDenominator() * x.getDenominator());
- return this.reduce();
- }
- /**.
- * divide
- * @param x divide
- */
- public Solution divide(Solution x) {
- this.setNumerator(this.getNumerator() * x.getDenominator());
- this.setDenominator(this.getDenominator() * x.getNumerator());
- return this.reduce();
- }
- /**.
- * equals
- * @param obj equals
- */
- @Override
- public boolean equals(Object obj) {
- if (obj instanceof Solution) {
- Solution other = (Solution) obj;
- if (other.reduce().getNumerator() == this.getNumerator()
- && other.reduce().getDenominator() == this.getDenominator()) {
- return true;
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- }
Editor
You can edit this paste and save as new: