7 Replies - 563 Views - Last Post: 15 November 2012 - 10:01 PM Rate Topic: -----

#1 mike31s  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 05-October 12

Trouble calling class method

Posted 15 November 2012 - 06:31 PM

I'm working on an assignment and have got stuck calling a method. I keep getting the 'Cannot find symbol' error and don't know why. Here is the class of the method I'm working with (Excuse the silly names)

class Leopard extends Mammal{
	public Leopard(){
		super("Hungry Cat");
	}
	public Leopard(String name){
		super(name);
	}
	public void move(){
		System.out.println(getName() + " the leopard pounces");
	}
	public void makeSound(){
		System.out.println(getName() + " the leopard purrs");
	}
	public void findTree(){
		System.out.println(getName() + " the leopard hides in a tree");
	}
}



I can call move() and makeSound() fine. I don't know if the fact that they override the super class methods is related or not.
I can't get findTree()to work in the following code from my application class:
		for (int i = 0; i < animal.length; i++){
		animal[i].move();
		animal[i].makeSound();
		        if (animal[i] instanceof Leopard)
				animal[i].findTree();
                 }


Thanks for the help!

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble calling class method

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 203
  • View blog
  • Posts: 1,731
  • Joined: 13-March 10

Re: Trouble calling class method

Posted 15 November 2012 - 08:00 PM

Show us all the relevant code including Mammal
Was This Post Helpful? 0
  • +
  • -

#3 WolverineX  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 49
  • Joined: 11-November 12

Re: Trouble calling class method

Posted 15 November 2012 - 08:31 PM

You are using object "animal", but I don't see it in the code....

Have you defined the object such as:

Mammal animal = new Mammal("Leopard");

???
Was This Post Helpful? 0
  • +
  • -

#4 mike31s  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 05-October 12

Re: Trouble calling class method

Posted 15 November 2012 - 08:35 PM

I was trying not to post too much code....


here is the full segment:

class Leopard extends Mammal{
	public Leopard(){
		super("Hungry Cat");
	}
	public Leopard(String name){
		super(name);
	}
	public void move(){
		System.out.println(getName() + " the leopard pounces");
	}
	public void makeSound(){
		System.out.println(getName() + " the leopard purrs");
	}
	public void findTree(){
		System.out.println(getName() + " the leopard hides in a tree");
	}
}

class Mammal extends Animal{
	public Mammal(){
		super("Mammoth");
	}
	public Mammal(String name){
		super(name);
	}
	public void makeSound(){
		System.out.println("Mammal makes sound");
	}
}

public class Animal {
private String name;
public Animal(){
	name = "Nameless";
}
public Animal(String name){
	this.name = name;
}


    public String getName(){
    	return name;
    }
    public void setName(String name){
    	this.name = name;
    }
    public void move(){
    	System.out.println("Animal moves");
    }
    public void makeSound(){
    	System.out.println("Animal makes a sound");
    }


}

Was This Post Helpful? 0
  • +
  • -

#5 WolverineX  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 49
  • Joined: 11-November 12

Re: Trouble calling class method

Posted 15 November 2012 - 08:39 PM

Yeah you see. You don't have any object called "animal" you only have a class called "Animal" You can't do class.method().. You have to make the object animal as I wrote in my previous post then do animal[i].move();

That is Object.Method();
Was This Post Helpful? 0
  • +
  • -

#6 mike31s  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 05-October 12

Re: Trouble calling class method

Posted 15 November 2012 - 08:46 PM

Sorry I posted in pieces, here is the full application:

public class Project7 {

    public static void main(String[] args) {

    	Animal [] animal = new Animal [7];

    	animal[0] = new Animal("Phoenix");
    	animal[1] = new Reptile("Crocodile");
    	animal[2] = new Mammal("Dire Wolf");
    	animal[3] = new Leopard("Snow Cat");
    	animal[4] = new Chameleon("Lizard", "green-blue");
    	animal[5] = new Leopard ("Hungry Tree Leopard");
    	animal[6] = new Bat ("Insect Eater");

		for (int i = 0; i < animal.length; i++){
		animal[i].move();
		animal[i].makeSound();
			if (animal[i] instanceof Leopard)
				animal[i].findTree();
			}
		System.out.println(animal[i].getName());
		System.out.println();
		}




    }
}


Again, I can call animal[i].move(); and animal[i].makeSound(); without any problem. Only animal[i].findTree(); will cause the error. Thank you again!!
Was This Post Helpful? 0
  • +
  • -

#7 mike31s  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 05-October 12

Re: Trouble calling class method

Posted 15 November 2012 - 09:03 PM

I found my answer. I had to Typecast my object for it to work. Thank you for your replies anyways!
Was This Post Helpful? 0
  • +
  • -

#8 WolverineX  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 49
  • Joined: 11-November 12

Re: Trouble calling class method

Posted 15 November 2012 - 10:01 PM

"animal" is an Object from the class "Animal" and inside that class there's no such method as findTree(), so of course it won't work... You're welcome!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1