So, i am now upto learning about classes & objects,

I have a rational number class to work with fractions i didnt write this class, it is part of my study text and i'm trying to understand how it works. so i thought i would write a run method to utilise this class to do calculations with fractions. (or subtract or multiply but for the purpose of this post just adding, i can figure in while loops and sentinels etc later)

i am not sure whether its a simply syntax issue that i haven't got my head around, or whether its the issue of using classes that i haven't got my head round,,

its probably a bit of both.

my run method gets all confused towards the end and doesn't make any sense theres all kind of rubbish at the end of the last method because i just left the rubbish in there so you could see where my thinking was at.

here is the class code for the Rational class

and here is my program/run method which makes no real sense, but to reiterate my question how to i define one object,(which i have done) and add to another object.Code:`public class Rational {`

/** Creates a new Rational initialised to zero.

*/

public Rational(){

this (0);

}

/** Creates a new Rational from the integer argument.

* @param n The initial value

*/

public Rational(int n) {

this(n,1);

}

/** Creates a new Rational with the value x / y.

* @param x The numerator of the rational number

* @param y The denominator of the rational number

*/

public Rational(int x, int y) {

if(y==0)throw new ErrorException (" Cannot Divide by Zero");

int g = gcd(Math.abs(x),Math.abs(y));

num = x / g;

den = Math.abs(y) / g;

if (y < 0) num = -num;

}

/** Adds the rational number r to this one and returns the sum.

* @param r The rational number to be added

* @return The sum of the current number and r

*/

public Rational add(Rational r) {

return new Rational(this.num * r.den + r.num * this.den, this.den * r.den);

}

/** Subtracts the rational number r from this one.

* @param r The rational number to be subtracted

* @return The result of subtracting r from the current number

*/

public Rational subtract(Rational r) {

return new Rational(this.num * r.den - r.num * this.den, this.den * r.den);

}

/** Multiplies this number by the rational number r.

* @param r The rational number used as a multiplier

* @return The result of multiplying the current number by r

*/

public Rational multiply(Rational r) {

return new Rational(this.num * r.num, this.den * r.den);

}

/** Divides this number by the rational number r.

* @param r The rational number used as a divisor

* @return The result of dividing the current number by r

*/

public Rational divide(Rational r) {

return new Rational(this.num * r.den, this.den * r.num);

}

/** Calculates the greatest common divisor using Euclid's algorithm.

* @param x First integer

* @param y Second integer

* @return The greatest common divisor of x and y

*/

private int gcd(int x, int y) {

int r = x % y;

while (r != 0) {

x = y;

y = r;

r = x % y;

}

return y;

}

/**

* Creates a string representation of this rational number.

* @return The string representation of this rational number

*/

public String toString() {

if (den == 1) {

return "" + num;

} else {

return num + "/" + den;

}

}

/* Private instance variables */

private int num; /* The numerator of this Rational */

private int den; /* The denominator of this Rational */

}

thanks in advance:confused:Code:`import acm.program.ConsoleProgram;`

public class UseRational extends ConsoleProgram{

public void run(){

intro();

enterFraction();

}

private void intro() {}

private void enterFraction() {

int num=readInt(" Numerator: ");

int den=readInt("Denominator: ");

Rational one = new Rational(num,den);

println(one);

enterOperation (one);

}

private void enterOperation(Rational one) {

String op = readLine("+, -, x, /, ");

enterNextFraction(one,op);

}

private void enterNextFraction(Rational one, String op) {

int num=readInt(" Numerator: ");

int den=readInt("Denominator: ");

Rational result = one add Rational(num,den);

println(result);

if(op.equals("+")){

Rational result = one add.Rational two;

println(one+" + "+two+" = "+one add Rational two);

}

if(op.equals("-"))

if(op.equals("x"))

}

}

Sonny.