This class implements rational number of the type 23 Require

This class implements rational number of the type 2/3.

Requirements:

Write a class Fraction;

The private data consists of: int num, (numerator of the fraction) and int den (denominator of the fraction).

The public interface consists of:

  Constructors that have two int args, to allow setting rational to any legitimate value

    one int arg, to construct rationals with arg numerator and denominator 1.

Overloaded operators << and >> to allow writing to screen in form 325/430 and reading from the keyboard in the same format. Overloaded operators + - * / < <= > >= ==

Notes: either num or den, or both may contain a negative integer . Please use the following main function to test your program :

Solution

public class RationalNum{

private int numerator,denom;

public RationalNum(){

int num=2;

int den=3;

eval();

}

public RationalNum(int num, int den){
        numerator = num;
        denom = den;
        eval();
        }

public RationalNum(RationalNum n){
        numerator = n.numerator;
        denom = n.denominator;
        eval();
        }

public void setNumer(int num){
        numerator = num;
        eval();
        }
        public void setDenom(int den){
        denom = den;
        eval();
        }
        public void setRational(int num, int den){
        numerator = num;
        denom = den;
        eval();
        }

public int getNumerator(){
        return numerator;
        }
        public int getDenom(){
        return denom;
        }

public boolean equals(RationalNum n){
        if (numerator / denom == n.numerator / n.denom){
        return(true);
                }
        else {
        return(false);
            }
        }

public int compare(RationalNum n){
        if (numerator / denom == n.numerator /n.denom){
        return (0);
        }
        else if (numerator / denom <n.numerator / n.denom){
        return (-1);
        }
        else{
        return (1);
            }   
        }

public void copyMethod(RationalNum n){
        numerator = n.numerator;
        denom = n.denom;
        eval();
        }

public boolean equals(RationalNum n){
        if (numerator / denom == n.numerator / n.denom){
        return(true);
                }
        else {
        return(false);
            }
        }

static int gcd(int x, int y){
        int r;
        while (y != 0) {
        r = x % y;
        x = y;
        y = r;
            }
        return x;
        }

         
        public void add(RationalNum n){
        int greatdenom = n.denom * denom;      
        int multx = greatdenom /n.denom;
        int mult = greatdenom / denom;
        denom =n.denom * denom;
        numerator = (n.numerator * multx) + (numerator * mult);
        eval();
        }


        public void subtract(RationalNum n){
        int greatdenom = n.denom * denom;      
        int multx = greatdenom / n.denom;
        int mult = greatdenom / denom;
        denom =n.denom * denom;
        if (n.numerator > numerator){
        numerator = (x.numerator * multx) - (numerator * mult);
            }
        else {
        numerator = (numerator * mult) - (n.numerator * multx);
            }
        eval();
        }

    
        public void num(RationalNum x){
        numerator = numerator * n.numerator ;
        denom = denom * n.denom;
        eval();
        }

     
        public void division(RationalNum n){
        numerator = numerator / n.numerator ;
        denom = denom / n.denom;
        eval();
        }

      
        private void eval(){
        int divisor;
        divisor = RationalNum.gcd(numerator , denom);
        numerator = numerator / divisor;
        denom = denom / divisor;
        }

This class implements rational number of the type 2/3. Requirements: Write a class Fraction; The private data consists of: int num, (numerator of the fraction)
This class implements rational number of the type 2/3. Requirements: Write a class Fraction; The private data consists of: int num, (numerator of the fraction)
This class implements rational number of the type 2/3. Requirements: Write a class Fraction; The private data consists of: int num, (numerator of the fraction)

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site