-1

I'm trying to solve a problem of codingbat. I have to write a method that given two non-negative int values, returns true if they have the same last digit. I'm trying to test quickly if my solution is correct so I create a class LastDigit and wrote:

public class LastDigit{
    public static void main(String[] args){
    System.out.println(lastDigit(7,17));
    System.out.println(lastDigit(6,17));
    System.out.println(lastDigit(3,113));
    }

    public boolean lastDigit(int a, int b){
       return (a%10==b%10);
    }
}

and I obtained the problem

non-static method lastDigit(int,int) cannot be referenced from a static context

But the problem is not the message (I'm imaging that I have to create somehow an object or somthing like that) but how can I test a method quickly?

Thanks :)

GniruT
  • 731
  • 1
  • 6
  • 14

1 Answers1

1

Yes. You can create an Object. That's one way.

public static void main(String[] args){
    LastDigit ld = LastDigit();
    System.out.println(ld.lastDigit(7,17));
    System.out.println(ld.lastDigit(6,17));
    System.out.println(ld.lastDigit(3,113));
  }

And It seems you need not to create if you just make that util method static.

public static void main(String[] args){
    System.out.println(lastDigit(7,17));
    System.out.println(lastDigit(6,17));
    System.out.println(lastDigit(3,113));
    }

    public static boolean lastDigit(int a, int b){
       return (a%10==b%10);
    }
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307