0

I am learning Java using Codecademy. I am trying to write code that would make an object (in this case a 3 year old dog named Spike) to run on on n legs where n = age. I tried "brute force debugging", but that didn't work. How should I change the line "how to change this" to make the code work?

class Dog {
    public Dog(int dogsAge) {
        int age = dogsAge;
    }

    public void bark() {
        System.out.println("Woof!");
    }

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

    public static void main(String[] args) {
        Dog spike = new Dog(3);
        spike.bark();
        spike.run(this.age); // How to change this?
    }
}
1
  • 1
    By declaring the variable age in the constructor, it's only visible within the constructor. This is an example of scope. Commented Apr 2, 2016 at 19:00

3 Answers 3

3

Your class needs to - add storage for the age variable - set that value in the construcor - provide a way for your main function to access it

class Dog {
 private int age; //storage for the age value within the instance
 public Dog(int dogsAge){
   this.age = dogsAge; // set the value
 }
 // provide a way to access the age
 public int getAge() {
   return this.age;
 }
 public void bark(){
   System.out.println("Woof!");

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


 public static void main(String[] args) {
  Dog spike = new Dog(3);
  spike.bark();
  spike.run(spike.getAge()); // retrieve the age and use it
 }

}
Sign up to request clarification or add additional context in comments.

2 Comments

What happens to a variable dogsAge if I don't add "public int getAge() { return this.age; }" ? Is it completely unaccessible without dogsAge function?
In my code above it would not be inaccessible because the main() function is within the Dog class. However if the main() function were in another class, then it would be inaccessible without getAge() because the 'age' property was declared 'private'. You could make it accessible without the getAge() function, by using 'public int age' instead, however that is generally considered poor practise.
2

Try to create a variable int age and initialize it in constructor like this:

  class Dog {
  int age;
 public Dog(int dogsAge){
    this.age = dogsAge;
  }

Comments

2
class Dog {
int age;   //your mistake
 public Dog(int dogsAge){
 this.age = dogsAge;
 }
 public void bark() {
    System.out.println("Woof!");
}

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

public static void main(String[] args) {
    Dog spike = new Dog(3);
    spike.bark();
    spike.run(spike.age); // How to change this?
}

1 Comment

Dog.java:25: error: non-static variable this cannot be referenced from a static context spike.run(this.age); // How to change this? ^ 1 error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.