10/13 Methods II: Inserting dog's name


#1

So I was wondering: is there a way to call the name of the new Dog object “spike” without having a “name” variable?

Where the run method will print out “Your dog ran…” is there a way to insert the object’s name (“spike”) in there so it reads “spike ran xxx feet!” without having to add a new variable to the Dog class? Thanks!

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 static void main(String[] args) {
		Dog spike = new Dog(1);
    spike.bark();
    spike.run(140);
	}

}


#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.