Here is my code:

class Catandmouse {
    public void CatAndMouse() {
    	Animal cat = new Animal();
    	Animal mouse = new Animal();
    	int mouseCaught = (int) (Math.random() * 10);
    	cat.CatchMouse();
    	int catgetsit = 0;
    	if ( mouseCaught == cat.number ) {
      	System.out.println("The cat caught the mouse!");
    	}
    	else {
      	System.out.println("The mouse got away!");
    	}
    }
}

Here is my Animal class:

class Animal {
	public void CatchMouse() {
		int number = (int) (Math.random() * 10);
      }
}

And this is the error:
CatAndMouse.java:8: cannot find symbol
symbol : variable number
location: class Animal
if ( mouseCaught == cat.number ) {
^
1 error

Why the error?
Reply quick. Also explain the error

Here is my code:

class Catandmouse {
    public void CatAndMouse() {
    	Animal cat = new Animal();
    	Animal mouse = new Animal();
    	int mouseCaught = (int) (Math.random() * 10);
    	cat.CatchMouse();
    	int catgetsit = 0;
    	if ( mouseCaught == cat.number ) {
      	System.out.println("The cat caught the mouse!");
    	}
    	else {
      	System.out.println("The mouse got away!");
    	}
    }
}

Here is my Animal class:

class Animal {
	public void CatchMouse() {
		int number = (int) (Math.random() * 10);
      }
}

And this is the error:
CatAndMouse.java:8: cannot find symbol
symbol : variable number
location: class Animal
if ( mouseCaught == cat.number ) {
^
1 error

Why the error?
Reply quick. Also explain the error

the first problem is your

class Animal {
	public void CatchMouse() {
		int number = (int) (Math.random() * 10);
      }
}

it should return an integer. then you can get this integer by:

int catnum=cat.CatchMouse();

and now use it in your if statement like so

if ( mouseCaught == catnum ) {}

Now it is working, but could you please explain why my solution did not work?

Now it is working, but could you please explain why my solution did not work?

because you called the method :

public void CatchMouse() {
		int number = (int) (Math.random() * 10);
      }

and now how must the answer in the variable 'number', extend to your other class? the only way it could be done is either by returning the int(like i advised in above post), or delcaring it as a global and accessing it like that:

class Animal {
static int number=0;
	public void CatchMouse() {
		 number = (int) (Math.random() * 10);
      }
}

and now in your other class you can access the variable which should have your answer after calling the method

And now when I try to start it from here:

class TestDrive {
	public static void main(String[] args) {
		Catandmouse startgame = new Catandmouse();
            startgame.CatAndMouse();
	}
}

I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: RunGame
Caused by: java.lang.ClassNotFoundException: RunGame
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: RunGame. Program will exit.

Now what? Explain.

And now when I try to start it from here:

class TestDrive {
	public static void main(String[] args) {
		Catandmouse startgame = new Catandmouse();
            startgame.CatAndMouse();
	}
}

I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: RunGame
Caused by: java.lang.ClassNotFoundException: RunGame
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: RunGame. Program will exit.

Now what? Explain.

well look how must java find you main method if their is no public class. a java program has a set structure and the main method can only exists in a public class and only one public class can exists in a *.java file. try adding public onto your TestDrive class, this will then make java know that it is your main class and not an extension i.e

public class x {//because its public java knows to look for main method here
}

however your filename should correspond with your public class name so in the above case it will be: 'x.java'

you have probably named your class as RunGame .It should be TestDrive

It's working now, I don't know how?! :-D Closing the thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.