Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigInteger;
- import java.util.Scanner;
- class ZeroDenominatorException extends Exception {
- @Override
- public String getMessage() {
- return "Denominator cannot be zero";
- }
- }
- class GenericFraction<T extends Number, U extends Number> {
- T numerator;
- U denominator;
- public GenericFraction(T numerator, U denominator) throws ZeroDenominatorException {
- if (denominator.doubleValue() == 0) {
- throw new ZeroDenominatorException();
- }
- this.numerator = numerator;
- this.denominator = denominator;
- }
- public GenericFraction<Double, Double> add(GenericFraction<? extends Number, ? extends Number> that)
- throws ZeroDenominatorException {
- double resultNumerator = this.numerator.doubleValue() * that.denominator.doubleValue() +
- that.numerator.doubleValue() * this.denominator.doubleValue();
- double resultDenominator = this.denominator.doubleValue() * that.denominator.doubleValue();
- return new GenericFraction<Double, Double>(resultNumerator, resultDenominator);
- }
- public GenericFraction<Double, Double> normalize() throws ZeroDenominatorException {
- int gcd = gcd((int) numerator.doubleValue(), (int) denominator.doubleValue());
- int AnotherWayToGetGCD = BigInteger.valueOf(this.numerator.longValue())
- .gcd(BigInteger.valueOf(this.denominator.longValue()))
- .intValue();
- double resultNumerator = this.numerator.doubleValue() / gcd;
- double resultDenominator = this.denominator.doubleValue() / gcd;
- return new GenericFraction<Double, Double>(resultNumerator, resultDenominator);
- }
- public int gcd(int a, int b) {
- if (b == 0) {
- return a;
- }
- return gcd(b, a % b);
- }
- public double toDouble() {
- return numerator.doubleValue() / denominator.doubleValue();
- }
- @Override
- public String toString() {
- GenericFraction<Double,Double> gf = null;
- try {
- gf = this.normalize();
- } catch (ZeroDenominatorException e) {
- e.printStackTrace();
- }
- return String.format("%.2f / %.2f", gf.numerator.doubleValue(), gf.denominator.doubleValue());
- }
- }
- public class GenericFractionTest {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- double n1 = scanner.nextDouble();
- double d1 = scanner.nextDouble();
- float n2 = scanner.nextFloat();
- float d2 = scanner.nextFloat();
- int n3 = scanner.nextInt();
- int d3 = scanner.nextInt();
- try {
- GenericFraction<Double, Double> gfDouble = new GenericFraction<Double, Double>(n1, d1);
- GenericFraction<Float, Float> gfFloat = new GenericFraction<Float, Float>(n2, d2);
- GenericFraction<Integer, Integer> gfInt = new GenericFraction<Integer, Integer>(n3, d3);
- System.out.printf("%.2f\n", gfDouble.toDouble());
- System.out.println(gfDouble.add(gfFloat));
- System.out.println(gfInt.add(gfFloat));
- System.out.println(gfDouble.add(gfInt));
- gfInt = new GenericFraction<Integer, Integer>(n3, 0);
- } catch (ZeroDenominatorException e) {
- System.out.println(e.getMessage());
- }
- scanner.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment