My main.java
public static void main(String[] args) {
// TODO code application logic here
Rational x = new Rational ();
x.num = 2.0;
x.den = 3.0;
System.out.println (x);
Rational.negate (x);
Rational y = new Rational (1.0, 3.0);
System.out.println (Rational.invert (y));
}
My rational.java
package rational;
public class Rational {
double num;
double den;
public Rational () {
this.num = 0.0;
this.den = 0.0;
}
public static void printRational (Rational r) {
System.out.println (r.num + " / " + r.den);
}
public Rational (double num, double den) {
this.num = num;
this.den = den;
}
public static void negate (Rational r) {
r.num = -r.num;
}
public static double invert (Rational r) {
return Math.pow (r.num / r.den, -1);
}
public double toDouble () {
return (double) num / den;
}
public static int gcd (int n, int d) {
if (n < 0) {
n = -n;
}
if (d < 0) {
d = -d;
}
return n * (d / gcd (n, d));
}
public static void reduce (Rational r) {
n / gcd (n, d);
d / gcd (n, d);
}
public static double add (Rational r1, Rational r2) {
return new Rational (add (r1.r, r2.r));
}
}
(a) Write a method called printRational that takes a Rational object as an argument and prints it in some reasonable format.
(b) Write a main method that creates a new object with type Rational, sets its instance variables to some values, and prints the object.
(c) Write a second constructor for your class that takes two arguments and that uses them to initalize the instance variables.
(d) Write a method called negate that reverses the sign of a rational number. This method should be a modifier, so it should return void. Add lines to main to test the new method.
(e) Write a method called invert that inverts the number by swapping the numerator and denominator. Remember the swap pattern we have seen before. Add lines to main to test the new method.
(f) Write a method called toDouble that converts the rational number to a double (floating-point number) and returns the result. This method is a pure function; it does not modify the object. As always, test the new method.
(g) Write a modifier named reduce that reduces a rational number to its lowest terms by finding the GCD of the numerator and denominator and then dividing top and bottom by the GCD. This method should be a pure function; it should not modify the instance variables of the object on which it is invoked. You may want to write a method called gcd that finds the greatest common
divisor of the numerator and the denominator.
(h) Write a method called add that takes two Rational numbers as arguments and returns a new Rational object. The return object, not surprisingly, should contain the sum of the arguments. There are several ways to add fractions. You can use any one you want, but you should make sure that the result of the operation is reduced so that the numerator and denominator have no common divisor (other than 1).
Am I on the right track here? Can somebody please check my code and tell me if I am doing everything or not? Also, I don't know how to test everything in the main method. Can anybody please give me some help there?
Thanks ;)