2
votes

Rational numbers contain an integer numerator and denominator. Write the code to implement a class named Rational which stores two private ints (numer and denom) with the following methods:

public Rational(int,int)
constructor that sets the numer and denom

public Rational(Rational)

//copy constructor for a Rational object

public void setNumer(int) //sets the numerator to the paramter value

public int getNumer()
//returns the stored numerator

public void setDenom(int) //sets the denominator to the paramter value

public int getDenom() //returns the stored denominator

//return a new Rational object that contains the reciprocal of the object that invokes the method.

public Rational reciprocal()

//returns a new Rational object that contains the product of the two paramteres.

public static Rational multiply(Rational a, Rational b)


I am stuck at the 7th method for this class. I don't understand how to flip the numbers so that they are reciprocals. Any help will be greatly Appreciated. This is my code so far:

class Rational {
    private int numer;
    private int denom;

    public Rational(int numer, int denom){
        this.numer = numer;
        this.denom = denom;
    }

    public Rational(Rational rational){
        rational = new Rational(numer, denom);
    }

    public void setNumber(int fum){
        numer = fum;
    }

    public int getNumber(){
        return 5;
    }

    public void setDenom(int Dum){
        denom = Dum;
    }

    public int getDenom(){
        return 10;
    }

    public Rational reciprocal(){
        ;
    }
}



public class Results {
    public static void main(String[] args){

    }

}
3
And for the last one I was trying this: public static Rational multiply(Rational a, Rational b) { return new Rational(a*b). And can't pass that. I'm confused.userb
What's a copy constructor? This is Java!Maarten Bodewes
I take it a copy constructor, is not a Java terminology. I've been trying to do the last one, but I get that my method is undefined Rational. Why is this exactly? And what is the best way to go about this?userb
You should use separate files for each Java class. Currently your class is package private (without public or protected in front of it). Maybe that has something to do with it? You would not need a copy constructor if you make the class immutable; you could just copy the reference. But maybe that's a next lession.Maarten Bodewes
Nitpicking: The description for reciprocal() is wrong. Instead of "… of the object that invokes the method." it should read something like "… of the object the method is called on.".siegi

3 Answers

3
votes

Math is Fun: Reciprocal of a Fraction says (in part) to get the reciprocal of a fraction, just turn it upside down.

public Rational reciprocal(){
    return new Rational(this.denom, this.number);
}
0
votes

You have to return a new Rational with the numbers fliped.

public Rational reciprocal(){
    return new Rational(this.denom,this.numer);
}
0
votes

Try this:

public Rational reciprocal(){
  return new Rational(denom, numer);
}

It get the reciprocal which is just the numerator and denominator flipped. return new Rational(denom, numer); does this by creating a new rational instance with the denominator from the current one as the numerator and as the numerator as the current instance as the denominator.

Really a reciprocal is one divided by the original number as said here, but flipping the numerator and denominator does the same thing as dividing by its self.