Search code examples
javamethodsstaticrational-number

Calling a Static Method In Main


How do call the last method on this class, in main? I am just trying to return a new Rational object that contains the product of the two parameters. Every time I try to call it in main, I can't use integer in the parameters, since it only accepts objects. So how do I multiply two number if it only accepts objects. (I can't change the parameters to integers, it's part of a problem set). Your help will be appreciated. Here my code:

 public class Rational {
        private int numer, denom;


        public Rational(int numer, int denom){
            this.numer = numer;
            this.denom = denom;


        }

        public Rational(Rational rational){
            rational = new Rational(numer, denom);


        }

        public void setNumber(int fum){
            numer = fum;
        }

        public int getNumber(){
            return numer;
        }

        public void setDenom(int Dum){
            denom = Dum;
        }

        public int getDenom(){
            return denom;
        }

        public Rational reciprocal(){
            System.out.println(denom + "/" + numer);
            return new Rational(denom, numer);
        }

        public static Rational multiply(Rational a, Rational b){
            int bar = a.numer;
            int bla = b.denom;
            int multi = bar * bla;
            System.out.println(multi);
            return new Rational(bar,bla);
        }

    }

Solution

  • You can call it like this:

    public class Main {
        public static void main(String[] args) {
            Rational rational1 = new Rational(1,2);
            Rational rational2 = new Rational(1,2);
            Rational result = Rational.multiply(rational1, rational2);
        }
    }