I'm having trouble calling up the methods that are in the constructor file from the main method.
Here's the code in the main method:
All the methods are errors.public static void main(String[] args) { // Create a new rational number and set it to 1/3 Rational rational1 = new Rational(); rational1.numerator = -1; rational1.denominator = 3; printRational(rational1); // -1/3 // Create a rational number using the second constructor Rational rational2 = new Rational(3, 5); printRational(rational2); // 3/5 // Negate the rational number negate(rational1); printRational(rational1); // 1/3 // Invert the numerator and denominator invert(rational1); printRational(rational1); // 3/1 // Convert the rational number to a double double decimal = toDouble(rational1); System.out.println(decimal); // 3.0 // Reduce a rational number to its lowest terms Rational rational3 = new Rational(3, 21); printRational(rational3); // 3/21 reduce(rational3); printRational(rational3); // 1/7 // Sum two rationals printRational(add(rational2, rational3)); } }
Here's my code in the rational file
Any help would be great!public class Rational { int numerator, denominator; public Rational() { this.numerator = 0; this.denominator = 0; } public Rational(int numerator, int denominator) { this.numerator = numerator; this.denominator = denominator; } public static void printRational(Rational rational) { System.out.println(rational.numerator + "/" + rational.denominator); } public static void negate(Rational rational) { int numerator = rational.numerator; int negatedNumerator = -1 * numerator; rational.numerator = negatedNumerator; } public static void invert(Rational rational) { int oldNumerator = rational.numerator; int oldDenominator = rational.denominator; rational.numerator = oldDenominator; rational.denominator = oldNumerator; } public static double toDouble(Rational rational) { double numerator = rational.numerator; double denominator = rational.denominator; return numerator / denominator; } public static int gcd(int a, int b) { if (b == 0) { return a; } int r = a % b; return gcd(b, r); } public static void reduce(Rational rational) { int gcd = gcd(rational.numerator, rational.denominator); rational.numerator = rational.numerator / gcd; rational.denominator = rational.denominator / gcd; } public static Rational add(Rational rational1, Rational rational2) { int newNumerator = rational1.numerator * rational2.denominator + rational1.denominator * rational2.numerator; int newDenominator = rational1.denominator * rational2.denominator; Rational newRational = new Rational(newNumerator, newDenominator); reduce(newRational); return newRational; } }