2

I have created a small Java application to automatically test some expressions for a true/false condition.

I am getting two compiler errors in both jGRASP and with the javac command.

The code follows:

public class MathTest {
    public static void main(String[] args) {

        int x = 10;
        int y = 20;
        int z = 30;
        String string1 = "six";
        String string2 = "six";

        if (x < 10 || x > 10)
            System.out.print("True");
        else
            System.out.print("False");

        if (z - y == x && Math.abs(y - z) == z)
            System.out.print("True");
        else
            System.out.print("False");

        if (x < 10 && x < 10)
            System.out.print("True");
        else
            System.out.print("False");

        if (string1.equals(string2))
            System.out.print("True");
        else
            System.out.print("False");
        if (x > y || y > x)
            System.out.print("True");
        else
            System.out.print("False");

        if (!(x < y + z) || !(x + 10 <= 20))
            System.out.print("True");
        else
            System.out.print("False");

        if (string1 == string2)
            System.out.print("True");
        else
            System.out.print("False");

    }
}

The error message is:

    MathTest.java:14: cannot find symbol
    symbol  : method abs(int)
    location: class Math
    if(z - y == x && Math.abs(y - z) == z)
                     ^
    ./Math.java:13: cannot find symbol
    symbol  : method abs(int)
    location: class Math
    if(z - y == x && Math.abs(y - z) == z)
                     ^
    2 errors

What am I doing wrong?

In the unlikely event that my instructor or any administrator from Salt Lake Community College ever comes across this question, let me make my intentions clear. This question is posted in the greatest spirit of academic honesty. I ask this question to seek general advice and help in understanding the proper way to use the Java programming language. I in no way use the work of others and represent it as my own work. I use the answers provided here as a general aid in my understanding. I do all my own work and do not copy work provided by people answering my question.

Patrick Cassell
  • 469
  • 2
  • 6
  • 10
  • Generally speaking is always better to use brackets than not using them. Prefer if(){ ... } else { ... } over if() .. else ... – OscarRyz Mar 10 '09 at 21:45

3 Answers3

6

You have a Math class, and you're attempting to use the abs() method.

The question is: Is it your intention to provide this function or are you trying to use the one in java.lang.Math?

For the first, you have to make sure you're declaring that function.

For the second you have to make sure you're using the correct parameters types; see Math.

Does your Math class have the abs method?

It seems like your Math class is shadowing the Math class that comes in the core of the language.

Your Math class is loaded and the abs method could not be found (hence the "Cannot find symbol" message)

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
5

If you want your program to use Java's Math.abs (instead of an abs() method in the Math class you wrote), you can say:

if(z - y == x && java.lang.Math.abs(y - z) == z)

...which isn't very pretty. In general, try not to name your classes the same as the ones in java.lang (or java.anything, really).

kris
  • 23,024
  • 10
  • 70
  • 79
  • It was my intention to use the Math class built into java.lang. I believe I accidentally called a class that I did not make instead of calling java.lang.Math. When I made the change the errors disappeared. Thank you. – Patrick Cassell Mar 10 '09 at 22:23
3

In your compiler output, you have:

./Math.java:13: cannot find symbol

It looks like you're trying to write your own Math class, and it's shadowing java.lang.Math, which is builtin.

Do you have to have a class of your own called Math? If not, then just delete Math.java and try to compile this again. If you do need Math.java, then try renaming it to something else (like, say, MyMath.java with public class MyMath defined inside).

Todd Gamblin
  • 58,354
  • 15
  • 89
  • 96