Write a java code, Extend class Fraction by adding methods for the arithmetic operators +, - and /, and the six relational operators (==, !=, <, >, <= and >=). Then compute a menu-driven fraction calculator. Program shoud do all 9 arithmetic operations and 0 to stop. User should first asked for the options 1 to 0 and then asked for first fraction and second fraction. It will keep running until user enter 0 to stop.

Fraction class code:

import java.util.*;  // Scanner
import java.io.*;    // PrintStream

public class Fraction extends Object
{
    private int myNumerator, myDenominator;
    public Fraction()
    {
        setFraction(0,1);
    }

    public Fraction (int numerator,int  denominator)
    {
        setFraction(numerator, denominator);
    }

    public void setFraction(int numerator, int denominator)

    {
        if (denominator == 0)
        throw new IllegalArgumentException("Denominator cannot be 0!");
        myNumerator = numerator;
        myDenominator = denominator;
        simplify();
    }   

    public int getNumerator()
    {
        return myNumerator;
    }

    public int getDenominator()
    {
        return myDenominator;
    }

    public void read(Scanner sc)
    {
        String fract = sc.next();   //"3/4"
                                                                //  System.out.println();
        StringTokenizer parser = new StringTokenizer(fract, "/");
        if (parser.countTokens() !=2)
            throw new IllegalArgumentException("Bad format for Fraction!");
        String first = parser.nextToken();
        int num = Integer.parseInt(first);
            //myNumerator = Integer.parseInt(first);
        String second = parser.nextToken();
            //myDenominator = Integer.parseInt(second);
        int denom = Integer.parseInt(second);
        setFraction(num,denom);

    }

    public Fraction times(Fraction rightOperand)
    {
        int num = myNumerator * rightOperand.getNumerator();
        int denom = myDenominator * rightOperand.getDenominator();
        return new Fraction(num, denom);

    }

    public Fraction divide(Fraction divi)
    {
    int num = myNumerator * divi.myDenominator;
    int denom = myDenominator * divi.myNumerator;
    return new Fraction(num, denom);
  }

    public Fraction add(Fraction frac1)
    {
        Fraction fraction_1 = new Fraction();
        Fraction fraction_2 = new Fraction();
        Fraction result = new Fraction();
        fraction_1.myDenominator = myDenominator * frac1.myDenominator;
        fraction_1.myNumerator = myNumerator * frac1.myDenominator;
        fraction_2.myDenominator = frac1.myDenominator * myDenominator;
        fraction_2.myNumerator = frac1.myNumerator * myDenominator;
        result.myNumerator = fraction_1.myNumerator + fraction_2.myNumerator;
        result.myDenominator = fraction_2.myDenominator;
        return result;
    }

    public Fraction subtract(Fraction subtr)
    {
    int num = myNumerator * subtr.myDenominator -
        subtr.myNumerator * myDenominator;
    int denom = myDenominator * subtr.myDenominator;
    return new Fraction(num, denom);
  }
    private void simplify()
    {
        int gcd;
        while(true)
        {
            gcd = greatestCommonDivisor( myNumerator, myDenominator);
            if (gcd == 1) return;
            myNumerator = myNumerator / gcd;
            myDenominator = myDenominator / gcd;
        }
    }

    public String toString()
    {
        return myNumerator  + "/" + myDenominator;
    }

    public void print ()
    {
        System.out.println(toString());
    }

    private static int greatestCommonDivisor(int alpha, int beta)
    {
        alpha = Math.abs(alpha);  // take absolute values of operands
        beta = Math.abs(beta);

        if (beta == 0)       // base case
            return alpha;
        else                 // induction step
        {
            int remainder = alpha % beta;

            return greatestCommonDivisor(beta, remainder);
        }
    }

}

what exactly is the problem in this code(if any)?what help you need from us?

Everything in java is object even the primitive datatypes can also be converted into object by using the wrapper class.

let's agree to disagree. here you are contradicting yourself (in my defense ;) ) first, you say that everything in Java is an Object, then you say there are primitive datatypes which can converted into Objects.

admitting there are primitive datatypes, you basically said: not everything is/can be an Object.

in a future version, they plan to remove the primitive datatypes, but for the moment, they're still there, so, is everything an Object? nope.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.