Nombre Rationnel Projet Java


Pour ce projet, je dois créer une classe de nombres rationnels qui a 2 parties un numérateur int et un dénominateur int. J'ai dû ajouter deux contractions où le dénominateur négatif doit être déplacé vers le numérateur. J'ai également ajouté des getters et des setters et un toString(). Les données doivent s'imprimer en numérateur / dénominateur. J'ai également dû coder des méthodes membres pour l'addition, la soustraction, les multiplications et la division et nier(?) Je ne suis pas sûr de ce que cette dernière partie signifie.

J'ai fait la classe déjà, mais Eclipse me donne une erreur avec la méthode add and soustract autour de la partie où j'ai tapé "temp". Veuillez me faire savoir si j'ai quelque chose qui est incorrect ou s'il me manque quelque chose.

public class Rational {     
    private int numerator;
    private int denominator;

    public Rational()
    {
        numerator = 0;
        denominator = 1;
    }

    public Rational(int n, int d, int num, int denom)
    {
        if (d < 0)
        {
            num = -n;
            denom = d;
        }           
        else if (d == 0)
        {
            num = n;
            denom = 1;
        }           
        else 
        {
            num = n;
            denom = 0;
        }
    }

    public int getNumerator()
    {
        return numerator;
    }

    public int getDenominator()
    {
        return denominator;
    }

    public void setNumerator(int n)
    {
        numerator = n;
    }

    public void setDenominator(int n, int d, int num, int denom) 
    {
        denominator = d;
        if (d < 0)
        {
            num = -n;
            denom = d;
        }           
        else if (d == 0)
        {
            num = n;
            denom = 1;
        }           
        else
        {
            num = n;
            denom = 0;
        }
    }

    public String toString()
    {
        return numerator + "/" + denominator; 
    }

    public boolean equals (Rational other)
    {
        if(numerator * other.denominator == denominator * other.numerator)
            return true;
        else 
            return false;
    }

    public boolean notequals(Rational other)
    {
        if (numerator * other.denominator != denominator * other.numerator)
            return true;
        else 
            return false;
    }

    //subtract method
    public Rational subtract(Rational other)
    {
        Rational temp;
        temp.numerator = numerator * other.denominator - denominator * other.numerator;
        temp.denominator = denominator * other.denominator;
        return temp;
    }

    //add method
    public Rational add(Rational other)
    {
        Rational temp;
        temp.numerator = numerator * other.denominator + denominator * other.numerator;
        temp.denominator = denominator * other.denominator;
        return temp;
    }

    public boolean lessThan(Rational other)
    {
        return(numerator * other.denominator < denominator * other.numerator);
    }

    public boolean greterThan(Rational other)
    {
        return(numerator * other.denominator > denominator * other.numerator);
    }

    public boolean lessThanEqualTo(Rational other)
    {
        return(numerator * other.denominator <= denominator * other.numerator);
    }

    public boolean greaterThanEqual(Rational other)
    {
        return(numerator * other.denominator >= denominator * other.numerator);
    }
}

Je me bats cependant avec le principal pour tester chaque méthode. Voici ce que j'ai jusqu'à présent:

public class Project4 {
    public static void main(String[] args) {
        Rational a = new Rational();
        Rational b = new Rational();
        Rational c;
        c = a.add(b);
    }   
}
Author: ochi, 2014-11-11

1 answers

//Here goes commented code:


public class Rational 
    {

        private int numerator; // You can declare private variables here all right
        private int denominator;


        // This constructor creates a new object of the class Rational. Objects are instances of that class, all right? 
        public Rational()
            {
                numerator = 0;
                denominator = 1;                
                //private int numerator = 0; 
                //private int denominator = 1; // I get it, you need them even when not giving arguments, but it is better to do stuff in constructors. You better try to make declarations in the constructor and not in the begining of a class, if possible.

            } 



        // Here is the constructor overriden, so you can create an instance that contains 4 arguments. (n,d,num,denom) Good.
        public Rational(int n, int d, int num, int denom)
        {
            // You repeat yourself in the constructor.
            // public void setDenominator(int n, int d, int num, int denom) does the same thing right?
            // I don't remember now, but I think that on instantiation you can't use methods, if you don't add the word static.
            // public static void setDenominator(int n, int d, int num, int denom) will make you able to call it in the constructor!
            if (d < 0)
            {
                num = -n;
                denom = d;
            }

            else if (d == 0)
            {
                num = n;
                denom = 1;
            }

            else 
            {
                num = n;
                denom = 0;
            }
        }

public int getNumerator()
{
    return numerator;
}


public int getDenominator()
{
    return denominator;
}

public void setNumerator(int n)
{
    numerator = n;
}

public void setDenominator(int n, int d, int num, int denom) // FIX IT. ADD STATIC
{
    denominator = d;
    if (d < 0)
    {
        num = -n;
        denom = d;
    }

    else if (d == 0)
    {
        num = n;
        denom = 1;
    }

    else
    {
        num = n;
        denom = 0;
    }
}

public String toString()
{
    return numerator + "/" + denominator; 
}

public boolean equals (Rational other)
{
    if(numerator * other.denominator == denominator * other.numerator)
        return true;
    else 
        return false;
}

public boolean notequals(Rational other)
{
    if (numerator * other.denominator != denominator * other.numerator)
        return true;
    else 
        return false;
}

//subtract method
public Rational subtract(Rational other)
{
    Rational temp;
    temp.numerator = numerator * other.denominator - denominator * other.numerator;
    temp.denominator = denominator * other.denominator;
    return temp;
} // THIS RETURNS temp THAT IS AN OBJECT OF THE TYPE Rational!!! Remember that.


//add method
public Rational add(Rational other)
{
    Rational temp;
    temp.numerator = numerator * other.denominator + denominator * other.numerator;
    temp.denominator = denominator * other.denominator;
    return temp;
}// THIS RETURNS temp THAT IS AN OBJECT OF THE TYPE Rational!!! Remember that.

public boolean lessThan(Rational other)
{
    return(numerator * other.denominator < denominator * other.numerator);
}// What type is this method return? Here is the gap you must fill. If you learn how to handel what methods return, you will be fine. Try stuff out.

public boolean greterThan(Rational other)
{
    return(numerator * other.denominator > denominator * other.numerator);
}

public boolean lessThanEqualTo(Rational other)
{
    return(numerator * other.denominator <= denominator * other.numerator);
}

public boolean greaterThanEqual(Rational other)
{
    return(numerator * other.denominator >= denominator * other.numerator);
}




}
 0
Author: George Eco, 2014-11-11 01:53:00