Skip to content Skip to sidebar Skip to footer

Java Program For Calculating Fractions

The purpose of the program is to get two user inputs for a fraction, receive a operator from the user, and then to get two more user inputs for a second fraction. The program must

Solution 1:

Maybe you want to enter OOP (object oriented programming):

Qx=newQ(1, 4);
Qy=newQ(1, 8);
Qz= x.plus(y);
System.out.println("%s + %s = %s%n", x, y, z);

(1 / 4) + (1 / 8) = (3 / 8)

publicclassQ {
    finalint numerator;
    finalint denominator;

    publicQ(int numerator, int denominator) {
        intg= gcd(numerator, denominator);
        this.numerator = numerator / g;
        this.denominator = denominator / g;
    }

    @Overridepublic String toString() {
        return String.format("(%d / %d)", numerator, denominator);
    }

    public Q plus(Q rhs) {
        returnnewQ(numerator * rhs.denominator + rhs.numerator * denominator,
            denominator * rhs.denominator);
    }

Solution 2:

One part of the problem: You have some logic that you need to perform both for '+' and '-', but the way you've written it, it will be executed for '+' only:

switch(o){
    ... other cases
    case'+':
        ... logic to compute lcm and other thingsn= tempN1 + tempN2;
        d = lcm;
        break; 

    case'-':
        n = tempN1 - tempN2;
        d = lcm;
        break;

When the user enters '-', the program won't go into the part under case '+'. This means that tempN1, tempN2, and lcm won't be set up. If you're getting errors from the compiler about uninitialized variables, this is one reason why.

One way to write code that is executed for multiple cases:

switch(o){
    ... other cases
    case'+':
    case'-':
        ... whatever logic will apply to both cases
        if(o == '+'){
            ... whatever logic will apply to + only
        } else {
            ... whatever logic will apply to - only
        }
        ... if you have more logic for both cases, you can put it here
        break;

That is, when you have more than one case right next to each other, with no code in between, the following code applies to multiple cases. (This is because of "fall-through". It actually goes to the case '+', and then since there's no break in the case '+' code, it falls through to the case '-'. But it's not recommended to take advantage of fall-through, except when there is no code at all as in the above.)

Solution 3:

UPDATE:

Be aware that this is my no means complete and/or the best solution, but at least it should take you in the right direction. You will still have to reduce fractions and do some other tweaks.

import java.math.BigInteger;
import java.util.Scanner;

publicclassFractionCalculator {
    publicstaticvoidmain(String[] args) {
        Scannerin=newScanner(System.in);

        int n1;
        int n2;
        int d1;
        int d2;
        intn=0;
        int d;
        char o;

        System.out.println("Enter a numerator for fraction 1: ");
        n1 = in.nextInt();
        System.out.println("Enter a denominator for fraction 1: ");
        d1 = in.nextInt();

        if (d1 > 0) {
            System.out.println();
        } else {
            System.out.println("Invalid denominator");
            System.exit(0);
        }

        System.out.println("Enter an operator: ");
        o = in.next().toCharArray()[0];
        System.out.println("Enter a numerator for fraction 2: ");
        n2 = in.nextInt();
        System.out.println("Enter a denominator for fraction 2: ");
        d2 = in.nextInt();

        if (d2 > 0) {
            System.out.println();
        } else {
            System.out.println("Invalid denominator");
            System.exit(0);
        }

        switch (o) {
            case'*':
                n = n1 * n2;
                d = d1 * d2;

                break;

            case'/':
                n = n1 * d2;
                d = n2 * d1;

                break;

            case'+':
            case'-':

                d = gcd(d1, d2);

                n1 *= d / d1;
                n2 *= d / d2;

                if(o == '+') {
                    n = n1 + n2;
                }
                elseif(o == '-') {
                    n = n1 - n2;
                }

                break;
            default:
                System.out.println("Illegal Operator: " + o);
                return;
        }

        System.out.printf(" %d     %d     %d\n", n1, n2, n);
        System.out.printf("--- %c --- = ---\n", o);
        System.out.printf(" %d     %d     %d\n", d1, d2, d);
    }

    privatestaticintgcd(int d1, int d2) {
        BigIntegergcd=newBigInteger(String.valueOf(d1)).gcd(newBigInteger(String.valueOf(d2)));
        return gcd.intValue();
    }
}

Post a Comment for "Java Program For Calculating Fractions"