12/13-completely lost


#1



https://www.codecademy.com/en/courses/learn-java/lessons/object-oriented-programming/exercises/inheritance?action=resume

First I am 100% a beginner ,I have been working on this slide for quite some time, I as I'm learning I did not want to post without at least trying everything I had in my notes. Clearly there is something wrong with the code for it to not run, In my newbie eyes, it seems to fit exactly what they instruct. Any advice/suggestions appreciated.


class Dog {
  class Dog extends Animal {
    
  }
  
  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(5);
    spike.bark();
    spike.run(40);
    int spikeAge = spike.getAge();
    System.out.println(spikeAge);
    
    checkStatus.spike();

	}

}


#2

How is it misbehaving?

Oh. Compile error.

$ javac Dog.java Animal.java 
Dog.java:2: error: class Dog is already defined in package unnamed package
  class Dog extends Animal {
  ^
Dog.java:34: error: non-static variable this cannot be referenced from a static context
    Dog spike = new Dog(5);
                ^
Dog.java:35: error: cannot find symbol
    spike.bark();
         ^
  symbol:   method bark()
  location: variable spike of type Dog.Dog
Dog.java:36: error: cannot find symbol
    spike.run(40);
         ^
  symbol:   method run(int)
  location: variable spike of type Dog.Dog
Dog.java:37: error: cannot find symbol
    int spikeAge = spike.getAge();
                        ^
  symbol:   method getAge()
  location: variable spike of type Dog.Dog
Dog.java:40: error: cannot find symbol
    checkStatus.spike();
    ^
  symbol:   variable checkStatus
  location: class Dog
6 errors

(assuming Animal.java is the following:)

class Animal {

	public void checkStatus() {

		System.out.println("Your pet is healthy and happy!");

	}
}

Start from the top. The first one should be pretty straight-forward to fix and a whole lot disappears after fixing it.


#3

Hi there,

checkStatus.spike();

This should be switched around because we want to call the checkStatus inherited method on the spike object (similarily to spike.bark()). It should instead read:

spike.checkStatus();

Hope that makes sense :slight_smile:


#4

I have managed to clean it up and fix most the errors now I'm getting the error I have posted. I am not looking for the answer, I just need help understanding where I'm going wrong so I can learn from it.
Dog.java:41: error: reached end of file while parsing
}
** ^**
1 error

here is an updated code:
class Dog {
class Dog extends Animal {

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(5);
spike.bark();
spike.run(40);
int spikeAge = spike.getAge();
System.out.println(spikeAge);
spike.checkStatus();

}

}


#5

Oh okay I see what's happening.

Dog.java:41: error: reached end of file while parsing

This basically means that the compiler couldn't find the ending point of the Dog class. You need to make sure an opening curly brace is eventually met with a closing curly brace. One critical error that I must point out though is that you have two Dog classes. You only need the one that extends Animal.


#6

thank you, honestly that was so simple and I cant believe I have been overlooking it for almost a hour....thank again


#7

A post was split to a new topic: 12. Can somebody post the correct code I've been trying to do this for the past 4 hours and I have no idea what I'm doing wrong


#8

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