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){
}
}
public
orprotected
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 Bodewesreciprocal()
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