Reputation: 61
Hi so i have my main class but im having trouble figuring out how to output my code from my test class. I dont understand even though tried many ways how to output the simple addition and subtraction of two fractions as it should do in my main method but can't seem to get it into my test class.
here is my code for the class with all the functions:
package rational;
public class Rational {
private int numer, denom;
//constructors
public Rational(){
int num = 1;
int den = 2;
reduce();
}
public Rational(int num, int den){
numer = num;
denom = den;
reduce();
}
public Rational(Rational x){
numer = x.numer;
denom = x.denom;
reduce();
}
//setters
public void setNumer(int num){
numer = num;
reduce();
}
public void setDenom(int den){
denom = den;
reduce();
}
public void setRational(int num, int den){
numer = num;
denom = den;
reduce();
}
//getters
public int getNumer(){
return numer;
}
public int getDenom(){
return denom;
}
//Copy method
public void copyFrom(Rational x){
numer = x.numer;
denom = x.denom;
reduce();
}
//Equals method
public boolean equals(Rational x){
if (numer / denom == x.numer / x.denom){
return(true);
}
else {
return(false);
}
}
//Compare to method
public int compareTo(Rational x){
if (numer / denom == x.numer / x.denom){
return (0);
}
else if (numer / denom < x.numer / x.denom){
return (-1);
}
else{
return (1);
}
}
//Find greatest common divisor
static int gcd(int x, int y){
int r;
while (y != 0) {
r = x % y;
x = y;
y = r;
}
return x;
}
//Rational Addition
public void plus(Rational x){
int greatdenom = x.denom * denom;
int multx = greatdenom / x.denom;
int mult = greatdenom / denom;
denom = x.denom * denom;
numer = (x.numer * multx) + (numer * mult);
reduce();
}
//Rational Subtraction
public void minus(Rational x){
int greatdenom = x.denom * denom;
int multx = greatdenom / x.denom;
int mult = greatdenom / denom;
denom = x.denom * denom;
if (x.numer > numer){
numer = (x.numer * multx) - (numer * mult);
}
else {
numer = (numer * mult) - (x.numer * multx);
}
reduce();
}
//Multiplication
public void times(Rational x){
numer = numer * x.numer;
denom = denom * x.denom;
reduce();
}
//Division
public void divBy(Rational x){
numer = numer / x.numer;
denom = denom / x.denom;
reduce();
}
//Fraction simplifier
private void reduce(){
int divisor;
divisor = Rational.gcd(numer, denom);
numer = numer / divisor;
denom = denom / divisor;
}
@Override
public String toString(){
if (denom == 1){
return numer + "";
}
else{
return numer + " / " + denom;
}
}
}
Upvotes: 0
Views: 1659
Reputation: 1268
You need to return a Rational (rather than void) in your plus method.
public Rational plus(Rational x){
//do addition stuff.
return new Rational(//what numerator be//,//what denom should be//)
}
As a suggestion, I would make all these operator methods static and take in 2 parameters too. And then you would use "getters" for each of the two rational numbers' numerators and denominators.
Like this:
public static Rational plus(Rational r1, Rational r2) {
int r1Num=r1.getNum();
int r1Denom=r1.getDenom();
int r2Num=r2.getNum();
int r2Denom=r2.getDenom();
//do all your plus stuff
return new Rational(//new num, //new denom);
}
Upvotes: 1
Reputation: 736
try following code as a test class.
Sample Code Test class
public class TestRational
{
public static void main(String[] args)
{
Rational rational= new Rational(2,3);
rational.plus(rational);
System.out.print(rational.toString());
}
}
Output Will be
4 / 3
Upvotes: 0