Filip_Markoski

[NP] Дропки

Nov 12th, 2017
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.39 KB | None | 0 0
  1. import java.math.BigInteger;
  2. import java.util.Scanner;
  3.  
  4. class ZeroDenominatorException extends Exception {
  5.     @Override
  6.     public String getMessage() {
  7.         return "Denominator cannot be zero";
  8.     }
  9. }
  10.  
  11. class GenericFraction<T extends Number, U extends Number> {
  12.     T numerator;
  13.     U denominator;
  14.  
  15.     public GenericFraction(T numerator, U denominator) throws ZeroDenominatorException {
  16.         if (denominator.doubleValue() == 0) {
  17.             throw new ZeroDenominatorException();
  18.         }
  19.         this.numerator = numerator;
  20.         this.denominator = denominator;
  21.     }
  22.  
  23.     public GenericFraction<Double, Double> add(GenericFraction<? extends Number, ? extends Number> that)
  24.             throws ZeroDenominatorException {
  25.         double resultNumerator = this.numerator.doubleValue() * that.denominator.doubleValue() +
  26.                 that.numerator.doubleValue() * this.denominator.doubleValue();
  27.         double resultDenominator = this.denominator.doubleValue() * that.denominator.doubleValue();
  28.         return new GenericFraction<Double, Double>(resultNumerator, resultDenominator);
  29.     }
  30.  
  31.     public GenericFraction<Double, Double> normalize() throws ZeroDenominatorException {
  32.         int gcd = gcd((int) numerator.doubleValue(), (int) denominator.doubleValue());
  33.  
  34.         int AnotherWayToGetGCD = BigInteger.valueOf(this.numerator.longValue())
  35.                 .gcd(BigInteger.valueOf(this.denominator.longValue()))
  36.                 .intValue();
  37.  
  38.         double resultNumerator = this.numerator.doubleValue() / gcd;
  39.         double resultDenominator = this.denominator.doubleValue() / gcd;
  40.         return new GenericFraction<Double, Double>(resultNumerator, resultDenominator);
  41.     }
  42.  
  43.     public int gcd(int a, int b) {
  44.         if (b == 0) {
  45.             return a;
  46.         }
  47.         return gcd(b, a % b);
  48.     }
  49.  
  50.     public double toDouble() {
  51.         return numerator.doubleValue() / denominator.doubleValue();
  52.     }
  53.  
  54.     @Override
  55.     public String toString() {
  56.         GenericFraction<Double,Double> gf = null;
  57.         try {
  58.             gf = this.normalize();
  59.         } catch (ZeroDenominatorException e) {
  60.             e.printStackTrace();
  61.         }
  62.         return String.format("%.2f / %.2f", gf.numerator.doubleValue(), gf.denominator.doubleValue());
  63.     }
  64. }
  65.  
  66. public class GenericFractionTest {
  67.     public static void main(String[] args) {
  68.         Scanner scanner = new Scanner(System.in);
  69.         double n1 = scanner.nextDouble();
  70.         double d1 = scanner.nextDouble();
  71.         float n2 = scanner.nextFloat();
  72.         float d2 = scanner.nextFloat();
  73.         int n3 = scanner.nextInt();
  74.         int d3 = scanner.nextInt();
  75.         try {
  76.             GenericFraction<Double, Double> gfDouble = new GenericFraction<Double, Double>(n1, d1);
  77.             GenericFraction<Float, Float> gfFloat = new GenericFraction<Float, Float>(n2, d2);
  78.             GenericFraction<Integer, Integer> gfInt = new GenericFraction<Integer, Integer>(n3, d3);
  79.             System.out.printf("%.2f\n", gfDouble.toDouble());
  80.             System.out.println(gfDouble.add(gfFloat));
  81.             System.out.println(gfInt.add(gfFloat));
  82.             System.out.println(gfDouble.add(gfInt));
  83.             gfInt = new GenericFraction<Integer, Integer>(n3, 0);
  84.         } catch (ZeroDenominatorException e) {
  85.             System.out.println(e.getMessage());
  86.         }
  87.  
  88.         scanner.close();
  89.     }
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment