I have attempted to play with the example and it doesn't work? Why?


#1

So I took the example and added in dogs name so that I could have it say "His name is X" but it returns the error:

// Dog.java:34: error: cannot find symbol
spike.name(2);
^
symbol: method name(int)
location: variable spike of type Dog
1 errorr //

Why does it return this error when my name/who method is written identically to the examples?

Thank you in advance for helping :slightly_smiling:

CODE BELOW -

class Dog {

int age;

public Dog(int dogsAge) {

age = dogsAge;

}

public void bark() {

System.out.println("Woof!");

}

public void run(int feet) {
  System.out.println("Your dog ran " + feet + " feet!");
}

public int getAge() {

return age;

}

public static void main(String[] args){

Dog spike = new Dog(9);
spike.bark();
spike.name(2);
spike.run(90);



int spikeAge = spike.getAge();
System.out.println("He is " + spikeAge + " years old");

}

}


#2

There is no method called name. The line is invalid.


#3

Apologies for the post I have sorted it out now.

Thank you for responding