1

This program is for me to practice creating objects and defining classes. I have multiple methods written in rational class and my main written in main class. All the methods are calling correctly in main, but when I call my add and reduce methods, I'm getting an error that the methods can't be found. I have commented on the lines below where I am receiving the errors.

Here is my rational class:

package main;


public class rational {
    int numerator, denominator;
    public rational() {
        this.numerator = 0;
        this.denominator = 0;

    }

    public static void printRational(rational tester) {
        System.out.println(tester.numerator +  "/"  + tester.denominator);
    }

    public rational(int n, int d) {
        this.numerator = n;
        this.denominator = d;
    }

    public static void negate(rational tester) {
        tester.numerator = tester.numerator * -1;
        if(tester.denominator <= 0) {
            tester.numerator = tester.numerator * -1;
            tester.denominator = tester.denominator * -1;}
        }
        public static void invert(rational tester) {
            int tempN = tester.numerator;
            int tempD = tester.denominator;

            tester.numerator = tempD;
            tester.denominator = tempN;
        }
        public static double toDouble(rational tester) {
            double retval = (double)tester.numerator / (double)tester.denominator;
            return retval;
        }

        public static void reduce(rational tester) {
            int remain, num1, num2;
            num1 = tester.numerator;
            num2 = tester.denominator;
            remain = num1 % num2;
            while(remain != 0) {
                remain = num1 % num2 ;
                num1 = num2;
                num2 = remain;
            }
            int GCD = num1;

            rational ret = new rational(tester.numerator/GCD, tester.denominator/GCD);

            printRational(ret);
        }

        //Step 11  Write a method called add that takes two rational numbers as arguments and returns a new rational object. The return object should contain the sum of the arguments.
        public static rational add(rational tester, rational testAdd) {
            rational retAdd = new rational(0, 0);
            if(tester.denominator == testAdd.denominator) {
                retAdd.numerator = tester.numerator + testAdd.numerator;
                retAdd.denominator = tester.denominator;
                return retAdd;
            } else {
                retAdd.numerator = tester.numerator * testAdd.denominator;
                retAdd.denominator = tester.denominator * testAdd.denominator;

                testAdd.numerator = testAdd.numerator * tester.denominator;
                testAdd.denominator = testAdd.denominator * tester.denominator;

                retAdd.numerator += testAdd.numerator;

                int remain, num1, num2;
                num1 = retAdd.numerator;
                num2 = retAdd.denominator;
                remain = num1 % num2;
                while(remain != 0) {
                    remain = num1 % num2 ;
                    num1 = num2;
                    num2 = remain;
                }
                int GCD = num1;

                retAdd.numerator = retAdd.numerator/GCD;
                retAdd.denominator = retAdd.denominator/GCD;

                return retAdd;
            }
        }
    }
}

And here is my main class in a different file:

package main;

import static main.rational.toDouble;

public class Main {

    public static void main(String[] args) {

        rational tester = new rational();
        tester.numerator=4;
        tester.denominator=3;

        rational testAdd = new rational(5, 7);    

        rational.printRational(tester);

        rational.negate(tester);
        rational.printRational(tester);

        rational.invert(tester);
        rational.printRational(tester);

        double doubleValue = toDouble(tester);
        System.out.println(doubleValue);

        reduce(tester);   //ERROR HERE

        printRational(add(tester, testAdd));    //ERROR HERE
    }
}
5
  • 1
    please follow naming convention for the class name it should start in Uppercase Letter Commented Apr 6, 2017 at 1:41
  • 1
    You should invoke the function with rational.reduce(tester); not just redduce() because when you call with reduce, compiler search for static method in the same class Commented Apr 6, 2017 at 1:48
  • @user7790438 Great, thank you!! Commented Apr 6, 2017 at 1:50
  • Please format (indent) code for human readability. Commented Apr 6, 2017 at 1:53
  • Also please provide the actual exception or error message. If you look carefully at the exception (or the compilation failure) it should give you enough information to solve your own problem. Commented Apr 6, 2017 at 2:00

2 Answers 2

1

You're statically importing the toDouble method from rational (BTW, Java classes traditionally are capitalized, so you should name your class Rational), but not any others, like reduce, so you can't invoke them without prefacing them with the class name.

So rational.reduce(tester); would work, as would adding import static main.rational.reduce; to the top of your Main class.

Sign up to request clarification or add additional context in comments.

Comments

0

only import 'static main.rational.toDouble' int class Main this method is not correct;u can change it into import static com.syoka.test1.rational.*;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.