Using methods: II Error


#1

I keep getting an error in this exercise, and I can't seem to find the problem.

The code:

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 void getAge() {
return age;
}

public static void main(String[] args) {

Dog spike = new Dog(7);
spike.bark();
spike.run(25);

}

}

The error:

Dog.java:24: error: incompatible types: unexpected return value
return age;
       ^

1 error

The error I'm getting seems to be coming from this part of the code here:
public void getAge() {
return age;
}

I can't find out what's wrong. Could somebody tell me what I need to fix?


#2

I tried this but im getting errors

class Dog{
int age;

public Dog(int dogsAge) {

age = dogsAge;
}

public int bark() {

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

public int 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(7);
spike.bark();
spike.run(25);

}
}

This is my error

Dog.java:12: error: missing return statement
}
^
Dog.java:17: error: missing return statement
}
^
2 errors


#3

It seems that quite a few other people are having this problem too. Hopefully someone explains what's wrong.


#4

This method is returning an int so you will have to change that void to an int.


#5

You aren't returning anything from this method so that int should be a void.

The same goes for your run method.


#6

Thank you, I changed
public void getAge() {
return age;
}

to

public int getAge() {
return age;
}

and it worked. Could've sworn I did that though when I tried debugging the code.


#7

It's a lot like when I try to plug in a USB cable.


#14

Try these

spike.bark();
spike.run(1);

and its work