0

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

3
Contributors
7
Replies
8
Views
5 Years
Discussion Span
Last Post by jackbauer24
0

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 ) {}
0

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

0

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

Edited by DavidKroukamp: n/a

0

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.

0

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'

Edited by DavidKroukamp: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.